Skip to content

Commit 5b99c45

Browse files
committed
Added config option for send period
1 parent a5b99d8 commit 5b99c45

File tree

8 files changed

+125
-58
lines changed

8 files changed

+125
-58
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
public final class GUIAPI extends JavaPlugin {
1212
private static GUIAPI singleton;
1313
private static GUIManager guiManager;
14+
private static GUIConfig config;
15+
1416
public static ProtocolManager protocolManager;
1517
private static PacketListener packetListener;
1618

@@ -19,14 +21,15 @@ public void onEnable() {
1921
if (singleton != null) { throw new IllegalStateException(); }
2022
singleton = this;
2123

24+
guiManager = new GUIManager();
25+
getServer().getPluginManager().registerEvents(guiManager, this);
26+
2227
saveDefaultConfig();
23-
reloadConfig();
28+
config = new GUIConfig();
29+
2430
// TODO add config for character widths
2531
this.getCommand("guiapi").setExecutor(new ReloadCommand());
2632

27-
guiManager = new GUIManager();
28-
getServer().getPluginManager().registerEvents(guiManager, this);
29-
3033
protocolManager = ProtocolLibrary.getProtocolManager();
3134
packetListener = new PacketListener(this, PacketType.Play.Server.TITLE);
3235
protocolManager.addPacketListener(packetListener);
@@ -35,8 +38,9 @@ public void onEnable() {
3538
@Override
3639
public void onDisable() {
3740
HandlerList.unregisterAll(guiManager);
38-
guiManager.close();
41+
guiManager.disable();
3942
guiManager = null;
43+
config = null;
4044

4145
protocolManager.removePacketListener(packetListener);
4246
packetListener = null;
@@ -48,4 +52,6 @@ public void onDisable() {
4852
public static GUIAPI getPlugin() { return singleton; }
4953

5054
public static GUIManager getGUIManager() { return guiManager; }
55+
56+
public static GUIConfig getGUIConfig() { return config; }
5157
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.sentropic.guiapi;
2+
3+
import com.sentropic.guiapi.gui.Alignment;
4+
import com.sentropic.guiapi.gui.Font;
5+
import com.sentropic.guiapi.gui.GUI;
6+
import com.sentropic.guiapi.gui.GUIComponent;
7+
import org.bukkit.configuration.ConfigurationSection;
8+
import org.bukkit.configuration.file.FileConfiguration;
9+
10+
import java.util.ArrayList;
11+
import java.util.Collections;
12+
import java.util.List;
13+
import java.util.Objects;
14+
15+
public class GUIConfig {
16+
private int period;
17+
private final List<GUIComponent> debugComponents = new ArrayList<>();
18+
private final List<GUIComponent> debugComponentsRead = Collections.unmodifiableList(debugComponents);
19+
20+
GUIConfig() { reload(); }
21+
22+
public void reload() {
23+
GUIAPI plugin = GUIAPI.getPlugin();
24+
plugin.reloadConfig();
25+
FileConfiguration configFile = plugin.getConfig();
26+
period = configFile.getInt("send_period_milis", 1900);
27+
28+
// Debug components
29+
{
30+
debugComponents.clear();
31+
ConfigurationSection debugSection = configFile.getConfigurationSection("debug");
32+
if (debugSection != null) {
33+
for (String componentKey : debugSection.getKeys(false)) {
34+
try {
35+
ConfigurationSection componentSection = Objects.requireNonNull(debugSection.getConfigurationSection(componentKey));
36+
ConfigurationSection fontSection = Objects.requireNonNull(componentSection.getConfigurationSection("font"));
37+
38+
String id = GUI.ID_DEBUG+componentKey;
39+
int offset = componentSection.getInt("offset");
40+
String text = Objects.requireNonNull(componentSection.getString("text"));
41+
int width = componentSection.getInt("width", -1);
42+
43+
String fontID = Objects.requireNonNull(fontSection.getString("id"));
44+
Font font = Font.getRegistered(fontID);
45+
if (font == null) { font = new Font(fontID, fontSection.getInt("height", 8)); }
46+
Alignment alignment = Alignment.valueOf(
47+
Objects.requireNonNull(componentSection.getString("alignment")).toUpperCase());
48+
boolean scale = componentSection.getBoolean("scale", true);
49+
50+
GUIComponent component;
51+
if (width == -1) {
52+
component = new GUIComponent(id, offset, text, font, alignment, scale);
53+
} else {
54+
component = new GUIComponent(id, offset, text, width, font, alignment);
55+
}
56+
debugComponents.add(component);
57+
} catch (NullPointerException | IllegalArgumentException ignored) { }
58+
}
59+
}
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+
}
68+
}
69+
70+
public int getSendPeriod() { return period; }
71+
72+
public List<GUIComponent> getDebugComponents() { return debugComponentsRead; }
73+
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
package com.sentropic.guiapi;
22

33
import com.sentropic.guiapi.gui.GUI;
4-
import org.bukkit.GameMode;
54
import org.bukkit.entity.Player;
65
import org.bukkit.event.EventHandler;
76
import org.bukkit.event.Listener;
87
import org.bukkit.event.player.PlayerQuitEvent;
98
import org.bukkit.scheduler.BukkitRunnable;
109

10+
import java.util.Collections;
1111
import java.util.HashMap;
1212
import java.util.Map;
1313

1414
public class GUIManager implements Listener {
1515
private final Map<Player,GUI> GUIS = new HashMap<>();
16+
private final Map<Player,GUI> GUIS_READ = Collections.unmodifiableMap(GUIS);
1617
private final Task task = new Task();
1718

18-
//TODO test bigger periods
1919
GUIManager() { task.runTaskTimer(GUIAPI.getPlugin(), 0, 1); }
2020

21-
void close() {
21+
void disable() {
2222
try { task.cancel(); } catch (IllegalStateException ignored) { }
2323
}
2424

@@ -31,6 +31,8 @@ public GUI getGUI(Player player) {
3131
return gui;
3232
}
3333

34+
public Map<Player,GUI> getGUIS() { return GUIS_READ; }
35+
3436
@EventHandler
3537
public void onPlayerLeave(PlayerQuitEvent event) { GUIS.remove(event.getPlayer()); }
3638

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public class ReloadCommand implements CommandExecutor {
1313
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
1414
if (!sender.hasPermission("misc.command.reload")) { return false; }
1515
if (args.length == 1 && args[0].equals("reload")) {
16-
GUIAPI.getPlugin().reloadConfig();
17-
sender.sendMessage(ChatColor.GREEN+"[GUIAPI] Reloaded config.yml");
16+
GUIAPI.getGUIConfig().reload();
17+
sender.sendMessage(ChatColor.GREEN+"[GUI API] Reloaded config.yml");
1818
return true;
1919
} else if (args.length == 1 && args[0].equals("debug")) {
2020
if (sender instanceof Player) {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ public class Font {
5252
}
5353

5454
@Nullable
55-
public static Font ofName(String name) { return registeredFonts.get(name); }
55+
public static Font getRegistered(String id) { return registeredFonts.get(id); }
5656

5757
public static void register(Font font) {
58-
String name = font.getID();
59-
if (registeredFonts.containsKey(name)) {
60-
throw new IllegalArgumentException("Font \""+name+"\" already exists");
58+
String id = font.getID();
59+
if (registeredFonts.containsKey(id)) {
60+
throw new IllegalArgumentException("Font \""+id+"\" already exists");
6161
}
62-
registeredFonts.put(name, font);
62+
registeredFonts.put(id, font);
6363
}
6464

6565
public static boolean unregister(Font font) {

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

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.comphenix.protocol.wrappers.WrappedChatComponent;
55
import com.sentropic.guiapi.GUIAPI;
66
import com.sentropic.guiapi.packet.WrapperPlayServerTitle;
7-
import org.bukkit.configuration.ConfigurationSection;
87
import org.bukkit.entity.Player;
98
import org.jetbrains.annotations.NotNull;
109

@@ -20,6 +19,7 @@ public class GUI {
2019
private boolean changed = true;
2120
private final WrapperPlayServerTitle packet;
2221
private boolean debug = false;
22+
private long lastSend = 0;
2323

2424
public GUI(Player player) {
2525
this.player = player;
@@ -102,37 +102,12 @@ public boolean removeIf(Predicate<GUIComponent> predicate) {
102102

103103
public boolean isDebugging() { return debug; }
104104

105-
private static final String ID_DEBUG = "debug:";
105+
public static final String ID_DEBUG = "debug:";
106106

107107
public void setDebug(boolean debug) {
108108
if (this.debug == debug) { return; } else { this.debug = debug; }
109109
if (debug) {
110-
List<GUIComponent> debugComponents = new ArrayList<>();
111-
ConfigurationSection debugSection = GUIAPI.getPlugin().getConfig().getConfigurationSection("debug");
112-
if (debugSection == null) { return; }
113-
for (String componentKey : debugSection.getKeys(false)) {
114-
try {
115-
ConfigurationSection componentSection = Objects.requireNonNull(debugSection.getConfigurationSection(componentKey));
116-
ConfigurationSection fontSection = Objects.requireNonNull(componentSection.getConfigurationSection("font"));
117-
118-
String id = ID_DEBUG+componentKey;
119-
int offset = componentSection.getInt("offset");
120-
String text = Objects.requireNonNull(componentSection.getString("text"));
121-
int width = componentSection.getInt("width", -1);
122-
123-
Font font = new Font(Objects.requireNonNull(fontSection.getString("id")),
124-
fontSection.getInt("height"));
125-
Alignment alignment = Alignment.valueOf(Objects.requireNonNull(componentSection.getString("alignment")).toUpperCase());
126-
boolean scale = componentSection.getBoolean("scale", true);
127-
128-
GUIComponent component;
129-
if (width == -1) { component = new GUIComponent(id, offset, text, font, alignment, scale); } else {
130-
component = new GUIComponent(id, offset, text, width, font, alignment);
131-
}
132-
debugComponents.add(component);
133-
} catch (NullPointerException | IllegalArgumentException ignored) { }
134-
}
135-
for (GUIComponent component : debugComponents) { putOnTop(component); }
110+
for (GUIComponent component : GUIAPI.getGUIConfig().getDebugComponents()) { putOnTop(component); }
136111
} else { removeIf(component -> component.getID().startsWith(ID_DEBUG)); }
137112
}
138113

@@ -176,10 +151,15 @@ public String getRawJson() {
176151
}
177152

178153
public void play() {
179-
if (changed) { build(); }
180-
sendingPacket = true;
181-
packet.sendPacket(player);
182-
sendingPacket = false;
154+
long time = System.currentTimeMillis();
155+
boolean play = changed || time-lastSend >= GUIAPI.getGUIConfig().getSendPeriod();
156+
if (play) {
157+
lastSend = time;
158+
if (changed) { build(); }
159+
sendingPacket = true;
160+
packet.sendPacket(player);
161+
sendingPacket = false;
162+
}
183163
}
184164

185165
public static String spacesOf(int amount) {

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ public GUIComponent(@NotNull String id,
1818
this(id, offset, text, font.getWidth(text, scale), font, alignment);
1919
}
2020

21-
GUIComponent(String id,
22-
int offset,
23-
String text,
24-
int width,
25-
Font font,
26-
Alignment alignment) {
21+
public GUIComponent(@NotNull String id,
22+
int offset,
23+
@NotNull String text,
24+
int width,
25+
Font font,
26+
@NotNull Alignment alignment) {
2727
this.id = id;
2828
this.text = text;
2929
this.font = font;

src/main/resources/config.yml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
send_period_milis: 1900
12
debug:
2-
1:
3+
default:
34
offset: 0
4-
text: Debug text
5+
text: "This text must start at the screen center"
56
font:
6-
id: minecraft:default
7+
id: "minecraft:default"
8+
alignment: "left"
9+
test:
10+
offset: -10
11+
text: "Some text"
12+
font:
13+
id: "minecraft:default"
714
height: 8
8-
alignment: left
9-
scale: true
15+
alignment: "right"

0 commit comments

Comments
 (0)