Skip to content

Commit ab83d55

Browse files
committed
General fixes/cleanup
1 parent a5c30c3 commit ab83d55

File tree

12 files changed

+102
-76
lines changed

12 files changed

+102
-76
lines changed

src/main/java/me/tom/sparse/spigot/chat/menu/ChatMenu.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,8 @@ else if(text.getWidth() > 320)
211211
return result;
212212
}
213213

214-
//FIXME: Javadocs unclear. says would return true, but return type isn't a boolean
215214
/**
216-
* @return true if any elements overlap
215+
* @return the first element found that overlaps
217216
*/
218217
@Nullable
219218
public Element findOverlap()
@@ -331,10 +330,4 @@ public boolean equals(Object o)
331330

332331
return id.equals(chatMenu.id);
333332
}
334-
335-
//FIXME: Redundant
336-
public ChatMenu getMenu()
337-
{
338-
return this;
339-
}
340333
}

src/main/java/me/tom/sparse/spigot/chat/menu/IElementContainer.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,39 @@
77

88
public interface IElementContainer
99
{
10+
/**
11+
* Add the specified element to this container
12+
*
13+
* @param element the element to add
14+
* @param <T> the type of element being added
15+
* @return the element that was added
16+
*/
1017
<T extends Element> T add(T element);
1118

19+
/**
20+
* Remove the specified element from this container
21+
*
22+
* @param element the element to remove
23+
* @return true if the element was removed
24+
*/
1225
boolean remove(Element element);
1326

27+
/**
28+
*
29+
* @return an unmodifiable list of all the elements in this container
30+
*/
1431
List<Element> getElements();
1532

33+
/**
34+
*
35+
* @param element the element to interact with
36+
* @return the command used to interact with the provided element
37+
*/
1638
String getCommand(Element element);
1739

40+
/**
41+
* Display this container to the specified player
42+
* @param player the player to open this container for
43+
*/
1844
void openFor(Player player);
1945
}

src/main/java/me/tom/sparse/spigot/chat/menu/element/BooleanElement.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
*/
2121
public class BooleanElement extends Element
2222
{
23-
// protected boolean value;
2423
@Nonnull
2524
public final State<Boolean> value;
2625

@@ -89,7 +88,6 @@ public ChatColor getFalseColor()
8988
/**
9089
* @param falseColor the color the symbol should be if the value is {@code false}
9190
*/
92-
//TODO: Annotate @Nonnull and get rid of default colour?
9391
public void setFalseColor(@Nullable ChatColor falseColor)
9492
{
9593
this.falseColor = falseColor == null ? ChatColor.RED : falseColor;
@@ -107,7 +105,6 @@ public ChatColor getTrueColor()
107105
/**
108106
* @param trueColor The color the symbol should be if the value is {@code true}
109107
*/
110-
//TODO: Same as above
111108
public void setTrueColor(@Nullable ChatColor trueColor)
112109
{
113110
this.trueColor = trueColor == null ? ChatColor.GREEN : trueColor;
@@ -129,7 +126,7 @@ public List<Text> render(@Nonnull IElementContainer context)
129126
String baseCommand = context.getCommand(this);
130127

131128
List<BaseComponent> components = new ArrayList<>();
132-
boolean current = value.getCurrent();
129+
boolean current = value.getOptionalCurrent().orElse(false);
133130
TextComponent c = new TextComponent(current ? "\u2714" : "\u2718");
134131
c.setColor(current ? trueColor : falseColor);
135132
c.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, baseCommand + !current));
@@ -149,10 +146,9 @@ public void edit(@Nonnull IElementContainer container, @Nonnull String[] args)
149146
/**
150147
* @return the current value
151148
*/
152-
@Nonnull
153149
public boolean getValue()
154150
{
155-
return value.getCurrent();
151+
return value.getOptionalCurrent().orElse(false);
156152
}
157153

158154
/**

src/main/java/me/tom/sparse/spigot/chat/menu/element/ButtonElement.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public ButtonElement(int x, int y, @Nonnull String text)
5757
public ButtonElement(int x, int y, @Nonnull String text, @Nullable Consumer<Player> callback)
5858
{
5959
this(x, y, text, player -> {
60-
callback.accept(player);
60+
if(callback != null)
61+
callback.accept(player);
6162
return true;
6263
});
6364
}

src/main/java/me/tom/sparse/spigot/chat/menu/element/GroupElement.java

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@ public GroupElement(@Nonnull IElementContainer parent, int x, int y)
2929
}
3030

3131
/**
32-
* Adds the provided element to this group.
3332
*
34-
* @param element the element to add to this menu
35-
* @param <T> the type of element
36-
* @return the element added
37-
* @throws NullPointerException if the element is null
33+
* @param element the element to add
34+
* @param <T> the type of element to add
35+
* @return the element that was added
3836
*/
3937
public <T extends Element> T add(@Nonnull T element)
4038
{
@@ -63,8 +61,11 @@ public List<Element> getElements()
6361
{
6462
return Collections.unmodifiableList(elements);
6563
}
66-
67-
//TODO: Documentation?
64+
65+
/**
66+
* @param element the element to interact with
67+
* @return the command used to interact with this element
68+
*/
6869
@Nonnull
6970
public String getCommand(@Nonnull Element element)
7071
{
@@ -135,29 +136,4 @@ public void openFor(@Nonnull Player player)
135136
{
136137
parent.openFor(player);
137138
}
138-
139-
// public static class GroupElementContainer implements IElementContainer
140-
// {
141-
// private final IElementContainer parent;
142-
// private final GroupElement group;
143-
//
144-
// private GroupElementContainer(IElementContainer parent, GroupElement group)
145-
// {
146-
// this.parent = parent;
147-
// this.group = group;
148-
// }
149-
//
150-
// public String getCommand(Element element)
151-
// {
152-
// int i = group.elements.indexOf(element);
153-
// if(i == -1)
154-
// throw new IllegalArgumentException("Unable to interact with the provided element");
155-
// return parent.getCommand(group) + i + " ";
156-
// }
157-
//
158-
// public ChatMenu getMenu()
159-
// {
160-
// return parent.getMenu();
161-
// }
162-
// }
163139
}

src/main/java/me/tom/sparse/spigot/chat/menu/element/HorizontalRuleElement.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,36 @@
1111

1212
public class HorizontalRuleElement extends Element
1313
{
14-
private static final String TEXT = "\u00a7m" + TextUtil.generateWidth(' ', 320, false);
15-
public static final int WIDTH = ChatMenuAPI.getWidth(TEXT);
14+
private final String text;
15+
private final int width;
1616

1717
/**
18-
* Constructs an element at the given x and y coordinates.
18+
* Constructs a {@code HorizontalRuleElement} at the provided Y coordinate and a width of {@code 320} (default chat width).
1919
*
2020
* @param y the y coordinate to put this element at
2121
*/
2222
public HorizontalRuleElement(int y)
2323
{
24-
super(0, y);
24+
this(0, y, 320);
25+
}
26+
27+
/**
28+
* Constructs a {@code HorizontalRuleElement}.
29+
*
30+
* @param x the x coordinate to put this element at
31+
* @param y the y coordinate to put this element at
32+
* @param width the width of this element
33+
*/
34+
public HorizontalRuleElement(int x, int y, int width)
35+
{
36+
super(x, y);
37+
this.text = "\u00a7m" + TextUtil.generateWidth(' ', width, false);
38+
this.width = ChatMenuAPI.getWidth(text);
2539
}
2640

2741
public int getWidth()
2842
{
29-
return WIDTH;
43+
return width;
3044
}
3145

3246
public int getHeight()
@@ -36,7 +50,7 @@ public int getHeight()
3650

3751
public List<Text> render(IElementContainer context)
3852
{
39-
return Collections.singletonList(new Text(TEXT));
53+
return Collections.singletonList(new Text(text));
4054
}
4155

4256
public void edit(@Nonnull IElementContainer container, @Nonnull String[] args)

src/main/java/me/tom/sparse/spigot/chat/menu/element/ImageElement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class ImageElement extends Element
1919
{
2020
public static final List<ChatColor> COLORS = Collections.unmodifiableList(
2121
Arrays.stream(ChatColor.values())
22-
.filter(c -> c != ChatColor.BOLD && c != ChatColor.MAGIC && c != ChatColor.UNDERLINE && c != ChatColor.ITALIC && c != ChatColor.STRIKETHROUGH).collect(Collectors.toList())
22+
.filter(c -> c != ChatColor.BOLD && c != ChatColor.MAGIC && c != ChatColor.UNDERLINE && c != ChatColor.ITALIC && c != ChatColor.STRIKETHROUGH && c != ChatColor.RESET).collect(Collectors.toList())
2323
);
2424

2525
protected int[] colors = new int[20 * 20];

src/main/java/me/tom/sparse/spigot/chat/menu/element/IncrementalElement.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public void setValue(int value)
104104
*/
105105
public int getValue()
106106
{
107-
return value.getCurrent();
107+
return value.getOptionalCurrent().orElse(0);
108108
}
109109

110110
public int getWidth()
@@ -129,7 +129,7 @@ public List<Text> render(@Nonnull IElementContainer context)
129129

130130
List<BaseComponent> components = new ArrayList<>();
131131
TextComponent decrement = new TextComponent("[-]");
132-
int current = value.getCurrent();
132+
int current = value.getOptionalCurrent().orElse(0);
133133
if(current - 1 >= min)
134134
{
135135
decrement.setColor(ChatColor.RED);

src/main/java/me/tom/sparse/spigot/chat/menu/element/InputElement.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,10 @@ public List<Text> render(@Nonnull IElementContainer context)
7373
{
7474
ClickEvent click = new ClickEvent(ClickEvent.Action.RUN_COMMAND, context.getCommand(this));
7575

76-
boolean tooLong = ChatMenuAPI.getWidth(value.getCurrent()) > width;
76+
String current = value.getOptionalCurrent().orElse("");
77+
boolean tooLong = ChatMenuAPI.getWidth(current) > width;
7778

78-
Text text = new Text(tooLong ? "Too long" : value.getCurrent());
79+
Text text = new Text(tooLong ? "Too long" : current);
7980
text.expandToWidth(width);
8081
text.getComponents().forEach(it -> {
8182
if(tooLong)

src/main/java/me/tom/sparse/spigot/chat/menu/element/NumberSliderElement.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public NumberSliderElement width(int width)
241241
*/
242242
public int getValue()
243243
{
244-
return value.getCurrent();
244+
return value.getOptionalCurrent().orElse(0);
245245
}
246246

247247
/**
@@ -272,7 +272,7 @@ public int getWidth()
272272

273273
private String getFormattedNumber()
274274
{
275-
return numberFormat == null ? "" : " " + numberFormat.format(value.getCurrent(), length);
275+
return " " + numberFormat.format(getValue(), length);
276276
}
277277

278278
public int getHeight()
@@ -289,7 +289,7 @@ public List<Text> render(IElementContainer context)
289289
{
290290
// double v = (double) (i + 1) / (double) length;
291291
TextComponent c = new TextComponent(String.valueOf((char) ('\u2588' + precision)));
292-
c.setColor(i <= value.getCurrent() ? isEnabled() ? fullColor : ChatColor.GRAY : isEnabled() ? emptyColor : ChatColor.DARK_GRAY);
292+
c.setColor(i <= getValue() ? isEnabled() ? fullColor : ChatColor.GRAY : isEnabled() ? emptyColor : ChatColor.DARK_GRAY);
293293
c.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, baseCommand + i));
294294
components.add(c);
295295
}

0 commit comments

Comments
 (0)