-
-
Notifications
You must be signed in to change notification settings - Fork 940
/
blocks.js
572 lines (509 loc) · 18.7 KB
/
blocks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
const { Vec3 } = require('vec3')
const assert = require('assert')
const Painting = require('../painting')
const { onceWithCleanup } = require('../promise_utils')
const { OctahedronIterator } = require('prismarine-world').iterators
module.exports = inject
const paintingFaceToVec = [
new Vec3(0, 0, -1),
new Vec3(-1, 0, 0),
new Vec3(0, 0, 1),
new Vec3(1, 0, 0)
]
const dimensionNames = {
'-1': 'minecraft:nether',
0: 'minecraft:overworld',
1: 'minecraft:end'
}
function inject (bot, { version, storageBuilder, hideErrors }) {
const Block = require('prismarine-block')(bot.registry)
const Chunk = require('prismarine-chunk')(bot.registry)
const World = require('prismarine-world')(bot.registry)
const paintingsByPos = {}
const paintingsById = {}
function addPainting (painting) {
paintingsById[painting.id] = painting
paintingsByPos[painting.position] = painting
}
function deletePainting (painting) {
delete paintingsById[painting.id]
delete paintingsByPos[painting.position]
}
function delColumn (chunkX, chunkZ) {
bot.world.unloadColumn(chunkX, chunkZ)
}
function addColumn (args) {
if (!args.bitMap && args.groundUp) {
// stop storing the chunk column
delColumn(args.x, args.z)
return
}
let column = bot.world.getColumn(args.x, args.z)
if (!column) {
// Allocates new chunk object while taking world's custom min/max height into account
column = new Chunk({ minY: bot.game.minY, worldHeight: bot.game.height })
}
try {
column.load(args.data, args.bitMap, args.skyLightSent, args.groundUp)
if (args.biomes !== undefined) {
column.loadBiomes(args.biomes)
}
if (args.skyLight !== undefined) {
column.loadParsedLight(args.skyLight, args.blockLight, args.skyLightMask, args.blockLightMask, args.emptySkyLightMask, args.emptyBlockLightMask)
}
bot.world.setColumn(args.x, args.z, column)
} catch (e) {
bot.emit('error', e)
}
}
async function waitForChunksToLoad () {
const dist = 2
// This makes sure that the bot's real position has been already sent
if (!bot.entity.height) await onceWithCleanup(bot, 'chunkColumnLoad')
const pos = bot.entity.position
const center = new Vec3(pos.x >> 4 << 4, 0, pos.z >> 4 << 4)
// get corner coords of 5x5 chunks around us
const chunkPosToCheck = new Set()
for (let x = -dist; x <= dist; x++) {
for (let y = -dist; y <= dist; y++) {
// ignore any chunks which are already loaded
const pos = center.plus(new Vec3(x, 0, y).scaled(16))
if (!bot.world.getColumnAt(pos)) chunkPosToCheck.add(pos.toString())
}
}
if (chunkPosToCheck.size) {
return new Promise((resolve) => {
function waitForLoadEvents (columnCorner) {
chunkPosToCheck.delete(columnCorner.toString())
if (chunkPosToCheck.size === 0) { // no chunks left to find
bot.world.off('chunkColumnLoad', waitForLoadEvents) // remove this listener instance
resolve()
}
}
// begin listening for remaining chunks to load
bot.world.on('chunkColumnLoad', waitForLoadEvents)
})
}
}
function getMatchingFunction (matching) {
if (typeof (matching) !== 'function') {
if (!Array.isArray(matching)) {
matching = [matching]
}
return isMatchingType
}
return matching
function isMatchingType (block) {
return block === null ? false : matching.indexOf(block.type) >= 0
}
}
function isBlockInSection (section, matcher) {
if (!section) return false // section is empty, skip it (yay!)
// If the chunk use a palette we can speed up the search by first
// checking the palette which usually contains less than 20 ids
// vs checking the 4096 block of the section. If we don't have a
// match in the palette, we can skip this section.
if (section.palette) {
for (const stateId of section.palette) {
if (matcher(Block.fromStateId(stateId, 0))) {
return true // the block is in the palette
}
}
return false // skip
}
return true // global palette, the block might be in there
}
function getFullMatchingFunction (matcher, useExtraInfo) {
if (typeof (useExtraInfo) === 'boolean') {
return fullSearchMatcher
}
return nonFullSearchMatcher
function nonFullSearchMatcher (point) {
const block = blockAt(point, true)
return matcher(block) && useExtraInfo(block)
}
function fullSearchMatcher (point) {
return matcher(bot.blockAt(point, useExtraInfo))
}
}
bot.findBlocks = (options) => {
const matcher = getMatchingFunction(options.matching)
const point = (options.point || bot.entity.position).floored()
const maxDistance = options.maxDistance || 16
const count = options.count || 1
const useExtraInfo = options.useExtraInfo || false
const fullMatcher = getFullMatchingFunction(matcher, useExtraInfo)
const start = new Vec3(Math.floor(point.x / 16), Math.floor(point.y / 16), Math.floor(point.z / 16))
const it = new OctahedronIterator(start, Math.ceil((maxDistance + 8) / 16))
// the octahedron iterator can sometime go through the same section again
// we use a set to keep track of visited sections
const visitedSections = new Set()
let blocks = []
let startedLayer = 0
let next = start
while (next) {
const column = bot.world.getColumn(next.x, next.z)
const sectionY = next.y + Math.abs(bot.game.minY >> 4)
const totalSections = bot.game.height >> 4
if (sectionY >= 0 && sectionY < totalSections && column && !visitedSections.has(next.toString())) {
const section = column.sections[sectionY]
if (useExtraInfo === true || isBlockInSection(section, matcher)) {
const begin = new Vec3(next.x * 16, sectionY * 16 + bot.game.minY, next.z * 16)
const cursor = begin.clone()
const end = cursor.offset(16, 16, 16)
for (cursor.x = begin.x; cursor.x < end.x; cursor.x++) {
for (cursor.y = begin.y; cursor.y < end.y; cursor.y++) {
for (cursor.z = begin.z; cursor.z < end.z; cursor.z++) {
if (fullMatcher(cursor) && cursor.distanceTo(point) <= maxDistance) blocks.push(cursor.clone())
}
}
}
}
visitedSections.add(next.toString())
}
// If we started a layer, we have to finish it otherwise we might miss closer blocks
if (startedLayer !== it.apothem && blocks.length >= count) {
break
}
startedLayer = it.apothem
next = it.next()
}
blocks.sort((a, b) => {
return a.distanceTo(point) - b.distanceTo(point)
})
// We found more blocks than needed, shorten the array to not confuse people
if (blocks.length > count) {
blocks = blocks.slice(0, count)
}
return blocks
}
function findBlock (options) {
const blocks = bot.findBlocks(options)
if (blocks.length === 0) return null
return bot.blockAt(blocks[0])
}
function blockAt (absolutePoint, extraInfos = true) {
const block = bot.world.getBlock(absolutePoint)
// null block means chunk not loaded
if (!block) return null
if (extraInfos) {
block.painting = paintingsByPos[block.position]
}
return block
}
// if passed in block is within line of sight to the bot, returns true
// also works on anything with a position value
function canSeeBlock (block) {
const headPos = bot.entity.position.offset(0, bot.entity.height, 0)
const range = headPos.distanceTo(block.position)
const dir = block.position.offset(0.5, 0.5, 0.5).minus(headPos)
const match = (inputBlock, iter) => {
const intersect = iter.intersect(inputBlock.shapes, inputBlock.position)
if (intersect) { return true }
return block.position.equals(inputBlock.position)
}
const blockAtCursor = bot.world.raycast(headPos, dir.normalize(), range, match)
return blockAtCursor && blockAtCursor.position.equals(block.position)
}
bot._client.on('unload_chunk', (packet) => {
delColumn(packet.chunkX, packet.chunkZ)
})
function updateBlockState (point, stateId) {
const oldBlock = blockAt(point)
bot.world.setBlockStateId(point, stateId)
const newBlock = blockAt(point)
// sometimes minecraft server sends us block updates before it sends
// us the column that the block is in. ignore this.
if (newBlock === null) {
return
}
if (oldBlock.type !== newBlock.type) {
const pos = point.floored()
const painting = paintingsByPos[pos]
if (painting) deletePainting(painting)
}
}
bot._client.on('update_light', (packet) => {
let column = bot.world.getColumn(packet.chunkX, packet.chunkZ)
if (!column) {
column = new Chunk({ minY: bot.game.minY, worldHeight: bot.game.height })
bot.world.setColumn(packet.chunkX, packet.chunkZ, column)
}
if (bot.supportFeature('dimensionDataIsAvailable')) {
column.loadParsedLight(packet.skyLight, packet.blockLight, packet.skyLightMask, packet.blockLightMask, packet.emptySkyLightMask, packet.emptyBlockLightMask)
} else {
column.loadLight(packet.data, packet.skyLightMask, packet.blockLightMask, packet.emptySkyLightMask, packet.emptyBlockLightMask)
}
})
bot._client.on('map_chunk', (packet) => {
addColumn({
x: packet.x,
z: packet.z,
bitMap: packet.bitMap,
heightmaps: packet.heightmaps,
biomes: packet.biomes,
skyLightSent: bot.game.dimension === 'overworld',
groundUp: packet.groundUp,
data: packet.chunkData,
trustEdges: packet.trustEdges,
skyLightMask: packet.skyLightMask,
blockLightMask: packet.blockLightMask,
emptySkyLightMask: packet.emptySkyLightMask,
emptyBlockLightMask: packet.emptyBlockLightMask,
skyLight: packet.skyLight,
blockLight: packet.blockLight
})
if (typeof packet.blockEntities !== 'undefined') {
const column = bot.world.getColumn(packet.x, packet.z)
if (!column) {
if (!hideErrors) console.warn('Ignoring block entities as chunk failed to load at', packet.x, packet.z)
return
}
for (const blockEntity of packet.blockEntities) {
if (blockEntity.x !== undefined) { // 1.17+
column.setBlockEntity(blockEntity, blockEntity.nbtData)
} else {
const pos = new Vec3(blockEntity.value.x.value & 0xf, blockEntity.value.y.value, blockEntity.value.z.value & 0xf)
column.setBlockEntity(pos, blockEntity)
}
}
}
})
bot._client.on('map_chunk_bulk', (packet) => {
let offset = 0
let meta
let i
let size
for (i = 0; i < packet.meta.length; ++i) {
meta = packet.meta[i]
size = (8192 + (packet.skyLightSent ? 2048 : 0)) *
onesInShort(meta.bitMap) + // block ids
2048 * onesInShort(meta.bitMap) + // (two bytes per block id)
256 // biomes
addColumn({
x: meta.x,
z: meta.z,
bitMap: meta.bitMap,
heightmaps: packet.heightmaps,
skyLightSent: packet.skyLightSent,
groundUp: true,
data: packet.data.slice(offset, offset + size)
})
offset += size
}
assert.strictEqual(offset, packet.data.length)
})
bot._client.on('multi_block_change', (packet) => {
// multi block change
for (let i = 0; i < packet.records.length; ++i) {
const record = packet.records[i]
let blockX, blockY, blockZ
if (bot.supportFeature('usesMultiblockSingleLong')) {
blockZ = (record >> 4) & 0x0f
blockX = (record >> 8) & 0x0f
blockY = record & 0x0f
} else {
blockZ = record.horizontalPos & 0x0f
blockX = (record.horizontalPos >> 4) & 0x0f
blockY = record.y
}
let pt
if (bot.supportFeature('usesMultiblock3DChunkCoords')) {
pt = new Vec3(packet.chunkCoordinates.x, packet.chunkCoordinates.y, packet.chunkCoordinates.z)
} else {
pt = new Vec3(packet.chunkX, 0, packet.chunkZ)
}
pt = pt.scale(16).offset(blockX, blockY, blockZ)
if (bot.supportFeature('usesMultiblockSingleLong')) {
updateBlockState(pt, record >> 12)
} else {
updateBlockState(pt, record.blockId)
}
}
})
bot._client.on('block_change', (packet) => {
const pt = new Vec3(packet.location.x, packet.location.y, packet.location.z)
updateBlockState(pt, packet.type)
})
bot._client.on('explosion', (packet) => {
// explosion
const p = new Vec3(packet.x, packet.y, packet.z)
packet.affectedBlockOffsets.forEach((offset) => {
const pt = p.offset(offset.x, offset.y, offset.z)
updateBlockState(pt, 0)
})
})
bot._client.on('spawn_entity_painting', (packet) => {
const pos = new Vec3(packet.location.x, packet.location.y, packet.location.z)
const painting = new Painting(packet.entityId,
pos, packet.title, paintingFaceToVec[packet.direction])
addPainting(painting)
})
bot._client.on('entity_destroy', (packet) => {
// destroy entity
packet.entityIds.forEach((id) => {
const painting = paintingsById[id]
if (painting) deletePainting(painting)
})
})
bot._client.on('update_sign', (packet) => {
const pos = new Vec3(packet.location.x & 0xf, packet.location.y, packet.location.z & 0xf)
// TODO: warn if out of loaded world?
const column = bot.world.getColumn(packet.location.x >> 4, packet.location.z >> 4)
if (!column) {
return
}
const blockAt = column.getBlock(pos)
blockAt.signText = [packet.text1, packet.text2, packet.text3, packet.text4].map(text => {
if (text === 'null' || text === '') return ''
return JSON.parse(text)
})
column.setBlock(pos, blockAt)
})
bot._client.on('tile_entity_data', (packet) => {
if (packet.location !== undefined) {
const column = bot.world.getColumn(packet.location.x >> 4, packet.location.z >> 4)
if (!column) return
const pos = new Vec3(packet.location.x & 0xf, packet.location.y, packet.location.z & 0xf)
column.setBlockEntity(pos, packet.nbtData)
} else {
const tag = packet.nbtData
const column = bot.world.getColumn(tag.value.x.value >> 4, tag.value.z.value >> 4)
if (!column) return
const pos = new Vec3(tag.value.x.value & 0xf, tag.value.y.value, tag.value.z.value & 0xf)
column.setBlockEntity(pos, tag)
}
})
bot.updateSign = (block, text, back = false) => {
const lines = text.split('\n')
if (lines.length > 4) {
bot.emit('error', new Error('too many lines for sign text'))
return
}
for (let i = 0; i < lines.length; ++i) {
if (lines[i].length > 15) {
bot.emit('error', new Error('signs have max line length 15'))
return
}
}
let signData
if (bot.supportFeature('sendStringifiedSignText')) {
signData = {
text1: lines[0] ? JSON.stringify(lines[0]) : '""',
text2: lines[1] ? JSON.stringify(lines[1]) : '""',
text3: lines[2] ? JSON.stringify(lines[2]) : '""',
text4: lines[3] ? JSON.stringify(lines[3]) : '""'
}
} else {
signData = {
text1: lines[0] ?? '',
text2: lines[1] ?? '',
text3: lines[2] ?? '',
text4: lines[3] ?? ''
}
}
bot._client.write('update_sign', {
location: block.position,
isFrontText: !back,
...signData
})
}
// if we get a respawn packet and the dimension is changed,
// unload all chunks from memory.
let dimension
let worldName
function dimensionToFolderName (dimension) {
if (bot.supportFeature('dimensionIsAnInt')) {
return dimensionNames[dimension]
} else if (bot.supportFeature('dimensionIsAString') || bot.supportFeature('dimensionIsAWorld')) {
return worldName
}
}
// only exposed for testing
bot._getDimensionName = () => worldName
async function switchWorld () {
if (bot.world) {
if (storageBuilder) {
await bot.world.async.waitSaving()
}
for (const [name, listener] of Object.entries(bot._events)) {
if (name.startsWith('blockUpdate:')) {
bot.emit(name, null, null)
bot.off(name, listener)
}
}
for (const [x, z] of Object.keys(bot.world.async.columns).map(key => key.split(',').map(x => parseInt(x, 10)))) {
bot.world.unloadColumn(x, z)
}
if (storageBuilder) {
bot.world.async.storageProvider = storageBuilder({ version: bot.version, worldName: dimensionToFolderName(dimension) })
}
} else {
bot.world = new World(null, storageBuilder ? storageBuilder({ version: bot.version, worldName: dimensionToFolderName(dimension) }) : null).sync
startListenerProxy()
}
}
bot._client.on('login', (packet) => {
if (bot.supportFeature('dimensionIsAnInt')) {
dimension = packet.dimension
worldName = dimensionToFolderName(dimension)
} else {
dimension = packet.dimension
worldName = /^minecraft:.+/.test(packet.worldName) ? packet.worldName : `minecraft:${packet.worldName}`
}
switchWorld()
})
bot._client.on('respawn', (packet) => {
if (bot.supportFeature('dimensionIsAnInt')) { // <=1.15.2
if (dimension === packet.dimension) return
dimension = packet.dimension
} else { // >= 1.15.2
if (dimension === packet.dimension) return
if (worldName === packet.worldName && packet.copyMetadata === true) return // don't unload chunks if in same world and metaData is true
// Metadata is true when switching dimensions however, then the world name is different
dimension = packet.dimension
worldName = packet.worldName
}
switchWorld()
})
let listener
let listenerRemove
function startListenerProxy () {
if (listener) {
// custom forwarder for custom events
bot.off('newListener', listener)
bot.off('removeListener', listenerRemove)
}
// standardized forwarding
const forwardedEvents = ['blockUpdate', 'chunkColumnLoad', 'chunkColumnUnload']
for (const event of forwardedEvents) {
bot.world.on(event, (...args) => bot.emit(event, ...args))
}
const blockUpdateRegex = /blockUpdate:\(-?\d+, -?\d+, -?\d+\)/
listener = (event, listener) => {
if (blockUpdateRegex.test(event)) {
bot.world.on(event, listener)
}
}
listenerRemove = (event, listener) => {
if (blockUpdateRegex.test(event)) {
bot.world.off(event, listener)
}
}
bot.on('newListener', listener)
bot.on('removeListener', listenerRemove)
}
bot.findBlock = findBlock
bot.canSeeBlock = canSeeBlock
bot.blockAt = blockAt
bot._updateBlockState = updateBlockState
bot.waitForChunksToLoad = waitForChunksToLoad
}
function onesInShort (n) {
n = n & 0xffff
let count = 0
for (let i = 0; i < 16; ++i) {
count = ((1 << i) & n) ? count + 1 : count
}
return count
}