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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
|
/*
* This file is part of Kenshins Hide and Seek
*
* Copyright (c) 2021 Tyler Murphy.
*
* Kenshins Hide and Seek free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* he Free Software Foundation version 3.
*
* Kenshins Hide and Seek is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package net.tylermurphy.hideAndSeek.game;
import net.tylermurphy.hideAndSeek.Main;
import net.tylermurphy.hideAndSeek.game.events.Border;
import net.tylermurphy.hideAndSeek.game.events.Glow;
import net.tylermurphy.hideAndSeek.game.events.Taunt;
import net.tylermurphy.hideAndSeek.game.util.Status;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.*;
import java.util.*;
import java.util.stream.Collectors;
import static net.tylermurphy.hideAndSeek.configuration.Config.*;
import static net.tylermurphy.hideAndSeek.configuration.Leaderboard.*;
import static net.tylermurphy.hideAndSeek.configuration.Localization.message;
public class Board {
private final List<String> Hider = new ArrayList<>(), Seeker = new ArrayList<>(), Spectator = new ArrayList<>();
private final Map<String, Player> playerList = new HashMap<>();
private final Map<String, CustomBoard> customBoards = new HashMap<>();
private final Map<String, Integer> hider_kills = new HashMap<>(), seeker_kills = new HashMap<>(), hider_deaths = new HashMap<>(), seeker_deaths = new HashMap<>();
public boolean contains(Player player) {
return playerList.containsKey(player.getUniqueId().toString());
}
public boolean isHider(Player player) {
return Hider.contains(player.getUniqueId().toString());
}
public boolean isHider(UUID uuid) {
return Hider.contains(uuid.toString());
}
public boolean isSeeker(Player player) {
return Seeker.contains(player.getUniqueId().toString());
}
public boolean isSeeker(UUID uuid) {
return Seeker.contains(uuid.toString());
}
public boolean isSpectator(Player player) {
return Spectator.contains(player.getUniqueId().toString());
}
public int sizeHider() {
return Hider.size();
}
public int sizeSeeker() {
return Seeker.size();
}
public int size() {
return playerList.values().size();
}
public List<Player> getHiders() {
return Hider.stream().filter(Objects::nonNull).map(playerList::get).collect(Collectors.toList());
}
public List<Player> getSeekers() {
return Seeker.stream().filter(Objects::nonNull).map(playerList::get).collect(Collectors.toList());
}
public Player getFirstSeeker() {
return playerList.get(Seeker.get(0));
}
public List<Player> getSpectators() {
return Spectator.stream().filter(Objects::nonNull).map(playerList::get).collect(Collectors.toList());
}
public List<Player> getPlayers() {
return playerList.values().stream().filter(Objects::nonNull).collect(Collectors.toList());
}
public Player getPlayer(UUID uuid) {
return playerList.get(uuid.toString());
}
public void addHider(Player player) {
Hider.add(player.getUniqueId().toString());
Seeker.remove(player.getUniqueId().toString());
Spectator.remove(player.getUniqueId().toString());
playerList.put(player.getUniqueId().toString(), player);
}
public void addSeeker(Player player) {
Hider.remove(player.getUniqueId().toString());
Seeker.add(player.getUniqueId().toString());
Spectator.remove(player.getUniqueId().toString());
playerList.put(player.getUniqueId().toString(), player);
}
public void addSpectator(Player player) {
Hider.remove(player.getUniqueId().toString());
Seeker.remove(player.getUniqueId().toString());
Spectator.add(player.getUniqueId().toString());
playerList.put(player.getUniqueId().toString(), player);
}
public void remove(Player player) {
Hider.remove(player.getUniqueId().toString());
Seeker.remove(player.getUniqueId().toString());
Spectator.remove(player.getUniqueId().toString());
playerList.remove(player.getUniqueId().toString());
}
public boolean onSameTeam(Player player1, Player player2) {
if (Hider.contains(player1.getUniqueId().toString()) && Hider.contains(player2.getUniqueId().toString())) return true;
else if (Seeker.contains(player1.getUniqueId().toString()) && Seeker.contains(player2.getUniqueId().toString())) return true;
else return Spectator.contains(player1.getUniqueId().toString()) && Spectator.contains(player2.getUniqueId().toString());
}
public void reload() {
Hider.clear();
Seeker.clear();
Spectator.clear();
hider_kills.clear();
seeker_kills.clear();
hider_deaths.clear();
seeker_deaths.clear();
}
public void addKill(UUID uuid) {
if (Hider.contains(uuid.toString())) {
if (hider_kills.containsKey(uuid.toString())) {
hider_kills.put(uuid.toString(), hider_kills.get(uuid.toString())+1);
} else {
hider_kills.put(uuid.toString(), 1);
}
} else if (Seeker.contains(uuid.toString())) {
if (seeker_kills.containsKey(uuid.toString())) {
seeker_kills.put(uuid.toString(), seeker_kills.get(uuid.toString())+1);
} else {
seeker_kills.put(uuid.toString(), 1);
}
}
}
public void addDeath(UUID uuid) {
if (Hider.contains(uuid.toString())) {
if (hider_deaths.containsKey(uuid.toString())) {
hider_deaths.put(uuid.toString(), hider_deaths.get(uuid.toString())+1);
} else {
hider_deaths.put(uuid.toString(), 1);
}
} else if (Seeker.contains(uuid.toString())) {
if (seeker_deaths.containsKey(uuid.toString())) {
seeker_deaths.put(uuid.toString(), seeker_deaths.get(uuid.toString())+1);
} else {
seeker_deaths.put(uuid.toString(), 1);
}
}
}
public Map<String, Integer> getHiderKills() {
return new HashMap<>(hider_kills);
}
public Map<String, Integer> getSeekerKills() {
return new HashMap<>(seeker_kills);
}
public Map<String, Integer> getHiderDeaths() {
return new HashMap<>(hider_deaths);
}
public Map<String, Integer> getSeekerDeaths() {
return new HashMap<>(seeker_deaths);
}
public void createLobbyBoard(Player player) {
createLobbyBoard(player, true);
}
private void createLobbyBoard(Player player, boolean recreate) {
CustomBoard board = customBoards.get(player.getUniqueId().toString());
if (recreate || board == null) {
board = new CustomBoard(player, LOBBY_TITLE);
board.updateTeams();
}
int i=0;
for(String line : LOBBY_CONTENTS) {
if (line.equalsIgnoreCase("")) {
board.addBlank();
} else if (line.contains("{COUNTDOWN}")) {
if (!lobbyCountdownEnabled) {
board.setLine(String.valueOf(i), line.replace("{COUNTDOWN}", COUNTDOWN_ADMINSTART));
} else if (Main.getInstance().getGame().getLobbyTime() == -1) {
board.setLine(String.valueOf(i), line.replace("{COUNTDOWN}", COUNTDOWN_WAITING));
} else {
board.setLine(String.valueOf(i), line.replace("{COUNTDOWN}", COUNTDOWN_COUNTING.replace("{AMOUNT}",Main.getInstance().getGame().getLobbyTime()+"")));
}
} else if (line.contains("{COUNT}")) {
board.setLine(String.valueOf(i), line.replace("{COUNT}", getPlayers().size()+""));
} else if (line.contains("{SEEKER%}")) {
board.setLine(String.valueOf(i), line.replace("{SEEKER%}", getSeekerPercent()+""));
} else if (line.contains("{HIDER%}")) {
board.setLine(String.valueOf(i), line.replace("{HIDER%}", getHiderPercent() + ""));
} else if (line.contains("{MAP}")) {
board.setLine(String.valueOf(i), line.replace("{MAP}", getMapName() + ""));
} else {
board.setLine(String.valueOf(i), line);
}
i++;
}
board.display();
customBoards.put(player.getUniqueId().toString(), board);
}
public String getMapName() {
net.tylermurphy.hideAndSeek.configuration.Map map = Main.getInstance().getGame().getCurrentMap();
if(map == null) return "Invalid";
else return map.getName();
}
public void createGameBoard(Player player) {
createGameBoard(player, true);
}
private void createGameBoard(Player player, boolean recreate) {
CustomBoard board = customBoards.get(player.getUniqueId().toString());
if (recreate || board == null) {
board = new CustomBoard(player, GAME_TITLE);
board.updateTeams();
}
int timeLeft = Main.getInstance().getGame().getTimeLeft();
Status status = Main.getInstance().getGame().getStatus();
Taunt taunt = Main.getInstance().getGame().getTaunt();
Border worldBorder = Main.getInstance().getGame().getCurrentMap().getWorldBorder();
Glow glow = Main.getInstance().getGame().getGlow();
int i = 0;
for(String line : GAME_CONTENTS) {
if (line.equalsIgnoreCase("")) {
board.addBlank();
} else {
if (line.contains("{TIME}")) {
String value = timeLeft/60 + "m" + timeLeft%60 + "s";
board.setLine(String.valueOf(i), line.replace("{TIME}", value));
} else if (line.contains("{TEAM}")) {
String value = getTeam(player);
board.setLine(String.valueOf(i), line.replace("{TEAM}", value));
} else if (line.contains("{BORDER}")) {
if (!Main.getInstance().getGame().getCurrentMap().isWorldBorderEnabled()) continue;
if (worldBorder == null || status == Status.STARTING) {
board.setLine(String.valueOf(i), line.replace("{BORDER}", BORDER_COUNTING.replace("{AMOUNT}", "0")));
} else if (!worldBorder.isRunning()) {
board.setLine(String.valueOf(i), line.replace("{BORDER}", BORDER_COUNTING.replaceFirst("\\{AMOUNT}", worldBorder.getDelay()/60+"").replaceFirst("\\{AMOUNT}", worldBorder.getDelay()%60+"")));
} else {
board.setLine(String.valueOf(i), line.replace("{BORDER}", BORDER_DECREASING));
}
} else if (line.contains("{TAUNT}")) {
if (!tauntEnabled) continue;
if (taunt == null || status == Status.STARTING) {
board.setLine(String.valueOf(i), line.replace("{TAUNT}", TAUNT_COUNTING.replace("{AMOUNT}", "0")));
} else if (!tauntLast && Hider.size() == 1) {
board.setLine(String.valueOf(i), line.replace("{TAUNT}", TAUNT_EXPIRED));
} else if (!taunt.isRunning()) {
board.setLine(String.valueOf(i), line.replace("{TAUNT}", TAUNT_COUNTING.replaceFirst("\\{AMOUNT}", taunt.getDelay() / 60 + "").replaceFirst("\\{AMOUNT}", taunt.getDelay() % 60 + "")));
} else {
board.setLine(String.valueOf(i), line.replace("{TAUNT}", TAUNT_ACTIVE));
}
} else if (line.contains("{GLOW}")) {
if (!glowEnabled) continue;
if (glow == null || status == Status.STARTING || !glow.isRunning()) {
board.setLine(String.valueOf(i), line.replace("{GLOW}", GLOW_INACTIVE));
} else {
board.setLine(String.valueOf(i), line.replace("{GLOW}", GLOW_ACTIVE));
}
} else if (line.contains("{#SEEKER}")) {
board.setLine(String.valueOf(i), line.replace("{#SEEKER}", getSeekers().size()+""));
} else if (line.contains("{#HIDER}")) {
board.setLine(String.valueOf(i), line.replace("{#HIDER}", getHiders().size()+""));
} else if (line.contains("{MAP}")) {
board.setLine(String.valueOf(i), line.replace("{MAP}", getMapName() + ""));
} else {
board.setLine(String.valueOf(i), line);
}
}
i++;
}
board.display();
customBoards.put(player.getUniqueId().toString(), board);
}
public void removeBoard(Player player) {
ScoreboardManager manager = Bukkit.getScoreboardManager();
assert manager != null;
player.setScoreboard(manager.getMainScoreboard());
customBoards.remove(player.getUniqueId().toString());
}
public void reloadLobbyBoards() {
for(Player player : playerList.values())
createLobbyBoard(player, false);
}
public void reloadGameBoards() {
for(Player player : playerList.values())
createGameBoard(player, false);
}
public void reloadBoardTeams() {
for(CustomBoard board : customBoards.values())
board.updateTeams();
}
private String getSeekerPercent() {
if (playerList.values().size() < 2)
return " --";
else
return " "+(int)(100*(1.0/playerList.size()));
}
private String getHiderPercent() {
if (playerList.size() < 2)
return " --";
else
return " "+(int)(100-100*(1.0/playerList.size()));
}
private String getTeam(Player player) {
if (isHider(player)) return message("HIDER_TEAM_NAME").toString();
else if (isSeeker(player)) return message("SEEKER_TEAM_NAME").toString();
else if (isSpectator(player)) return message("SPECTATOR_TEAM_NAME").toString();
else return ChatColor.WHITE + "UNKNOWN";
}
public void cleanup() {
playerList.clear();
Hider.clear();
Seeker.clear();
Spectator.clear();
customBoards.clear();
}
}
@SuppressWarnings("deprecation")
class CustomBoard {
private final Scoreboard board;
private final Objective obj;
private final Player player;
private final Map<String,Line> LINES;
private int blanks;
private boolean displayed;
public CustomBoard(Player player, String title) {
ScoreboardManager manager = Bukkit.getScoreboardManager();
assert manager != null;
this.board = manager.getNewScoreboard();
this.LINES = new HashMap<>();
this.player = player;
if (Main.getInstance().supports(13)) {
this.obj = board.registerNewObjective(
"Scoreboard", "dummy", ChatColor.translateAlternateColorCodes('&', title));
} else {
this.obj = board.registerNewObjective("Scoreboard", "dummy");
this.obj.setDisplayName(ChatColor.translateAlternateColorCodes('&', title));
}
this.blanks = 0;
this.displayed = false;
this.updateTeams();
}
public void updateTeams() {
try{ board.registerNewTeam("Hider"); } catch (Exception ignored) {}
try{ board.registerNewTeam("Seeker"); } catch (Exception ignored) {}
Team hiderTeam = board.getTeam("Hider");
assert hiderTeam != null;
for(String entry : hiderTeam.getEntries())
hiderTeam.removeEntry(entry);
for(Player player : Main.getInstance().getBoard().getHiders())
hiderTeam.addEntry(player.getName());
Team seekerTeam = board.getTeam("Seeker");
assert seekerTeam != null;
for(String entry : seekerTeam.getEntries())
seekerTeam.removeEntry(entry);
for(Player player : Main.getInstance().getBoard().getSeekers())
seekerTeam.addEntry(player.getName());
if (Main.getInstance().supports(9)) {
if (nameTagsVisible) {
hiderTeam.setOption(Team.Option.NAME_TAG_VISIBILITY, Team.OptionStatus.FOR_OWN_TEAM);
seekerTeam.setOption(Team.Option.NAME_TAG_VISIBILITY, Team.OptionStatus.FOR_OTHER_TEAMS);
} else {
hiderTeam.setOption(Team.Option.NAME_TAG_VISIBILITY, Team.OptionStatus.NEVER);
seekerTeam.setOption(Team.Option.NAME_TAG_VISIBILITY, Team.OptionStatus.NEVER);
}
hiderTeam.setOption(Team.Option.COLLISION_RULE, Team.OptionStatus.NEVER);
seekerTeam.setOption(Team.Option.COLLISION_RULE, Team.OptionStatus.NEVER);
} else {
if (nameTagsVisible) {
hiderTeam.setNameTagVisibility(NameTagVisibility.HIDE_FOR_OTHER_TEAMS);
seekerTeam.setNameTagVisibility(NameTagVisibility.HIDE_FOR_OWN_TEAM);
} else {
hiderTeam.setNameTagVisibility(NameTagVisibility.NEVER);
seekerTeam.setNameTagVisibility(NameTagVisibility.NEVER);
}
}
if (Main.getInstance().supports(12)) {
hiderTeam.setColor(ChatColor.GOLD);
seekerTeam.setColor(ChatColor.RED);
} else {
hiderTeam.setPrefix(ChatColor.translateAlternateColorCodes('&', "&6"));
seekerTeam.setPrefix(ChatColor.translateAlternateColorCodes('&', "&c"));
}
}
public void setLine(String key, String message) {
Line line = LINES.get(key);
if (line == null)
addLine(key, ChatColor.translateAlternateColorCodes('&',message));
else
updateLine(key, ChatColor.translateAlternateColorCodes('&',message));
}
private void addLine(String key, String message) {
Score score = obj.getScore(message);
score.setScore(LINES.values().size()+1);
Line line = new Line(LINES.values().size()+1, message);
LINES.put(key, line);
}
public void addBlank() {
if (displayed) return;
StringBuilder temp = new StringBuilder();
for(int i = 0; i <= blanks; i ++)
temp.append(ChatColor.RESET);
blanks++;
addLine("blank"+blanks, temp.toString());
}
private void updateLine(String key, String message) {
Line line = LINES.get(key);
board.resetScores(line.getMessage());
line.setMessage(message);
Score newScore = obj.getScore(message);
newScore.setScore(line.getScore());
}
public void display() {
displayed = true;
obj.setDisplaySlot(DisplaySlot.SIDEBAR);
player.setScoreboard(board);
}
}
class Line {
private final int score;
private String message;
public Line(int score, String message) {
this.score = score;
this.message = message;
}
public int getScore() {
return score;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
|