Skip to content

Commit 580332e

Browse files
committed
Added support for Adventure's Mini Message API and no more legacy scoreboard support
1 parent f8f34d1 commit 580332e

10 files changed

Lines changed: 261 additions & 731 deletions

File tree

.github/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ Commons is an open-source library that provides useful utilities for Java and Mi
2424
<dependency>
2525
<groupId>dev.despical</groupId>
2626
<artifactId>commons</artifactId>
27-
<version>2.0.9</version>
27+
<version>2.1.0</version>
2828
</dependency>
2929
```
3030

3131
### Gradle
3232
```gradle
3333
dependencies {
34-
implementation 'dev.despical:commons:2.0.9'
34+
implementation 'dev.despical:commons:2.1.0'
3535
}
3636
```
3737

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>dev.despical</groupId>
66
<artifactId>commons</artifactId>
7-
<version>2.0.9</version>
7+
<version>2.1.0</version>
88

99
<name>Commons</name>
1010
<inceptionYear>2020</inceptionYear>

src/main/java/dev/despical/commons/scoreboard/Scoreboard.java

Lines changed: 107 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -18,140 +18,125 @@
1818

1919
package dev.despical.commons.scoreboard;
2020

21+
import net.kyori.adventure.text.Component;
2122
import org.bukkit.Bukkit;
2223
import org.bukkit.entity.Player;
2324
import org.bukkit.scheduler.BukkitRunnable;
2425
import org.bukkit.scoreboard.DisplaySlot;
2526
import org.bukkit.scoreboard.Objective;
2627
import org.bukkit.scoreboard.Team;
2728

28-
import java.util.Optional;
29-
3029
/**
3130
* @author Despical
3231
* <p>
3332
* Created at 17.06.2020
3433
*/
3534
public abstract class Scoreboard implements AutoUpdatable {
3635

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+
}
157142
}

src/main/java/dev/despical/commons/scoreboard/ScoreboardHandler.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package dev.despical.commons.scoreboard;
2020

21+
import net.kyori.adventure.text.Component;
2122
import dev.despical.commons.scoreboard.common.Entry;
2223
import org.bukkit.entity.Player;
2324

@@ -28,9 +29,10 @@
2829
* <p>
2930
* Created at 17.06.2020
3031
*/
32+
3133
public interface ScoreboardHandler {
3234

33-
String getTitle(Player player);
35+
Component getTitle(Player player);
3436

35-
List<Entry> getEntries(Player player);
37+
List<Entry> getEntries(Player player);
3638
}

src/main/java/dev/despical/commons/scoreboard/ScoreboardLib.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818

1919
package dev.despical.commons.scoreboard;
2020

21-
import com.cryptomorin.xseries.reflection.XReflection;
22-
import dev.despical.commons.scoreboard.type.LegacySimpleScoreboard;
23-
import dev.despical.commons.scoreboard.type.SimpleScoreboard;
2421
import org.bukkit.entity.Player;
2522
import org.bukkit.plugin.Plugin;
2623

@@ -42,14 +39,6 @@ public static void setPluginInstance(Plugin instance) {
4239
}
4340

4441
public static Scoreboard createScoreboard(Player holder) {
45-
if (XReflection.supports(13)) {
46-
return new SimpleScoreboard(holder);
47-
}
48-
49-
return new LegacySimpleScoreboard(holder);
50-
}
51-
52-
public static Scoreboard createLegacyScoreboard(Player holder) {
53-
return new LegacySimpleScoreboard(holder);
42+
return new SimpleScoreboard(holder);
5443
}
5544
}

0 commit comments

Comments
 (0)