Skip to content

Commit f85f45f

Browse files
Increase readability and fix bugs.
Bugs fixed: when executing inventory click actions, only outside click actions are executed, as in when an action isn't null the outside click action is executed. fun right? that was fixed
1 parent 4a7cad6 commit f85f45f

File tree

1 file changed

+86
-68
lines changed

1 file changed

+86
-68
lines changed

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

Lines changed: 86 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
import me.flame.menus.components.nbt.ItemNbt;
3131
import me.flame.menus.items.MenuItem;
3232
import me.flame.menus.menu.BaseMenu;
33-
3433
import me.flame.menus.menu.PaginatedMenu;
34+
3535
import org.bukkit.event.Event;
3636
import org.bukkit.event.EventHandler;
3737
import org.bukkit.event.EventPriority;
@@ -45,93 +45,82 @@
4545
import org.jetbrains.annotations.Nullable;
4646

4747
import java.util.EnumSet;
48+
import java.util.Objects;
4849
import java.util.Set;
4950
import java.util.function.Consumer;
5051

5152
public final class MenuListeners implements Listener {
52-
private static final Set<InventoryAction> TAKE = EnumSet.of(InventoryAction.PICKUP_ONE, InventoryAction.PICKUP_SOME, InventoryAction.PICKUP_HALF, InventoryAction.PICKUP_ALL, InventoryAction.COLLECT_TO_CURSOR, InventoryAction.HOTBAR_SWAP, InventoryAction.MOVE_TO_OTHER_INVENTORY);
53-
private static final Set<InventoryAction> PLACE = EnumSet.of(InventoryAction.PLACE_ONE, InventoryAction.PLACE_SOME, InventoryAction.PLACE_ALL);
54-
private static final Set<InventoryAction> SWAP = EnumSet.of(InventoryAction.HOTBAR_SWAP, InventoryAction.SWAP_WITH_CURSOR, InventoryAction.HOTBAR_MOVE_AND_READD);
55-
private static final Set<InventoryAction> DROP = EnumSet.of(InventoryAction.DROP_ONE_SLOT, InventoryAction.DROP_ALL_SLOT, InventoryAction.DROP_ONE_CURSOR, InventoryAction.DROP_ALL_CURSOR);
53+
private static final EnumSet<InventoryAction> TAKE = EnumSet.of(
54+
InventoryAction.PICKUP_ONE,
55+
InventoryAction.PICKUP_SOME,
56+
InventoryAction.PICKUP_HALF,
57+
InventoryAction.PICKUP_ALL,
58+
InventoryAction.COLLECT_TO_CURSOR,
59+
InventoryAction.HOTBAR_SWAP,
60+
InventoryAction.MOVE_TO_OTHER_INVENTORY
61+
);
62+
63+
private static final EnumSet<InventoryAction> PLACE = EnumSet.of(
64+
InventoryAction.PLACE_ONE,
65+
InventoryAction.PLACE_SOME,
66+
InventoryAction.PLACE_ALL
67+
);
68+
69+
private static final EnumSet<InventoryAction> SWAP = EnumSet.of(
70+
InventoryAction.HOTBAR_SWAP,
71+
InventoryAction.SWAP_WITH_CURSOR,
72+
InventoryAction.HOTBAR_MOVE_AND_READD
73+
);
74+
75+
private static final EnumSet<InventoryAction> DROP = EnumSet.of(
76+
InventoryAction.DROP_ONE_SLOT,
77+
InventoryAction.DROP_ALL_SLOT,
78+
InventoryAction.DROP_ONE_CURSOR,
79+
InventoryAction.DROP_ALL_CURSOR
80+
);
5681

5782
private static final Event.Result denied = Event.Result.DENY;
5883
private static final InventoryType player = InventoryType.PLAYER;
5984
private static final InventoryAction otherInv = InventoryAction.MOVE_TO_OTHER_INVENTORY;
6085

6186
@EventHandler(priority = EventPriority.HIGHEST)
62-
public void onInventoryClick(InventoryClickEvent event) {
63-
InventoryHolder holder = event.getInventory().getHolder();
87+
public void onInventoryClick(final @NotNull InventoryClickEvent event) {
88+
Inventory inv = event.getInventory();
89+
InventoryHolder holder = inv.getHolder();
6490
if (!(holder instanceof BaseMenu<?>)) return;
65-
BaseMenu<?> menu = (BaseMenu<?>) holder;
6691

92+
BaseMenu<?> m = (BaseMenu<?>) holder;
6793
Inventory ci = event.getClickedInventory();
68-
Inventory inv = event.getInventory();
6994
InventoryAction action = event.getAction();
7095

7196
if (ci == null) {
72-
val outsideClick = menu.getOutsideClickAction();
73-
if (outsideClick == null) return;
74-
outsideClick.accept(event);
97+
val outsideClick = m.getOutsideClickAction();
98+
if (outsideClick != null) outsideClick.accept(event);
7599
return;
76100
}
101+
Objects.requireNonNull(ci);
77102

78-
InventoryType ciType = ci.getType();
79-
InventoryType invType = inv.getType();
80-
81-
boolean unremovable = !menu.areItemsRemovable();
82-
if ((!menu.areItemsPlaceable() && isPlaceItemEvent(ci, action, ciType, invType)) ||
83-
(unremovable && isTakeItemEvent(ci, action, ciType, invType)) ||
84-
(!menu.areItemsSwappable() && isSwapItemEvent(ci, action, ciType, invType)) ||
85-
(unremovable && isDropItemEvent(ci, action, invType)) ||
86-
(!menu.areItemsCloneable() && isOtherEvent(ci, action, invType)))
87-
event.setResult(denied);
88-
89-
val topClick = menu.getOutsideClickAction();
90-
val bottomClick = menu.getOutsideClickAction();
91-
val defaultClick = menu.getOutsideClickAction();
92-
93-
if (topClick != null && inv.equals(ci)) topClick.accept(event);
94-
else if (bottomClick != null && event.getView().getBottomInventory().equals(ci)) bottomClick.accept(event);
95-
else if (defaultClick != null) defaultClick.accept(event);
96-
97-
MenuItem guiItem;
98-
99-
// Checks whether it's a paginated menu or not
100-
if (menu instanceof PaginatedMenu) {
101-
final PaginatedMenu paginatedGui = (PaginatedMenu) menu;
102-
103-
// Gets the menu item from the added items or the page items
104-
guiItem = paginatedGui.getItem(event.getSlot());
105-
if (guiItem == null) guiItem = paginatedGui.getFromPageItems(event.getSlot());
106-
107-
} else {
108-
// The clicked GUI Item
109-
guiItem = menu.getItem(event.getSlot());
110-
}
111-
112-
if (!isMenuItem(event.getCurrentItem(), guiItem)) return;
113-
114-
// Executes the action of the item
115-
final Consumer<InventoryClickEvent> itemAction = guiItem.getClickAction();
116-
if (itemAction != null) itemAction.accept(event);
103+
denyIfModifierApplied(event, m, ci, action, ci.getType(), inv.getType());
104+
executeActions(event, m, inv, ci);
105+
executeMenuItem(event, event.getCurrentItem(), m, event.getSlot());
117106
}
118107

119108
@EventHandler(priority = EventPriority.HIGHEST)
120-
public void onGuiDrag(final InventoryDragEvent event) {
109+
public void onGuiDrag(final @NotNull InventoryDragEvent event) {
121110
val inventory = event.getInventory();
122111
val holder = inventory.getHolder();
123112
if (!(holder instanceof BaseMenu<?>)) return;
124113
val menu = (BaseMenu<?>) holder;
125-
val rawSlots = event.getRawSlots();
126114

115+
val rawSlots = event.getRawSlots();
127116
if (!menu.areItemsPlaceable() || isDraggingOnGui(inventory, rawSlots))
128117
event.setResult(denied);
129-
final Consumer<InventoryDragEvent> dragAction = menu.getDragAction();
118+
val dragAction = menu.getDragAction();
130119
if (dragAction != null) dragAction.accept(event);
131120
}
132121

133122
@EventHandler(priority = EventPriority.MONITOR)
134-
public void onGuiClose(final InventoryCloseEvent event) {
123+
public void onGuiClose(final @NotNull InventoryCloseEvent event) {
135124
val holder = event.getInventory().getHolder();
136125
if (!(holder instanceof BaseMenu<?>)) return;
137126
val menu = (BaseMenu<?>) holder;
@@ -141,7 +130,7 @@ public void onGuiClose(final InventoryCloseEvent event) {
141130
}
142131

143132
@EventHandler(priority = EventPriority.MONITOR)
144-
public void onGuiOpen(final InventoryOpenEvent event) {
133+
public void onGuiOpen(final @NotNull InventoryOpenEvent event) {
145134
val holder = event.getInventory().getHolder();
146135
if (!(holder instanceof BaseMenu<?>)) return;
147136
val menu = (BaseMenu<?>) holder;
@@ -186,17 +175,46 @@ private boolean isOtherAction(final InventoryAction action) {
186175
return action == InventoryAction.CLONE_STACK || action == InventoryAction.UNKNOWN;
187176
}
188177

189-
/**
190-
* Checks if the item is or not a GUI item
191-
*
192-
* @param currentItem The current item clicked
193-
* @param guiItem The GUI item in the slot
194-
* @return Whether it is or not a GUI item
195-
*/
196-
private boolean isMenuItem(@Nullable final ItemStack currentItem, @Nullable final MenuItem guiItem) {
197-
if (currentItem == null || guiItem == null) return false;
198-
final String nbt = ItemNbt.getString(currentItem, "woody-menu");
199-
if (nbt == null) return false;
200-
return nbt.equals(guiItem.getUniqueId().toString());
178+
private void executeActions(final @NotNull InventoryClickEvent event,
179+
final @NotNull BaseMenu<?> m,
180+
final @NotNull Inventory inv,
181+
final Inventory ci) {
182+
val topClick = m.getTopClickAction();
183+
val bottomClick = m.getBottomClickAction();
184+
val defaultClick = m.getClickAction();
185+
186+
if (topClick != null && inv.equals(ci)) topClick.accept(event);
187+
else if (bottomClick != null && event.getView().getBottomInventory().equals(ci)) bottomClick.accept(event);
188+
else if (defaultClick != null) defaultClick.accept(event);
189+
}
190+
191+
private void denyIfModifierApplied(final @NotNull InventoryClickEvent event,
192+
final @NotNull BaseMenu<?> m,
193+
final @NotNull Inventory ci,
194+
final @NotNull InventoryAction action,
195+
final @NotNull InventoryType ciType,
196+
final @NotNull InventoryType invType) {
197+
boolean unremovable = !m.areItemsRemovable();
198+
if ((!m.areItemsPlaceable() && isPlaceItemEvent(ci, action, ciType, invType)) ||
199+
(unremovable && isTakeItemEvent(ci, action, ciType, invType)) ||
200+
(!m.areItemsSwappable() && isSwapItemEvent(ci, action, ciType, invType)) ||
201+
(unremovable && isDropItemEvent(ci, action, invType)) ||
202+
(!m.areItemsCloneable() && isOtherEvent(ci, action, invType)))
203+
event.setResult(denied);
204+
}
205+
206+
private void executeMenuItem(final @NotNull InventoryClickEvent event,
207+
final @Nullable ItemStack it,
208+
final @NotNull BaseMenu<?> m,
209+
final int num) {
210+
MenuItem menuItem = (m instanceof PaginatedMenu)
211+
? (m.getOptionalItem(num).orElse(((PaginatedMenu) m).getFromPageItems(num)))
212+
: (m.getItem(num));
213+
if (it == null || menuItem == null) return;
214+
final String nbt = ItemNbt.getString(it, "woody-menu");
215+
if (nbt == null || !nbt.equals(menuItem.getUniqueId().toString())) return;
216+
217+
final Consumer<InventoryClickEvent> itemAction = menuItem.getClickAction();
218+
if (itemAction != null) itemAction.accept(event);
201219
}
202220
}

0 commit comments

Comments
 (0)