30
30
import me .flame .menus .components .nbt .ItemNbt ;
31
31
import me .flame .menus .items .MenuItem ;
32
32
import me .flame .menus .menu .BaseMenu ;
33
-
34
33
import me .flame .menus .menu .PaginatedMenu ;
34
+
35
35
import org .bukkit .event .Event ;
36
36
import org .bukkit .event .EventHandler ;
37
37
import org .bukkit .event .EventPriority ;
45
45
import org .jetbrains .annotations .Nullable ;
46
46
47
47
import java .util .EnumSet ;
48
+ import java .util .Objects ;
48
49
import java .util .Set ;
49
50
import java .util .function .Consumer ;
50
51
51
52
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
+ );
56
81
57
82
private static final Event .Result denied = Event .Result .DENY ;
58
83
private static final InventoryType player = InventoryType .PLAYER ;
59
84
private static final InventoryAction otherInv = InventoryAction .MOVE_TO_OTHER_INVENTORY ;
60
85
61
86
@ 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 ();
64
90
if (!(holder instanceof BaseMenu <?>)) return ;
65
- BaseMenu <?> menu = (BaseMenu <?>) holder ;
66
91
92
+ BaseMenu <?> m = (BaseMenu <?>) holder ;
67
93
Inventory ci = event .getClickedInventory ();
68
- Inventory inv = event .getInventory ();
69
94
InventoryAction action = event .getAction ();
70
95
71
96
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 );
75
99
return ;
76
100
}
101
+ Objects .requireNonNull (ci );
77
102
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 ());
117
106
}
118
107
119
108
@ EventHandler (priority = EventPriority .HIGHEST )
120
- public void onGuiDrag (final InventoryDragEvent event ) {
109
+ public void onGuiDrag (final @ NotNull InventoryDragEvent event ) {
121
110
val inventory = event .getInventory ();
122
111
val holder = inventory .getHolder ();
123
112
if (!(holder instanceof BaseMenu <?>)) return ;
124
113
val menu = (BaseMenu <?>) holder ;
125
- val rawSlots = event .getRawSlots ();
126
114
115
+ val rawSlots = event .getRawSlots ();
127
116
if (!menu .areItemsPlaceable () || isDraggingOnGui (inventory , rawSlots ))
128
117
event .setResult (denied );
129
- final Consumer < InventoryDragEvent > dragAction = menu .getDragAction ();
118
+ val dragAction = menu .getDragAction ();
130
119
if (dragAction != null ) dragAction .accept (event );
131
120
}
132
121
133
122
@ EventHandler (priority = EventPriority .MONITOR )
134
- public void onGuiClose (final InventoryCloseEvent event ) {
123
+ public void onGuiClose (final @ NotNull InventoryCloseEvent event ) {
135
124
val holder = event .getInventory ().getHolder ();
136
125
if (!(holder instanceof BaseMenu <?>)) return ;
137
126
val menu = (BaseMenu <?>) holder ;
@@ -141,7 +130,7 @@ public void onGuiClose(final InventoryCloseEvent event) {
141
130
}
142
131
143
132
@ EventHandler (priority = EventPriority .MONITOR )
144
- public void onGuiOpen (final InventoryOpenEvent event ) {
133
+ public void onGuiOpen (final @ NotNull InventoryOpenEvent event ) {
145
134
val holder = event .getInventory ().getHolder ();
146
135
if (!(holder instanceof BaseMenu <?>)) return ;
147
136
val menu = (BaseMenu <?>) holder ;
@@ -186,17 +175,46 @@ private boolean isOtherAction(final InventoryAction action) {
186
175
return action == InventoryAction .CLONE_STACK || action == InventoryAction .UNKNOWN ;
187
176
}
188
177
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 );
201
219
}
202
220
}
0 commit comments