Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS
GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/lib/plugins/boss_bar.js
1467 views
1
module.exports = inject
2
3
function inject (bot, { version }) {
4
const BossBar = require('../bossbar')(bot.registry)
5
const bars = {}
6
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) {
15
if (packet.action === 0) {
16
bars[packet.entityUUID] = new BossBar(
17
packet.entityUUID,
18
extractTitle(packet.title),
19
packet.health,
20
packet.dividers,
21
packet.color,
22
packet.flags
23
)
24
bot.emit('bossBarCreated', bars[packet.entityUUID])
25
} else if (packet.action === 1) {
26
bot.emit('bossBarDeleted', bars[packet.entityUUID])
27
delete bars[packet.entityUUID]
28
} else {
29
if (!(packet.entityUUID in bars)) {
30
return
31
}
32
if (packet.action === 2 && packet.health !== undefined) {
33
bars[packet.entityUUID].health = packet.health
34
}
35
if (packet.action === 3 && packet.title !== undefined) {
36
bars[packet.entityUUID].title = extractTitle(packet.title)
37
}
38
if (packet.action === 4) {
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
}
45
}
46
if (packet.action === 5 && packet.flags !== undefined) {
47
bars[packet.entityUUID].flags = packet.flags
48
}
49
bot.emit('bossBarUpdated', bars[packet.entityUUID])
50
}
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)
57
58
Object.defineProperty(bot, 'bossBars', {
59
get () {
60
return Object.values(bars)
61
}
62
})
63
}
64
65