summaryrefslogtreewikicommitdiff
path: root/src/main/java/net/tylermurphy/hideAndSeek/util/CustomBoard.java
blob: 69d865aa0c6be10bdb7fb81e4b906b4b31363b88 (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
package net.tylermurphy.hideAndSeek.util;

import net.tylermurphy.hideAndSeek.Main;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.*;

import java.util.HashMap;
import java.util.Map;

import static net.tylermurphy.hideAndSeek.configuration.Config.*;

public 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){
        this.board = Bukkit.getScoreboardManager().getNewScoreboard();
        this.LINES = new HashMap<String,Line>();
        this.player = player;
        this.obj = board.registerNewObjective(
                "Scoreboard", "dummy", ChatColor.translateAlternateColorCodes('&', title));
        this.blanks = 0;
        this.displayed = false;
        this.updateTeams();
    }

    public void updateTeams() {
        try{ board.registerNewTeam("Hider"); } catch (Exception e){}
        try{ board.registerNewTeam("Seeker"); } catch (Exception e){}
        Team hiderTeam = board.getTeam("Hider");
        for(String entry : hiderTeam.getEntries())
            hiderTeam.removeEntry(entry);
        for(Player player : Main.plugin.board.getHiders())
            hiderTeam.addEntry(player.getName());
        Team seekerTeam = board.getTeam("Seeker");
        for(String entry : seekerTeam.getEntries())
            seekerTeam.removeEntry(entry);
        for(Player player  : Main.plugin.board.getSeekers())
            seekerTeam.addEntry(player.getName());
        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.setColor(ChatColor.GOLD);
        seekerTeam.setColor(ChatColor.RED);
    }

    public void setLine(String key, String message){
        Line line = LINES.get(key);
        if(line == null)
            addLine(key, message);
        else
            updateLine(key, 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;
        String temp = "";
        for(int i = 0; i <= blanks; i ++)
            temp += ChatColor.RESET;
        blanks++;
        addLine("blank"+blanks, temp);
    }

    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 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;
    }

}