Skip to content

Commit 259beca

Browse files
NBT.
NBT menu item checking and menu action execution.
1 parent 998654a commit 259beca

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/main/java/me/flame/menus/listeners/MenuListeners.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
import lombok.val;
44

5+
import me.flame.menus.components.nbt.ItemNbt;
6+
import me.flame.menus.items.MenuItem;
57
import me.flame.menus.menu.BaseMenu;
68

9+
import me.flame.menus.menu.PaginatedMenu;
710
import org.bukkit.event.Event;
811
import org.bukkit.event.EventHandler;
912
import org.bukkit.event.EventPriority;
@@ -12,7 +15,9 @@
1215
import org.bukkit.inventory.Inventory;
1316
import org.bukkit.inventory.InventoryHolder;
1417

18+
import org.bukkit.inventory.ItemStack;
1519
import org.jetbrains.annotations.NotNull;
20+
import org.jetbrains.annotations.Nullable;
1621

1722
import java.util.EnumSet;
1823
import java.util.Set;
@@ -63,6 +68,27 @@ public void onInventoryClick(InventoryClickEvent event) {
6368
if (topClick != null && inv.equals(ci)) topClick.accept(event);
6469
else if (bottomClick != null && event.getView().getBottomInventory().equals(ci)) bottomClick.accept(event);
6570
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);
6692
}
6793

6894
@EventHandler(priority = EventPriority.HIGHEST)
@@ -134,4 +160,18 @@ private boolean isDraggingOnGui(final @NotNull Inventory inventory, Set<Integer>
134160
private boolean isOtherAction(final InventoryAction action) {
135161
return action == InventoryAction.CLONE_STACK || action == InventoryAction.UNKNOWN;
136162
}
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+
}
137177
}

0 commit comments

Comments
 (0)