-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathMenuLib.java
More file actions
238 lines (200 loc) · 8.71 KB
/
Copy pathMenuLib.java
File metadata and controls
238 lines (200 loc) · 8.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package fr.openmc.api.menulib;
import fr.openmc.api.menulib.template.ConfirmMenu;
import fr.openmc.api.menulib.utils.ItemBuilder;
import fr.openmc.core.OMCPlugin;
import fr.openmc.core.features.homes.menu.HomeDeleteConfirmMenu;
import fr.openmc.core.utils.ItemUtils;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.*;
import java.util.function.Consumer;
/**
* Made by Xernas
* <p>
* {@code MenuLib} is a utility class designed to handle custom menus in a Bukkit/Spigot plugin environment.
* It provides functionality for managing menu interactions, associating click events with specific items,
* and handling player-menu associations.
* <p>
* This class is intended for use in creating interactive menus within Minecraft plugins,
* allowing developers to define custom behavior for item clicks within menus.
* <p>
* The {@code MenuLib} class implements the {@link Listener} interface to handle inventory-related events.
*/
public final class MenuLib implements Listener {
private static final Map<Player, Deque<Menu>> menuHistory = new HashMap<>();
private static final Set<Class<? extends Menu>> ignoredMenus = new HashSet<>();
static {
ignoredMenus.add(ConfirmMenu.class);
ignoredMenus.add(fr.openmc.core.features.adminshop.menus.ConfirmMenu.class);
ignoredMenus.add(HomeDeleteConfirmMenu.class);
}
@Getter
private static NamespacedKey itemIdKey;
/**
* Constructs a new {@code MenuLib} instance and registers it as an event listener.
* Also initializes the {@link NamespacedKey} used for item identification.
*
* @param plugin The {@link JavaPlugin} instance used to register events and create the {@link NamespacedKey}
*/
private MenuLib(JavaPlugin plugin) {
Bukkit.getPluginManager().registerEvents(this, plugin);
itemIdKey = new NamespacedKey(plugin, "itemId");
}
/**
* Initializes the {@code MenuLib} library for the given plugin.
* This method sets up necessary event handling and utilities
* required for managing custom menus.
*
* @param plugin The {@link JavaPlugin} instance representing the plugin
* that integrates the {@code MenuLib} library. This is used
* to register event listeners and initialize key functionality.
*/
public static void init(JavaPlugin plugin) {
new MenuLib(plugin);
}
/**
* Associates a click event handler with a specific item in a given menu.
* When a player clicks on the specified {@link ItemStack} in the menu,
* the provided {@link Consumer} is executed to handle the {@link InventoryClickEvent}.
*
* @param menu The {@link Menu} in which the click event will be associated.
* @param itemBuilder The {@link ItemBuilder} that will trigger the event when clicked.
* @param e A {@link Consumer} of {@link InventoryClickEvent} representing the event handler
* to be executed when the {@link ItemStack} is clicked within the menu.
*/
public static void setItemClickEvent(Menu menu, ItemBuilder itemBuilder, Consumer<InventoryClickEvent> e) {
menu.getItemClickEvents().put(itemBuilder, e);
}
public static void clearHistory(Player player) {
menuHistory.remove(player);
}
public static void pushMenu(Player player, Menu menu) {
menuHistory.computeIfAbsent(player, k -> new ArrayDeque<>()).push(menu);
}
public static Menu getCurrentLastMenu(Player player) {
Deque<Menu> history = menuHistory.get(player);
if (history == null || history.isEmpty()) {
return null;
}
return history.peek();
}
public static Menu getLastMenu(Player player) {
Deque<Menu> history = menuHistory.get(player);
if (history == null || history.size() < 2) {
return null;
}
Iterator<Menu> iterator = history.iterator();
Menu current = iterator.next();
while (iterator.hasNext()) {
Menu previous = iterator.next();
if (!ignoredMenus.contains(previous.getClass())
&& !previous.getClass().equals(current.getClass())
) {
return previous;
}
}
return null;
}
public static Menu popAndGetPreviousMenu(Player player) {
Deque<Menu> history = menuHistory.get(player);
if (history == null || history.size() < 2) return null;
Menu current = history.pop();
while (!history.isEmpty()) {
Menu previous = history.pop();
if (!ignoredMenus.contains(previous.getClass()) && previous != current) {
return previous;
}
current = history.pop();
}
return null;
}
public static boolean hasPreviousMenu(Player player) {
Deque<Menu> history = menuHistory.get(player);
return history != null && history.size() > 1;
}
/**
* Handles click events in an inventory associated with a {@link Menu}.
* This method ensures that clicks within the menu's inventory are canceled,
* and delegates further handling to the menu's implementation of {@code onInventoryClick}.
* Additionally, it triggers any registered item-specific click event handlers.
*
* @param e The {@link InventoryClickEvent} representing the inventory interaction
* triggered by the player. Contains information about the clicked
* inventory, the clicked item, and other event details.
*/
@EventHandler
public void onInventoryClick(InventoryClickEvent e) {
if (!(e.getInventory().getHolder() instanceof Menu menu))
return;
if (!menu.getTakableSlot().contains(e.getRawSlot())) {
e.setCancelled(true);
}
menu.onInventoryClick(e);
ItemBuilder itemClicked = menu.getContent().get(e.getRawSlot());
if (itemClicked != null && itemClicked.isBackButton()) {
Player player = (Player) e.getWhoClicked();
Menu previous = MenuLib.popAndGetPreviousMenu(player);
if (previous != null) previous.open();
return;
}
try {
Map<ItemBuilder, Consumer<InventoryClickEvent>> itemClickEvents = menu.getItemClickEvents();
if (itemClickEvents.isEmpty())
return;
Consumer<InventoryClickEvent> action = itemClickEvents.get(itemClicked);
if (action != null) {
action.accept(e);
return;
}
for (Map.Entry<ItemBuilder, Consumer<InventoryClickEvent>> entry : itemClickEvents.entrySet()) {
if (ItemUtils.isSimilarMenu(entry.getKey(), e.getCurrentItem())) {
entry.getValue().accept(e);
break;
}
}
} catch (Exception ex) {
OMCPlugin.getInstance().getSLF4JLogger().error("An error occurred while handling a click event in a menu: {}", ex.getMessage(), ex);
}
}
@EventHandler
public void onInventoryDrag(InventoryDragEvent e) {
if (!(e.getInventory().getHolder() instanceof Menu menu))
return;
if (e.getRawSlots().stream().anyMatch(menu.getTakableSlot()::contains))
return;
e.setCancelled(true);
}
/**
* Handles the event that occurs when a player closes an inventory associated with a {@link Menu}.
*/
@EventHandler
public void onClose(InventoryCloseEvent e) {
if (!(e.getPlayer() instanceof Player player)) return;
if (e.getInventory().getHolder(false) instanceof PaginatedMenu paginatedMenu) {
paginatedMenu.onClose(e);
Bukkit.getScheduler().runTaskLater(OMCPlugin.getInstance(), () -> {
if (!(e.getPlayer().getOpenInventory().getTopInventory().getHolder() instanceof PaginatedMenu)) {
MenuLib.clearHistory(player);
}
}, 1L);
return;
}
if (e.getInventory().getHolder(false) instanceof Menu menu) {
menu.onClose(e);
Bukkit.getScheduler().runTaskLater(OMCPlugin.getInstance(), () -> {
if (!(e.getPlayer().getOpenInventory().getTopInventory().getHolder() instanceof Menu)) {
MenuLib.clearHistory(player);
}
}, 1L);
}
}
}