Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
rom1504 authored Feb 26, 2024
2 parents fe74897 + bb3e587 commit a075ef0
Show file tree
Hide file tree
Showing 34 changed files with 236 additions and 208 deletions.
4 changes: 4 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1774,6 +1774,8 @@ the event will be called `"chat:name"`, with name being the name passed

returns a number which can be used with bot.removeChatPattern() to only delete this pattern

- :eyes: cf. [examples/chat_parsing](https://github.com/PrismarineJS/mineflayer/blob/master/examples/chat_parsing.js#L17-L36)

#### bot.addChatPatternSet(name, patterns, chatPatternOptions)

make an event that is called every time all patterns havee been matched to messages,
Expand All @@ -1786,6 +1788,8 @@ the event will be called `"chat:name"`, with name being the name passed

returns a number which can be used with bot.removeChatPattern() to only delete this patternset

- :eyes: cf. [examples/chat_parsing](https://github.com/PrismarineJS/mineflayer/blob/master/examples/chat_parsing.js#L17-L36)

#### bot.removeChatPattern(name)

removes a chat pattern(s)
Expand Down
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
8 changes: 8 additions & 0 deletions lib/plugins/scoreboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ function inject (bot) {
if (packet.action === 1) {
bot.emit('scoreboardDeleted', scoreboards[packet.name])
delete scoreboards[packet.name]

for (const position in ScoreBoard.positions) {
const scoreboard = ScoreBoard.positions[position]
if (scoreboard.name === packet.name) {
delete ScoreBoard.positions[position]
break
}
}
}

if (packet.action === 2) {
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)
}
}
})
}
Loading

0 comments on commit a075ef0

Please sign in to comment.