From c6755fc3758eeb3bbbb98479efe03b7935486247 Mon Sep 17 00:00:00 2001 From: Vitaly Date: Sat, 31 Aug 2024 17:46:41 +0300 Subject: [PATCH] correctly clean entities on world switch! --- index.d.ts | 5 +++-- lib/plugins/blocks.js | 1 + lib/plugins/entities.js | 13 ++++++++----- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/index.d.ts b/index.d.ts index 748e56a78..8403df714 100644 --- a/index.d.ts +++ b/index.d.ts @@ -62,6 +62,7 @@ export interface BotEvents { ) => Promise | void actionBar: (jsonMsg: ChatMessage) => Promise | void error: (err: Error) => Promise | void + worldSwitch: () => Promise | void message: (jsonMsg: ChatMessage, position: string) => Promise | void messagestr: (message: string, position: string, jsonMsg: ChatMessage) => Promise | void unmatchedMessage: (stringMsg: string, jsonMsg: ChatMessage) => Promise | void @@ -387,8 +388,8 @@ export interface Bot extends TypedEmitter { times?: number ) => Promise - - + + setCommandBlock: (pos: Vec3, command: string, options: CommandBlockOptions) => void clickWindow: ( diff --git a/lib/plugins/blocks.js b/lib/plugins/blocks.js index d3010c992..0055667de 100644 --- a/lib/plugins/blocks.js +++ b/lib/plugins/blocks.js @@ -502,6 +502,7 @@ function inject (bot, { version, storageBuilder, hideErrors }) { bot._getDimensionName = () => worldName async function switchWorld () { + bot.emit('worldSwitch') if (bot.world) { if (storageBuilder) { await bot.world.async.waitSaving() diff --git a/lib/plugins/entities.js b/lib/plugins/entities.js index f33ca57e0..43fda2598 100644 --- a/lib/plugins/entities.js +++ b/lib/plugins/entities.js @@ -95,12 +95,7 @@ function inject (bot) { return best } - // Reset list of players and entities on login bot._client.on('login', (packet) => { - bot.players = {} - bot.uuidToUsername = {} - removeAllEntities() - bot.entities = {} // login bot.entity = fetchEntity(packet.entityId) bot.username = bot._client.username @@ -112,6 +107,14 @@ function inject (bot) { bot.entity.eyeHeight = PLAYER_EYEHEIGHT }) + // Reset list of players and entities on world switch (login or respawn packet) + bot.on('worldSwitch', () => { + bot.players = {} + bot.uuidToUsername = {} + removeAllEntities() + bot.entities = {} + }) + bot._client.on('entity_equipment', (packet) => { // entity equipment const entity = fetchEntity(packet.entityId)