Skip to content

Commit

Permalink
Support 1.20.4 (#3310)
Browse files Browse the repository at this point in the history
* Support 1.20.4

* Update package.json

* fix externalTest hang on op message waiting

* add more context to to errors

* replace nodejs once() usage with mineflayer once() with a default 20s timeout

* fix path

* fix ChatMessages to use .fromNotch

* Update chat.js add debug

* add more debug

* internalTest: fix chat

* fix nbt.string wrap

* update prismarine-windows dep

* fix

* add logging

* replace grass -> mycelium for test

* remove debug console.log in chat.js

* Update package.json

* Update package.json

---------

Co-authored-by: extremeheat <extreme@protonmail.ch>
  • Loading branch information
rom1504 and extremeheat authored Feb 26, 2024
1 parent b9491ae commit aa99daa
Show file tree
Hide file tree
Showing 32 changed files with 224 additions and 208 deletions.
187 changes: 92 additions & 95 deletions lib/bossbar.js
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions lib/plugins/anvil.js
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/book.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const assert = require('assert')
const { once } = require('events')
const { once } = require('../promise_utils')

module.exports = inject

Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('/')) {
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/chest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions lib/plugins/craft.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const assert = require('assert')
const { once } = require('events')
const { once } = require('../promise_utils')

module.exports = inject

Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/creative.js
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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 () {
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/enchantment_table.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const assert = require('assert')
const { once } = require('events')
const { once } = require('../promise_utils')

module.exports = inject

Expand All @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions lib/plugins/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ function inject (bot) {
}

if (item.displayName) {
obj.displayName = new ChatMessage(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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand Down
3 changes: 1 addition & 2 deletions lib/plugins/inventory.js
Original file line number Diff line number Diff line change
@@ -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

Expand Down
19 changes: 12 additions & 7 deletions lib/plugins/tablist.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
})
}
4 changes: 2 additions & 2 deletions lib/plugins/villager.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const assert = require('assert')
const { once } = require('events')
const { once } = require('../promise_utils')

module.exports = inject

Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/promise_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -82,6 +86,7 @@ function withTimeout (promise, timeout) {
}

module.exports = {
once,
sleep,
createTask,
createDoneTask,
Expand Down
Loading

0 comments on commit aa99daa

Please sign in to comment.