Skip to content

Commit 7872a9f

Browse files
committed
Added debug command and config options
1 parent 1f68e22 commit 7872a9f

File tree

6 files changed

+87
-57
lines changed

6 files changed

+87
-57
lines changed

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

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

33
import com.sentropic.guiapi.GUIAPI;
4+
import com.sentropic.guiapi.GUIManager;
5+
import com.sentropic.guiapi.gui.GUI;
6+
import org.bukkit.Bukkit;
47
import org.bukkit.ChatColor;
58
import org.bukkit.command.Command;
69
import org.bukkit.command.CommandExecutor;
710
import org.bukkit.command.CommandSender;
11+
import org.bukkit.entity.Player;
812

913
public class ReloadCommand implements CommandExecutor {
1014
@Override
@@ -13,6 +17,11 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
1317
GUIAPI.getPlugin().reloadConfig();
1418
sender.sendMessage(ChatColor.GREEN+"[GUIAPI] Reloaded config.yml");
1519
return true;
20+
} else if (args.length == 1 && args[0].equals("debug")) {
21+
if (sender instanceof Player) {
22+
GUI gui = GUIAPI.getGUIManager().getGUI((Player) sender);
23+
gui.setDebug(!gui.isDebugging());
24+
}
1625
}
1726
return false;
1827
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.sentropic.guiapi.gui;
2+
3+
public enum Alignment {
4+
LEFT, RIGHT, CENTER
5+
}

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

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

3+
import org.jetbrains.annotations.NotNull;
34
import org.jetbrains.annotations.Nullable;
45

56
import java.util.HashMap;
67
import java.util.Map;
8+
import java.util.Objects;
79

810
public class Font {
911

@@ -29,7 +31,7 @@ public class Font {
2931
public static Font ofName(String name) { return registeredFonts.get(name); }
3032

3133
public static void register(Font font) {
32-
String name = font.getName();
34+
String name = font.getID();
3335
if (registeredFonts.containsKey(name)) {
3436
throw new IllegalArgumentException("Font \""+name+"\" already exists");
3537
}
@@ -44,12 +46,12 @@ public static boolean unregister(Font font) {
4446

4547
// Instance code
4648

47-
private final String name;
49+
private final String id;
4850
private final int height;
4951
private Map<Character,Integer> widths;
5052

51-
public Font(String name, int height) {
52-
this.name = name;
53+
public Font(@NotNull String id, int height) {
54+
this.id = id;
5355
if (height < 5) { height += 1; } // Correction necessary for some reason
5456
this.height = height;
5557
}
@@ -59,32 +61,32 @@ public void registerWidth(char character, int width) {
5961
widths.put(character, width);
6062
}
6163

62-
public int getWidth(char character, boolean custom) {
64+
public int getWidth(char character, boolean scale) {
6365
int result;
6466
if (this == DEFAULT) {
6567
result = widths.getOrDefault(character, 6);
6668
} else {
6769
result = widths == null ?
68-
DEFAULT.getWidth(character, custom) :
69-
widths.getOrDefault(character, DEFAULT.getWidth(character, custom));
70+
DEFAULT.getWidth(character, scale) :
71+
widths.getOrDefault(character, DEFAULT.getWidth(character, scale));
7072
}
71-
if (!custom) {
73+
if (!scale) {
7274
result = Math.round(result*height/8f); // Scale
7375
}
7476
return result;
7577
}
7678

77-
public int getWidth(String text) {
79+
public int getWidth(String text, boolean scale) {
7880
if (text.equals("")) { return 0; }
7981
int total = 0;
80-
for (char character : text.toCharArray()) { total += getWidth(character, false); }
82+
for (char character : text.toCharArray()) { total += getWidth(character, scale); }
8183
return total;
8284
}
8385

8486
@Override
85-
public String toString() { return name; }
87+
public String toString() { return id; }
8688

87-
public String getName() { return name; }
89+
public String getID() { return id; }
8890

8991
public int getHeight() { return height; }
9092
}

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

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import com.comphenix.protocol.wrappers.EnumWrappers;
44
import com.comphenix.protocol.wrappers.WrappedChatComponent;
5+
import com.sentropic.guiapi.GUIAPI;
56
import com.sentropic.guiapi.packet.WrapperPlayServerTitle;
7+
import org.apache.commons.lang.ObjectUtils;
8+
import org.bukkit.configuration.ConfigurationSection;
69
import org.bukkit.entity.Player;
710
import org.jetbrains.annotations.NotNull;
811

@@ -17,6 +20,7 @@ public class GUI {
1720
private String rawJson;
1821
private boolean changed = true;
1922
private final WrapperPlayServerTitle packet;
23+
private boolean debug = false;
2024

2125
public GUI(Player player) {
2226
this.player = player;
@@ -27,23 +31,23 @@ public GUI(Player player) {
2731
public Player getPlayer() { return player; }
2832

2933
public void putOnTop(@NotNull GUIComponent guiComponent) {
30-
remove(guiComponent.getId());
34+
remove(guiComponent.getID());
3135
guiComponents.add(guiComponent);
3236
changed = true;
3337
}
3438

3539
public void putUnderneath(@NotNull GUIComponent guiComponent) {
36-
remove(guiComponent.getId());
40+
remove(guiComponent.getID());
3741
guiComponents.add(0, guiComponent);
3842
changed = true;
3943
}
4044

4145
public boolean update(@NotNull GUIComponent guiComponent) {
4246
boolean success = false;
43-
String id = guiComponent.getId();
47+
String id = guiComponent.getID();
4448
for (ListIterator<GUIComponent> iterator = guiComponents.listIterator(); iterator.hasNext(); ) {
4549
GUIComponent component = iterator.next();
46-
if (component.getId().equals(id)) {
50+
if (component.getID().equals(id)) {
4751
iterator.set(guiComponent);
4852
changed = true;
4953
success = true;
@@ -55,11 +59,11 @@ public boolean update(@NotNull GUIComponent guiComponent) {
5559

5660
public boolean putAfter(String after, @NotNull GUIComponent guiComponent) {
5761
boolean success = false;
58-
remove(guiComponent.getId());
62+
remove(guiComponent.getID());
5963
int i = 0;
6064
for (GUIComponent component : guiComponents) {
6165
i++;
62-
if (component.getId().equals(after)) {
66+
if (component.getID().equals(after)) {
6367
guiComponents.add(i, guiComponent);
6468
changed = true;
6569
success = true;
@@ -71,11 +75,11 @@ public boolean putAfter(String after, @NotNull GUIComponent guiComponent) {
7175

7276
public boolean putBefore(String before, @NotNull GUIComponent guiComponent) {
7377
boolean success = false;
74-
remove(guiComponent.getId());
78+
remove(guiComponent.getID());
7579
int i = -1;
7680
for (GUIComponent component : guiComponents) {
7781
i++;
78-
if (component.getId().equals(before)) {
82+
if (component.getID().equals(before)) {
7983
guiComponents.add(i, guiComponent);
8084
changed = true;
8185
success = true;
@@ -86,7 +90,7 @@ public boolean putBefore(String before, @NotNull GUIComponent guiComponent) {
8690
}
8791

8892
public boolean remove(String id) {
89-
boolean success = guiComponents.removeIf(guiComponent -> guiComponent.getId().equals(id));
93+
boolean success = guiComponents.removeIf(guiComponent -> guiComponent.getID().equals(id));
9094
changed = success || changed;
9195
return success;
9296
}
@@ -95,6 +99,36 @@ public boolean removeIf(Predicate<GUIComponent> predicate) {
9599
return guiComponents.removeIf(predicate);
96100
}
97101

102+
public boolean isDebugging() { return debug; }
103+
104+
private static final String ID_DEBUG = "debug:";
105+
public void setDebug(boolean debug) {
106+
if (this.debug == debug) { return; }
107+
else { this.debug = debug; }
108+
if (debug) {
109+
List<GUIComponent> debugComponents = new ArrayList<>();
110+
ConfigurationSection debugSection = GUIAPI.getPlugin().getConfig().getConfigurationSection("debug");
111+
if (debugSection == null) { return; }
112+
for (String componentKey : debugSection.getKeys(false)) {
113+
try {
114+
ConfigurationSection componentSection = Objects.requireNonNull(debugSection.getConfigurationSection(componentKey));
115+
ConfigurationSection fontSection = Objects.requireNonNull(componentSection.getConfigurationSection("font"));
116+
117+
String id = ID_DEBUG+componentKey;
118+
int offset = componentSection.getInt("offset");
119+
String text = Objects.requireNonNull(componentSection.getString("text"));
120+
Font font = new Font(Objects.requireNonNull(fontSection.getString("id")),
121+
fontSection.getInt("height"));
122+
Alignment alignment = Alignment.valueOf(Objects.requireNonNull(componentSection.getString("alignment")).toUpperCase());
123+
boolean scale = componentSection.getBoolean("scale", true);
124+
125+
debugComponents.add(new GUIComponent(id, offset, text, font, alignment, scale));
126+
} catch (NullPointerException | IllegalArgumentException ignored) { }
127+
}
128+
for (GUIComponent component : debugComponents) { putOnTop(component); }
129+
} else { removeIf(component -> component.getID().startsWith(ID_DEBUG)); }
130+
}
131+
98132
private void build() {
99133
StringBuilder builder = new StringBuilder("[{\"text\":\"");
100134
int offset = 0;

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

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,55 +12,26 @@ public class GUIComponent {
1212
private final int leftOffset;
1313
private final int rightOffset;
1414

15-
public enum Alignment {LEFT, RIGHT, CENTERED}
16-
17-
public GUIComponent(@NotNull String id,
18-
int offset,
19-
char character,
20-
Font font,
21-
@NotNull Alignment alignment) {
22-
this.id = id;
23-
this.text = String.valueOf(character);
24-
this.font = font;
25-
26-
int width = font.getWidth(character, true);
27-
switch (alignment) {
28-
case LEFT:
29-
this.leftOffset = offset;
30-
this.rightOffset = -offset-width;
31-
break;
32-
case RIGHT:
33-
this.leftOffset = offset-width;
34-
this.rightOffset = -offset;
35-
break;
36-
case CENTERED:
37-
this.leftOffset = offset-width/2;
38-
this.rightOffset = -this.leftOffset-width;
39-
break;
40-
default:
41-
throw new IllegalArgumentException();
42-
}
43-
}
44-
4515
public GUIComponent(@NotNull String id,
4616
int offset,
4717
@NotNull String text,
4818
Font font,
49-
@NotNull Alignment alignment) {
19+
@NotNull Alignment alignment,
20+
boolean scale) {
5021
this.id = id;
5122
this.text = text;
5223
this.font = font;
5324
switch (alignment) {
5425
case LEFT:
5526
this.leftOffset = offset;
56-
this.rightOffset = -offset-font.getWidth(text);
27+
this.rightOffset = -offset-font.getWidth(text, scale);
5728
break;
5829
case RIGHT:
59-
this.leftOffset = offset-font.getWidth(text);
30+
this.leftOffset = offset-font.getWidth(text, scale);
6031
this.rightOffset = -offset;
6132
break;
62-
case CENTERED:
63-
int width = font.getWidth(text);
33+
case CENTER:
34+
int width = font.getWidth(text, scale);
6435
this.leftOffset = offset-width/2;
6536
this.rightOffset = -this.leftOffset-width;
6637
break;
@@ -92,7 +63,7 @@ public List<GUIComponent> byWord() {
9263
return list;
9364
}
9465

95-
public String getId() { return id; }
66+
public String getID() { return id; }
9667

9768
public String getText() { return text; }
9869

src/main/resources/config.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
debug:
2+
1:
3+
offset: 0
4+
text: Debug text
5+
font:
6+
id: minecraft:default
7+
height: 8
8+
alignment: left
9+
scale: true

0 commit comments

Comments
 (0)