diff --git a/docs/api.md b/docs/api.md index 9b5c56365..bc1a7f05c 100644 --- a/docs/api.md +++ b/docs/api.md @@ -665,6 +665,23 @@ Create and return an instance of the class bot. A sync representation of the world. Check the doc at http://github.com/PrismarineJS/prismarine-world +#### Events: + +#### "blockUpdate" (oldBlock, newBlock) + +Fires when a block updates. Both `oldBlock` and `newBlock` provided for +comparison. + +Note that `oldBlock` may be `null`. + +#### "blockUpdate:(x, y, z)" (oldBlock, newBlock) + +Fires for a specific point. Both `oldBlock` and `newBlock` provided for +comparison. + +Note that `oldBlock` may be `null`. + + #### bot.entity Your own entity. See `Entity`. @@ -1036,14 +1053,14 @@ or boat. #### "blockUpdate" (oldBlock, newBlock) -Fires when a block updates. Both `oldBlock` and `newBlock` provided for +(It is better to use this event from bot.world instead of bot directly) Fires when a block updates. Both `oldBlock` and `newBlock` provided for comparison. Note that `oldBlock` may be `null`. #### "blockUpdate:(x, y, z)" (oldBlock, newBlock) -Fires for a specific point. Both `oldBlock` and `newBlock` provided for +(It is better to use this event from bot.world instead of bot directly) Fires for a specific point. Both `oldBlock` and `newBlock` provided for comparison. Note that `oldBlock` may be `null`. diff --git a/lib/plugins/blocks.js b/lib/plugins/blocks.js index 22fc69193..385c012c3 100644 --- a/lib/plugins/blocks.js +++ b/lib/plugins/blocks.js @@ -80,13 +80,10 @@ function inject (bot, { version, storageBuilder }) { } function delColumn (chunkX, chunkZ) { - const columnCorner = new Vec3(chunkX * 16, 0, chunkZ * 16) bot.world.unloadColumn(chunkX, chunkZ) - bot.emit('chunkColumnUnload', columnCorner) } function addColumn (args) { - const columnCorner = new Vec3(args.x * 16, 0, args.z * 16) if (!args.bitMap && args.groundUp) { // stop storing the chunk column delColumn(args.x, args.z) @@ -95,7 +92,6 @@ function inject (bot, { version, storageBuilder }) { let column = bot.world.getColumn(args.x, args.z) if (!column) { column = new Chunk() - bot.world.setColumn(args.x, args.z, column) } try { @@ -103,12 +99,10 @@ function inject (bot, { version, storageBuilder }) { if (args.biomes !== undefined) { column.loadBiomes(args.biomes) } + bot.world.setColumn(args.x, args.z, column) } catch (e) { bot.emit('error', e) - return } - - bot.emit('chunkColumnLoad', columnCorner) } async function waitForChunksToLoad () { @@ -253,14 +247,6 @@ function inject (bot, { version, storageBuilder }) { return blockAtCursor && blockAtCursor.position.equals(block.position) } - function emitBlockUpdate (oldBlock, newBlock) { - bot.emit('blockUpdate', oldBlock, newBlock) - const position = oldBlock - ? oldBlock.position - : (newBlock ? newBlock.position : null) - if (position) bot.emit(`blockUpdate:${newBlock.position}`, oldBlock, newBlock) - } - bot._client.on('unload_chunk', (packet) => { delColumn(packet.chunkX, packet.chunkZ) }) @@ -283,8 +269,6 @@ function inject (bot, { version, storageBuilder }) { const painting = paintingsByPos[loc.floored] if (painting) deletePainting(painting) } - - emitBlockUpdate(oldBlock, newBlock) } bot._client.on('update_light', (packet) => { @@ -406,7 +390,6 @@ function inject (bot, { version, storageBuilder }) { bot._client.on('update_sign', (packet) => { const pos = new Vec3(packet.location.x, packet.location.y, packet.location.z) - const oldBlock = blockAt(pos) const prepareString = (i) => { let text = packet[`text${i}`] @@ -429,8 +412,6 @@ function inject (bot, { version, storageBuilder }) { prepareString(3), prepareString(4) ].join('\n') - - emitBlockUpdate(oldBlock, blockAt(pos)) }) bot._client.on('tile_entity_data', (packet) => { @@ -473,13 +454,35 @@ function inject (bot, { version, storageBuilder }) { bot._client.on('login', (packet) => { dimension = packet.dimension bot.world = new World(null, storageBuilder ? storageBuilder({ version: bot.version, worldName: dimensionToFolderName(packet.dimension) }) : null).sync + startListenerProxy() }) bot._client.on('respawn', (packet) => { if (dimension === packet.dimension) return dimension = packet.dimension bot.world = new World(null, storageBuilder ? storageBuilder({ version: bot.version, worldName: dimensionToFolderName(packet.dimension) }) : null).sync + startListenerProxy() }) + function startListenerProxy () { + // standardized forwarding + const forwardedEvents = ['blockUpdate', 'chunkColumnLoad', 'chunkColumnUnload'] + for (const event of forwardedEvents) { + bot.world.on(event, (...args) => bot.emit(event, ...args)) + } + // custom forwarder for custom events + const blockUpdateRegex = /blockUpdate:\(-?\d+, -?\d+, -?\d+\)/ + bot.on('newListener', (event, listener) => { + if (blockUpdateRegex.test(event)) { + bot.world.on(event, listener) + } + }) + bot.on('removeListener', (event, listener) => { + if (blockUpdateRegex.test(event)) { + bot.world.on(event, listener) + } + }) + } + bot.findBlock = findBlock bot.canSeeBlock = canSeeBlock bot.blockAt = blockAt diff --git a/package.json b/package.json index b7b066784..7ff7267cb 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "prismarine-physics": "^1.0.4", "prismarine-recipe": "^1.1.0", "prismarine-windows": "^2.0.0", - "prismarine-world": "^3.3.1", + "prismarine-world": "^3.4.0", "protodef": "^1.8.0", "typed-emitter": "^1.2.0", "vec3": "^0.1.6"