11
11
import org .bukkit .Bukkit ;
12
12
import org .bukkit .event .Event ;
13
13
import org .bukkit .event .EventHandler ;
14
- import org .bukkit .event .EventPriority ;
15
14
import org .bukkit .event .Listener ;
16
15
import org .bukkit .event .inventory .*;
17
16
import org .bukkit .inventory .Inventory ;
23
22
24
23
import java .util .EnumSet ;
25
24
import java .util .Set ;
25
+ import java .util .function .Consumer ;
26
26
27
27
public final class MenuListeners implements Listener {
28
28
private static final EnumSet <InventoryAction > TAKE = EnumSet .of (
@@ -56,11 +56,11 @@ public final class MenuListeners implements Listener {
56
56
57
57
private final Plugin plugin = Menus .plugin ();
58
58
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 ;
61
61
private static final BukkitScheduler SCHEDULER = Bukkit .getScheduler ();
62
62
63
- @ EventHandler ( priority = EventPriority . HIGHEST )
63
+ @ EventHandler
64
64
public void onInventoryClick (InventoryClickEvent event ) {
65
65
Inventory inv = event .getInventory ();
66
66
InventoryHolder holder = inv .getHolder ();
@@ -76,11 +76,13 @@ public void onInventoryClick(InventoryClickEvent event) {
76
76
return ;
77
77
}
78
78
79
+ if (modifierDetected (m , action , ci .getType (), inv .getType ()))
80
+ event .setResult (Event .Result .DENY );
79
81
executeActions (event , m , inv , ci );
80
- executeMenuItem (event , event .getCurrentItem (), ci , inv , action , m , event .getSlot ());
82
+ executeItem (event , event .getCurrentItem (), m , event .getSlot ());
81
83
}
82
84
83
- @ EventHandler ( priority = EventPriority . HIGHEST )
85
+ @ EventHandler
84
86
public void onGuiDrag (InventoryDragEvent event ) {
85
87
val inventory = event .getInventory ();
86
88
val holder = inventory .getHolder ();
@@ -91,10 +93,10 @@ public void onGuiDrag(InventoryDragEvent event) {
91
93
if (!menu .areItemsPlaceable () || isDraggingOnGui (inventory , rawSlots ))
92
94
event .setResult (Event .Result .DENY );
93
95
val dragAction = menu .getDragAction ();
94
- if ( dragAction != null ) dragAction .accept (event );
96
+ dragAction .accept (event );
95
97
}
96
98
97
- @ EventHandler ( priority = EventPriority . MONITOR )
99
+ @ EventHandler
98
100
public void onGuiClose (InventoryCloseEvent e ) {
99
101
val holder = e .getInventory ().getHolder ();
100
102
if (!(holder instanceof BaseMenu <?>)) return ;
@@ -109,37 +111,37 @@ public void onGuiClose(InventoryCloseEvent e) {
109
111
}
110
112
}
111
113
112
- @ EventHandler ( priority = EventPriority . MONITOR )
114
+ @ EventHandler
113
115
public void onGuiOpen (InventoryOpenEvent event ) {
114
116
val holder = event .getInventory ().getHolder ();
115
117
if (!(holder instanceof BaseMenu <?>)) return ;
116
118
val menu = (BaseMenu <?>) holder ;
117
119
118
120
val openAction = menu .getOpenAction ();
119
- if (!menu .isUpdating () && openAction != null ) openAction .accept (event );
121
+ if (!menu .isUpdating ()) openAction .accept (event );
120
122
}
121
123
122
124
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 );
125
127
}
126
128
127
129
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 );
130
132
}
131
133
132
134
133
135
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 );
135
137
}
136
138
137
139
private static boolean isDropItemEvent (InventoryAction action , InventoryType type ) {
138
- return (type != player ) && DROP .contains (action );
140
+ return (type != PLAYER ) && DROP .contains (action );
139
141
}
140
142
141
143
private static boolean isOtherEvent (InventoryAction action , InventoryType type ) {
142
- return isOtherAction (action ) && (type != player );
144
+ return isOtherAction (action ) && (type != PLAYER );
143
145
}
144
146
private static boolean isDraggingOnGui (Inventory inventory , Set <Integer > rawSlots ) {
145
147
final int topSlots = inventory .getSize ();
@@ -151,25 +153,17 @@ private static boolean isOtherAction(final InventoryAction action) {
151
153
return action == InventoryAction .CLONE_STACK || action == InventoryAction .UNKNOWN ;
152
154
}
153
155
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 ();
159
159
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 );
164
162
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
+ }
172
165
166
+ private static void executeItem (InventoryClickEvent event , ItemStack it , BaseMenu <?> m , int num ) {
173
167
MenuItem menuItem = m .getItem (num );
174
168
175
169
if (it == null || menuItem == null ) return ;
0 commit comments