Skip to content

Commit 95252a5

Browse files
committed
Added documentation
1 parent ac906de commit 95252a5

File tree

10 files changed

+361
-63
lines changed

10 files changed

+361
-63
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.sentropic.guiapi;
22

33
import com.sentropic.guiapi.command.ReloadCommand;
4+
import com.sentropic.guiapi.gui.GUIManager;
45
import com.sentropic.guiapi.packet.PacketManager;
6+
import com.sentropic.guiapi.gui.GUI;
7+
import org.bukkit.entity.Player;
58
import org.bukkit.event.EventHandler;
69
import org.bukkit.event.HandlerList;
710
import org.bukkit.event.Listener;
@@ -79,9 +82,21 @@ private void setProtocolLib(boolean enabled) {
7982
}
8083
}
8184

85+
/**
86+
* Gets the singleton {@link GUIAPI} object loaded by the server
87+
* @return the singleton {@link GUIAPI} object
88+
*/
8289
public static GUIAPI getPlugin() { return singleton; }
8390

91+
/**
92+
* Gets the singleton {@link GUIManager} loaded by the plugin. Use this to access a {@link Player}s' {@link GUI}
93+
* @return the {@link GUIManager} instance loaded by the plugin
94+
*/
8495
public static GUIManager getGUIManager() { return guiManager; }
8596

97+
/**
98+
* Gets the singleton {@link GUIConfig} loaded by the plugin
99+
* @return the {@link GUIConfig} instance loaded by the plugin
100+
*/
86101
public static GUIConfig getGUIConfig() { return config; }
87102
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
import java.util.List;
1414
import java.util.Objects;
1515

16+
/**
17+
* Represents the config options of the plugin
18+
* For efficiency, use instead of directly getting the config file from the plugin
19+
*/
1620
public class GUIConfig {
1721
private int sendPeriod;
1822
private int anonPeriod;
@@ -22,6 +26,9 @@ public class GUIConfig {
2226

2327
GUIConfig() { reload(); }
2428

29+
/**
30+
* Reloads the plugin's config and stores all its values
31+
*/
2532
public void reload() {
2633
GUIAPI plugin = GUIAPI.getPlugin();
2734
plugin.reloadConfig();
@@ -68,11 +75,31 @@ public void reload() {
6875
}
6976
}
7077

78+
/**
79+
* @return the maximum delay between sending GUIs to players, in milisecons
80+
*/
7181
public int getSendPeriod() { return sendPeriod; }
7282

83+
/**
84+
* Gets the amount of ticks that each anonymous action bar text (sent without the usage of GUIAPI)
85+
* is shown in the GUI before switching to a different one, if more than one is available
86+
*
87+
* @return the period at which anonymous action bar texts are cycled around in the GUI
88+
*/
7389
public int getAnonPeriod() { return anonPeriod; }
7490

91+
/**
92+
* Gets the amount of ticks that each anonymous action bar text (sent without the usage of GUIAPI)
93+
* remains in memory before being removed (defaults to 40 in vanilla Minecraft)
94+
*
95+
* @return the duration of anonymous action bars
96+
*/
7597
public int getAnonDuration() { return anonDuration; }
7698

99+
/**
100+
* Gets the debug {@link GUIComponent}s defined in the config
101+
*
102+
* @return a {@link List} containing the debug {@link GUIComponent}s defined in the config, in typing order
103+
*/
77104
public List<GUIComponent> getDebugComponents() { return debugComponentsRead; }
78105
}

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

Lines changed: 0 additions & 45 deletions
This file was deleted.

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111

1212
public class ReloadCommand implements CommandExecutor {
1313
@Override
14-
public boolean onCommand(CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
15-
if (!sender.hasPermission("misc.command.reload")) { return false; }
14+
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
1615
if (args.length == 1 && args[0].equals("reload")) {
16+
if (!sender.hasPermission("misc.command.reload")) { return false; }
1717
GUIAPI.getGUIConfig().reload();
1818
sender.sendMessage(ChatColor.GREEN+"[GUI API] Reloaded config.yml");
1919
return true;
2020
} else if (args.length == 1 && args[0].equals("debug")) {
21+
if (!sender.hasPermission("misc.command.debug")) { return false; }
2122
if (sender instanceof Player) {
2223
GUI gui = GUIAPI.getGUIManager().getGUI((Player) sender);
2324
gui.setDebug(!gui.isDebugging());

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

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import java.util.HashMap;
77
import java.util.Map;
88

9+
/**
10+
* Represents a resource pack font, containing its ID and character widths
11+
*/
912
public class Font {
1013

1114
// Static code
@@ -51,9 +54,19 @@ public class Font {
5154
register(DEFAULT);
5255
}
5356

57+
/**
58+
* Gets a registered font by its ID
59+
* @param id the id of the font
60+
* @return the font for the given ID, or null if was not found
61+
*/
5462
@Nullable
5563
public static Font getRegistered(String id) { return registeredFonts.get(id); }
5664

65+
/**
66+
* Registers a Font to be accessed statically later, through {@link Font#getRegistered(String)}
67+
* @param font the Font to be registered
68+
* @throws IllegalArgumentException if a font with the same ID already is registered
69+
*/
5770
public static void register(Font font) {
5871
String id = font.getID();
5972
if (registeredFonts.containsKey(id)) {
@@ -62,6 +75,11 @@ public static void register(Font font) {
6275
registeredFonts.put(id, font);
6376
}
6477

78+
/**
79+
* Unregisters a Font from the static context
80+
* @param font the font to unregister
81+
* @return true if the font was unregistered, false if it was not previously registered
82+
*/
6583
@SuppressWarnings("unused")
6684
public static boolean unregister(Font font) {
6785
boolean success = false;
@@ -73,23 +91,56 @@ public static boolean unregister(Font font) {
7391

7492
private final String id;
7593
private final int height;
94+
private Font parent;
7695
private Map<Character,Integer> widths;
7796

97+
/**
98+
* @param id the namespaced ID of the font, as used by the resource pack (i.e. "minecraft:default")
99+
* @param height the default height of the characters in the font, as specified in the resource pack
100+
*/
78101
public Font(@NotNull String id, int height) {
79102
this.id = id;
80103
this.height = height;
81104
}
82105

106+
/**
107+
* Creates a font that inherits its character widths from a parent font
108+
* 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")
110+
* @param height the default height of the characters in the font, as specified in the resource pack
111+
* @param parent the font to inherit its character widths from
112+
*/
113+
public Font(@NotNull String id, int height, Font parent) {
114+
this.id = id;
115+
this.height = height;
116+
this.parent = parent;
117+
}
118+
119+
/**
120+
* Registers the width of a character for this font, if different from the default of 6
121+
* @param character the character to register the width for
122+
* @param width the width of the character
123+
*/
83124
public void registerWidth(char character, int width) {
84125
if (widths == null) { widths = new HashMap<>(); }
85126
widths.put(character, width);
86127
}
87128

129+
/**
130+
* Gets the width of a given character for this font
131+
* @param character the character to get the width for
132+
* @param scale whether to scale the width according to the font's height
133+
* @return the width of the character
134+
*/
88135
public int getWidth(char character, boolean scale) {
89136
Integer result = null;
90137
if (this == DEFAULT) { result = widths.getOrDefault(character, 6); } else {
91138
try { result = widths.get(character); } catch (NullPointerException ignored) { }
92-
if (result == null) { result = DEFAULT.getWidth(character, false); }
139+
if (result == null) {
140+
result = parent == null ?
141+
DEFAULT.getWidth(character, false) :
142+
parent.getWidth(character, false);
143+
}
93144
}
94145
if (scale && this != DEFAULT && character != ' ') {
95146
// Formula figured out experimentally (pain)
@@ -98,17 +149,32 @@ public int getWidth(char character, boolean scale) {
98149
return result;
99150
}
100151

152+
/**
153+
* Calculates the width of a given {@link String} for this font
154+
* @param text the String to calculate the width for
155+
* @param scale whether to scale the width according to the font's height
156+
* @return the calculated width of the character
157+
*/
101158
public int getWidth(String text, boolean scale) {
102159
int total = 0;
103160
for (Character character : text.toCharArray()) { total += getWidth(character, scale); }
104161
return total;
105162
}
106163

164+
/**
165+
* @return the namespaced ID of the font, as used by the resource pack (i.e. "minecraft:default")
166+
*/
107167
@Override
108168
public String toString() { return id; }
109169

170+
/**
171+
* @return the namespaced ID of the font, as used by the resource pack (i.e. "minecraft:default")
172+
*/
110173
public String getID() { return id; }
111174

175+
/**
176+
* @return the default height of the characters in the font, as specified in the resource pack
177+
*/
112178
@SuppressWarnings("unused")
113179
public int getHeight() { return height; }
114180
}

0 commit comments

Comments
 (0)