Skip to content

Commit b5d74d9

Browse files
Improved order of code.
1 parent 2766c69 commit b5d74d9

File tree

1 file changed

+27
-33
lines changed

1 file changed

+27
-33
lines changed

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

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.bukkit.Bukkit;
1212
import org.bukkit.event.Event;
1313
import org.bukkit.event.EventHandler;
14-
import org.bukkit.event.EventPriority;
1514
import org.bukkit.event.Listener;
1615
import org.bukkit.event.inventory.*;
1716
import org.bukkit.inventory.Inventory;
@@ -23,6 +22,7 @@
2322

2423
import java.util.EnumSet;
2524
import java.util.Set;
25+
import java.util.function.Consumer;
2626

2727
public final class MenuListeners implements Listener {
2828
private static final EnumSet<InventoryAction> TAKE = EnumSet.of(
@@ -56,11 +56,11 @@ public final class MenuListeners implements Listener {
5656

5757
private final Plugin plugin = Menus.plugin();
5858

59-
private static final InventoryType player = InventoryType.PLAYER;
60-
private static final InventoryAction otherInv = InventoryAction.MOVE_TO_OTHER_INVENTORY;
59+
private static final InventoryType PLAYER = InventoryType.PLAYER;
60+
private static final InventoryAction OTHER_INV = InventoryAction.MOVE_TO_OTHER_INVENTORY;
6161
private static final BukkitScheduler SCHEDULER = Bukkit.getScheduler();
6262

63-
@EventHandler(priority = EventPriority.HIGHEST)
63+
@EventHandler
6464
public void onInventoryClick(InventoryClickEvent event) {
6565
Inventory inv = event.getInventory();
6666
InventoryHolder holder = inv.getHolder();
@@ -76,11 +76,13 @@ public void onInventoryClick(InventoryClickEvent event) {
7676
return;
7777
}
7878

79+
if (modifierDetected(m, action, ci.getType(), inv.getType()))
80+
event.setResult(Event.Result.DENY);
7981
executeActions(event, m, inv, ci);
80-
executeMenuItem(event, event.getCurrentItem(), ci, inv, action, m, event.getSlot());
82+
executeItem(event, event.getCurrentItem(), m, event.getSlot());
8183
}
8284

83-
@EventHandler(priority = EventPriority.HIGHEST)
85+
@EventHandler
8486
public void onGuiDrag(InventoryDragEvent event) {
8587
val inventory = event.getInventory();
8688
val holder = inventory.getHolder();
@@ -91,10 +93,10 @@ public void onGuiDrag(InventoryDragEvent event) {
9193
if (!menu.areItemsPlaceable() || isDraggingOnGui(inventory, rawSlots))
9294
event.setResult(Event.Result.DENY);
9395
val dragAction = menu.getDragAction();
94-
if (dragAction != null) dragAction.accept(event);
96+
dragAction.accept(event);
9597
}
9698

97-
@EventHandler(priority = EventPriority.MONITOR)
99+
@EventHandler
98100
public void onGuiClose(InventoryCloseEvent e) {
99101
val holder = e.getInventory().getHolder();
100102
if (!(holder instanceof BaseMenu<?>)) return;
@@ -109,37 +111,37 @@ public void onGuiClose(InventoryCloseEvent e) {
109111
}
110112
}
111113

112-
@EventHandler(priority = EventPriority.MONITOR)
114+
@EventHandler
113115
public void onGuiOpen(InventoryOpenEvent event) {
114116
val holder = event.getInventory().getHolder();
115117
if (!(holder instanceof BaseMenu<?>)) return;
116118
val menu = (BaseMenu<?>) holder;
117119

118120
val openAction = menu.getOpenAction();
119-
if (!menu.isUpdating() && openAction != null) openAction.accept(event);
121+
if (!menu.isUpdating()) openAction.accept(event);
120122
}
121123

122124
private static boolean isTakeItemEvent(InventoryAction action, InventoryType ciType, InventoryType type) {
123-
if (ciType == player || type == player) return false;
124-
return action == otherInv || TAKE.contains(action);
125+
if (ciType == PLAYER || type == PLAYER) return false;
126+
return action == OTHER_INV || TAKE.contains(action);
125127
}
126128

127129
private static boolean isPlaceItemEvent(InventoryAction action, InventoryType ciType, InventoryType type) {
128-
if (action == otherInv && ciType == player && type != ciType) return true;
129-
return (ciType != player && type != player) || PLACE.contains(action);
130+
if (action == OTHER_INV && ciType == PLAYER && type != ciType) return true;
131+
return (ciType != PLAYER && type != PLAYER) || PLACE.contains(action);
130132
}
131133

132134

133135
private static boolean isSwapItemEvent(InventoryAction action, InventoryType ciType, InventoryType type) {
134-
return (ciType != player && type != player) && SWAP.contains(action);
136+
return (ciType != PLAYER && type != PLAYER) && SWAP.contains(action);
135137
}
136138

137139
private static boolean isDropItemEvent(InventoryAction action, InventoryType type) {
138-
return (type != player) && DROP.contains(action);
140+
return (type != PLAYER) && DROP.contains(action);
139141
}
140142

141143
private static boolean isOtherEvent(InventoryAction action, InventoryType type) {
142-
return isOtherAction(action) && (type != player);
144+
return isOtherAction(action) && (type != PLAYER);
143145
}
144146
private static boolean isDraggingOnGui(Inventory inventory, Set<Integer> rawSlots) {
145147
final int topSlots = inventory.getSize();
@@ -151,25 +153,17 @@ private static boolean isOtherAction(final InventoryAction action) {
151153
return action == InventoryAction.CLONE_STACK || action == InventoryAction.UNKNOWN;
152154
}
153155

154-
private static void executeActions(InventoryClickEvent event, BaseMenu<?> m,
155-
Inventory inv, Inventory ci) {
156-
val topClick = m.getTopClickAction();
157-
val bottomClick = m.getBottomClickAction();
158-
val defaultClick = m.getClickAction();
156+
private static void executeActions(InventoryClickEvent event, BaseMenu<?> m, Inventory inv, Inventory ci) {
157+
Consumer<InventoryClickEvent> topClick = m.getTopClickAction(),
158+
bottomClick = m.getBottomClickAction(), defaultClick = m.getClickAction();
159159

160-
if (topClick != null && inv.equals(ci)) topClick.accept(event);
161-
else if (bottomClick != null && event.getView().getBottomInventory().equals(ci)) bottomClick.accept(event);
162-
else if (defaultClick != null) defaultClick.accept(event);
163-
}
160+
if (inv.equals(ci)) topClick.accept(event);
161+
else if (event.getView().getBottomInventory().equals(ci)) bottomClick.accept(event);
164162

165-
private static void executeMenuItem(InventoryClickEvent event,
166-
ItemStack it, Inventory ci,
167-
Inventory inv, InventoryAction action,
168-
BaseMenu<?> m, int num) {
169-
if (modifierDetected(m, action, ci.getType(), inv.getType())) {
170-
event.setResult(Event.Result.DENY);
171-
}
163+
defaultClick.accept(event);
164+
}
172165

166+
private static void executeItem(InventoryClickEvent event, ItemStack it, BaseMenu<?> m, int num) {
173167
MenuItem menuItem = m.getItem(num);
174168

175169
if (it == null || menuItem == null) return;

0 commit comments

Comments
 (0)