Skip to content

Commit 7832c26

Browse files
committed
1.2.2
1 parent 9060e2a commit 7832c26

11 files changed

Lines changed: 219 additions & 38 deletions

File tree

pom.xml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>eu.xap3y</groupId>
88
<artifactId>xagui</artifactId>
9-
<version>1.2-SNAPSHOT</version>
9+
<version>1.2.2</version>
1010
<packaging>jar</packaging>
1111
<url>https://xagui.xap3y.space</url>
1212
<name>XaGui</name>
@@ -37,7 +37,7 @@
3737
</license>
3838
</licenses>
3939

40-
<description>LightWeight minecraft library</description>
40+
<description>LightWeight minecraft GUI library</description>
4141
<developers>
4242
<developer>
4343
<id>xap3y</id>
@@ -142,14 +142,9 @@
142142
<dependency>
143143
<groupId>io.papermc.paper</groupId>
144144
<artifactId>paper-api</artifactId>
145-
<version>1.20.4-R0.1-SNAPSHOT</version>
145+
<version>1.21.10-R0.1-20251104.194106-38</version>
146146
<scope>provided</scope>
147147
</dependency>
148-
<dependency>
149-
<groupId>com.github.cryptomorin</groupId>
150-
<artifactId>XSeries</artifactId>
151-
<version>11.2.0</version>
152-
</dependency>
153148
<dependency>
154149
<groupId>org.projectlombok</groupId>
155150
<artifactId>lombok</artifactId>

src/main/java/eu/xap3y/xagui/GuiMenu.java

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package eu.xap3y.xagui;
22

3-
import com.cryptomorin.xseries.XMaterial;
43
import eu.xap3y.xagui.exception.PageOutOfBoundException;
54
import eu.xap3y.xagui.interfaces.GuiButtonInterface;
65
import eu.xap3y.xagui.interfaces.GuiMenuInterface;
@@ -22,6 +21,7 @@
2221
import org.bukkit.inventory.ItemStack;
2322
import org.bukkit.inventory.meta.ItemMeta;
2423
import org.bukkit.plugin.java.JavaPlugin;
24+
import org.jetbrains.annotations.NotNull;
2525

2626
import java.util.*;
2727
import java.util.concurrent.ConcurrentHashMap;
@@ -106,11 +106,21 @@ public GuiMenu(JavaPlugin plugin, String title, int rowsToSet, int pages) {
106106
* @return the currently active page inventory
107107
*/
108108
@Override
109-
public Inventory getInventory() {
109+
public @NotNull Inventory getInventory() {
110110
Inventory inv = invMapping.get(currentOpenedPage);
111111
return inv != null ? inv : Bukkit.createInventory(this, getSize(), getName());
112112
}
113113

114+
/**
115+
* Close the GUI for the current viewer.
116+
*/
117+
@Override
118+
public void close() {
119+
Bukkit.getScheduler().runTask(plugin, () -> {
120+
getInventory().close();
121+
});
122+
}
123+
114124
// Open/Close/Click wiring
115125

116126
/**
@@ -697,6 +707,34 @@ public void fillSlots(ItemStack item, Integer[] slots) {
697707
}
698708
}
699709

710+
/**
711+
* Fill the given slots on the current page with an ItemStack.
712+
*
713+
* @param item itemstack to place
714+
* @param slots set of slot indices
715+
*/
716+
@Override
717+
public void fillSlots(ItemStack item, Set<Integer> slots) {
718+
int page = currentOpenedPage;
719+
for (int s : slots) {
720+
setSlot(page, s, new GuiButton(item));
721+
}
722+
}
723+
724+
/**
725+
* Fill the given slots on the current page with an ItemStack.
726+
*
727+
* @param item itemstack to place
728+
* @param slots list of slot indices
729+
*/
730+
@Override
731+
public void fillSlots(ItemStack item, List<Integer> slots) {
732+
int page = currentOpenedPage;
733+
for (int s : slots) {
734+
setSlot(page, s, new GuiButton(item));
735+
}
736+
}
737+
700738
/**
701739
* Fill the given slots on the current page with a button instance.
702740
*
@@ -1012,8 +1050,7 @@ public void setTotalPages(int pages) {
10121050
* @return a filler ItemStack
10131051
*/
10141052
private static ItemStack grayPaneNamedSpace() {
1015-
Material mat = XMaterial.GRAY_STAINED_GLASS_PANE.parseMaterial();
1016-
if (mat == null) mat = Material.AIR;
1053+
Material mat = Material.GRAY_STAINED_GLASS_PANE;
10171054
ItemStack item = new ItemStack(mat);
10181055
ItemMeta meta = item.getItemMeta();
10191056
if (meta != null) {

src/main/java/eu/xap3y/xagui/VirtualMenu.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,20 +107,20 @@ public GuiMenuInterface build() {
107107
* <p>
108108
* Implementations should mutate {@link #gui} as needed and return it.
109109
*
110-
* @param o context object (non-null)
110+
* @param ctx context object (non-null)
111111
* @return the (possibly modified) GUI instance to open
112112
*/
113113
@Override
114-
public abstract @NotNull GuiMenuInterface build(@NotNull T o);
114+
public abstract @NotNull GuiMenuInterface build(@NotNull T ctx);
115115

116116
/**
117117
* Build the GUI with a context object and open it for a player.
118118
*
119119
* @param player target player
120-
* @param o contextual data
120+
* @param ctx contextual data
121121
*/
122-
public void open(Player player, T o) {
123-
GuiMenuInterface built = build(o);
122+
public void open(Player player, T ctx) {
123+
GuiMenuInterface built = build(ctx);
124124
built.open(player);
125125
}
126126

src/main/java/eu/xap3y/xagui/XaGui.java

Lines changed: 76 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package eu.xap3y.xagui;
22

3-
import com.cryptomorin.xseries.XMaterial;
43
import eu.xap3y.xagui.interfaces.GuiButtonInterface;
4+
import eu.xap3y.xagui.interfaces.GuiMenuInterface;
55
import eu.xap3y.xagui.listeners.MenuListener;
66
import eu.xap3y.xagui.models.GuiButton;
77
import lombok.Getter;
@@ -12,10 +12,16 @@
1212
import org.bukkit.plugin.java.JavaPlugin;
1313
import org.jetbrains.annotations.NotNull;
1414

15+
import java.util.Map;
16+
import java.util.UUID;
17+
import java.util.concurrent.ConcurrentHashMap;
18+
1519
public class XaGui {
1620

1721
private final JavaPlugin plugin;
1822

23+
private final static Map<UUID, GuiMenuInterface> openMenus = new ConcurrentHashMap<>();
24+
1925
public XaGui(@NotNull JavaPlugin plugin) {
2026
this.plugin = plugin;
2127
plugin.getServer().getPluginManager().registerEvents(new MenuListener(plugin), plugin);
@@ -46,6 +52,28 @@ public GuiMenu createMenu(String name, int rows, int pages) {
4652
return new GuiMenu(plugin, name, rows, pages);
4753
}
4854

55+
/**
56+
* Register an open menu
57+
*
58+
* @param uuid The UUID of the player
59+
*/
60+
public void closeMenu(UUID uuid) {
61+
if (openMenus.containsKey(uuid)) {
62+
GuiMenuInterface menu = openMenus.get(uuid);
63+
menu.close();
64+
}
65+
openMenus.remove(uuid);
66+
}
67+
68+
/**
69+
* Close all open menus
70+
*/
71+
public void closeAll() {
72+
for (UUID uuid : openMenus.keySet()) {
73+
closeMenu(uuid);
74+
}
75+
}
76+
4977
/**
5078
* Set the item that will be used to fill the menu border
5179
*
@@ -55,6 +83,15 @@ public void setBorderItem(ItemStack item) {
5583
borderFiller = item;
5684
}
5785

86+
/**
87+
* Get all open menus
88+
*
89+
* @return A map of all open menus
90+
*/
91+
public Map<UUID, GuiMenuInterface> getOpenMenus() {
92+
return openMenus;
93+
}
94+
5895
/**
5996
* Set the item that will be used as the close button
6097
*
@@ -100,26 +137,46 @@ public void setCloseButtonSound(Sound sound) {
100137
closeButtonSound = sound;
101138
}
102139

140+
/**
141+
* Set the sound that will be played when a player clicks a button
142+
*
143+
* @param sound The sound that will be played
144+
*/
145+
public void setButtonClickSound(Sound sound, float volume) {
146+
buttonClickSoundVolume = volume;
147+
buttonClickSound = sound;
148+
}
149+
150+
/**
151+
* Set the sound that will be played when a player clicks a button
152+
*
153+
* @param sound The sound that will be played
154+
*/
155+
public void setClickSound(Sound sound) {
156+
clickSound = sound;
157+
}
158+
103159
@Getter
104160
private static Sound redirectSound = null;
105161

162+
@Getter
163+
private static Sound clickSound = null;
164+
165+
@Getter
166+
private static Sound buttonClickSound = null;
167+
168+
@Getter
169+
private static float buttonClickSoundVolume = 1f;
170+
106171
@Getter
107172
private static Sound closeButtonSound = null;
108173

109174
@Getter
110-
private static ItemStack borderFiller = new GuiButton(
111-
XMaterial.GRAY_STAINED_GLASS_PANE.parseItem() != null
112-
? XMaterial.GRAY_STAINED_GLASS_PANE.parseItem()
113-
: new ItemStack(Material.AIR)
114-
).setName("&r").getItem();
175+
private static ItemStack borderFiller = new GuiButton(Material.GRAY_STAINED_GLASS_PANE).setName("&r").getItem();
115176

116177
@Getter
117178
private static GuiButtonInterface closeButton = new GuiButton(
118-
new ItemStack(
119-
XMaterial.BARRIER.parseMaterial() != null
120-
? XMaterial.BARRIER.parseMaterial()
121-
: Material.AIR
122-
)
179+
new ItemStack(Material.BARRIER)
123180
).setName("&cClose").withListener(e -> {
124181
e.getWhoClicked().closeInventory();
125182
if (closeButtonSound != null) {
@@ -133,4 +190,12 @@ public void setCloseButtonSound(Sound sound) {
133190

134191
@Getter
135192
private static ItemStack previousPageButton = new GuiButton(Material.ARROW).setName("&ePrevious page").getItem();
193+
194+
public static void addOpenMenu(UUID uuid, GuiMenuInterface menu) {
195+
openMenus.put(uuid, menu);
196+
}
197+
198+
public static void removeOpenMenu(UUID uuid) {
199+
openMenus.remove(uuid);
200+
}
136201
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package eu.xap3y.xagui.events;
2+
3+
import org.bukkit.entity.Player;
4+
import org.bukkit.event.HandlerList;
5+
import org.bukkit.event.inventory.ClickType;
6+
import org.bukkit.event.inventory.InventoryAction;
7+
import org.bukkit.event.inventory.InventoryClickEvent;
8+
import org.bukkit.event.inventory.InventoryType;
9+
import org.bukkit.inventory.InventoryView;
10+
import org.jetbrains.annotations.NotNull;
11+
12+
public class GuiClickEvent extends InventoryClickEvent {
13+
14+
private static final HandlerList HANDLERS = new HandlerList();
15+
16+
public GuiClickEvent(InventoryView view, InventoryType.SlotType slotType, int slot, ClickType click, InventoryAction action) {
17+
super(view, slotType, slot, click, action);
18+
}
19+
20+
public GuiClickEvent(InventoryView view, InventoryType.SlotType slotType, int slot, ClickType click, InventoryAction action, int hotbarButton) {
21+
super(view, slotType, slot, click, action, hotbarButton);
22+
}
23+
24+
public GuiClickEvent(InventoryClickEvent e) {
25+
super(e.getView(), e.getSlotType(), e.getSlot(), e.getClick(), e.getAction(), e.getHotbarButton());
26+
}
27+
28+
/**
29+
* Convenience accessor casting getWhoClicked() to Player.
30+
* This will throw ClassCastException if the clicker is not a Player.
31+
*/
32+
public Player getPlayer() {
33+
return (Player) getWhoClicked();
34+
}
35+
36+
@Override
37+
public @NotNull HandlerList getHandlers() {
38+
return HANDLERS;
39+
}
40+
41+
public static @NotNull HandlerList getHandlerList() {
42+
return HANDLERS;
43+
}
44+
}

src/main/java/eu/xap3y/xagui/interfaces/GuiButtonInterface.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ public interface GuiButtonInterface {
4545

4646
GuiClickInterface getClickListener();
4747
Sound getClickSound();
48+
float getClickSoundVolume();
4849
GuiButton clone();
4950

5051
GuiButton withRedirect(Supplier<GuiMenuInterface> menu);
51-
GuiButton withClickSound(Sound sound);
52+
GuiButton withClickSound(Sound sound, float volume);
5253

5354
void callRedirect(Player p);
5455
}

src/main/java/eu/xap3y/xagui/interfaces/GuiMenuInterface.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import org.bukkit.inventory.ItemStack;
1010
import org.bukkit.plugin.java.JavaPlugin;
1111

12+
import java.util.List;
13+
import java.util.Set;
1214
import java.util.function.Consumer;
1315

1416
public interface GuiMenuInterface {
@@ -23,6 +25,7 @@ public interface GuiMenuInterface {
2325
void setOnPageSwitch(GuiPageSwitchInterface onPageSwitch);
2426

2527
void setName(String newName);
28+
void close();
2629
String getName();
2730
String getRawName();
2831
int getSize();
@@ -72,6 +75,8 @@ public interface GuiMenuInterface {
7275

7376
void fillSlots(ItemStack item, int... slots);
7477
void fillSlots(ItemStack item, Integer[] slots);
78+
void fillSlots(ItemStack item, Set<Integer> slots);
79+
void fillSlots(ItemStack item, List<Integer> slots);
7580
void fillSlots(GuiButtonInterface item, int... slots);
7681

7782
void fillSlots(int page, ItemStack item, int... slots);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package eu.xap3y.xagui.interfaces.listeners;
22

3-
import org.bukkit.event.inventory.InventoryClickEvent;
3+
import eu.xap3y.xagui.events.GuiClickEvent;
44
import org.jetbrains.annotations.NotNull;
55

66
public interface GuiClickInterface {
7-
void onClick(@NotNull InventoryClickEvent event);
7+
void onClick(@NotNull GuiClickEvent event);
88
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package eu.xap3y.xagui.interfaces.listeners;
22

3-
import org.bukkit.event.inventory.InventoryClickEvent;
3+
import eu.xap3y.xagui.events.GuiClickEvent;
44
import org.jetbrains.annotations.NotNull;
55

66
public interface GuiOwnClickInterface {
77

8-
void onClick(@NotNull InventoryClickEvent event);
8+
void onClick(@NotNull GuiClickEvent event);
99
}

0 commit comments

Comments
 (0)