Skip to content

Commit 910716c

Browse files
committed
Optimized GUI storage
1 parent c96be95 commit 910716c

File tree

8 files changed

+90
-52
lines changed

8 files changed

+90
-52
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package com.sentropic.guiapi;
22

33
import com.sentropic.guiapi.command.ReloadCommand;
4-
import com.sentropic.guiapi.gui.GUIManager;
5-
import com.sentropic.guiapi.packet.PacketManager;
64
import com.sentropic.guiapi.gui.GUI;
5+
import com.sentropic.guiapi.packet.PacketManager;
76
import org.bukkit.entity.Player;
87
import org.bukkit.event.EventHandler;
98
import org.bukkit.event.HandlerList;
@@ -84,18 +83,21 @@ private void setProtocolLib(boolean enabled) {
8483

8584
/**
8685
* Gets the singleton {@link GUIAPI} object loaded by the server
86+
*
8787
* @return the singleton {@link GUIAPI} object
8888
*/
8989
public static GUIAPI getPlugin() { return singleton; }
9090

9191
/**
9292
* Gets the singleton {@link GUIManager} loaded by the plugin. Use this to access a {@link Player}s' {@link GUI}
93+
*
9394
* @return the {@link GUIManager} instance loaded by the plugin
9495
*/
9596
public static GUIManager getGUIManager() { return guiManager; }
9697

9798
/**
9899
* Gets the singleton {@link GUIConfig} loaded by the plugin
100+
*
99101
* @return the {@link GUIConfig} instance loaded by the plugin
100102
*/
101103
public static GUIConfig getGUIConfig() { return config; }

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void reload() {
7070
} catch (NullPointerException | IllegalArgumentException ignored) { }
7171
}
7272
}
73-
GUIAPI.getGUIManager().getGUIS().values().forEach(GUI::onReload);
73+
GUIAPI.getGUIManager().GUIS.forEach(GUI::onReload);
7474
}
7575
}
7676

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
package com.sentropic.guiapi.gui;
1+
package com.sentropic.guiapi;
22

3-
import com.sentropic.guiapi.GUIAPI;
3+
import com.sentropic.guiapi.gui.GUI;
44
import org.bukkit.entity.Player;
55
import org.bukkit.event.EventHandler;
66
import org.bukkit.event.Listener;
77
import org.bukkit.event.player.PlayerQuitEvent;
8+
import org.bukkit.metadata.FixedMetadataValue;
89
import org.bukkit.scheduler.BukkitRunnable;
910

10-
import java.util.Collections;
11-
import java.util.HashMap;
12-
import java.util.Map;
11+
import java.util.HashSet;
12+
import java.util.Set;
1313

1414
/**
1515
* Class in charge of storing, accessing and updating the {@link Player}s' {@link GUI}
1616
* To access the instance used by the plugin, use {@link GUIAPI#getGUIManager()}
1717
*/
1818
public class GUIManager implements Listener {
19-
private final Map<Player,GUI> GUIS = new HashMap<>();
20-
private final Map<Player,GUI> GUIS_READ = Collections.unmodifiableMap(GUIS);
19+
private static final String METADATA_KEY = "guiapi:gui";
20+
21+
final Set<GUI> GUIS = new HashSet<>();
2122
private final Task task = new Task();
2223

2324
/**
@@ -28,40 +29,41 @@ public class GUIManager implements Listener {
2829
/**
2930
* For internal use only. Used to clear the stored {@link GUI}s from memory and cancel GUI updating
3031
*/
31-
public void disable() {
32+
void disable() {
3233
try {
3334
task.cancel();
35+
for (GUI gui : GUIS) {
36+
gui.getPlayer().removeMetadata(METADATA_KEY, GUIAPI.getPlugin());
37+
}
3438
GUIS.clear();
3539
} catch (IllegalStateException ignored) { }
3640
}
3741

3842
/**
3943
* Gets the {@link GUI} of a given {@link Player}
44+
*
4045
* @param player the {@link Player} to get the {@link GUI} for
4146
* @return the existing {@link GUI} of the {@link Player}, or a new one if nonexistent
4247
*/
4348
public GUI getGUI(Player player) {
44-
GUI gui = GUIS.get(player);
45-
if (gui == null) {
49+
GUI gui;
50+
if (player.hasMetadata(METADATA_KEY)) {
51+
gui = (GUI) player.getMetadata(METADATA_KEY).get(0).value();
52+
} else {
4653
gui = new GUI(player);
47-
GUIS.put(player, gui);
54+
GUIS.add(gui);
55+
player.setMetadata(METADATA_KEY, new FixedMetadataValue(GUIAPI.getPlugin(), gui));
4856
}
4957
return gui;
5058
}
5159

52-
/**
53-
* @return a read-only {@link Map} containing all {@link Player}s who have a {@link GUI},
54-
* * and the respective GUIs
55-
*/
56-
public Map<Player,GUI> getGUIS() { return GUIS_READ; }
57-
5860
@EventHandler
59-
public void onPlayerLeave(PlayerQuitEvent event) { GUIS.remove(event.getPlayer()); }
61+
public void onPlayerLeave(PlayerQuitEvent event) { GUIS.remove(this.getGUI(event.getPlayer())); }
6062

6163
private class Task extends BukkitRunnable {
6264
@Override
6365
public void run() {
64-
GUIS.values().forEach(GUI::play);
66+
GUIS.forEach(GUI::play);
6567
}
6668
}
6769
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
2323
GUI gui = GUIAPI.getGUIManager().getGUI((Player) sender);
2424
gui.setDebug(!gui.isDebugging());
2525
return true;
26-
} else { sender.sendMessage("Command only runnable ingame"); }
26+
} else { sender.sendMessage("Command only runnable in-game"); }
2727
}
2828
return false;
2929
}

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public class Font {
5656

5757
/**
5858
* Gets a registered font by its ID
59+
*
5960
* @param id the id of the font
6061
* @return the font for the given ID, or null if was not found
6162
*/
@@ -64,6 +65,7 @@ public class Font {
6465

6566
/**
6667
* Registers a Font to be accessed statically later, through {@link Font#getRegistered(String)}
68+
*
6769
* @param font the Font to be registered
6870
* @throws IllegalArgumentException if a font with the same ID already is registered
6971
*/
@@ -77,6 +79,7 @@ public static void register(Font font) {
7779

7880
/**
7981
* Unregisters a Font from the static context
82+
*
8083
* @param font the font to unregister
8184
* @return true if the font was unregistered, false if it was not previously registered
8285
*/
@@ -95,7 +98,7 @@ public static boolean unregister(Font font) {
9598
private Map<Character,Integer> widths;
9699

97100
/**
98-
* @param id the namespaced ID of the font, as used by the resource pack (i.e. "minecraft:default")
101+
* @param id the namespaced ID of the font, as used by the resource pack (i.e. "minecraft:default")
99102
* @param height the default height of the characters in the font, as specified in the resource pack
100103
*/
101104
public Font(@NotNull String id, int height) {
@@ -106,7 +109,8 @@ public Font(@NotNull String id, int height) {
106109
/**
107110
* Creates a font that inherits its character widths from a parent font
108111
* Used for fonts that share the same textures with another one
109-
* @param id the namespaced ID of the font, as used by the resource pack (i.e. "minecraft:default")
112+
*
113+
* @param id the namespaced ID of the font, as used by the resource pack (i.e. "minecraft:default")
110114
* @param height the default height of the characters in the font, as specified in the resource pack
111115
* @param parent the font to inherit its character widths from
112116
*/
@@ -118,8 +122,9 @@ public Font(@NotNull String id, int height, Font parent) {
118122

119123
/**
120124
* Registers the width of a character for this font, if different from the default of 6
125+
*
121126
* @param character the character to register the width for
122-
* @param width the width of the character
127+
* @param width the width of the character
123128
*/
124129
public void registerWidth(char character, int width) {
125130
if (widths == null) { widths = new HashMap<>(); }
@@ -128,8 +133,9 @@ public void registerWidth(char character, int width) {
128133

129134
/**
130135
* Gets the width of a given character for this font
136+
*
131137
* @param character the character to get the width for
132-
* @param scale whether to scale the width according to the font's height
138+
* @param scale whether to scale the width according to the font's height
133139
* @return the width of the character
134140
*/
135141
public int getWidth(char character, boolean scale) {
@@ -151,7 +157,8 @@ public int getWidth(char character, boolean scale) {
151157

152158
/**
153159
* Calculates the width of a given {@link String} for this font
154-
* @param text the String to calculate the width for
160+
*
161+
* @param text the String to calculate the width for
155162
* @param scale whether to scale the width according to the font's height
156163
* @return the calculated width of the character
157164
*/

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.sentropic.guiapi.gui;
22

33
import com.sentropic.guiapi.GUIAPI;
4+
import com.sentropic.guiapi.GUIManager;
45
import net.md_5.bungee.api.ChatMessageType;
56
import net.md_5.bungee.api.chat.BaseComponent;
67
import net.md_5.bungee.api.chat.TextComponent;
@@ -63,13 +64,22 @@ public class GUI {
6364
private final Player player;
6465
private final List<GUIComponent> guiComponents = new ArrayList<>();
6566

66-
GUI(Player player) {
67+
68+
/**
69+
* Important: If you need to get a player's GUI, use {@link GUIManager#getGUI(Player)},
70+
* since GUIs created through this constructor will not be processed.
71+
*
72+
* @param player the player the GUI belongs to.
73+
*/
74+
@Deprecated
75+
public GUI(Player player) {
6776
this.player = player;
6877
guiComponents.add(defaultComponent);
6978
}
7079

7180
/**
7281
* Gets the owner of this GUI
82+
*
7383
* @return the {@link Player} to whom this GUI belongs to
7484
*/
7585
@SuppressWarnings("unused")
@@ -80,6 +90,7 @@ public class GUI {
8090
/**
8191
* Adds the given {@link GUIComponent} to the GUI after all previously added GUIComponents,
8292
* and removes any other component with the same ID as the given one
93+
*
8394
* @param component the GUIComponent to add to the GUI
8495
*/
8596
public void putOnTop(@NotNull GUIComponent component) {
@@ -92,6 +103,7 @@ public void putOnTop(@NotNull GUIComponent component) {
92103
/**
93104
* Adds the given {@link GUIComponent} to the GUI before all previously added GUIComponents,
94105
* and removes any other component with the same ID as the given one
106+
*
95107
* @param component the GUIComponent to add to the GUI
96108
*/
97109
@SuppressWarnings("unused")
@@ -105,6 +117,7 @@ public void putUnderneath(@NotNull GUIComponent component) {
105117
/**
106118
* If a {@link GUIComponent} exists with an ID matching the one of the given component,
107119
* removes it and puts the given one in its place
120+
*
108121
* @param component the GUIComponent to add to the GUI
109122
* @return whether the component could be added
110123
*/
@@ -130,6 +143,7 @@ public boolean update(@NotNull GUIComponent component) {
130143
/**
131144
* If a {@link GUIComponent} exists with an ID matching the given id,
132145
* adds the provided component after it, and removes any other component with the same ID as it
146+
*
133147
* @param component the GUIComponent to add to the GUI
134148
* @return whether the component could be added
135149
*/
@@ -154,6 +168,7 @@ public boolean putAfter(String id, @NotNull GUIComponent component) {
154168
/**
155169
* If a {@link GUIComponent} exists with an ID matching the given id,
156170
* adds the provided component} before it, and removes any other component with the same ID as it
171+
*
157172
* @param component the GUIComponent to add to the GUI
158173
* @return whether the component could be added
159174
*/
@@ -177,6 +192,7 @@ public boolean putBefore(String before, @NotNull GUIComponent component) {
177192

178193
/**
179194
* If a {@link GUIComponent} exists with an ID matching the given id, removes it from the GUI
195+
*
180196
* @return whether a component with a matching id was removed
181197
*/
182198
@SuppressWarnings("UnusedReturnValue")
@@ -187,6 +203,7 @@ public boolean remove(String id) {
187203

188204
/**
189205
* Removes any {@link GUIComponent}s that meet a given predicate from the GUI
206+
*
190207
* @param predicate the predicate that to-be-removed {@link GUIComponent}s must meet
191208
* @return whether any {@link GUIComponent}s were removed
192209
*/
@@ -218,12 +235,14 @@ private static void checkID(String id) {
218235

219236
/**
220237
* Gets whether this GUI is in debug mode (displaying the debug {@link GUIComponent}s defined in the plugin's config)
238+
*
221239
* @return whether this GUI is in debug mode
222240
*/
223241
public boolean isDebugging() { return debug; }
224242

225243
/**
226244
* Sets this GUI to debug mode if debug is true, or disables it otherwise
245+
*
227246
* @param debug whether to put the GUI in debug mode or not
228247
*/
229248
public void setDebug(boolean debug) {
@@ -308,6 +327,7 @@ public void onReload() {
308327
* Adds an {@link AnonComponent} containing the given baseComponent
309328
* Because the content of other {@link BaseComponent} implementations are unknown to the server,
310329
* only supports {@link TextComponent} at this moment
330+
*
311331
* @param baseComponent the anonymous chat component to add to the GUI
312332
* @return whether the given baseComponent was of the supported types and could be added
313333
*/
@@ -334,6 +354,7 @@ public boolean addAnonComponent(BaseComponent baseComponent) {
334354

335355
/**
336356
* Removes the given anonymous component from the GUI
357+
*
337358
* @param component the anonymous component to remove
338359
*/
339360
public void removeAnonComponent(AnonComponent component) {
@@ -394,13 +415,15 @@ private void reschedule() {
394415

395416
/**
396417
* Used to distinguish between action bar text sent by {@link GUIAPI} and those send anonymously
418+
*
397419
* @return whether {@link GUIAPI} is sending an action bar packet at the moment of calling
398420
*/
399421
public static boolean isSending() { return sending; }
400422

401423
/**
402424
* Builds a {@link String} containing the specified amount of space,
403425
* from space characters provided by AmberW's Negative Space resource pack
426+
*
404427
* @param amount the amount of space to generate the string for, whether positive, negative or zero
405428
* @return the built {@link String} containing the specified amount of space
406429
*/

0 commit comments

Comments
 (0)