Skip to content

Commit 3f2ac4a

Browse files
committed
Added method to change text in TextElement
Made VerticalSelectorElement actually work
1 parent 913f2e2 commit 3f2ac4a

File tree

4 files changed

+77
-2
lines changed

4 files changed

+77
-2
lines changed

pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@
2222
</license>
2323
</licenses>
2424

25+
<build>
26+
<plugins>
27+
<plugin>
28+
<groupId>org.apache.maven.plugins</groupId>
29+
<artifactId>maven-compiler-plugin</artifactId>
30+
<version>3.6.1</version>
31+
<configuration>
32+
<source>1.8</source>
33+
<target>1.8</target>
34+
</configuration>
35+
</plugin>
36+
</plugins>
37+
</build>
38+
2539
<dependencies>
2640
<dependency>
2741
<groupId>org.spigotmc</groupId>

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package me.tom.sparse.spigot.chat.menu;
22

33
import io.netty.util.internal.ConcurrentSet;
4+
import me.tom.sparse.spigot.chat.menu.element.ButtonElement;
45
import me.tom.sparse.spigot.chat.menu.element.Element;
56
import me.tom.sparse.spigot.chat.protocol.PlayerChatIntercept;
67
import me.tom.sparse.spigot.chat.util.Text;
@@ -86,6 +87,19 @@ public void addElement(Element element)
8687
elements.sort(Comparator.comparingInt(Element::getX));
8788
}
8889

90+
/**
91+
* Adds the provided element to this menu.
92+
*
93+
* @param t the element to add to this menu
94+
* @param <T> the type of element
95+
* @return the element added
96+
*/
97+
public <T extends Element> T add(T t)
98+
{
99+
addElement(t);
100+
return t;
101+
}
102+
89103
/**
90104
* @return an unmodifiable list of all the elements in this menu.
91105
*/
@@ -219,17 +233,44 @@ public boolean isRegistered()
219233
return registered;
220234
}
221235

236+
/**
237+
*
238+
* @return true if this menu will pause chat when it is opened
239+
*/
222240
public boolean doesPauseChat()
223241
{
224242
return pauseChat;
225243
}
226244

245+
/**
246+
* Makes this menu pause chat when it is opened
247+
* @return this
248+
*/
227249
public ChatMenu pauseChat()
228250
{
229251
setPauseChat(true);
230252
return this;
231253
}
232254

255+
/**
256+
* Makes this menu pause chat when it is opened and adds a close button.
257+
*
258+
* @param x the x coordinate of the close button
259+
* @param y the y coordinate of the close button
260+
* @param text the text of the close button
261+
* @return this
262+
*/
263+
public ChatMenu pauseChat(int x, int y, String text)
264+
{
265+
setPauseChat(true);
266+
addElement(ButtonElement.createCloseButton(x, y, text, this));
267+
return this;
268+
}
269+
270+
/**
271+
*
272+
* @param pauseChat true if this menu should pause chat when it is opened
273+
*/
233274
public void setPauseChat(boolean pauseChat)
234275
{
235276
this.pauseChat = pauseChat;

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,26 @@ public TextElement(int x, int y, String... text)
6868
}
6969
}
7070

71+
public void setText(String text)
72+
{
73+
setLines(text.split("\n"));
74+
}
75+
76+
public void setLines(String... lines)
77+
{
78+
int newWidth = 0;
79+
for(String line : lines)
80+
{
81+
if(line.contains("\n"))
82+
throw new IllegalArgumentException("Cannot use TextElement line constructor with newline characters.");
83+
int w = ChatMenuAPI.getWidth(line);
84+
if(w > newWidth)
85+
newWidth = w;
86+
}
87+
this.lines = lines;
88+
this.width = newWidth;
89+
}
90+
7191
/**
7292
* Adds a border around the text
7393
*

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class VerticalSelectorElement extends Element
2424
protected int width;
2525

2626
// protected int selectedIndex;
27-
protected State<Integer> value;
27+
public final State<Integer> value;
2828

2929
protected ChatColor selectedColor = ChatColor.GREEN;
3030

@@ -39,7 +39,6 @@ public class VerticalSelectorElement extends Element
3939
public VerticalSelectorElement(int x, int y, int defaultSelected, String... options)
4040
{
4141
super(x, y);
42-
this.value = new State<>(defaultSelected, this::filter);
4342
for(String option : options)
4443
{
4544
if(option.contains("\n"))
@@ -51,6 +50,7 @@ public VerticalSelectorElement(int x, int y, int defaultSelected, String... opti
5150
}
5251

5352
this.options = options;
53+
this.value = new State<>(defaultSelected, this::filter);
5454
}
5555

5656
private int filter(int v)

0 commit comments

Comments
 (0)