Skip to content

Commit 1da2814

Browse files
committed
Overhauled GUIComponent and GUI building
- Made ProtocolLib soft depend - If ProtocolLib is enabled, anonymous action bar packets will be added to the GUI and cycled though - Optimized GUI building by using the Spigot Chat API
1 parent 5b99c45 commit 1da2814

File tree

12 files changed

+508
-200
lines changed

12 files changed

+508
-200
lines changed

src/main/java/com/sentropic/guiapi/GUIAPI.java

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,45 @@
11
package com.sentropic.guiapi;
22

3-
import com.comphenix.protocol.PacketType;
4-
import com.comphenix.protocol.ProtocolLibrary;
5-
import com.comphenix.protocol.ProtocolManager;
63
import com.sentropic.guiapi.command.ReloadCommand;
7-
import com.sentropic.guiapi.listener.PacketListener;
4+
import com.sentropic.guiapi.packet.PacketManager;
5+
import org.bukkit.event.EventHandler;
86
import org.bukkit.event.HandlerList;
7+
import org.bukkit.event.Listener;
8+
import org.bukkit.event.server.PluginDisableEvent;
9+
import org.bukkit.event.server.PluginEnableEvent;
10+
import org.bukkit.plugin.PluginManager;
911
import org.bukkit.plugin.java.JavaPlugin;
1012

11-
public final class GUIAPI extends JavaPlugin {
13+
import java.util.Arrays;
14+
import java.util.Objects;
15+
16+
public final class GUIAPI extends JavaPlugin implements Listener {
1217
private static GUIAPI singleton;
1318
private static GUIManager guiManager;
1419
private static GUIConfig config;
15-
16-
public static ProtocolManager protocolManager;
17-
private static PacketListener packetListener;
20+
private static PacketManager packetManager = null;
1821

1922
@Override
2023
public void onEnable() {
2124
if (singleton != null) { throw new IllegalStateException(); }
2225
singleton = this;
26+
PluginManager pluginManager = getServer().getPluginManager();
27+
pluginManager.registerEvents(this, this);
2328

2429
guiManager = new GUIManager();
25-
getServer().getPluginManager().registerEvents(guiManager, this);
30+
pluginManager.registerEvents(guiManager, this);
2631

2732
saveDefaultConfig();
2833
config = new GUIConfig();
2934

3035
// TODO add config for character widths
31-
this.getCommand("guiapi").setExecutor(new ReloadCommand());
36+
Objects.requireNonNull(this.getCommand("guiapi")).setExecutor(new ReloadCommand());
37+
38+
if (Arrays.stream(pluginManager.getPlugins()).anyMatch(
39+
plugin -> plugin.getName().equals("ProtocolLib") && plugin.isEnabled())) {
40+
setProtocolLib(true);
41+
}
3242

33-
protocolManager = ProtocolLibrary.getProtocolManager();
34-
packetListener = new PacketListener(this, PacketType.Play.Server.TITLE);
35-
protocolManager.addPacketListener(packetListener);
3643
}
3744

3845
@Override
@@ -42,13 +49,36 @@ public void onDisable() {
4249
guiManager = null;
4350
config = null;
4451

45-
protocolManager.removePacketListener(packetListener);
46-
packetListener = null;
47-
protocolManager = null;
52+
setProtocolLib(false);
4853

54+
HandlerList.unregisterAll((Listener) this);
4955
singleton = null;
5056
}
5157

58+
@EventHandler
59+
public void onPluginEnable(PluginEnableEvent event) {
60+
if (event.getPlugin().getName().equals("ProtocolLib")) {
61+
setProtocolLib(true);
62+
}
63+
}
64+
65+
@EventHandler
66+
public void onPluginDisable(PluginDisableEvent event) {
67+
if (event.getPlugin().getName().equals("ProtocolLib")) {
68+
setProtocolLib(false);
69+
}
70+
}
71+
72+
private void setProtocolLib(boolean enabled) {
73+
if ((packetManager != null) == enabled) { return; }
74+
if (enabled) {
75+
packetManager = new PacketManager();
76+
} else {
77+
packetManager.disable();
78+
packetManager = null;
79+
}
80+
}
81+
5282
public static GUIAPI getPlugin() { return singleton; }
5383

5484
public static GUIManager getGUIManager() { return guiManager; }

src/main/java/com/sentropic/guiapi/GUIConfig.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.sentropic.guiapi.gui.Font;
55
import com.sentropic.guiapi.gui.GUI;
66
import com.sentropic.guiapi.gui.GUIComponent;
7+
import net.md_5.bungee.api.chat.TextComponent;
78
import org.bukkit.configuration.ConfigurationSection;
89
import org.bukkit.configuration.file.FileConfiguration;
910

@@ -13,7 +14,9 @@
1314
import java.util.Objects;
1415

1516
public class GUIConfig {
16-
private int period;
17+
private int sendPeriod;
18+
private int anonPeriod;
19+
private int anonDuration;
1720
private final List<GUIComponent> debugComponents = new ArrayList<>();
1821
private final List<GUIComponent> debugComponentsRead = Collections.unmodifiableList(debugComponents);
1922

@@ -23,7 +26,9 @@ public void reload() {
2326
GUIAPI plugin = GUIAPI.getPlugin();
2427
plugin.reloadConfig();
2528
FileConfiguration configFile = plugin.getConfig();
26-
period = configFile.getInt("send_period_milis", 1900);
29+
sendPeriod = configFile.getInt("send_period_milis", 1900);
30+
anonPeriod = configFile.getInt("anon_period_ticks", 20);
31+
anonDuration = configFile.getInt("anon_duration_ticks", 40);
2732

2833
// Debug components
2934
{
@@ -36,7 +41,7 @@ public void reload() {
3641
ConfigurationSection fontSection = Objects.requireNonNull(componentSection.getConfigurationSection("font"));
3742

3843
String id = GUI.ID_DEBUG+componentKey;
39-
int offset = componentSection.getInt("offset");
44+
int offset = componentSection.getInt("offset", 0);
4045
String text = Objects.requireNonNull(componentSection.getString("text"));
4146
int width = componentSection.getInt("width", -1);
4247

@@ -47,27 +52,27 @@ public void reload() {
4752
Objects.requireNonNull(componentSection.getString("alignment")).toUpperCase());
4853
boolean scale = componentSection.getBoolean("scale", true);
4954

55+
TextComponent textComponent = new TextComponent(text);
56+
textComponent.setFont(font.getID());
5057
GUIComponent component;
5158
if (width == -1) {
52-
component = new GUIComponent(id, offset, text, font, alignment, scale);
59+
component = new GUIComponent(id, textComponent, font.getWidth(text, scale), offset, alignment);
5360
} else {
54-
component = new GUIComponent(id, offset, text, width, font, alignment);
61+
component = new GUIComponent(id, textComponent, width, offset, alignment);
5562
}
5663
debugComponents.add(component);
5764
} catch (NullPointerException | IllegalArgumentException ignored) { }
5865
}
5966
}
60-
// Update
61-
for (GUI gui : GUIAPI.getGUIManager().getGUIS().values()) {
62-
if (gui.isDebugging()) {
63-
gui.setDebug(false);
64-
gui.setDebug(true);
65-
}
66-
}
67+
GUIAPI.getGUIManager().getGUIS().values().forEach(GUI::onReload);
6768
}
6869
}
6970

70-
public int getSendPeriod() { return period; }
71+
public int getSendPeriod() { return sendPeriod; }
72+
73+
public int getAnonPeriod() { return anonPeriod; }
74+
75+
public int getAnonDuration() { return anonDuration; }
7176

7277
public List<GUIComponent> getDebugComponents() { return debugComponentsRead; }
7378
}

src/main/java/com/sentropic/guiapi/command/ReloadCommand.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
import org.bukkit.command.CommandExecutor;
88
import org.bukkit.command.CommandSender;
99
import org.bukkit.entity.Player;
10+
import org.jetbrains.annotations.NotNull;
1011

1112
public class ReloadCommand implements CommandExecutor {
1213
@Override
13-
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
14+
public boolean onCommand(CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
1415
if (!sender.hasPermission("misc.command.reload")) { return false; }
1516
if (args.length == 1 && args[0].equals("reload")) {
1617
GUIAPI.getGUIConfig().reload();
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.sentropic.guiapi.gui;
2+
3+
import com.sentropic.guiapi.GUIAPI;
4+
import net.md_5.bungee.api.chat.BaseComponent;
5+
import org.bukkit.scheduler.BukkitRunnable;
6+
import org.jetbrains.annotations.NotNull;
7+
8+
public class AnonComponent extends GUIComponent {
9+
private RemoveTask task;
10+
private final GUI gui;
11+
12+
AnonComponent(@NotNull BaseComponent component, @NotNull GUI gui) {
13+
super(GUI.ID_DEFAULT, component, 0, Alignment.CENTER, true);
14+
this.gui = gui;
15+
}
16+
17+
void refresh() {
18+
if (task != null) {
19+
try {
20+
task.cancel();
21+
} catch (IllegalArgumentException ignored) { }
22+
}
23+
task = new RemoveTask();
24+
task.runTaskLater(GUIAPI.getPlugin(), GUIAPI.getGUIConfig().getAnonDuration());
25+
}
26+
27+
void cancelTask() {
28+
if (task != null) {
29+
try {
30+
task.cancel();
31+
} catch (IllegalArgumentException ignored) { }
32+
}
33+
}
34+
35+
private class RemoveTask extends BukkitRunnable {
36+
@Override
37+
public void run() { gui.removeAnonComponent(AnonComponent.this); }
38+
}
39+
}

src/main/java/com/sentropic/guiapi/gui/Font.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@ public class Font {
5757
public static void register(Font font) {
5858
String id = font.getID();
5959
if (registeredFonts.containsKey(id)) {
60-
throw new IllegalArgumentException("Font \""+id+"\" already exists");
60+
throw new IllegalArgumentException("Font \""+id+"\" is already registered");
6161
}
6262
registeredFonts.put(id, font);
6363
}
6464

65+
@SuppressWarnings("unused")
6566
public static boolean unregister(Font font) {
6667
boolean success = false;
6768
if (registeredFonts != null) { success = registeredFonts.remove(font.toString()) != null; }
@@ -98,7 +99,6 @@ public int getWidth(char character, boolean scale) {
9899
}
99100

100101
public int getWidth(String text, boolean scale) {
101-
if (text.equals("")) { throw new IllegalArgumentException(); }
102102
int total = 0;
103103
for (Character character : text.toCharArray()) { total += getWidth(character, scale); }
104104
return total;
@@ -109,5 +109,6 @@ public int getWidth(String text, boolean scale) {
109109

110110
public String getID() { return id; }
111111

112+
@SuppressWarnings("unused")
112113
public int getHeight() { return height; }
113114
}

0 commit comments

Comments
 (0)