Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS
GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/lib/plugins/scoreboard.js
1467 views
1
module.exports = inject
2
3
function inject (bot) {
4
const ScoreBoard = require('../scoreboard')(bot)
5
const scoreboards = {}
6
7
bot._client.on('scoreboard_objective', (packet) => {
8
if (packet.action === 0) {
9
const { name } = packet
10
const scoreboard = new ScoreBoard(packet)
11
scoreboards[name] = scoreboard
12
13
bot.emit('scoreboardCreated', scoreboard)
14
}
15
16
if (packet.action === 1) {
17
bot.emit('scoreboardDeleted', scoreboards[packet.name])
18
delete scoreboards[packet.name]
19
20
for (const position in ScoreBoard.positions) {
21
if (!ScoreBoard.positions[position]) continue
22
const scoreboard = ScoreBoard.positions[position]
23
24
if (scoreboard && scoreboard.name === packet.name) {
25
delete ScoreBoard.positions[position]
26
break
27
}
28
}
29
}
30
31
if (packet.action === 2) {
32
if (!Object.hasOwn(scoreboards, packet.name)) {
33
bot.emit('error', new Error(`Received update for unknown objective ${packet.name}`))
34
return
35
}
36
scoreboards[packet.name].setTitle(packet.displayText)
37
bot.emit('scoreboardTitleChanged', scoreboards[packet.name])
38
}
39
})
40
41
bot._client.on('scoreboard_score', (packet) => {
42
const scoreboard = scoreboards[packet.scoreName]
43
if (scoreboard !== undefined && packet.action === 0) {
44
const updated = scoreboard.add(packet.itemName, packet.value)
45
bot.emit('scoreUpdated', scoreboard, updated)
46
}
47
48
if (packet.action === 1) {
49
if (scoreboard !== undefined) {
50
const removed = scoreboard.remove(packet.itemName)
51
return bot.emit('scoreRemoved', scoreboard, removed)
52
}
53
54
for (const sb of Object.values(scoreboards)) {
55
if (packet.itemName in sb.itemsMap) {
56
const removed = sb.remove(packet.itemName)
57
return bot.emit('scoreRemoved', sb, removed)
58
}
59
}
60
}
61
})
62
63
bot._client.on('scoreboard_display_objective', (packet) => {
64
const { name, position } = packet
65
const scoreboard = scoreboards[name]
66
67
if (scoreboard !== undefined) {
68
bot.emit('scoreboardPosition', position, scoreboard, ScoreBoard.positions[position])
69
ScoreBoard.positions[position] = scoreboard
70
}
71
})
72
73
bot.scoreboards = scoreboards
74
bot.scoreboard = ScoreBoard.positions
75
}
76
77