|
18 | 18 |
|
19 | 19 | package dev.despical.commons.scoreboard; |
20 | 20 |
|
| 21 | +import net.kyori.adventure.text.Component; |
21 | 22 | import org.bukkit.Bukkit; |
22 | 23 | import org.bukkit.entity.Player; |
23 | 24 | import org.bukkit.scheduler.BukkitRunnable; |
24 | 25 | import org.bukkit.scoreboard.DisplaySlot; |
25 | 26 | import org.bukkit.scoreboard.Objective; |
26 | 27 | import org.bukkit.scoreboard.Team; |
27 | 28 |
|
28 | | -import java.util.Optional; |
29 | | - |
30 | 29 | /** |
31 | 30 | * @author Despical |
32 | 31 | * <p> |
33 | 32 | * Created at 17.06.2020 |
34 | 33 | */ |
35 | 34 | public abstract class Scoreboard implements AutoUpdatable { |
36 | 35 |
|
37 | | - protected static final String TEAM_PREFIX = "Board_"; |
38 | | - |
39 | | - protected final Player holder; |
40 | | - protected final org.bukkit.scoreboard.Scoreboard scoreboard; |
41 | | - protected final org.bukkit.scoreboard.Scoreboard previousBoard; |
42 | | - protected final Objective objective; |
43 | | - |
44 | | - protected boolean activated; |
45 | | - protected boolean autoUpdateEnabled = true; |
46 | | - protected ScoreboardHandler handler; |
47 | | - protected BukkitRunnable updateTask; |
48 | | - protected long updateInterval = 10L; |
49 | | - |
50 | | - public Scoreboard(Player holder) { |
51 | | - this.holder = holder; |
52 | | - this.previousBoard = holder.getScoreboard(); |
53 | | - |
54 | | - scoreboard = Bukkit.getScoreboardManager().getNewScoreboard(); |
55 | | - scoreboard.registerNewObjective("board", "dummy").setDisplaySlot(DisplaySlot.SIDEBAR); |
56 | | - |
57 | | - objective = scoreboard.getObjective(DisplaySlot.SIDEBAR); |
58 | | - } |
59 | | - |
60 | | - public abstract void update(); |
61 | | - |
62 | | - public void activate() { |
63 | | - if (activated) { |
64 | | - return; |
65 | | - } |
66 | | - |
67 | | - if (handler == null) { |
68 | | - throw new IllegalStateException("Scoreboard handler not set yet!"); |
69 | | - } |
70 | | - |
71 | | - activated = true; |
72 | | - |
73 | | - holder.setScoreboard(scoreboard); |
74 | | - |
75 | | - if (!autoUpdateEnabled) { |
76 | | - return; |
77 | | - } |
78 | | - |
79 | | - updateTask = new BukkitRunnable() { |
80 | | - |
81 | | - @Override |
82 | | - public void run() { |
83 | | - update(); |
84 | | - } |
85 | | - }; |
86 | | - |
87 | | - updateTask.runTaskTimer(ScoreboardLib.getInstance(), 0, updateInterval); |
88 | | - } |
89 | | - |
90 | | - public void deactivate() { |
91 | | - if (!activated) { |
92 | | - return; |
93 | | - } |
94 | | - |
95 | | - activated = false; |
96 | | - |
97 | | - if (holder.isOnline()) { |
98 | | - synchronized (this) { |
99 | | - holder.setScoreboard(previousBoard); |
100 | | - } |
101 | | - } |
102 | | - |
103 | | - for (Team team : scoreboard.getTeams()) { |
104 | | - team.unregister(); |
105 | | - } |
106 | | - |
107 | | - Optional.ofNullable(updateTask).ifPresent(BukkitRunnable::cancel); |
108 | | - } |
109 | | - |
110 | | - @Override |
111 | | - public long getUpdateInterval() { |
112 | | - return updateInterval; |
113 | | - } |
114 | | - |
115 | | - @Override |
116 | | - public void setUpdateInterval(long updateInterval) { |
117 | | - if (activated) { |
118 | | - throw new IllegalStateException("You cannot change update interval after the scoreboard has been activated"); |
119 | | - } |
120 | | - |
121 | | - this.updateInterval = updateInterval; |
122 | | - } |
123 | | - |
124 | | - @Override |
125 | | - public void disableAutoUpdate() { |
126 | | - if (activated) { |
127 | | - throw new IllegalStateException("You can not disable auto-updating after the scoreboard has been activated"); |
128 | | - } |
129 | | - |
130 | | - this.autoUpdateEnabled = false; |
131 | | - } |
132 | | - |
133 | | - public Player getHolder() { |
134 | | - return holder; |
135 | | - } |
136 | | - |
137 | | - public org.bukkit.scoreboard.Scoreboard getScoreboard() { |
138 | | - return scoreboard; |
139 | | - } |
140 | | - |
141 | | - public Objective getObjective() { |
142 | | - return objective; |
143 | | - } |
144 | | - |
145 | | - public boolean isActivated() { |
146 | | - return activated; |
147 | | - } |
148 | | - |
149 | | - public ScoreboardHandler getHandler() { |
150 | | - return handler; |
151 | | - } |
152 | | - |
153 | | - public Scoreboard setHandler(ScoreboardHandler handler) { |
154 | | - this.handler = handler; |
155 | | - return this; |
156 | | - } |
| 36 | + protected static final String TEAM_PREFIX = "Board_"; |
| 37 | + |
| 38 | + protected final Player holder; |
| 39 | + protected final org.bukkit.scoreboard.Scoreboard scoreboard; |
| 40 | + protected final org.bukkit.scoreboard.Scoreboard previousBoard; |
| 41 | + protected final Objective objective; |
| 42 | + |
| 43 | + protected boolean activated; |
| 44 | + protected boolean autoUpdateEnabled = true; |
| 45 | + protected ScoreboardHandler handler; |
| 46 | + protected BukkitRunnable updateTask; |
| 47 | + protected long updateInterval = 10L; |
| 48 | + |
| 49 | + public Scoreboard(Player holder) { |
| 50 | + this.holder = holder; |
| 51 | + this.previousBoard = holder.getScoreboard(); |
| 52 | + this.scoreboard = Bukkit.getScoreboardManager().getNewScoreboard(); |
| 53 | + this.objective = scoreboard.registerNewObjective("board", "dummy", Component.empty()); |
| 54 | + this.objective.setDisplaySlot(DisplaySlot.SIDEBAR); |
| 55 | + } |
| 56 | + |
| 57 | + public abstract void update(); |
| 58 | + |
| 59 | + public void activate() { |
| 60 | + if (activated) return; |
| 61 | + if (handler == null) throw new IllegalStateException("Scoreboard handler not set yet!"); |
| 62 | + |
| 63 | + activated = true; |
| 64 | + holder.setScoreboard(scoreboard); |
| 65 | + |
| 66 | + if (!autoUpdateEnabled) return; |
| 67 | + |
| 68 | + updateTask = new BukkitRunnable() { |
| 69 | + |
| 70 | + @Override |
| 71 | + public void run() { |
| 72 | + if (!holder.isOnline()) { |
| 73 | + deactivate(); |
| 74 | + return; |
| 75 | + } |
| 76 | + update(); |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + updateTask.runTaskTimer(ScoreboardLib.getInstance(), 0L, updateInterval); |
| 81 | + } |
| 82 | + |
| 83 | + public void deactivate() { |
| 84 | + if (!activated) return; |
| 85 | + activated = false; |
| 86 | + |
| 87 | + if (holder.isOnline()) { |
| 88 | + holder.setScoreboard(previousBoard); |
| 89 | + } |
| 90 | + |
| 91 | + for (Team team : scoreboard.getTeams()) { |
| 92 | + team.unregister(); |
| 93 | + } |
| 94 | + |
| 95 | + if (updateTask != null) { |
| 96 | + updateTask.cancel(); |
| 97 | + updateTask = null; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public long getUpdateInterval() { |
| 103 | + return updateInterval; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public void setUpdateInterval(long updateInterval) { |
| 108 | + if (activated) throw new IllegalStateException("Cannot change interval after activation"); |
| 109 | + this.updateInterval = updateInterval; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public void disableAutoUpdate() { |
| 114 | + if (activated) throw new IllegalStateException("Cannot disable auto-update after activation"); |
| 115 | + this.autoUpdateEnabled = false; |
| 116 | + } |
| 117 | + |
| 118 | + public Player getHolder() { |
| 119 | + return holder; |
| 120 | + } |
| 121 | + |
| 122 | + public org.bukkit.scoreboard.Scoreboard getScoreboard() { |
| 123 | + return scoreboard; |
| 124 | + } |
| 125 | + |
| 126 | + public Objective getObjective() { |
| 127 | + return objective; |
| 128 | + } |
| 129 | + |
| 130 | + public boolean isActivated() { |
| 131 | + return activated; |
| 132 | + } |
| 133 | + |
| 134 | + public ScoreboardHandler getHandler() { |
| 135 | + return handler; |
| 136 | + } |
| 137 | + |
| 138 | + public Scoreboard setHandler(ScoreboardHandler handler) { |
| 139 | + this.handler = handler; |
| 140 | + return this; |
| 141 | + } |
157 | 142 | } |
0 commit comments