Skip to content

Commit

Permalink
world events through p-world instead of mineflayer (#1676)
Browse files Browse the repository at this point in the history
* world events through p-world instead of mineflayer

* replace unsafe api call with call to world

* replace updateBlockState with bot.world.setBlockStateId

* undo

* undo

* get rid of emitblockupdate since it's handled by p-world

* regex blockUpdate for coords, then pass to bot.world

* removed oldBlock

* add new bot.world events to api

* added info about proxied event

* go back to prod version of p-world

* add an easier way to proxy event listeners + proxy new events

* remove columncorner because not used anymore

* rom suggested change
  • Loading branch information
u9g authored Feb 20, 2021
1 parent bf5b3ee commit 79a7533
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
21 changes: 19 additions & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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`.
Expand Down
43 changes: 23 additions & 20 deletions lib/plugins/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -95,20 +92,17 @@ 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 {
column.load(args.data, args.bitMap, args.skyLightSent, args.groundUp)
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 () {
Expand Down Expand Up @@ -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)
})
Expand All @@ -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) => {
Expand Down Expand Up @@ -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}`]
Expand All @@ -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) => {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 79a7533

Please sign in to comment.