29
29
30
30
import static org .bukkit .ChatColor .translateAlternateColorCodes ;
31
31
32
- // changed
33
-
34
32
@ SuppressWarnings ({ "unused" , "BooleanMethodIsAlwaysInverted" ,
35
33
"unchecked" , "UnusedReturnValue" })
36
34
public abstract class BaseMenu <M extends BaseMenu <M >>
@@ -47,7 +45,7 @@ public abstract class BaseMenu<M extends BaseMenu<M>>
47
45
private @ Getter @ Setter boolean dynamicSizing = false ;
48
46
49
47
private final EnumSet <Modifier > modifiers ;
50
- protected final Map <Integer , MenuItem > itemMap ;
48
+ protected final LinkedHashMap <Integer , MenuItem > itemMap ;
51
49
52
50
private final @ Getter BorderFiller borderFiller ;
53
51
private final @ Getter MenuFiller menuFiller ;
@@ -158,7 +156,7 @@ public M addItem(ItemStack item, boolean shouldUpdate) {
158
156
if (!dynamicSizing || this .rows >= 6 || type != MenuType .CHEST ) return (M ) this ;
159
157
recreateInventory ();
160
158
}
161
- itemMap .put (size + 1 , new MenuItem (item , null ));
159
+ itemMap .put (size , new MenuItem (item , null ));
162
160
if (shouldUpdate ) update ();
163
161
return (M ) this ;
164
162
}
@@ -179,13 +177,13 @@ public M addItem(ItemStack @NotNull ... items) {
179
177
* @return the object for chaining
180
178
*/
181
179
public M addItem (MenuItem @ NotNull ... items ) {
182
- for (val item : items ) {
180
+ for (final MenuItem item : items ) {
183
181
int size = this .itemMap .size ();
184
182
if (size >= this .size ) {
185
183
if (!dynamicSizing || this .rows >= 6 || type != MenuType .CHEST ) return (M ) this ;
186
184
recreateInventory ();
187
185
}
188
- this .itemMap .put (size + 1 , item );
186
+ this .itemMap .put (size , item );
189
187
}
190
188
return (M ) this ;
191
189
}
@@ -201,7 +199,7 @@ public M addItem(MenuItem item, boolean update) {
201
199
if (!dynamicSizing || this .rows >= 6 || type != MenuType .CHEST ) return (M ) this ;
202
200
recreateInventory ();
203
201
}
204
- itemMap .put (size + 1 , item );
202
+ itemMap .put (size , item );
205
203
if (update ) update ();
206
204
return (M ) this ;
207
205
}
@@ -214,6 +212,7 @@ public M addItem(MenuItem item, boolean update) {
214
212
* @return the object for chaining
215
213
*/
216
214
public M setItem (@ NotNull Slot slot , ItemStack item , boolean update ) {
215
+ if (!validSlot (slot )) return (M ) this ;
217
216
itemMap .put (slot .getSlot (), new MenuItem (item , null ));
218
217
if (update ) update ();
219
218
return (M ) this ;
@@ -226,6 +225,7 @@ public M setItem(@NotNull Slot slot, ItemStack item, boolean update) {
226
225
* @return the object for chaining
227
226
*/
228
227
public M setItem (@ NotNull Slot slot , MenuItem item , boolean update ) {
228
+ if (!validSlot (slot )) return (M ) this ;
229
229
itemMap .put (slot .getSlot (), item );
230
230
if (update ) update ();
231
231
return (M ) this ;
@@ -298,7 +298,7 @@ public M addItem(ItemStack item) {
298
298
if (!dynamicSizing || this .rows >= 6 || type != MenuType .CHEST ) return (M ) this ;
299
299
recreateInventory ();
300
300
}
301
- itemMap .put (size + 1 , new MenuItem (item , null ));
301
+ itemMap .put (size , new MenuItem (item , null ));
302
302
return (M ) this ;
303
303
}
304
304
@@ -313,7 +313,7 @@ public M addItem(MenuItem item) {
313
313
if (!dynamicSizing || this .rows >= 6 || type != MenuType .CHEST ) return (M ) this ;
314
314
recreateInventory ();
315
315
}
316
- itemMap .put (size + 1 , item );
316
+ itemMap .put (size , item );
317
317
return (M ) this ;
318
318
}
319
319
@@ -325,6 +325,7 @@ public M addItem(MenuItem item) {
325
325
* @return the object for chaining
326
326
*/
327
327
public M setItem (@ NotNull Slot slot , ItemStack item ) {
328
+ if (!validSlot (slot )) return (M ) this ;
328
329
itemMap .put (slot .getSlot (), new MenuItem (item , null ));
329
330
return (M ) this ;
330
331
}
@@ -335,6 +336,7 @@ public M setItem(@NotNull Slot slot, ItemStack item) {
335
336
* @return the object for chaining
336
337
*/
337
338
public M setItem (@ NotNull Slot slot , MenuItem item ) {
339
+ if (!validSlot (slot )) return (M ) this ;
338
340
itemMap .put (slot .getSlot (), item );
339
341
return (M ) this ;
340
342
}
@@ -417,6 +419,7 @@ public Optional<MenuItem> getOptionalItem(int i) {
417
419
* @return the itemStack or null
418
420
*/
419
421
public @ Nullable MenuItem getItem (@ NotNull Slot slot ) {
422
+ if (!validSlot (slot )) return null ;
420
423
return itemMap .get (slot .getSlot ());
421
424
}
422
425
@@ -430,10 +433,12 @@ public Optional<MenuItem> getOptionalItem(int i) {
430
433
* @return the optional itemStack or an empty optional
431
434
*/
432
435
public Optional <MenuItem > getOptionalItem (@ NotNull Slot slot ) {
436
+ if (!validSlot (slot )) return Optional .empty ();
433
437
return Optional .ofNullable (itemMap .get (slot .getSlot ()));
434
438
}
435
439
436
440
public boolean hasItem (@ NotNull Slot slot ) {
441
+ if (!validSlot (slot )) return false ;
437
442
return itemMap .get (slot .getSlot ()) != null ;
438
443
}
439
444
@@ -529,53 +534,6 @@ public M removeItems(@NotNull final List<MenuItem> itemStacks) {
529
534
return (M ) this ;
530
535
}
531
536
532
- /**
533
- * Add the specified items to the inventory.
534
- * @param expandIfFull whether to expand the inventory to add the un-added items or not
535
- * @param items the items to add to the inventory
536
- * @apiNote the expand might fail if the inventory is at 6 rows (at maximum capacity) or the type is not InventoryType.CHEST,
537
- * and the rest of the items will not be added.
538
- * @return the object for chaining
539
- */
540
- public M addItem (final boolean expandIfFull , @ NotNull final MenuItem @ NotNull ... items ) {
541
- int itemsSize = this .itemMap .size ();
542
- List <MenuItem > notAddedItems = new ArrayList <>(size );
543
-
544
- for (final MenuItem guiItem : items ) {
545
- for (int slot = 0 ; slot < size ; slot ++) {
546
- if (this .itemMap .get (slot ) != null ) {
547
- if (slot != size - 1 ) continue ;
548
- notAddedItems .add (guiItem );
549
- }
550
- this .itemMap .put (slot , guiItem );
551
- break ;
552
- }
553
- }
554
-
555
- if (!expandIfFull || notAddedItems .isEmpty ()
556
- || this .rows >= 6 || type != MenuType .CHEST ) return (M ) this ;
557
- final int size = notAddedItems .size ();
558
- recreateInventory ();
559
-
560
- /*
561
- * """
562
- * * surely there's something wrong I can feel it,
563
- * * but no one knows I know there's a problem with this line.
564
- * * please just trust my gut. don't do this, it's bad quality!
565
- * * your code can & will be ruined, and your code will never work!!!
566
- * """
567
- * - Best IDE, Intellij 26/8/2023 @ Saturday @ 6:32am
568
- * (while I was staying up and my body
569
- * was begging me to go to sleep)
570
- */
571
- @ SuppressWarnings ("DataFlowIssue" )
572
- final MenuItem [] newItems = (MenuItem []) notAddedItems .toArray ();
573
- MenuItem [] remainingItems = new MenuItem [size ];
574
- System .arraycopy (newItems , 0 , remainingItems , 0 , size );
575
- return this .addItem (true , remainingItems );
576
- }
577
-
578
-
579
537
/**
580
538
* Update the inventory which recreates the items on default
581
539
* @return the object for chaining
@@ -609,9 +567,9 @@ public M updateTitle(String title) {
609
567
610
568
protected void recreateItems () {
611
569
if (itemMap .isEmpty ()) return ;
612
- for (Map .Entry <Integer , MenuItem > entry : itemMap .entrySet ()) {
613
- int i = entry .getKey () - 1 ;
614
- ItemStack item = entry .getValue ().getItemStack ();
570
+ for (Map .Entry <Integer , MenuItem > entry : this . itemMap .entrySet ()) {
571
+ int i = entry .getKey ();
572
+ ItemStack item = entry .getValue ().getItemStack ();
615
573
if (item == null || item .getType () == AIR ) {
616
574
inventory .setItem (i , null );
617
575
continue ;
@@ -620,7 +578,6 @@ protected void recreateItems() {
620
578
}
621
579
}
622
580
623
-
624
581
/**
625
582
* Open the inventory for the provided player.
626
583
* @apiNote Will not work if the player is sleeping.
@@ -689,6 +646,7 @@ public void updateItem(final int slot, @NotNull final ItemStack itemStack) {
689
646
* @param itemStack The {@link ItemStack} to replace in the original one in the {@link MenuItem}.
690
647
*/
691
648
public void updateItem (@ NotNull Slot slot , @ NotNull final ItemStack itemStack ) {
649
+ if (!validSlot (slot )) return ;
692
650
final int slotNum = slot .getSlot ();
693
651
final MenuItem guiItem = itemMap .get (slotNum );
694
652
@@ -709,6 +667,7 @@ public void updateItem(@NotNull Slot slot, @NotNull final ItemStack itemStack) {
709
667
* @param itemStack The {@link ItemStack} to replace in the original one in the {@link MenuItem}.
710
668
*/
711
669
public void updateItem (@ NotNull Slot slot , @ NotNull final MenuItem itemStack ) {
670
+ if (!validSlot (slot )) return ;
712
671
final int slotNum = slot .getSlot ();
713
672
final MenuItem guiItem = itemMap .get (slotNum );
714
673
@@ -720,4 +679,8 @@ public void updateItem(@NotNull Slot slot, @NotNull final MenuItem itemStack) {
720
679
guiItem .setItemStack (itemStack .getItemStack ());
721
680
itemMap .put (slotNum , guiItem );
722
681
}
682
+
683
+ protected boolean validSlot (Slot slot ) {
684
+ return slot .isSlot ();
685
+ }
723
686
}
0 commit comments