|
2 | 2 |
|
3 | 3 | import lombok.val;
|
4 | 4 |
|
| 5 | +import me.flame.menus.components.nbt.ItemNbt; |
| 6 | +import me.flame.menus.items.MenuItem; |
5 | 7 | import me.flame.menus.menu.BaseMenu;
|
6 | 8 |
|
| 9 | +import me.flame.menus.menu.PaginatedMenu; |
7 | 10 | import org.bukkit.event.Event;
|
8 | 11 | import org.bukkit.event.EventHandler;
|
9 | 12 | import org.bukkit.event.EventPriority;
|
|
12 | 15 | import org.bukkit.inventory.Inventory;
|
13 | 16 | import org.bukkit.inventory.InventoryHolder;
|
14 | 17 |
|
| 18 | +import org.bukkit.inventory.ItemStack; |
15 | 19 | import org.jetbrains.annotations.NotNull;
|
| 20 | +import org.jetbrains.annotations.Nullable; |
16 | 21 |
|
17 | 22 | import java.util.EnumSet;
|
18 | 23 | import java.util.Set;
|
@@ -63,6 +68,27 @@ public void onInventoryClick(InventoryClickEvent event) {
|
63 | 68 | if (topClick != null && inv.equals(ci)) topClick.accept(event);
|
64 | 69 | else if (bottomClick != null && event.getView().getBottomInventory().equals(ci)) bottomClick.accept(event);
|
65 | 70 | else if (defaultClick != null) defaultClick.accept(event);
|
| 71 | + |
| 72 | + MenuItem guiItem; |
| 73 | + |
| 74 | + // Checks whether it's a paginated menu or not |
| 75 | + if (menu instanceof PaginatedMenu) { |
| 76 | + final PaginatedMenu paginatedGui = (PaginatedMenu) menu; |
| 77 | + |
| 78 | + // Gets the menu item from the added items or the page items |
| 79 | + guiItem = paginatedGui.getItem(event.getSlot()); |
| 80 | + if (guiItem == null) guiItem = paginatedGui.getFromPageItems(event.getSlot()); |
| 81 | + |
| 82 | + } else { |
| 83 | + // The clicked GUI Item |
| 84 | + guiItem = menu.getItem(event.getSlot()); |
| 85 | + } |
| 86 | + |
| 87 | + if (!isMenuItem(event.getCurrentItem(), guiItem)) return; |
| 88 | + |
| 89 | + // Executes the action of the item |
| 90 | + final Consumer<InventoryClickEvent> itemAction = guiItem.getClickAction(); |
| 91 | + if (itemAction != null) itemAction.accept(event); |
66 | 92 | }
|
67 | 93 |
|
68 | 94 | @EventHandler(priority = EventPriority.HIGHEST)
|
@@ -134,4 +160,18 @@ private boolean isDraggingOnGui(final @NotNull Inventory inventory, Set<Integer>
|
134 | 160 | private boolean isOtherAction(final InventoryAction action) {
|
135 | 161 | return action == InventoryAction.CLONE_STACK || action == InventoryAction.UNKNOWN;
|
136 | 162 | }
|
| 163 | + |
| 164 | + /** |
| 165 | + * Checks if the item is or not a GUI item |
| 166 | + * |
| 167 | + * @param currentItem The current item clicked |
| 168 | + * @param guiItem The GUI item in the slot |
| 169 | + * @return Whether it is or not a GUI item |
| 170 | + */ |
| 171 | + private boolean isMenuItem(@Nullable final ItemStack currentItem, @Nullable final MenuItem guiItem) { |
| 172 | + if (currentItem == null || guiItem == null) return false; |
| 173 | + final String nbt = ItemNbt.getString(currentItem, "woody-menu"); |
| 174 | + if (nbt == null) return false; |
| 175 | + return nbt.equals(guiItem.getUniqueId().toString()); |
| 176 | + } |
137 | 177 | }
|
0 commit comments