44import com .google .common .cache .CacheBuilder ;
55import com .google .common .collect .HashMultimap ;
66import com .google .common .collect .Multimap ;
7- import io .papermc .paper .entity .PlayerGiveResult ;
87import it .unimi .dsi .fastutil .ints .*;
9- import net .kyori .adventure .text .Component ;
108import org .bukkit .Bukkit ;
119import org .bukkit .entity .Player ;
1210import org .bukkit .event .inventory .*;
1715import org .bukkit .plugin .Plugin ;
1816import org .jetbrains .annotations .ApiStatus ;
1917import org .jetbrains .annotations .NotNull ;
20- import pt .supercrafting .menu .editor . MenuUpdatable ;
18+ import pt .supercrafting .menu .bridge . ItemBridge ;
2119import pt .supercrafting .menu .item .MenuItem ;
2220import pt .supercrafting .menu .slot .MenuSlot ;
2321
@@ -56,10 +54,10 @@ final class MenuClickProcessor {
5654 ));
5755
5856 if (hasBundleActions ) {
59- defaultBehaviors .add (InventoryAction . PICKUP_ALL_INTO_BUNDLE );
60- defaultBehaviors .add (InventoryAction . PLACE_ALL_INTO_BUNDLE );
61- defaultBehaviors .add (InventoryAction . PICKUP_SOME_INTO_BUNDLE );
62- defaultBehaviors .add (InventoryAction . PLACE_SOME_INTO_BUNDLE );
57+ defaultBehaviors .add (getInventoryAction ( "PLACE_ALL_INTO_BUNDLE" ) );
58+ defaultBehaviors .add (getInventoryAction ( "PICKUP_ALL_INTO_BUNDLE" ) );
59+ defaultBehaviors .add (getInventoryAction ( " PICKUP_SOME_INTO_BUNDLE" ) );
60+ defaultBehaviors .add (getInventoryAction ( " PLACE_SOME_INTO_BUNDLE" ) );
6361 }
6462
6563 DEFAULT_BEHAVIORS = Collections .unmodifiableSet (defaultBehaviors );
@@ -80,6 +78,15 @@ final class MenuClickProcessor {
8078 REVERSED_PLAYER_INVENTORY_SLOTS = IntLists .unmodifiable (reversedPrioritySlots );
8179 }
8280
81+ @ NotNull
82+ private static InventoryAction getInventoryAction (@ NotNull String name ) {
83+ try {
84+ return InventoryAction .valueOf (name );
85+ } catch (IllegalArgumentException e ) {
86+ throw new IllegalArgumentException ("Invalid InventoryAction name: " + name , e );
87+ }
88+ }
89+
8390 private final Menu menu ;
8491 private final Cache <UUID , Boolean > closeCache = CacheBuilder .newBuilder ()
8592 .expireAfterWrite (250 , TimeUnit .MILLISECONDS )
@@ -120,7 +127,7 @@ private void clickMenu(@NotNull InventoryClickEvent event) {
120127
121128 ClickType clickType = event .getClick ();
122129 ItemStack cursor = event .getCursor ();
123- boolean isAdd = !cursor .isEmpty () && !clickType .isShiftClick ();
130+ boolean isAdd = !ItemBridge .isEmpty (cursor ) && !clickType .isShiftClick ();
124131
125132 boolean handled ;
126133 if (isAdd ) {
@@ -149,7 +156,7 @@ private void clickMenu(@NotNull InventoryClickEvent event) {
149156 IntList slots = new IntArrayList (PLAYER_INVENTORY_SLOTS .size ());
150157 for (int i = 0 ; i < inventory .getSize (); i ++) {
151158 ItemStack itemStack = inventory .getItem (i );
152- if (itemStack != null && !itemStack .isEmpty () && itemStack .isSimilar (result ))
159+ if (itemStack != null && !ItemBridge .isEmpty (itemStack ) && itemStack .isSimilar (result ))
153160 slots .add (i );
154161 }
155162
@@ -161,31 +168,31 @@ private void clickMenu(@NotNull InventoryClickEvent event) {
161168
162169 ItemStack playerItem = inventory .getItem (playerSlot );
163170 if (playerItem == null )
164- playerItem = ItemStack .empty ();
171+ playerItem = ItemBridge .empty ();
165172
166- if (!playerItem .isEmpty () && !playerItem .isSimilar (result ))
173+ if (!ItemBridge .isEmpty (playerItem ) && !playerItem .isSimilar (result ))
167174 continue ;
168175
169- int allowedToAdd = playerItem .isEmpty () ? result .getMaxStackSize () : playerItem .getMaxStackSize () - playerItem .getAmount ();
176+ int allowedToAdd = ItemBridge .isEmpty (playerItem ) ? result .getMaxStackSize () : playerItem .getMaxStackSize () - playerItem .getAmount ();
170177 if (allowedToAdd <= 0 )
171178 continue ;
172179
173180 int toAdd = Math .min (allowedToAdd , result .getAmount ());
174- ItemStack newPlayerItem = result .asQuantity (playerItem .getAmount () + toAdd );
181+ ItemStack newPlayerItem = ItemBridge .asQuantity (playerItem , playerItem .getAmount () + toAdd );
175182 inventory .setItem (playerSlot , newPlayerItem );
176183
177- result = result .asQuantity (result .getAmount () - toAdd );
184+ result = ItemBridge .asQuantity (result , result .getAmount () - toAdd );
178185 if (result .getAmount () <= 0 )
179186 break ;
180187
181188 }
182189
183- if (!result .isEmpty ()) {
190+ if (!ItemBridge .isEmpty (result )) {
184191 MenuSlot .Add add = new MenuSlot .PlayerAdd (result , result .getAmount (), player );
185192 slot .add (add );
186193
187194 ItemStack remaining = add .getResult ();
188- if (!remaining .isEmpty ()) // Drop overflow items
195+ if (!ItemBridge .isEmpty (remaining )) // Drop overflow items
189196 player .getWorld ().dropItemNaturally (player .getLocation (), remaining );
190197 }
191198
@@ -222,23 +229,30 @@ private void clickHotBar(@NotNull InventoryClickEvent event) {
222229 Inventory inventory = player .getInventory ();
223230
224231 ItemStack hotbarItem =inventory .getItem (hotbarButton );
225- if (hotbarItem != null && ! hotbarItem .isEmpty ()) {
232+ if (hotbarItem != null && ItemBridge .isEmpty (hotbarItem )) {
226233
227234 MenuSlot .Add add = new MenuSlot .PlayerAdd (hotbarItem , hotbarItem .getAmount (), player );
228235 slot .add (add );
229236
230237 ItemStack remaining = add .getResult ();
231- if (remaining .isEmpty ())
232- remaining = ItemStack .empty ();
238+ if (ItemBridge .isEmpty (remaining ))
239+ remaining = ItemBridge .empty ();
233240
234241 inventory .setItem (hotbarButton , remaining );
235242 }
236243
237244 hotbarItem = inventory .getItem (hotbarButton ); // Updated
238- if (hotbarItem == null || hotbarItem .isEmpty ())
245+ if (hotbarItem == null || ItemBridge .isEmpty (hotbarItem ))
239246 player .getInventory ().setItem (hotbarButton , result );
240- else
241- player .give (hotbarItem );
247+ else {
248+ //player.give(hotbarItem);
249+ Map <Integer , ItemStack > overflow = player .getInventory ().addItem (hotbarItem );
250+ for (ItemStack drop : overflow .values ()) {
251+ if (!ItemBridge .isEmpty (drop )) {
252+ player .getWorld ().dropItemNaturally (player .getLocation (), drop );
253+ }
254+ }
255+ }
242256
243257 menu .refresh ();
244258 }
@@ -259,18 +273,18 @@ private void clickInventory(@NotNull InventoryClickEvent event) {
259273
260274 ItemStack playerItem = playerInventory .getItem (slot );
261275 if (playerItem == null )
262- playerItem = ItemStack .empty ();
263- if (playerItem .isEmpty () || !playerItem .isSimilar (cursor ))
276+ playerItem = ItemBridge .empty ();
277+ if (ItemBridge .isEmpty (playerItem ) || !playerItem .isSimilar (cursor ))
264278 continue ;
265279
266280 int allowedToAdd = cursor .getMaxStackSize () - cursor .getAmount ();
267281 if (allowedToAdd <= 0 )
268282 continue ;
269283
270284 int toAdd = Math .min (allowedToAdd , playerItem .getAmount ());
271- ItemStack newCursor = cursor .asQuantity (cursor .getAmount () + toAdd );
285+ ItemStack newCursor = ItemBridge .asQuantity (cursor , cursor .getAmount () + toAdd );
272286
273- ItemStack newPlayerItem = playerItem .asQuantity (playerItem .getAmount () - toAdd );
287+ ItemStack newPlayerItem = ItemBridge .asQuantity (playerItem , playerItem .getAmount () - toAdd );
274288 playerInventory .setItem (slot , newPlayerItem );
275289 cursor = newCursor ;
276290
@@ -281,7 +295,7 @@ private void clickInventory(@NotNull InventoryClickEvent event) {
281295 }
282296
283297 ItemStack currentItem = event .getCurrentItem ();
284- if (currentItem == null || currentItem .isEmpty ())
298+ if (currentItem == null || ItemBridge .isEmpty (currentItem ))
285299 return ;
286300
287301 if (DEFAULT_BEHAVIORS .contains (action ))
@@ -334,7 +348,7 @@ public void drag(@NotNull InventoryDragEvent event) {
334348 InventoryView view = event .getView ();
335349 for (int slot : slots ) {
336350
337- Inventory inventory = view .getInventory ( slot );
351+ Inventory inventory = slot < view .getTopInventory (). getSize () ? view . getTopInventory () : view . getBottomInventory ( );
338352 if (inventory == null )
339353 continue ;
340354
@@ -353,7 +367,7 @@ public void drag(@NotNull InventoryDragEvent event) {
353367
354368 IntList draggedSlots = new IntArrayList (slotsByInventory .get (menuInventory ));
355369 ItemStack cursor = event .getOldCursor ();
356- if (cursor .isEmpty ())
370+ if (ItemBridge .isEmpty (cursor ))
357371 return ;
358372
359373 event .setCancelled (false );
@@ -374,23 +388,29 @@ public void drag(@NotNull InventoryDragEvent event) {
374388 menuSlot .add (add );
375389
376390 ItemStack result = add .getResult ();
377- if (!result .isEmpty () && !result .isSimilar (cursor )) {
391+ if (!ItemBridge .isEmpty (result ) && !result .isSimilar (cursor )) {
378392 overFlow .add (result );
379393 } else if (result .isSimilar (cursor ) && !add .isSuccessful ())
380394 giveBack += amountPerSlot ;
381395
382396 }
383397
384- ItemStack newCursor = cursor .asQuantity (giveBack );
398+ ItemStack newCursor = ItemBridge .asQuantity (cursor , giveBack );
385399 event .setCursor (newCursor );
386400
387401 Plugin plugin = MenuManager .instance .getPlugin ();
388- player .getScheduler ().runDelayed (plugin , (s ) -> menu .refresh (), () -> {}, 3 );
402+ // player.getScheduler().runDelayed(plugin, (s) -> menu.refresh(), () -> {}, 3);
389403
390- if (!overFlow .isEmpty ())
391- player .give (overFlow );
404+ if (!overFlow .isEmpty ()) {
405+ //player.give(overFlow);
406+ for (ItemStack itemStack : overFlow ) {
407+ if (!ItemBridge .isEmpty (itemStack )) {
408+ player .getWorld ().dropItemNaturally (player .getLocation (), itemStack );
409+ }
410+ }
411+ }
392412
393- // Bukkit.getScheduler().runTaskLater(plugin, menu::refresh, 1); // Think about a better way to do this
413+ Bukkit .getScheduler ().runTaskLater (plugin , menu ::refresh , 1 ); // Think about a better way to do this
394414
395415 }
396416
0 commit comments