-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSidebar.lua
More file actions
executable file
·175 lines (145 loc) · 4.56 KB
/
Copy pathSidebar.lua
File metadata and controls
executable file
·175 lines (145 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
local paper_NumberFormat = importOrNil("io.papermc.paper.scoreboard.numbers.NumberFormat")
---@class bukkit.scoreboard.sidebar.Sidebar
---@field board bukkit.scoreboard.Scoreboard
---@field objective? bukkit.scoreboard.Objective
local this = {}
this.__index = this
---Gets or creates a new Sidebar instance for a player.
---@param player bukkit.entity.Player
function this.get(player)
local self = setmetatable({}, this)
self.board = bukkit.scoreboard.controller.get(player)
self.objective = self.board.getObjective("sidebar")
if self.objective == nil then
self.objective = self.board.registerNewObjective(
"sidebar",
bukkit.scoreboard.criteria("DUMMY"),
" "
)
self.objective.setDisplaySlot(
bukkit.scoreboard.displaySlot("SIDEBAR")
)
if paper_NumberFormat ~= nil then
self.objective.numberFormat(paper_NumberFormat.blank())
end
end
return self
end
function this:destroy()
self.objective.unregister()
self.objective = nil
end
---Sets the displayed title of the sidebar.
---@param title string
function this:setTitle(title)
self.objective.setDisplayName(bukkit.hex(title))
end
---Sets the displayed title of the sidebar.
---@param v adventure.text.Component
function this:title(v)
self.objective.setDisplayName(comp.legacySerialize(v))
end
---@param score string
---@param value number
function this:setRaw(score, value)
if self.objective == nil then return end
self.objective.getScore(score).setScore(value)
end
---@param line string
function this:remove(line)
if self.objective == nil then return end
self.board.resetScores(line)
end
function this:clear()
if self.objective == nil then return end
for entry in forEach(self.board.getEntries()) do
local score = self.objective.getScore(entry)
if score.isScoreSet() then
self.board.resetScores(entry)
end
end
end
---@param text string
---@param value number
---@param id? integer=`value` order of the line
function this:set(text, value, id)
if self.objective == nil then return end
if id == nil then id = value end
local score = "§"..string.format("%x", id).."§r"
self:setRaw(score, value)
local teamName = "s"..id
local team = self.board.getTeam(teamName)
or self.board.registerNewTeam(teamName)
team.addEntry(score)
team.setSuffix(text)
end
---@param lines string[]|java.array<string>
function this:setLines(lines)
if self.objective == nil then return end
for i = 1, 15 do
local line = lines[i]
if line == nil then
local team = self.board.getTeam("s"..i)
if team ~= nil then
team.unregister()
self:remove("§"..string.format("%x", i).."§r")
end
else
self:set(bukkit.hex(line), 0, i)
end
end
end
---@param v java.List<adventure.text.Component>
function this:lines(v)
if self.objective == nil then return end
local vSize = v.size()
for i = 1, 15 do
if i > vSize then
local team = self.board.getTeam("s"..i)
if team ~= nil then
team.unregister()
self:remove("§"..string.format("%x", i).."§r")
end
else
self:set(comp.legacySerialize(v.get(i - 1)), 0, i)
end
end
end
---@deprecated
function this:setScore(key, value)
scripting.warningDeprecated("bukkit/scoreboard/Sidebar#setScore")
self:set(bukkit.hex(key), value)
end
---@deprecated
function this:setScoreSimple(key, value)
scripting.warningDeprecated("bukkit/scoreboard/Sidebar#setScoreSimple")
self:setRaw(key, value)
end
---@deprecated
function this:removeScore(key)
scripting.warningDeprecated("bukkit/scoreboard/Sidebar#removeScore")
self.objective.getScoreboard().resetScores(key)
end
---@deprecated
function this:clearScores()
scripting.warningDeprecated("bukkit/scoreboard/Sidebar#clearScores")
for entry in forEach(self.board.getEntries()) do
local score = self.objective.getScore(entry)
if score.isScoreSet() then
self.board.resetScores(entry)
end
end
end
---@deprecated
---@param scores table<string, number>
function this:setEntries(scores)
scripting.warningDeprecated("bukkit/scoreboard/Sidebar#setEntries")
self:setLines(table.keys(scores))
end
---@deprecated
function this:setScores(list, startingIndex)
scripting.warningDeprecated("bukkit/scoreboard/Sidebar#setScores")
self:setLines(list)
end
bukkit.scoreboard.Sidebar = this
return this