Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support 1.20.4 #3310

Merged
merged 18 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 5 additions & 1 deletion lib/plugins/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,12 @@ function inject (bot, options) {
})

addDefaultPatterns()
bot._client.on('player_chat', (msg) => console.dir(msg, { depth: null }))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

debug code

bot._client.on('profileless_chat', (msg) => console.dir(msg, { depth: null }))

bot._client.on('playerChat', (data) => {
console.log('playerChat')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove

console.dir(data, { depth: null })
const message = data.formattedMessage
const verified = data.verified
let msg
Expand Down Expand Up @@ -144,7 +148,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
Loading
Loading