Skip to content

Commit

Permalink
Fix world switch leak (#1773)
Browse files Browse the repository at this point in the history
  • Loading branch information
Karang authored Mar 18, 2021
1 parent 0746508 commit c3c3995
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions lib/plugins/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,17 +451,44 @@ function inject (bot, { version, storageBuilder }) {
return dimension
}
}

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) => {
dimension = packet.dimension
bot.world = new World(null, storageBuilder ? storageBuilder({ version: bot.version, worldName: dimensionToFolderName(packet.dimension) }) : null).sync
startListenerProxy()
switchWorld()
})

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()
switchWorld()
})

let listener
let listenerRemove
function startListenerProxy () {
Expand Down

0 comments on commit c3c3995

Please sign in to comment.