Skip to content

Commit 919c4da

Browse files
committed
Synchronized menu interaction with all viewers
1 parent b2d11d9 commit 919c4da

File tree

5 files changed

+31
-6
lines changed

5 files changed

+31
-6
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ public void unregister()
7878
registered = false;
7979
}
8080

81+
/**
82+
* @param autoUnregister true if this menu should automatically be unregistered after all players close it.
83+
*/
84+
public void setAutoUnregister(boolean autoUnregister)
85+
{
86+
this.autoUnregister = autoUnregister;
87+
}
88+
8189
/**
8290
* Adds the provided element to this menu.
8391
*
@@ -140,7 +148,7 @@ public void edit(@Nonnull Player player, int elementIndex, @Nonnull String[] arg
140148
Element element = elements.get(elementIndex);
141149
element.edit(this, args);
142150
if(element.onClick(this, player))
143-
openFor(player);
151+
refresh();
144152
}
145153

146154
/**
@@ -157,6 +165,16 @@ public void openFor(@Nonnull Player player)
157165
chat.sendMessage(line);
158166
ChatMenuAPI.setCurrentMenu(player, this);
159167
}
168+
169+
/**
170+
* Sends this menu again to all of the players currently viewing it
171+
*/
172+
public void refresh()
173+
{
174+
viewers.removeIf(it -> !it.isOnline());
175+
for(Player viewer : viewers)
176+
openFor(viewer);
177+
}
160178

161179
@Nonnull
162180
public List<BaseComponent[]> build()

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,9 @@ public interface IElementContainer
4242
* @param player the player to open this container for
4343
*/
4444
void openFor(Player player);
45+
46+
/**
47+
* Displays this container again to all of the players currently viewing it
48+
*/
49+
void refresh();
4550
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,9 @@ public void openFor(@Nonnull Player player)
136136
{
137137
parent.openFor(player);
138138
}
139+
140+
public void refresh()
141+
{
142+
parent.refresh();
143+
}
139144
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@
1717
@Deprecated
1818
public class ImageElement extends Element
1919
{
20-
public static final List<ChatColor> COLORS = Collections.unmodifiableList(
21-
Arrays.stream(ChatColor.values())
22-
.filter(c -> c != ChatColor.BOLD && c != ChatColor.MAGIC && c != ChatColor.UNDERLINE && c != ChatColor.ITALIC && c != ChatColor.STRIKETHROUGH && c != ChatColor.RESET).collect(Collectors.toList())
23-
);
20+
public static final List<ChatColor> COLORS = Collections.unmodifiableList(Arrays.stream(org.bukkit.ChatColor.values()).filter(org.bukkit.ChatColor::isColor).map(org.bukkit.ChatColor::asBungee).collect(Collectors.toList()));
2421

2522
protected int[] colors = new int[20 * 20];
2623
protected PixelClickCallback callback;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public boolean onClick(@Nonnull IElementContainer container, @Nonnull Player pla
101101
CMListener.expectPlayerChat(player, (p, m) -> {
102102
editing = false;
103103
setValue(m);
104-
container.openFor(p);
104+
container.refresh();
105105
return true;
106106
});
107107
}else

0 commit comments

Comments
 (0)