From ffcba9d273fbb42cbfddbb98e33db251c09646ec Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Fri, 16 Feb 2024 23:05:07 +0100 Subject: [PATCH 01/18] Support 1.20.4 --- lib/version.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/version.js b/lib/version.js index 059ef3dea..b5306c667 100644 --- a/lib/version.js +++ b/lib/version.js @@ -1,4 +1,4 @@ -const testedVersions = ['1.8.8', '1.9.4', '1.10.2', '1.11.2', '1.12.2', '1.13.2', '1.14.4', '1.15.2', '1.16.5', '1.17.1', '1.18.2', '1.19', '1.19.2', '1.19.3', '1.19.4', '1.20.1', '1.20.2'] +const testedVersions = ['1.8.8', '1.9.4', '1.10.2', '1.11.2', '1.12.2', '1.13.2', '1.14.4', '1.15.2', '1.16.5', '1.17.1', '1.18.2', '1.19', '1.19.2', '1.19.3', '1.19.4', '1.20.1', '1.20.2', '1.20.4'] module.exports = { testedVersions, From 16121da4b7208843f89153ee2713368acd52e08b Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Fri, 16 Feb 2024 23:06:00 +0100 Subject: [PATCH 02/18] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5881f496b..e3f7e7aaa 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "license": "MIT", "dependencies": { "minecraft-data": "^3.56.0", - "minecraft-protocol": "^1.44.0", + "minecraft-protocol": "wgaylord/node-minecraft-protocol#1.20.3", "prismarine-biome": "^1.1.1", "prismarine-block": "^1.17.0", "prismarine-chat": "^1.7.1", From 43dfe27213f51884a2af18f00783e4790c3f0ba4 Mon Sep 17 00:00:00 2001 From: extremeheat Date: Thu, 22 Feb 2024 03:04:25 +0000 Subject: [PATCH 03/18] fix externalTest hang on op message waiting --- test/externalTest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/externalTest.js b/test/externalTest.js index 91997f5b5..02cdbe44e 100644 --- a/test/externalTest.js +++ b/test/externalTest.js @@ -68,7 +68,7 @@ for (const supportedVersion of mineflayer.testedVersions) { bot.once('spawn', () => { wrap.writeServer('op flatbot\n') bot.once('messagestr', msg => { - if (msg === '[Server: Made flatbot a server operator]' || msg === '[Server: Opped flatbot]') { + if (msg.includes('Made flatbot a server operator') || msg === '[Server: Opped flatbot]') { done() } }) From 95b36440342936dec5daa5bff340d1cc2fd39086 Mon Sep 17 00:00:00 2001 From: extremeheat Date: Thu, 22 Feb 2024 03:19:08 +0000 Subject: [PATCH 04/18] add more context to to errors --- lib/plugins/anvil.js | 2 +- lib/plugins/chat.js | 2 +- lib/plugins/chest.js | 2 +- lib/plugins/craft.js | 4 ++-- lib/plugins/enchantment_table.js | 2 +- lib/plugins/villager.js | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/plugins/anvil.js b/lib/plugins/anvil.js index 5bfe97387..d3e23a81b 100644 --- a/lib/plugins/anvil.js +++ b/lib/plugins/anvil.js @@ -12,7 +12,7 @@ function inject (bot) { async function openAnvil (anvilBlock) { const anvil = await bot.openBlock(anvilBlock) if (!matchWindowType(anvil)) { - throw new Error('This is not a anvil-like window') + throw new Error('Not a anvil-like window: ' + JSON.stringify(anvil)) } function err (name) { diff --git a/lib/plugins/chat.js b/lib/plugins/chat.js index 8a24a5451..313258209 100644 --- a/lib/plugins/chat.js +++ b/lib/plugins/chat.js @@ -144,7 +144,7 @@ function inject (bot, options) { function chatWithHeader (header, message) { if (typeof message === 'number') message = message.toString() if (typeof message !== 'string') { - throw new Error('Incorrect type! Should be a string or number.') + throw new Error('Chat message type must be a string or number: ' + typeof message) } if (!header && message.startsWith('/')) { diff --git a/lib/plugins/chest.js b/lib/plugins/chest.js index bafd59e0c..82e85ff00 100644 --- a/lib/plugins/chest.js +++ b/lib/plugins/chest.js @@ -23,7 +23,7 @@ function inject (bot) { throw new Error('containerToOpen is neither a block nor an entity') } - if (!matchWindowType(chest)) { throw new Error('Non-container window used as a container') } + if (!matchWindowType(chest)) { throw new Error('Non-container window used as a container: ' + JSON.stringify(chest)) } return chest } diff --git a/lib/plugins/craft.js b/lib/plugins/craft.js index 3f773d9dd..6e88f3551 100644 --- a/lib/plugins/craft.js +++ b/lib/plugins/craft.js @@ -12,7 +12,7 @@ function inject (bot) { assert.ok(recipe) count = parseInt(count ?? 1, 10) if (recipe.requiresTable && !craftingTable) { - throw new Error('recipe requires craftingTable') + throw new Error('Recipe requires craftingTable, but one was not supplied: ' + JSON.stringify(recipe)) } try { @@ -41,7 +41,7 @@ function inject (bot) { windowCraftingTable = window } if (!windowCraftingTable.type.startsWith('minecraft:crafting')) { - throw new Error('crafting: non craftingTable used as craftingTable') + throw new Error('crafting: non craftingTable used as craftingTable: ' + windowCraftingTable.type) } await startClicking(windowCraftingTable, 3, 3) } else { diff --git a/lib/plugins/enchantment_table.js b/lib/plugins/enchantment_table.js index e6c4df937..3bfe486bd 100644 --- a/lib/plugins/enchantment_table.js +++ b/lib/plugins/enchantment_table.js @@ -9,7 +9,7 @@ function inject (bot) { let ready = false const enchantmentTable = await bot.openBlock(enchantmentTableBlock) if (!enchantmentTable.type.startsWith('minecraft:enchant')) { - throw new Error('This is not an enchantment table') + throw new Error('Expected minecraft:enchant when opening table but got ' + enchantmentTable.type) } resetEnchantmentOptions() diff --git a/lib/plugins/villager.js b/lib/plugins/villager.js index e6fdf5f57..0689f7267 100644 --- a/lib/plugins/villager.js +++ b/lib/plugins/villager.js @@ -78,7 +78,7 @@ function inject (bot, { version }) { bot._client.on(tradeListPacket, gotTrades) const villager = await villagerPromise if (villager.type !== 'minecraft:villager' && villager.type !== 'minecraft:merchant') { - throw new Error('This is not a villager') + throw new Error('Expected minecraft:villager or minecraft:mechant type, but got ' + villager.type) } villager.trades = null From 2a0f054bed91d7adae811e5cb3d420a8c3daf187 Mon Sep 17 00:00:00 2001 From: extremeheat Date: Thu, 22 Feb 2024 03:54:04 +0000 Subject: [PATCH 05/18] replace nodejs once() usage with mineflayer once() with a default 20s timeout --- lib/plugins/anvil.js | 2 +- lib/plugins/book.js | 2 +- lib/plugins/craft.js | 2 +- lib/plugins/creative.js | 4 ++-- lib/plugins/enchantment_table.js | 2 +- lib/plugins/inventory.js | 3 +-- lib/plugins/villager.js | 2 +- lib/promise_utils.js | 5 +++++ test/externalTests/anvil.js | 2 +- test/externalTests/bed.js | 2 +- test/externalTests/chat.js | 2 +- test/externalTests/commandBlock.js | 3 +-- test/externalTests/crafting.js | 2 +- test/externalTests/nether.js | 2 +- test/externalTests/placeEntity.js | 2 +- test/externalTests/plugins/testCommon.js | 2 +- test/externalTests/rain.js | 2 +- test/externalTests/scoreboard.js | 2 +- test/externalTests/spawnEvent.js | 2 +- test/externalTests/team.js | 2 +- test/externalTests/trade.js | 2 +- test/externalTests/useChests.js | 3 +-- 22 files changed, 27 insertions(+), 25 deletions(-) diff --git a/lib/plugins/anvil.js b/lib/plugins/anvil.js index d3e23a81b..9675a3a41 100644 --- a/lib/plugins/anvil.js +++ b/lib/plugins/anvil.js @@ -1,6 +1,6 @@ const assert = require('assert') const { sleep } = require('../promise_utils') -const { once } = require('events') +const { once } = require('../promise_utils') module.exports = inject diff --git a/lib/plugins/book.js b/lib/plugins/book.js index 4f1605813..56fb69bfb 100644 --- a/lib/plugins/book.js +++ b/lib/plugins/book.js @@ -1,5 +1,5 @@ const assert = require('assert') -const { once } = require('events') +const { once } = require('../promise_utils') module.exports = inject diff --git a/lib/plugins/craft.js b/lib/plugins/craft.js index 6e88f3551..a5fe56363 100644 --- a/lib/plugins/craft.js +++ b/lib/plugins/craft.js @@ -1,5 +1,5 @@ const assert = require('assert') -const { once } = require('events') +const { once } = require('../promise_utils') module.exports = inject diff --git a/lib/plugins/creative.js b/lib/plugins/creative.js index bb0dc878e..33c27305b 100644 --- a/lib/plugins/creative.js +++ b/lib/plugins/creative.js @@ -1,7 +1,7 @@ const assert = require('assert') const { Vec3 } = require('vec3') const { sleep, onceWithCleanup } = require('../promise_utils') -const { once } = require('events') +const { once } = require('../promise_utils') module.exports = inject @@ -70,7 +70,7 @@ function inject (bot) { // last step bot.entity.position = destination - await once(bot, 'move') + await once(bot, 'move', /* no timeout */ 0) } function startFlying () { diff --git a/lib/plugins/enchantment_table.js b/lib/plugins/enchantment_table.js index 3bfe486bd..988bed4ce 100644 --- a/lib/plugins/enchantment_table.js +++ b/lib/plugins/enchantment_table.js @@ -1,5 +1,5 @@ const assert = require('assert') -const { once } = require('events') +const { once } = require('../promise_utils') module.exports = inject diff --git a/lib/plugins/inventory.js b/lib/plugins/inventory.js index 89380cf9b..497021841 100644 --- a/lib/plugins/inventory.js +++ b/lib/plugins/inventory.js @@ -1,7 +1,6 @@ const assert = require('assert') const { Vec3 } = require('vec3') -const { once } = require('events') -const { sleep, createDoneTask, createTask, withTimeout } = require('../promise_utils') +const { once, sleep, createDoneTask, createTask, withTimeout } = require('../promise_utils') module.exports = inject diff --git a/lib/plugins/villager.js b/lib/plugins/villager.js index 0689f7267..e20e34a70 100644 --- a/lib/plugins/villager.js +++ b/lib/plugins/villager.js @@ -1,5 +1,5 @@ const assert = require('assert') -const { once } = require('events') +const { once } = require('../promise_utils') module.exports = inject diff --git a/lib/promise_utils.js b/lib/promise_utils.js index b0381362c..a2f2873b6 100644 --- a/lib/promise_utils.js +++ b/lib/promise_utils.js @@ -72,6 +72,10 @@ function onceWithCleanup (emitter, event, { timeout = 0, checkCondition = undefi return task.promise } +function once (emitter, event, timeout = 20000) { + return onceWithCleanup(emitter, event, { timeout }) +} + function withTimeout (promise, timeout) { return Promise.race([ promise, @@ -82,6 +86,7 @@ function withTimeout (promise, timeout) { } module.exports = { + once, sleep, createTask, createDoneTask, diff --git a/test/externalTests/anvil.js b/test/externalTests/anvil.js index 71f051272..7ad1d7c93 100644 --- a/test/externalTests/anvil.js +++ b/test/externalTests/anvil.js @@ -1,5 +1,5 @@ const assert = require('assert') -const { once } = require('events') +const { once } = require('../../lib/promise_utils') module.exports = () => { async function runTest (bot, testFunction) { diff --git a/test/externalTests/bed.js b/test/externalTests/bed.js index 35ecf034b..3f1e56267 100644 --- a/test/externalTests/bed.js +++ b/test/externalTests/bed.js @@ -1,5 +1,5 @@ const assert = require('assert') -const { once } = require('events') +const { once } = require('../../lib/promise_utils') module.exports = () => async (bot) => { const midnight = 18000 diff --git a/test/externalTests/chat.js b/test/externalTests/chat.js index 0c26cf02f..0d41a9102 100644 --- a/test/externalTests/chat.js +++ b/test/externalTests/chat.js @@ -1,5 +1,5 @@ const assert = require('assert') -const { once } = require('events') +const { once } = require('../../lib/promise_utils') module.exports = () => { async function runTest (bot, testFunction) { diff --git a/test/externalTests/commandBlock.js b/test/externalTests/commandBlock.js index c47234dd6..3004605fb 100644 --- a/test/externalTests/commandBlock.js +++ b/test/externalTests/commandBlock.js @@ -1,7 +1,6 @@ const assert = require('assert') -const { once } = require('events') const { Vec3 } = require('vec3') -const { onceWithCleanup } = require('../../lib/promise_utils') +const { once, onceWithCleanup } = require('../../lib/promise_utils') module.exports = () => async (bot) => { const command = `/say ${Math.floor(Math.random() * 1000)}` diff --git a/test/externalTests/crafting.js b/test/externalTests/crafting.js index 6bf7fd47c..eeed3d417 100644 --- a/test/externalTests/crafting.js +++ b/test/externalTests/crafting.js @@ -1,4 +1,4 @@ -const { once } = require('events') +const { once } = require('../../lib/promise_utils') const { Vec3 } = require('vec3') module.exports = () => async (bot) => { diff --git a/test/externalTests/nether.js b/test/externalTests/nether.js index 84c99fec6..c2560b302 100644 --- a/test/externalTests/nether.js +++ b/test/externalTests/nether.js @@ -1,6 +1,6 @@ const assert = require('assert') const Vec3 = require('vec3') -const { once } = require('events') +const { once } = require('../../lib/promise_utils') module.exports = () => async (bot) => { // Test spawn event on death diff --git a/test/externalTests/placeEntity.js b/test/externalTests/placeEntity.js index 48155e554..c88b92a9f 100644 --- a/test/externalTests/placeEntity.js +++ b/test/externalTests/placeEntity.js @@ -1,6 +1,6 @@ const assert = require('assert') const { Vec3 } = require('vec3') -const { once } = require('events') +const { once } = require('../../lib/promise_utils') module.exports = (version) => { async function runTest (bot, testFunction) { diff --git a/test/externalTests/plugins/testCommon.js b/test/externalTests/plugins/testCommon.js index 55d4d792b..b65154eed 100644 --- a/test/externalTests/plugins/testCommon.js +++ b/test/externalTests/plugins/testCommon.js @@ -1,7 +1,7 @@ const { Vec3 } = require('vec3') const { spawn } = require('child_process') -const { once } = require('events') +const { once } = require('../../lib/promise_utils') const process = require('process') const assert = require('assert') const { sleep, onceWithCleanup, withTimeout } = require('../../../lib/promise_utils') diff --git a/test/externalTests/rain.js b/test/externalTests/rain.js index be23c6502..8c3a952ad 100644 --- a/test/externalTests/rain.js +++ b/test/externalTests/rain.js @@ -1,5 +1,5 @@ const assert = require('assert') -const { once } = require('events') +const { once } = require('../../lib/promise_utils') module.exports = () => async (bot) => { bot.test.sayEverywhere('/weather clear') diff --git a/test/externalTests/scoreboard.js b/test/externalTests/scoreboard.js index cab934d0a..44c158d01 100644 --- a/test/externalTests/scoreboard.js +++ b/test/externalTests/scoreboard.js @@ -1,5 +1,5 @@ // const assert = require('assert') -// const { once } = require('events') +// const { once } = require('../../lib/promise_utils') module.exports = () => async (bot) => { // TODO: This is failing randomly, investigate and fix diff --git a/test/externalTests/spawnEvent.js b/test/externalTests/spawnEvent.js index 94d04ce72..251981411 100644 --- a/test/externalTests/spawnEvent.js +++ b/test/externalTests/spawnEvent.js @@ -1,5 +1,5 @@ const mineflayer = require('mineflayer') -const { once } = require('events') +const { once } = require('../../lib/promise_utils') module.exports = () => async (bot) => { // Test spawn event on login diff --git a/test/externalTests/team.js b/test/externalTests/team.js index 0eccbf937..6045d79a7 100644 --- a/test/externalTests/team.js +++ b/test/externalTests/team.js @@ -1,5 +1,5 @@ const assert = require('assert') -const { once } = require('events') +const { once } = require('../../lib/promise_utils') module.exports = () => async (bot) => { const p = once(bot, 'teamMemberAdded') diff --git a/test/externalTests/trade.js b/test/externalTests/trade.js index 1ad502bc8..7651a0dfc 100644 --- a/test/externalTests/trade.js +++ b/test/externalTests/trade.js @@ -1,5 +1,5 @@ const assert = require('assert') -const { once } = require('events') +const { once } = require('../../lib/promise_utils') module.exports = () => async (bot) => { const Item = require('prismarine-item')(bot.registry) diff --git a/test/externalTests/useChests.js b/test/externalTests/useChests.js index 7c0f216b4..fff7a7445 100644 --- a/test/externalTests/useChests.js +++ b/test/externalTests/useChests.js @@ -1,7 +1,6 @@ const { Vec3 } = require('vec3') const assert = require('assert') -const { once } = require('events') -const { onceWithCleanup } = require('../../lib/promise_utils') +const { once, onceWithCleanup } = require('../../lib/promise_utils') module.exports = () => async (bot) => { const Item = require('prismarine-item')(bot.registry) From 7bb9ea09f2be4d9dc741beedc67625efe243438d Mon Sep 17 00:00:00 2001 From: extremeheat Date: Thu, 22 Feb 2024 03:58:10 +0000 Subject: [PATCH 06/18] fix path --- test/externalTests/plugins/testCommon.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/externalTests/plugins/testCommon.js b/test/externalTests/plugins/testCommon.js index b65154eed..c93b3547f 100644 --- a/test/externalTests/plugins/testCommon.js +++ b/test/externalTests/plugins/testCommon.js @@ -1,7 +1,7 @@ const { Vec3 } = require('vec3') const { spawn } = require('child_process') -const { once } = require('../../lib/promise_utils') +const { once } = require('../../../lib/promise_utils') const process = require('process') const assert = require('assert') const { sleep, onceWithCleanup, withTimeout } = require('../../../lib/promise_utils') From 074413c3299c684d4d41300d45113c8b9cea62b1 Mon Sep 17 00:00:00 2001 From: extremeheat Date: Thu, 22 Feb 2024 04:49:07 +0000 Subject: [PATCH 07/18] fix ChatMessages to use .fromNotch --- lib/bossbar.js | 187 ++++++++++++++++++++-------------------- lib/plugins/entities.js | 6 +- lib/plugins/tablist.js | 19 ++-- lib/team.js | 101 ++++++++++------------ 4 files changed, 154 insertions(+), 159 deletions(-) diff --git a/lib/bossbar.js b/lib/bossbar.js index 719bb2305..53a7b9d7e 100644 --- a/lib/bossbar.js +++ b/lib/bossbar.js @@ -1,101 +1,98 @@ -let ChatMessage const colors = ['pink', 'blue', 'red', 'green', 'yellow', 'purple', 'white'] const divisions = [0, 6, 10, 12, 20] -module.exports = loader - function loader (registry) { - ChatMessage = require('prismarine-chat')(registry) - return BossBar -} - -class BossBar { - constructor (uuid, title, health, dividers, color, flags) { - this._entityUUID = uuid - this._title = new ChatMessage(JSON.parse(title)) - this._health = health - this._dividers = divisions[dividers] - this._color = colors[color] - this._shouldDarkenSky = flags & 0x1 - this._isDragonBar = flags & 0x2 - this._createFog = flags & 0x4 - } - - set entityUUID (uuid) { - this._entityUUID = uuid - } - - set title (title) { - this._title = new ChatMessage(JSON.parse(title)) - } - - set health (health) { - this._health = health - } - - set dividers (dividers) { - this._dividers = divisions[dividers] - } - - set color (color) { - this._color = colors[color] - } - - set flags (flags) { - this._shouldDarkenSky = flags & 0x1 - this._isDragonBar = flags & 0x2 - this._createFog = flags & 0x4 - } - - get flags () { - return (this._shouldDarkenSky) | (this._isDragonBar << 1) | (this._createFog << 2) - } - - set shouldDarkenSky (darkenSky) { - this._shouldDarkenSky = darkenSky - } - - set isDragonBar (dragonBar) { - this._isDragonBar = dragonBar - } - - get createFog () { - return this._createFog - } - - set createFog (createFog) { - this._createFog = createFog - } - - get entityUUID () { - return this._entityUUID - } - - get title () { - return this._title - } - - get health () { - return this._health - } - - get dividers () { - return this._dividers - } - - get color () { - return this._color - } - - get shouldDarkenSky () { - return this._shouldDarkenSky - } - - get isDragonBar () { - return this._isDragonBar - } - - get shouldCreateFog () { - return this._createFog + const ChatMessage = require('prismarine-chat')(registry) + return class BossBar { + constructor (uuid, title, health, dividers, color, flags) { + this._entityUUID = uuid + this._title = ChatMessage.fromNotch(title) + this._health = health + this._dividers = divisions[dividers] + this._color = colors[color] + this._shouldDarkenSky = flags & 0x1 + this._isDragonBar = flags & 0x2 + this._createFog = flags & 0x4 + } + + set entityUUID (uuid) { + this._entityUUID = uuid + } + + set title (title) { + this._title = ChatMessage.fromNotch(title) + } + + set health (health) { + this._health = health + } + + set dividers (dividers) { + this._dividers = divisions[dividers] + } + + set color (color) { + this._color = colors[color] + } + + set flags (flags) { + this._shouldDarkenSky = flags & 0x1 + this._isDragonBar = flags & 0x2 + this._createFog = flags & 0x4 + } + + get flags () { + return (this._shouldDarkenSky) | (this._isDragonBar << 1) | (this._createFog << 2) + } + + set shouldDarkenSky (darkenSky) { + this._shouldDarkenSky = darkenSky + } + + set isDragonBar (dragonBar) { + this._isDragonBar = dragonBar + } + + get createFog () { + return this._createFog + } + + set createFog (createFog) { + this._createFog = createFog + } + + get entityUUID () { + return this._entityUUID + } + + get title () { + return this._title + } + + get health () { + return this._health + } + + get dividers () { + return this._dividers + } + + get color () { + return this._color + } + + get shouldDarkenSky () { + return this._shouldDarkenSky + } + + get isDragonBar () { + return this._isDragonBar + } + + get shouldCreateFog () { + return this._createFog + } } } + +module.exports = loader diff --git a/lib/plugins/entities.js b/lib/plugins/entities.js index 135219521..84034e0b5 100644 --- a/lib/plugins/entities.js +++ b/lib/plugins/entities.js @@ -597,7 +597,7 @@ function inject (bot) { } if (item.displayName) { - obj.displayName = new ChatMessage(JSON.parse(item.displayName)) + obj.displayName = ChatMessage.fromNotch(JSON.parse(item.displayName)) } else if (packet.action & 32) obj.displayName = new ChatMessage({ text: '', extra: [{ text: player.username || obj.username }] }) if (newPlayer) { @@ -660,7 +660,7 @@ function inject (bot) { } if (item.displayName) { - player.displayName = new ChatMessage(JSON.parse(item.displayName)) + player.displayName = ChatMessage.fromNotch(item.displayName) } const playerEntity = Object.values(bot.entities).find(e => e.type === 'player' && e.username === item.name) @@ -681,7 +681,7 @@ function inject (bot) { } else if (packet.action === 3 && !item.displayName) { player.displayName = new ChatMessage({ text: '', extra: [{ text: player.username }] }) } else if (packet.action === 3 && item.displayName) { - player.displayName = new ChatMessage(JSON.parse(item.displayName)) + player.displayName = ChatMessage.fromNotch(item.displayName) } else if (packet.action === 4) { if (player.entity === bot.entity) continue diff --git a/lib/plugins/tablist.js b/lib/plugins/tablist.js index c8799c42e..b27b49da8 100644 --- a/lib/plugins/tablist.js +++ b/lib/plugins/tablist.js @@ -13,14 +13,19 @@ function inject (bot) { } bot._client.on('playerlist_header', (packet) => { - if (packet.header) { - const header = escapeValueNewlines(packet.header) - bot.tablist.header = new ChatMessage(JSON.parse(header)) - } + if (bot.supportFeature('chatPacketsUseNbtComponents')) { // 1.20.3+ + bot.tablist.header = ChatMessage.fromNotch(packet.header) + bot.tablist.footer = ChatMessage.fromNotch(packet.footer) + } else { + if (packet.header) { + const header = escapeValueNewlines(packet.header) + bot.tablist.header = ChatMessage.fromNotch(header) + } - if (packet.footer) { - const footer = escapeValueNewlines(packet.footer) - bot.tablist.footer = new ChatMessage(JSON.parse(footer)) + if (packet.footer) { + const footer = escapeValueNewlines(packet.footer) + bot.tablist.footer = ChatMessage.fromNotch(footer) + } } }) } diff --git a/lib/team.js b/lib/team.js index 2b83ff878..080c00c24 100644 --- a/lib/team.js +++ b/lib/team.js @@ -1,14 +1,3 @@ -let ChatMessage -let MessageBuilder - -module.exports = loader - -function loader (registry) { - ChatMessage = require('prismarine-chat')(registry) - MessageBuilder = ChatMessage.MessageBuilder - return Team -} - function colorString (color) { const formatting = [ 'black', @@ -38,56 +27,60 @@ function colorString (color) { return formatting[color] } -class Team { - constructor (team, name, friendlyFire, nameTagVisibility, collisionRule, formatting, prefix, suffix) { - this.team = team - this.update(name, friendlyFire, nameTagVisibility, collisionRule, formatting, prefix, suffix) - this.membersMap = {} - } +function loader (registry) { + const ChatMessage = require('prismarine-chat')(registry) + const MessageBuilder = ChatMessage.MessageBuilder + return class Team { + constructor (team, name, friendlyFire, nameTagVisibility, collisionRule, formatting, prefix, suffix) { + this.team = team + this.update(name, friendlyFire, nameTagVisibility, collisionRule, formatting, prefix, suffix) + this.membersMap = {} + } - parseMessage (value) { - let result - try { - result = new ChatMessage(JSON.parse(value)) // version>1.13-pre7 - } catch { - result = MessageBuilder.fromString(value, { colorSeparator: '§' }) - if (result === null) { - return new ChatMessage('') + parseMessage (value) { + if (registry.supportFeature('teamUsesChatComponents')) { // 1.13+ + return ChatMessage.fromNotch(value) + } else { + const result = MessageBuilder.fromString(value, { colorSeparator: '§' }) + if (result === null) { + return new ChatMessage('') + } + return new ChatMessage(result.toJSON()) } - return new ChatMessage(result.toJSON()) } - return result - } - add (name) { - this.membersMap[name] = '' - return this.membersMap[name] - } + add (name) { + this.membersMap[name] = '' + return this.membersMap[name] + } - remove (name) { - const removed = this.membersMap[name] - delete this.membersMap[name] - return removed - } + remove (name) { + const removed = this.membersMap[name] + delete this.membersMap[name] + return removed + } - update (name, friendlyFire, nameTagVisibility, collisionRule, formatting, prefix, suffix) { - this.name = this.parseMessage(name) - this.friendlyFire = friendlyFire - this.nameTagVisibility = nameTagVisibility - this.collisionRule = collisionRule - this.color = colorString(formatting) - this.prefix = this.parseMessage(prefix) - this.suffix = this.parseMessage(suffix) - } + update (name, friendlyFire, nameTagVisibility, collisionRule, formatting, prefix, suffix) { + this.name = this.parseMessage(name) + this.friendlyFire = friendlyFire + this.nameTagVisibility = nameTagVisibility + this.collisionRule = collisionRule + this.color = colorString(formatting) + this.prefix = this.parseMessage(prefix) + this.suffix = this.parseMessage(suffix) + } - // Return a chat component with prefix + color + name + suffix - displayName (member) { - const name = this.prefix.clone() - name.append(new ChatMessage({ text: member, color: this.color }), this.suffix) - return name - } + // Return a chat component with prefix + color + name + suffix + displayName (member) { + const name = this.prefix.clone() + name.append(new ChatMessage({ text: member, color: this.color }), this.suffix) + return name + } - get members () { - return Object.keys(this.membersMap) + get members () { + return Object.keys(this.membersMap) + } } } + +module.exports = loader From 39976f3abec641bddcc01798b1ea582bf66c3bda Mon Sep 17 00:00:00 2001 From: extremeheat Date: Thu, 22 Feb 2024 22:31:25 -0500 Subject: [PATCH 08/18] Update chat.js add debug --- lib/plugins/chat.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/plugins/chat.js b/lib/plugins/chat.js index 313258209..b242b1ad7 100644 --- a/lib/plugins/chat.js +++ b/lib/plugins/chat.js @@ -109,6 +109,8 @@ function inject (bot, options) { addDefaultPatterns() bot._client.on('playerChat', (data) => { + console.log('playerChat') + console.dir(data, { depth: null }) const message = data.formattedMessage const verified = data.verified let msg From aa33c8e184466ca7969a0c4f7f8a8abb358bc9f6 Mon Sep 17 00:00:00 2001 From: extremeheat Date: Thu, 22 Feb 2024 23:01:33 -0500 Subject: [PATCH 09/18] add more debug --- lib/plugins/chat.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/plugins/chat.js b/lib/plugins/chat.js index b242b1ad7..d4d431d17 100644 --- a/lib/plugins/chat.js +++ b/lib/plugins/chat.js @@ -107,6 +107,8 @@ function inject (bot, options) { }) addDefaultPatterns() + bot._client.on('player_chat', (msg) => console.dir(msg, { depth: null })) + bot._client.on('profileless_chat', (msg) => console.dir(msg, { depth: null })) bot._client.on('playerChat', (data) => { console.log('playerChat') From 0527933da7e1cdf45b01585a0ac11791d0839c15 Mon Sep 17 00:00:00 2001 From: extremeheat Date: Thu, 22 Feb 2024 23:57:32 -0500 Subject: [PATCH 10/18] internalTest: fix chat --- test/internalTest.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/test/internalTest.js b/test/internalTest.js index 25b44e774..8dc07b69c 100644 --- a/test/internalTest.js +++ b/test/internalTest.js @@ -5,6 +5,7 @@ const vec3 = require('vec3') const mc = require('minecraft-protocol') const assert = require('assert') const { sleep } = require('../lib/promise_utils') +const nbt = require('prismarine-nbt') for (const supportedVersion of mineflayer.testedVersions) { const registry = require('prismarine-registry')(supportedVersion) @@ -12,6 +13,14 @@ for (const supportedVersion of mineflayer.testedVersions) { const Chunk = require('prismarine-chunk')(supportedVersion) const hasSignedChat = registry.supportFeature('signedChat') + function chatText (text) { + // TODO: move this to prismarine-chat in a new ChatMessage(text).toNotch(asNbt) method + return registry.supportFeature('chatPacketsUseNbtComponents') + ? nbt.comp({ text }) + : JSON.stringify({ text }) + } + + function generateChunkPacket (chunk) { const lights = chunk.dumpLight() return { @@ -119,13 +128,14 @@ for (const supportedVersion of mineflayer.testedVersions) { if (hasSignedChat) { const uuid = 'd3527a0b-bc03-45d5-a878-2aafdd8c8a43' // random + const networkName = chatText('gary') if (registry.supportFeature('useChatSessions')) { client.write('player_chat', { plainMessage: 'hello', filterType: 0, type: 0, - networkName: JSON.stringify({ text: 'gary' }), + networkName, previousMessages: [], senderUuid: uuid, timestamp: Date.now(), @@ -137,7 +147,7 @@ for (const supportedVersion of mineflayer.testedVersions) { plainMessage: 'hello', filterType: 0, type: 0, - networkName: JSON.stringify({ text: 'gary' }), + networkName, previousMessages: [], senderUuid: uuid, timestamp: Date.now(), @@ -539,7 +549,7 @@ for (const supportedVersion of mineflayer.testedVersions) { }, gamemode: 0, latency: 0, - displayName: '{"text":"wvffle"}' + displayName: chatText('wvffle') }] }) } else { @@ -556,7 +566,7 @@ for (const supportedVersion of mineflayer.testedVersions) { gamemode: 0, ping: 0, hasDisplayName: true, - displayName: '{"text":"wvffle"}' + displayName: chatText('wvffle') }] }) } From 4e2f6933a17907a690dbc81a7193dd92a610acfb Mon Sep 17 00:00:00 2001 From: extremeheat Date: Fri, 23 Feb 2024 00:19:28 -0500 Subject: [PATCH 11/18] fix nbt.string wrap --- test/internalTest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/internalTest.js b/test/internalTest.js index 8dc07b69c..8c2163127 100644 --- a/test/internalTest.js +++ b/test/internalTest.js @@ -16,7 +16,7 @@ for (const supportedVersion of mineflayer.testedVersions) { function chatText (text) { // TODO: move this to prismarine-chat in a new ChatMessage(text).toNotch(asNbt) method return registry.supportFeature('chatPacketsUseNbtComponents') - ? nbt.comp({ text }) + ? nbt.comp({ text: nbt.string(text) }) : JSON.stringify({ text }) } From 02425a777b76c0839bad26303f73b896b58b50ab Mon Sep 17 00:00:00 2001 From: extremeheat Date: Fri, 23 Feb 2024 00:24:30 -0500 Subject: [PATCH 12/18] update prismarine-windows dep --- package.json | 2 +- test/internalTest.js | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/package.json b/package.json index e3f7e7aaa..f4e410caf 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "prismarine-physics": "^1.8.0", "prismarine-recipe": "^1.3.0", "prismarine-registry": "^1.5.0", - "prismarine-windows": "^2.8.0", + "prismarine-windows": "wgaylord/prismarine-windows#patch-1", "prismarine-world": "^3.6.0", "protodef": "^1.14.0", "typed-emitter": "^1.0.0", diff --git a/test/internalTest.js b/test/internalTest.js index 8c2163127..912026dbe 100644 --- a/test/internalTest.js +++ b/test/internalTest.js @@ -20,7 +20,6 @@ for (const supportedVersion of mineflayer.testedVersions) { : JSON.stringify({ text }) } - function generateChunkPacket (chunk) { const lights = chunk.dumpLight() return { From d8a357f296fda50e1f0ebb96def9eca166ca641a Mon Sep 17 00:00:00 2001 From: extremeheat Date: Fri, 23 Feb 2024 00:44:41 -0500 Subject: [PATCH 13/18] fix --- lib/plugins/entities.js | 2 +- test/internalTest.js | 24 ++++++++++++++++-------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/plugins/entities.js b/lib/plugins/entities.js index 84034e0b5..e5eb918f4 100644 --- a/lib/plugins/entities.js +++ b/lib/plugins/entities.js @@ -597,7 +597,7 @@ function inject (bot) { } if (item.displayName) { - obj.displayName = ChatMessage.fromNotch(JSON.parse(item.displayName)) + obj.displayName = ChatMessage.fromNotch(item.displayName) } else if (packet.action & 32) obj.displayName = new ChatMessage({ text: '', extra: [{ text: player.username || obj.username }] }) if (newPlayer) { diff --git a/test/internalTest.js b/test/internalTest.js index 912026dbe..4df49da60 100644 --- a/test/internalTest.js +++ b/test/internalTest.js @@ -16,7 +16,7 @@ for (const supportedVersion of mineflayer.testedVersions) { function chatText (text) { // TODO: move this to prismarine-chat in a new ChatMessage(text).toNotch(asNbt) method return registry.supportFeature('chatPacketsUseNbtComponents') - ? nbt.comp({ text: nbt.string(text) }) + ? nbt.comp({ text: nbt.string(text), }) : JSON.stringify({ text }) } @@ -988,7 +988,6 @@ for (const supportedVersion of mineflayer.testedVersions) { it('handles newlines in header and footer', (done) => { const HEADER = 'asd\ndsa' const FOOTER = '\nas\nas\nas\n' - bot._client.on('playerlist_header', (packet) => { setImmediate(() => { assert.strictEqual(bot.tablist.header.toString(), HEADER) @@ -996,13 +995,22 @@ for (const supportedVersion of mineflayer.testedVersions) { done() }) }) - - server.on('playerJoin', (client) => { - client.write('playerlist_header', { - header: JSON.stringify({ text: '', extra: [{ text: HEADER, color: 'yellow' }] }), - footer: JSON.stringify({ text: '', extra: [{ text: FOOTER, color: 'yellow' }] }) + // TODO: figure out how the "extra" should be encoded in NBT so this branch can be removed + if (registry.supportFeature('chatPacketsUseNbtComponents')) { + server.on('playerJoin', (client) => { + client.write('playerlist_header', { + header: chatText(HEADER), + footer: chatText(FOOTER) + }) }) - }) + } else { + server.on('playerJoin', (client) => { + client.write('playerlist_header', { + header: JSON.stringify({ text: '', extra: [{ text: HEADER, color: 'yellow' }] }), + footer: JSON.stringify({ text: '', extra: [{ text: FOOTER, color: 'yellow' }] }) + }) + }) + } }) }) }) From 9995d23ebb844c729b6e90677b7e3d560dcb4fff Mon Sep 17 00:00:00 2001 From: extremeheat Date: Fri, 23 Feb 2024 01:09:09 -0500 Subject: [PATCH 14/18] add logging --- test/externalTests/useChests.js | 4 +++- test/internalTest.js | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/test/externalTests/useChests.js b/test/externalTests/useChests.js index fff7a7445..8fa0b2742 100644 --- a/test/externalTests/useChests.js +++ b/test/externalTests/useChests.js @@ -150,7 +150,9 @@ module.exports = () => async (bot) => { for (let slot = 0; slot < window.inventoryStart; slot++) { if (Math.random() < slotPopulationFactor) { - const item = bot.registry.itemsByName[getRandomStackableItem()] + const randomItem = getRandomStackableItem() + const item = bot.registry.itemsByName[randomItem] + console.log('createRandomLayout', randomItem, bot.registry.itemsByName) bot.chat(`/give ${bot.username} ${item.name} ${Math.ceil(Math.random() * item.stackSize)}`) await onceWithCleanup(window, 'updateSlot', { checkCondition: (slot, oldItem, newItem) => slot === window.hotbarStart && newItem?.name === item.name }) diff --git a/test/internalTest.js b/test/internalTest.js index 4df49da60..a8d917bfa 100644 --- a/test/internalTest.js +++ b/test/internalTest.js @@ -16,7 +16,7 @@ for (const supportedVersion of mineflayer.testedVersions) { function chatText (text) { // TODO: move this to prismarine-chat in a new ChatMessage(text).toNotch(asNbt) method return registry.supportFeature('chatPacketsUseNbtComponents') - ? nbt.comp({ text: nbt.string(text), }) + ? nbt.comp({ text: nbt.string(text) }) : JSON.stringify({ text }) } From 18850a0e6b4637a2856800b23c8beadaaacca334 Mon Sep 17 00:00:00 2001 From: extremeheat Date: Fri, 23 Feb 2024 01:22:24 -0500 Subject: [PATCH 15/18] replace grass -> mycelium for test --- test/externalTests/useChests.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/externalTests/useChests.js b/test/externalTests/useChests.js index 8fa0b2742..16526e1cf 100644 --- a/test/externalTests/useChests.js +++ b/test/externalTests/useChests.js @@ -128,7 +128,7 @@ module.exports = () => async (bot) => { await withdrawBones(largeTrappedChestLocations[0], 2) const itemsWithStackSize = { - 64: ['stone', 'grass'], + 64: ['stone', 'mycelium'], 16: ['ender_pearl', 'egg'], 1: ['fishing_rod', 'bow'] } From d7270a0a15a69e22f7b7552f59b8926ad2df007d Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Sun, 25 Feb 2024 00:31:34 +0100 Subject: [PATCH 16/18] remove debug console.log in chat.js --- lib/plugins/chat.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/plugins/chat.js b/lib/plugins/chat.js index d4d431d17..313258209 100644 --- a/lib/plugins/chat.js +++ b/lib/plugins/chat.js @@ -107,12 +107,8 @@ function inject (bot, options) { }) addDefaultPatterns() - bot._client.on('player_chat', (msg) => console.dir(msg, { depth: null })) - bot._client.on('profileless_chat', (msg) => console.dir(msg, { depth: null })) bot._client.on('playerChat', (data) => { - console.log('playerChat') - console.dir(data, { depth: null }) const message = data.formattedMessage const verified = data.verified let msg From 129c8043c1395a2a61266bd42c755261fd5b97c9 Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Sun, 25 Feb 2024 00:32:27 +0100 Subject: [PATCH 17/18] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f4e410caf..2a0070ac1 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "prismarine-physics": "^1.8.0", "prismarine-recipe": "^1.3.0", "prismarine-registry": "^1.5.0", - "prismarine-windows": "wgaylord/prismarine-windows#patch-1", + "prismarine-windows": "^2.9.0", "prismarine-world": "^3.6.0", "protodef": "^1.14.0", "typed-emitter": "^1.0.0", From 1f943650f4ae764a27755f5eed3c2fbbf5f5d233 Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Mon, 26 Feb 2024 01:21:16 +0100 Subject: [PATCH 18/18] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2a0070ac1..1d41cce19 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "license": "MIT", "dependencies": { "minecraft-data": "^3.56.0", - "minecraft-protocol": "wgaylord/node-minecraft-protocol#1.20.3", + "minecraft-protocol": "^1.47.0", "prismarine-biome": "^1.1.1", "prismarine-block": "^1.17.0", "prismarine-chat": "^1.7.1",