/
ScoreboardHelper.java
398 lines (340 loc) · 14.5 KB
/
ScoreboardHelper.java
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package net.aufdemrand.denizen.utilities;
import com.google.common.base.Splitter;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.scripts.commands.server.ScoreboardCommand;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.objects.aH;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.scoreboard.*;
import java.util.*;
/**
* Creates, saves and loads scoreboards
*
* @author David Cernat
*/
public class ScoreboardHelper {
public static ScoreboardManager manager = Bukkit.getScoreboardManager();
// A map with scoreboard IDs as keys and scoreboards as values
public static Map<String, Scoreboard> scoreboardMap = new HashMap<String, Scoreboard>();
// A map with viewer names as keys and scoreboard IDs as values
public static Map<String, String> viewerMap = new HashMap<String, String>();
/*
* Called on server startup or /denizen reload saves
*/
public static void _recallScoreboards() {
// Clear every existing ingame scoreboard
for (Map.Entry<String, Scoreboard> entry : scoreboardMap.entrySet()) {
clearScoreboard(entry.getValue());
}
Scoreboard emptyBoard = createScoreboard();
// Clear every viewer's set scoreboard
for (Map.Entry<String, String> entry : viewerMap.entrySet()) {
OfflinePlayer player = ScoreboardCommand.getOfflinePlayer(entry.getKey());
if (player.isOnline()) {
player.getPlayer().setScoreboard(emptyBoard);
}
}
// Delete the contents of the scoreboardMap and viewerMap
scoreboardMap.clear();
viewerMap.clear();
ConfigurationSection rootSection = DenizenAPI.getCurrentInstance()
.getScoreboards().getConfigurationSection("Scoreboards");
// Go no further if we have no scoreboards saved
if (rootSection == null) {
return;
}
Scoreboard board = null;
// Iterate through scoreboards
for (String id : rootSection.getKeys(false)) {
// Create a scoreboard with this id
board = createScoreboard(id);
// Get the list of viewers
List<String> viewerList = rootSection.getStringList(id + ".Viewers");
// Iterate through viewers, store them in the viewerMap,
// and make them see this scoreboard if they are online
for (String viewer : viewerList) {
if (dPlayer.matches(viewer)) {
dPlayer player = dPlayer.valueOf(viewer);
viewerMap.put(player.getName(), id);
if (player.isOnline()) {
player.getPlayerEntity().setScoreboard(board);
}
}
}
ConfigurationSection objSection = rootSection
.getConfigurationSection(id + ".Objectives");
// Go no further if we have no objectives saved
if (objSection == null) {
return;
}
// Iterate through objectives
for (String obj : objSection.getKeys(false)) {
// Get display slot and criteria for this objective
String displaySlot = objSection.getString(obj + ".Display slot");
String criteria = objSection.getString(obj + ".Criteria");
// Use default criteria if necessary
if (criteria == null) {
criteria = "dummy";
}
// Use default display slot if necessary
if (displaySlot == null) {
displaySlot = "NONE";
}
// Register the objective and set it up
Objective o = board.registerNewObjective(obj, criteria);
o.setDisplayName(obj);
// Only set display slot if it's valid
if (aH.Argument.valueOf(displaySlot).matchesEnum(DisplaySlot.values())) {
o.setDisplaySlot(DisplaySlot.valueOf(displaySlot.toUpperCase()));
}
ConfigurationSection scoreSection = objSection
.getConfigurationSection(obj + ".Scores");
if (scoreSection != null) {
// Iterate through scores and add them to this objective
for (String scoreName : scoreSection.getKeys(false)) {
int scoreInt = scoreSection.getInt(scoreName);
addScore(o, ScoreboardCommand.getOfflinePlayer(scoreName), scoreInt);
}
}
}
}
}
/*
* Called on server shutdown or /denizen save
*/
public static void _saveScoreboards() {
try {
// Clear scoreboards.yml
DenizenAPI.getCurrentInstance().getScoreboards()
.set("Scoreboards", null);
// Iterate through scoreboards map
for (Map.Entry<String, Scoreboard> scoreboardEntry : scoreboardMap.entrySet()) {
String id = scoreboardEntry.getKey();
List<String> viewerList = new ArrayList<String>();
// Find all of the viewers that are viewing this scoreboard
// and put them on a list
for (Map.Entry<String, String> viewerEntry : viewerMap.entrySet()) {
if (id.equalsIgnoreCase(viewerEntry.getValue())) {
viewerList.add(viewerEntry.getKey());
}
}
// Save viewer list
DenizenAPI.getCurrentInstance().getScoreboards()
.set("Scoreboards." + id + ".Viewers", viewerList);
// Iterate through objectives
for (Objective obj : scoreboardEntry.getValue().getObjectives()) {
String objPath = "Scoreboards." + id + ".Objectives."
+ obj.getName();
// Save criteria for this objective
DenizenAPI.getCurrentInstance().getScoreboards()
.set(objPath + ".Criteria", obj.getCriteria());
String displaySlot;
// If the display slot is null, save it as "NONE";
// otherwise, save it with its regular name
if (obj.getDisplaySlot() != null) {
displaySlot = obj.getDisplaySlot().name();
}
else {
displaySlot = "NONE";
}
DenizenAPI.getCurrentInstance().getScoreboards()
.set(objPath + ".Display slot", displaySlot);
// There is no method for getting an objective's
// scores, so iterate through all of the scoreboard's
// players, check if they have a score for this
// objective, and save it if they do
for (String player : scoreboardEntry.getValue().getEntries()) {
int score = obj.getScore(player).getScore();
Team team = scoreboardEntry.getValue().getTeam(player);
// TODO: Properly save and load teams
player = (team != null && team.getPrefix() != null ? team.getPrefix() : "") + player + (team != null && team.getSuffix() != null ? team.getSuffix() : "");
// If a player has no score for this objective,
// getScore() will return 0, so ignore scores
// of 0
if (score != 0) {
DenizenAPI.getCurrentInstance().getScoreboards()
.set(objPath + ".Scores." + player, score);
}
}
}
}
}
catch (Exception e) {
dB.echoError(e);
}
}
/////////////////////
// OBJECTIVE METHODS
/////////////////
/**
* Add a score to an Objective for an OfflinePlayer.
*
* @param o the Objective to add the score to
* @param player the OfflinePlayer to set the score for
* @param score the score
*/
public static void addScore(Objective o, OfflinePlayer player, int score) {
Score sc;
if (player.getName().length() <= 16) {
sc = o.getScore(player.getName());
}
else {
Map.Entry<Team, String> teamData = createTeam(o.getScoreboard(), player.getName());
sc = o.getScore(teamData.getValue());
teamData.getKey().addPlayer(ScoreboardCommand.getOfflinePlayer(teamData.getValue()));
}
// If the score is 0, it won't normally be displayed at first,
// so force it to be displayed by using setScore() like below on it
if (score == 0) {
sc.setScore(1);
sc.setScore(0);
}
else {
sc.setScore(score);
}
}
/**
* Remove a score from an Objective for an OfflinePlayer.
*
* @param o the Objective to remove the score from
* @param player the OfflinePlayer to remove the score for
*/
public static void removeScore(Objective o, OfflinePlayer player) {
// There is no method to remove a single score from an
// objective, as confirmed here:
// https://bukkit.atlassian.net/browse/BUKKIT-4014
//
// So use crazy workaround below
Scoreboard board = o.getScoreboard();
Map<String, Integer> scoreMap = new HashMap<String, Integer>();
// Go through every score for this (real or fake) player
// and put it in scoreMap if it doesn't belong to the
// objective we want to remove the score from
String name = createTeam(board, player.getName()).getValue();
// TODO: Properly remove when teams are involved
for (Score sc : board.getScores(name)) {
if (!sc.getObjective().equals(o)) {
scoreMap.put(sc.getObjective().getName(), sc.getScore());
}
}
// Remove all the scores for this (real or fake) player
board.resetScores(player.getName());
// Go through scoreMap and add back all the scores we saved
// for this (real or fake) player
for (Map.Entry<String, Integer> entry : scoreMap.entrySet()) {
board.getObjective(entry.getKey())
.getScore(player.getName()).setScore(entry.getValue());
}
}
private static Map.Entry<Team, String> createTeam(Scoreboard scoreboard, String text) {
if (text.length() <= 16) {
return new HashMap.SimpleEntry<Team, String>(null, text);
}
if (text.length() <= 32) {
Team team = scoreboard.registerNewTeam("text-" + scoreboard.getTeams().size());
team.setPrefix(text.substring(0, text.length() - 16));
String result = text.substring(text.length() - 16);
return new HashMap.SimpleEntry<Team, String>(team, result);
}
Team team = scoreboard.registerNewTeam("text-" + scoreboard.getTeams().size());
Iterator<String> iterator = Splitter.fixedLength(16).split(text).iterator();
team.setPrefix(iterator.next());
String result = iterator.next();
if (text.length() > 32) {
team.setSuffix(iterator.next());
}
return new HashMap.SimpleEntry<Team, String>(team, result);
}
/////////////////////
// SCOREBOARD METHODS
/////////////////
/**
* Clears all the objectives from a Scoreboard, making
* it empty.
*
* @param board the Scoreboard to clear
*/
public static void clearScoreboard(Scoreboard board) {
for (Objective o : board.getObjectives()) {
o.unregister();
}
}
/**
* Creates an anonymous new Scoreboard that isn't
* saved anywhere.
*
* @return the new Scoreboard
*/
public static Scoreboard createScoreboard() {
return manager.getNewScoreboard();
}
/**
* Creates a new Scoreboard with a certain id and
* stories it in the scoreboards map.
*
* @param id the id of the new Scoreboard
* @return the new Scoreboard
*/
public static Scoreboard createScoreboard(String id) {
Scoreboard board = manager.getNewScoreboard();
scoreboardMap.put(id.toUpperCase(), board);
return board;
}
/**
* Deletes a Scoreboard, clearing it and removing it from
* the scoreboards map, unless it is the server's main
* scoreboard, in which case it is just cleared.
*
* @param id the id of the Scoreboard
*/
public static void deleteScoreboard(String id) {
if (id.equalsIgnoreCase("main")) {
clearScoreboard(getMain());
}
else {
clearScoreboard(getScoreboard(id));
scoreboardMap.remove(id.toUpperCase());
}
}
/**
* Returns the server's main scoreboard, that isn't stored
* in the scoreboards map because Bukkit already saves it
* by itself.
*
* @return the main Scoreboard
*/
public static Scoreboard getMain() {
return manager.getMainScoreboard();
}
/**
* Returns a Scoreboard from the scoreboards map.
*
* @param id the id of the Scoreboard
* @return the Scoreboard returned
*/
public static Scoreboard getScoreboard(String id) {
return scoreboardMap.get(id.toUpperCase());
}
/**
* Returns true if the scoreboards map contains a certain
* scoreboard id.
*
* @param id the id of the Scoreboard
* @return true or false
*/
public static boolean hasScoreboard(String id) {
return scoreboardMap.containsKey(id.toUpperCase());
}
/**
* Removes all the scores of an OfflinePlayer from a
* Scoreboard.
*
* @param id the id of the Scoreboard
* @param player the OfflinePlayer
*/
public static void removePlayer(String id, OfflinePlayer player) {
scoreboardMap.get(id.toUpperCase()).resetScores(player.getName());
}
}