getEntries(Player player);
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/me/tigerhix/lib/scoreboard/type/SimpleScoreboard.java:
--------------------------------------------------------------------------------
1 | package me.tigerhix.lib.scoreboard.type;
2 |
3 | import me.tigerhix.lib.scoreboard.common.Strings;
4 | import org.bukkit.Bukkit;
5 | import org.bukkit.ChatColor;
6 | import org.bukkit.entity.Player;
7 | import org.bukkit.scoreboard.DisplaySlot;
8 | import org.bukkit.scoreboard.Objective;
9 | import org.bukkit.scoreboard.Team;
10 |
11 | import java.util.ArrayList;
12 | import java.util.List;
13 |
14 | /**
15 | * @author Tigerpanzer_02
16 | *
17 | * Created at 28.12.2020
18 | */
19 | public class SimpleScoreboard implements Scoreboard {
20 |
21 | private static final String TEAM_PREFIX = "Plugily_";
22 | private final org.bukkit.scoreboard.Scoreboard scoreboard;
23 | private final Objective objective;
24 | protected Player holder;
25 | private boolean activated;
26 | private ScoreboardHandler handler;
27 |
28 | public SimpleScoreboard(Player holder) {
29 | this.holder = holder;
30 | // Initiate the Bukkit scoreboard
31 | scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
32 | scoreboard.registerNewObjective("plugily", "dummy").setDisplaySlot(DisplaySlot.SIDEBAR);
33 | objective = scoreboard.getObjective(DisplaySlot.SIDEBAR);
34 | for(int i = 1; i <= 15; i++) {
35 | scoreboard.registerNewTeam(TEAM_PREFIX + i).addEntry(getEntry(i));
36 | }
37 | }
38 |
39 | @Override
40 | public void activate() {
41 | if(activated) {
42 | return;
43 | }
44 | if(handler == null) {
45 | throw new IllegalArgumentException("Scoreboard handler not set");
46 | }
47 | activated = true;
48 | // Set to the custom scoreboard
49 | holder.setScoreboard(scoreboard);
50 | }
51 |
52 | @Override
53 | public void deactivate() {
54 | if(!activated) {
55 | return;
56 | }
57 | activated = false;
58 | // Set to the main scoreboard
59 | if(holder.isOnline()) {
60 | synchronized(this) {
61 | holder.setScoreboard(Bukkit.getScoreboardManager().getMainScoreboard());
62 | }
63 | }
64 | // Unregister teams that are created for this scoreboard
65 | for(Team team : scoreboard.getTeams()) {
66 | team.unregister();
67 | }
68 | }
69 |
70 | @Override
71 | public boolean isActivated() {
72 | return activated;
73 | }
74 |
75 | @Override
76 | public ScoreboardHandler getHandler() {
77 | return handler;
78 | }
79 |
80 | @Override
81 | public Scoreboard setHandler(ScoreboardHandler handler) {
82 | this.handler = handler;
83 | return this;
84 | }
85 |
86 | @Override
87 | public long getUpdateInterval() {
88 | throw new UnsupportedOperationException("Update interval is not supported anymore");
89 | }
90 |
91 | @Override
92 | public LegacySimpleScoreboard setUpdateInterval(long updateInterval) {
93 | throw new UnsupportedOperationException("Update interval is not supported anymore");
94 | }
95 |
96 | @Override
97 | public Player getHolder() {
98 | return holder;
99 | }
100 |
101 | @Override
102 | public void update() {
103 | if (!activated) {
104 | return;
105 | }
106 |
107 | if(!holder.isOnline()) {
108 | deactivate();
109 | return;
110 | }
111 |
112 | String handlerTitle = handler.getTitle(holder);
113 | String finalTitle = handlerTitle != null ? Strings.format(handlerTitle) : ChatColor.BOLD.toString();
114 |
115 | if(!objective.getDisplayName().equals(finalTitle)) {
116 | objective.setDisplayName(finalTitle);
117 | }
118 |
119 | List passed = handler.getEntries(holder);
120 |
121 | if(passed == null) {
122 | return;
123 | }
124 |
125 | List current = new ArrayList<>(passed.size());
126 |
127 | for(Entry entry : passed) {
128 | int score = entry.getPosition();
129 | Team team = scoreboard.getTeam(TEAM_PREFIX + score);
130 | String temp = getEntry(score);
131 |
132 | if(!scoreboard.getEntries().contains(temp)) {
133 | objective.getScore(temp).setScore(score);
134 | }
135 |
136 | String key = Strings.format(entry.getName());
137 | int length = key.length();
138 |
139 | String prefix = length > 64 ? key.substring(0, 64) : key;
140 | String suffix = ChatColor.getLastColors(prefix) + (prefix.charAt(prefix.length() - 1) == '§' ? "§" : "") + limitKey(length, key);
141 |
142 | team.setPrefix(prefix);
143 | team.setSuffix(suffix.length() > 64 ? suffix.substring(0, 64) : suffix);
144 |
145 | current.add(score);
146 | }
147 |
148 | // Remove duplicated or non-existent entries
149 | for(int i = 1; i <= 15; i++) {
150 | if(!current.contains(i)) {
151 | String entry = getEntry(i);
152 |
153 | if(scoreboard.getEntries().contains(entry)) {
154 | scoreboard.resetScores(entry);
155 | }
156 | }
157 | }
158 | }
159 |
160 | public Objective getObjective() {
161 | return objective;
162 | }
163 |
164 | private final ChatColor[] values = ChatColor.values();
165 |
166 | private String getEntry(int slot) {
167 | return values[slot].toString();
168 | }
169 |
170 | public org.bukkit.scoreboard.Scoreboard getScoreboard() {
171 | return scoreboard;
172 | }
173 |
174 | private String limitKey(int length, String str) {
175 | if(length > 128) {
176 | return str.substring(0, 128);
177 | }
178 |
179 | return length > 64 ? str.substring(64) : "";
180 | }
181 | }
182 |
--------------------------------------------------------------------------------
/src/main/java/me/tigerhix/lib/scoreboard/util/ServerVersion.java:
--------------------------------------------------------------------------------
1 | package me.tigerhix.lib.scoreboard.util;
2 |
3 | import org.bukkit.Bukkit;
4 |
5 | import java.util.regex.Matcher;
6 | import java.util.regex.Pattern;
7 |
8 | public class ServerVersion {
9 |
10 | public Version getVersion() {
11 | return Version.getCurrent();
12 | }
13 |
14 | public enum Version {
15 | v0_0_0(0, 0),
16 | v1_8_8(8, 4),
17 | v1_9(9, 4),
18 | v1_10(10, 2),
19 | v1_11(11, 0),
20 | v1_12(12, 0),
21 | v1_13(13, 1),
22 | v1_14(14, 0),
23 | v1_15(15, 0),
24 | v1_16(16, 0),
25 | v1_17(17, 0),
26 | v1_18(18, 0),
27 | v1_19(19, 0),
28 | v1_20(20, 0),
29 | v1_21(21, 0);
30 |
31 |
32 | private static Version current;
33 | private final int minor;
34 | private final int minPatch;
35 |
36 | Version(int minor, int minPatch) {
37 | this.minor = minor;
38 | this.minPatch = minPatch;
39 | }
40 |
41 | public int getMinor() {
42 | return minor;
43 | }
44 |
45 | public int getMinPatch() {
46 | return minPatch;
47 | }
48 |
49 | public static Version getCurrent() {
50 | if(current != null) {
51 | return current;
52 | }
53 |
54 | Matcher serverVersion = Pattern.compile("^(?\\d+)\\.(?\\d+)(?:\\.(?\\d+))?").matcher(Bukkit.getBukkitVersion());
55 | if(serverVersion.find()) {
56 | int serverMinor = Integer.parseInt(serverVersion.group("minor"));
57 | String patch = serverVersion.group("patch");
58 | int serverPatch = Integer.parseInt((patch == null || patch.isEmpty()) ? "0" : patch);
59 |
60 | for(Version value : values()) {
61 | if(value.getMinor() == serverMinor && serverPatch >= value.getMinPatch()) {
62 | current = value;
63 | break;
64 | }
65 | }
66 | } else {
67 | throw new IllegalStateException("Cannot parse server version: \"" + Bukkit.getBukkitVersion() + '"');
68 | }
69 |
70 | if(current == null) { // Fallback
71 | current = Version.v0_0_0;
72 | }
73 |
74 | return current;
75 | }
76 |
77 | public boolean isLower(Version version) {
78 | return minor < version.getMinor();
79 | }
80 |
81 | public boolean isHigher(Version version) {
82 | return minor > version.getMinor();
83 | }
84 |
85 | public boolean isEqual(Version version) {
86 | return minor == version.getMinor();
87 | }
88 |
89 | public boolean isEqualOrLower(Version version) {
90 | return minor <= version.getMinor();
91 | }
92 |
93 | public boolean isEqualOrHigher(Version version) {
94 | return minor >= version.getMinor();
95 | }
96 |
97 | public static boolean isCurrentEqualOrHigher(Version fixedVersion) {
98 | return getCurrent().getMinor() >= fixedVersion.getMinor();
99 | }
100 |
101 | public static boolean isCurrentHigher(Version fixedVersion) {
102 | return getCurrent().getMinor() > fixedVersion.getMinor();
103 | }
104 |
105 | public static boolean isCurrentLower(Version fixedVersion) {
106 | return getCurrent().getMinor() < fixedVersion.getMinor();
107 | }
108 |
109 | public static boolean isCurrentEqualOrLower(Version fixedVersion) {
110 | return getCurrent().getMinor() <= fixedVersion.getMinor();
111 | }
112 |
113 | public static boolean isCurrentEqual(Version fixedVersion) {
114 | return getCurrent().getMinor() == fixedVersion.getMinor();
115 | }
116 | }
117 | }
--------------------------------------------------------------------------------
/src/main/resources/plugin.yml:
--------------------------------------------------------------------------------
1 | name: ScoreboardLib
2 | version: ${version}
3 | description: Utilizing scoreboards for a UI enhancement.
4 | authors:
5 | - TigerHix
6 | - Tigerpanzer_02
7 | - Plugily Projects
8 | main: me.tigerhix.lib.scoreboard.ScoreboardLib
--------------------------------------------------------------------------------