Skip to content

Commit b6950e9

Browse files
authored
Add boss bar test (#3655)
* wip * starts passing * fix lint * do not fail before 1.13 * add boss bar example * remove debug logging
1 parent faf32b0 commit b6950e9

4 files changed

Lines changed: 171 additions & 16 deletions

File tree

examples/bossbar.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const mineflayer = require('mineflayer')
2+
3+
if (process.argv.length < 4 || process.argv.length > 6) {
4+
console.log('Usage : node bossbar.js <host> <port> [<name>] [<password>]')
5+
process.exit(1)
6+
}
7+
8+
const bot = mineflayer.createBot({
9+
host: process.argv[2],
10+
port: parseInt(process.argv[3]),
11+
username: process.argv[4] ? process.argv[4] : 'bossbar_bot',
12+
password: process.argv[5]
13+
})
14+
15+
// Wait for spawn
16+
bot.once('spawn', () => {
17+
console.log('Bot spawned!')
18+
19+
// Create a boss bar
20+
bot.chat('/bossbar add test:bar "Test Boss Bar"')
21+
bot.chat('/bossbar set test:bar players ' + bot.username)
22+
bot.chat('/bossbar set test:bar color red')
23+
bot.chat('/bossbar set test:bar style notched_6')
24+
bot.chat('/bossbar set test:bar value 50')
25+
})
26+
27+
// Listen for boss bar events
28+
bot.on('bossBarCreated', (bossBar) => {
29+
console.log('Boss bar created:', bossBar.title.toString())
30+
})
31+
32+
bot.on('bossBarUpdated', (bossBar) => {
33+
console.log('Boss bar updated:', {
34+
title: bossBar.title.toString(),
35+
health: bossBar.health,
36+
color: bossBar.color,
37+
dividers: bossBar.dividers
38+
})
39+
})
40+
41+
bot.on('bossBarDeleted', (bossBar) => {
42+
console.log('Boss bar deleted:', bossBar.title.toString())
43+
})
44+
45+
// After 5 seconds, update the boss bar
46+
setTimeout(() => {
47+
bot.chat('/bossbar set test:bar color blue')
48+
bot.chat('/bossbar set test:bar style notched_10')
49+
bot.chat('/bossbar set test:bar value 75')
50+
}, 5000)
51+
52+
// After 10 seconds, remove the boss bar
53+
setTimeout(() => {
54+
bot.chat('/bossbar remove test:bar')
55+
}, 10000)

lib/bossbar.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function loader (registry) {
66
return class BossBar {
77
constructor (uuid, title, health, dividers, color, flags) {
88
this._entityUUID = uuid
9-
this._title = ChatMessage.fromNotch(title)
9+
this.title = title
1010
this._health = health
1111
this._dividers = divisions[dividers]
1212
this._color = colors[color]
@@ -20,7 +20,18 @@ function loader (registry) {
2020
}
2121

2222
set title (title) {
23-
this._title = ChatMessage.fromNotch(title)
23+
if (title && typeof title === 'object' && title.type === 'string' && 'value' in title) {
24+
this._title = title.value
25+
} else {
26+
const chatMsg = ChatMessage.fromNotch(title)
27+
if (chatMsg !== undefined && chatMsg !== null) {
28+
this._title = chatMsg
29+
} else if (typeof title === 'string') {
30+
this._title = title
31+
} else {
32+
this._title = ''
33+
}
34+
}
2435
}
2536

2637
set health (health) {

lib/plugins/boss_bar.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,23 @@ function inject (bot, { version }) {
44
const BossBar = require('../bossbar')(bot.registry)
55
const bars = {}
66

7-
bot._client.on('boss_bar', (packet) => {
7+
function extractTitle (title) {
8+
if (!title) return ''
9+
if (typeof title === 'string') return title
10+
// Return the original object for BossBar to handle
11+
return title
12+
}
13+
14+
function handleBossBarPacket (packet) {
815
if (packet.action === 0) {
916
bars[packet.entityUUID] = new BossBar(
1017
packet.entityUUID,
11-
packet.title,
18+
extractTitle(packet.title),
1219
packet.health,
1320
packet.dividers,
1421
packet.color,
1522
packet.flags
1623
)
17-
1824
bot.emit('bossBarCreated', bars[packet.entityUUID])
1925
} else if (packet.action === 1) {
2026
bot.emit('bossBarDeleted', bars[packet.entityUUID])
@@ -23,26 +29,31 @@ function inject (bot, { version }) {
2329
if (!(packet.entityUUID in bars)) {
2430
return
2531
}
26-
if (packet.action === 2) {
32+
if (packet.action === 2 && packet.health !== undefined) {
2733
bars[packet.entityUUID].health = packet.health
2834
}
29-
30-
if (packet.action === 3) {
31-
bars[packet.entityUUID].title = packet.title
35+
if (packet.action === 3 && packet.title !== undefined) {
36+
bars[packet.entityUUID].title = extractTitle(packet.title)
3237
}
33-
3438
if (packet.action === 4) {
35-
bars[packet.entityUUID].dividers = packet.dividers
36-
bars[packet.entityUUID].color = packet.color
39+
if (packet.dividers !== undefined) {
40+
bars[packet.entityUUID].dividers = packet.dividers
41+
}
42+
if (packet.color !== undefined) {
43+
bars[packet.entityUUID].color = packet.color
44+
}
3745
}
38-
39-
if (packet.action === 5) {
46+
if (packet.action === 5 && packet.flags !== undefined) {
4047
bars[packet.entityUUID].flags = packet.flags
4148
}
42-
4349
bot.emit('bossBarUpdated', bars[packet.entityUUID])
4450
}
45-
})
51+
}
52+
53+
// Handle all possible packet names
54+
bot._client.on('boss_bar', handleBossBarPacket)
55+
bot._client.on('bossbar', handleBossBarPacket)
56+
bot._client.on('boss_bar_update', handleBossBarPacket)
4657

4758
Object.defineProperty(bot, 'bossBars', {
4859
get () {

test/externalTests/bossBar.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
const assert = require('assert')
2+
const { once } = require('../../lib/promise_utils')
3+
4+
module.exports = (version) => async (bot) => {
5+
// Skip test for versions older than 1.13 (bossbar command not available)
6+
if (bot.registry.isOlderThan('1.13')) return
7+
8+
console.log('[bossBar test] Starting boss bar tests')
9+
10+
// Test boss bar creation
11+
const createBossBar = async () => {
12+
const uuid = 'test:bar1'
13+
const title = 'Test Boss Bar'
14+
const colorName = 'red'
15+
const styleName = 'notched_6'
16+
const health = 0.5
17+
18+
bot.test.sayEverywhere(`/bossbar add ${uuid} "${title}"`)
19+
bot.test.sayEverywhere(`/bossbar set ${uuid} players ${bot.username}`)
20+
bot.test.sayEverywhere(`/bossbar set ${uuid} color ${colorName}`)
21+
bot.test.sayEverywhere(`/bossbar set ${uuid} style ${styleName}`)
22+
bot.test.sayEverywhere(`/bossbar set ${uuid} value ${health * 100}`)
23+
bot.test.sayEverywhere(`/bossbar set ${uuid} visible true`)
24+
25+
// Wait for the boss bar to be created
26+
const [bossBar] = await once(bot, 'bossBarCreated')
27+
console.log('DEBUG bossBar:', bossBar)
28+
console.log('DEBUG bossBar own properties:', Object.getOwnPropertyNames(bossBar))
29+
console.log('DEBUG bossBar prototype:', Object.getPrototypeOf(bossBar))
30+
console.log('DEBUG bossBar.title:', bossBar.title, 'type:', typeof bossBar.title)
31+
assert.strictEqual(bossBar.title.toString(), title)
32+
// Do not check dividers or color here; they will be updated in the next step
33+
}
34+
35+
// Test boss bar update
36+
const updateBossBar = async () => {
37+
const uuid = 'test:bar1'
38+
const newHealth = 0.75
39+
const newColor = 'blue'
40+
const newStyle = 'notched_10'
41+
42+
bot.test.sayEverywhere(`/bossbar set ${uuid} color ${newColor}`)
43+
bot.test.sayEverywhere(`/bossbar set ${uuid} style ${newStyle}`)
44+
bot.test.sayEverywhere(`/bossbar set ${uuid} value ${newHealth * 100}`)
45+
46+
// Wait for the boss bar to have the expected state
47+
let bossBar
48+
while (true) {
49+
[bossBar] = await once(bot, 'bossBarUpdated')
50+
if (
51+
bossBar.health === newHealth &&
52+
bossBar.color === newColor &&
53+
bossBar.dividers === 10
54+
) break
55+
}
56+
assert.strictEqual(bossBar.health, newHealth)
57+
assert.strictEqual(bossBar.color, newColor)
58+
assert.strictEqual(bossBar.dividers, 10)
59+
}
60+
61+
// Test boss bar deletion
62+
const deleteBossBar = async () => {
63+
const uuid = 'test:bar1'
64+
bot.test.sayEverywhere(`/bossbar remove ${uuid}`)
65+
await once(bot, 'bossBarDeleted')
66+
assert.strictEqual(bot.bossBars.length, 0)
67+
}
68+
69+
try {
70+
await createBossBar()
71+
await updateBossBar()
72+
await deleteBossBar()
73+
console.log('[bossBar test] All tests passed!')
74+
} catch (err) {
75+
console.error('[bossBar test] Test failed:', err)
76+
throw err
77+
}
78+
}

0 commit comments

Comments
 (0)