summaryrefslogtreewikicommitdiff
path: root/src/main/java/net/tylermurphy/hideAndSeek/game/Board.java
blob: d569a7f65dabe2d8c8102e4725fe44ff9ebaac9c (plain)
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
/*
 * 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 static net.tylermurphy.hideAndSeek.configuration.Config.*;

import java.util.*;
import java.util.stream.Collectors;

import net.tylermurphy.hideAndSeek.util.Status;
import net.tylermurphy.hideAndSeek.util.Version;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.*;

public class Board {

    private static final List<String> Hider = new ArrayList<>(), Seeker = new ArrayList<>(), Spectator = new ArrayList<>();
    private static final Map<String, Player> playerList = new HashMap<>();
    private static final Map<String, CustomBoard> customBoards = new HashMap<>();

    public static boolean isPlayer(Player player) {
        return playerList.containsKey(player.getUniqueId().toString());
    }

    public static boolean isPlayer(CommandSender sender) {
        return playerList.containsKey(Bukkit.getPlayer(sender.getName()).getUniqueId().toString());
    }

    public static boolean isHider(Player player) {
        return Hider.contains(player.getUniqueId().toString());
    }

    public static boolean isSeeker(Player player) {
        return Seeker.contains(player.getUniqueId().toString());
    }

    public static boolean isSpectator(Player player) {
        return Spectator.contains(player.getUniqueId().toString());
    }

    public static int sizeHider() {
        return Hider.size();
    }

    public static int sizeSeeker() {
        return Seeker.size();
    }

    public static int size() {
        return playerList.values().size();
    }

    public static List<Player> getHiders(){
        return Hider.stream().map(playerList::get).collect(Collectors.toList());
    }

    public static List<Player> getSeekers(){
        return Seeker.stream().map(playerList::get).collect(Collectors.toList());
    }

    public static Player getFirstSeeker(){
        return playerList.get(Seeker.get(0));
    }

    public static List<Player> getSpectators(){
        return Spectator.stream().map(playerList::get).collect(Collectors.toList());
    }

    public static List<Player> getPlayers(){
        return new ArrayList<>(playerList.values());
    }

    public static Player getPlayer(UUID uuid) {
        return playerList.get(uuid.toString());
    }

    public static 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 static 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 static 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 static 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 static 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 static void reload() {
        Hider.clear();
        Seeker.clear();
        Spectator.clear();
    }

    public static void createLobbyBoard(Player player) {
        createLobbyBoard(player, true);
    }

    private static void createLobbyBoard(Player player, boolean recreate) {
        CustomBoard board = customBoards.get(player.getUniqueId().toString());
        if(recreate) {
            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(Game.countdownTime == -1){
                    board.setLine(String.valueOf(i), line.replace("{COUNTDOWN}", COUNTDOWN_WAITING));
                } else {
                    board.setLine(String.valueOf(i), line.replace("{COUNTDOWN}", COUNTDOWN_COUNTING.replace("{AMOUNT}",Game.countdownTime+"")));
                }
            } 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 {
                board.setLine(String.valueOf(i), line);
            }
            i++;
        }
        board.display();
        customBoards.put(player.getUniqueId().toString(), board);
    }

    public static void createGameBoard(Player player){
        createGameBoard(player, true);
    }

    private static void createGameBoard(Player player, boolean recreate){
        CustomBoard board = customBoards.get(player.getUniqueId().toString());
        if(recreate) {
            board = new CustomBoard(player, GAME_TITLE);
            board.updateTeams();
        }

        int i = 0;
        for(String line : GAME_CONTENTS){
            if(line.equalsIgnoreCase("")){
                board.addBlank();
            } else {
                if(line.contains("{TIME}")) {
                    String value = Game.timeLeft/60 + "m" + Game.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(!worldborderEnabled) continue;
                    if(Game.worldBorder == null || Game.status == Status.STARTING){
                        board.setLine(String.valueOf(i), line.replace("{BORDER}", BORDER_COUNTING.replace("{AMOUNT}", "0")));
                    } else if(!Game.worldBorder.isRunning()) {
                        board.setLine(String.valueOf(i), line.replace("{BORDER}", BORDER_COUNTING.replaceFirst("\\{AMOUNT}", Game.worldBorder.getDelay()/60+"").replaceFirst("\\{AMOUNT}", Game.worldBorder.getDelay()%60+"")));
                    } else {
                        board.setLine(String.valueOf(i), line.replace("{BORDER}", BORDER_DECREASING));
                    }
                } else if(line.contains("{TAUNT}")){
                    if(!tauntEnabled) continue;
                    if(Game.taunt == null || Game.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(!Game.taunt.isRunning()) {
                        board.setLine(String.valueOf(i), line.replace("{TAUNT}", TAUNT_COUNTING.replaceFirst("\\{AMOUNT}", Game.taunt.getDelay() / 60 + "").replaceFirst("\\{AMOUNT}", Game.taunt.getDelay() % 60 + "")));
                    } else {
                        board.setLine(String.valueOf(i), line.replace("{TAUNT}", TAUNT_ACTIVE));
                    }
                } else if(line.contains("{GLOW}")){
                    if(!glowEnabled)  return;
                    if(Game.glow == null || Game.status == Status.STARTING || !Game.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 {
                    board.setLine(String.valueOf(i), line);
                }
            }
            i++;
        }
        board.display();
        customBoards.put(player.getUniqueId().toString(), board);
    }

    public static void removeBoard(Player player) {
        ScoreboardManager manager = Bukkit.getScoreboardManager();
        assert manager != null;
        player.setScoreboard(manager.getMainScoreboard());
        customBoards.remove(player.getUniqueId().toString());
    }

    public static void reloadLobbyBoards() {
        for(Player player : playerList.values())
            createLobbyBoard(player, false);
    }

    public static void reloadGameBoards() {
        for(Player player : playerList.values())
            createGameBoard(player, false);
    }

    public static void reloadBoardTeams() {
        for(CustomBoard board : customBoards.values())
            board.updateTeams();
    }

    private static String getSeekerPercent() {
        if(playerList.values().size() < 2)
            return " --";
        else
            return " "+(int)(100*(1.0/playerList.size()));
    }

    private static String getHiderPercent() {
        if(playerList.size() < 2)
            return " --";
        else
            return " "+(int)(100-100*(1.0/playerList.size()));
    }

    private static String getTeam(Player player) {
        if(isHider(player)) return ChatColor.GOLD + "HIDER";
        else if(isSeeker(player)) return ChatColor.RED + "SEEKER";
        else if(isSpectator(player)) return ChatColor.GRAY + "SPECTATOR";
        else return ChatColor.WHITE + "UNKNOWN";
    }

    public  static void cleanup(){
        playerList.clear();
        Hider.clear();
        Seeker.clear();
        Spectator.clear();
        customBoards.clear();
    }

}

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(Version.atLeast("1.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 : Board.getHiders())
            hiderTeam.addEntry(player.getName());
        Team seekerTeam = board.getTeam("Seeker");
        assert seekerTeam != null;
        for(String entry : seekerTeam.getEntries())
            seekerTeam.removeEntry(entry);
        for(Player player  : Board.getSeekers())
            seekerTeam.addEntry(player.getName());
        if(Version.atLeast("1.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);
            }
        } 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(Version.atLeast("1.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;
    }

}