Skip to content

Commit 17b42d6

Browse files
Fix many bugs, 0 index menus
1 parent 1b57f43 commit 17b42d6

File tree

1 file changed

+23
-60
lines changed

1 file changed

+23
-60
lines changed

src/main/java/me/flame/menus/menu/BaseMenu.java

Lines changed: 23 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929

3030
import static org.bukkit.ChatColor.translateAlternateColorCodes;
3131

32-
// changed
33-
3432
@SuppressWarnings({ "unused", "BooleanMethodIsAlwaysInverted",
3533
"unchecked", "UnusedReturnValue" })
3634
public abstract class BaseMenu<M extends BaseMenu<M>>
@@ -47,7 +45,7 @@ public abstract class BaseMenu<M extends BaseMenu<M>>
4745
private @Getter @Setter boolean dynamicSizing = false;
4846

4947
private final EnumSet<Modifier> modifiers;
50-
protected final Map<Integer, MenuItem> itemMap;
48+
protected final LinkedHashMap<Integer, MenuItem> itemMap;
5149

5250
private final @Getter BorderFiller borderFiller;
5351
private final @Getter MenuFiller menuFiller;
@@ -158,7 +156,7 @@ public M addItem(ItemStack item, boolean shouldUpdate) {
158156
if (!dynamicSizing || this.rows >= 6 || type != MenuType.CHEST) return (M) this;
159157
recreateInventory();
160158
}
161-
itemMap.put(size + 1, new MenuItem(item, null));
159+
itemMap.put(size, new MenuItem(item, null));
162160
if (shouldUpdate) update();
163161
return (M) this;
164162
}
@@ -179,13 +177,13 @@ public M addItem(ItemStack @NotNull ... items) {
179177
* @return the object for chaining
180178
*/
181179
public M addItem(MenuItem @NotNull ... items) {
182-
for (val item : items) {
180+
for (final MenuItem item : items) {
183181
int size = this.itemMap.size();
184182
if (size >= this.size) {
185183
if (!dynamicSizing || this.rows >= 6 || type != MenuType.CHEST) return (M) this;
186184
recreateInventory();
187185
}
188-
this.itemMap.put(size + 1, item);
186+
this.itemMap.put(size, item);
189187
}
190188
return (M) this;
191189
}
@@ -201,7 +199,7 @@ public M addItem(MenuItem item, boolean update) {
201199
if (!dynamicSizing || this.rows >= 6 || type != MenuType.CHEST) return (M) this;
202200
recreateInventory();
203201
}
204-
itemMap.put(size + 1, item);
202+
itemMap.put(size, item);
205203
if (update) update();
206204
return (M) this;
207205
}
@@ -214,6 +212,7 @@ public M addItem(MenuItem item, boolean update) {
214212
* @return the object for chaining
215213
*/
216214
public M setItem(@NotNull Slot slot, ItemStack item, boolean update) {
215+
if (!validSlot(slot)) return (M) this;
217216
itemMap.put(slot.getSlot(), new MenuItem(item, null));
218217
if (update) update();
219218
return (M) this;
@@ -226,6 +225,7 @@ public M setItem(@NotNull Slot slot, ItemStack item, boolean update) {
226225
* @return the object for chaining
227226
*/
228227
public M setItem(@NotNull Slot slot, MenuItem item, boolean update) {
228+
if (!validSlot(slot)) return (M) this;
229229
itemMap.put(slot.getSlot(), item);
230230
if (update) update();
231231
return (M) this;
@@ -298,7 +298,7 @@ public M addItem(ItemStack item) {
298298
if (!dynamicSizing || this.rows >= 6 || type != MenuType.CHEST) return (M) this;
299299
recreateInventory();
300300
}
301-
itemMap.put(size + 1, new MenuItem(item, null));
301+
itemMap.put(size, new MenuItem(item, null));
302302
return (M) this;
303303
}
304304

@@ -313,7 +313,7 @@ public M addItem(MenuItem item) {
313313
if (!dynamicSizing || this.rows >= 6 || type != MenuType.CHEST) return (M) this;
314314
recreateInventory();
315315
}
316-
itemMap.put(size + 1, item);
316+
itemMap.put(size, item);
317317
return (M) this;
318318
}
319319

@@ -325,6 +325,7 @@ public M addItem(MenuItem item) {
325325
* @return the object for chaining
326326
*/
327327
public M setItem(@NotNull Slot slot, ItemStack item) {
328+
if (!validSlot(slot)) return (M) this;
328329
itemMap.put(slot.getSlot(), new MenuItem(item, null));
329330
return (M) this;
330331
}
@@ -335,6 +336,7 @@ public M setItem(@NotNull Slot slot, ItemStack item) {
335336
* @return the object for chaining
336337
*/
337338
public M setItem(@NotNull Slot slot, MenuItem item) {
339+
if (!validSlot(slot)) return (M) this;
338340
itemMap.put(slot.getSlot(), item);
339341
return (M) this;
340342
}
@@ -417,6 +419,7 @@ public Optional<MenuItem> getOptionalItem(int i) {
417419
* @return the itemStack or null
418420
*/
419421
public @Nullable MenuItem getItem(@NotNull Slot slot) {
422+
if (!validSlot(slot)) return null;
420423
return itemMap.get(slot.getSlot());
421424
}
422425

@@ -430,10 +433,12 @@ public Optional<MenuItem> getOptionalItem(int i) {
430433
* @return the optional itemStack or an empty optional
431434
*/
432435
public Optional<MenuItem> getOptionalItem(@NotNull Slot slot) {
436+
if (!validSlot(slot)) return Optional.empty();
433437
return Optional.ofNullable(itemMap.get(slot.getSlot()));
434438
}
435439

436440
public boolean hasItem(@NotNull Slot slot) {
441+
if (!validSlot(slot)) return false;
437442
return itemMap.get(slot.getSlot()) != null;
438443
}
439444

@@ -529,53 +534,6 @@ public M removeItems(@NotNull final List<MenuItem> itemStacks) {
529534
return (M) this;
530535
}
531536

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-
579537
/**
580538
* Update the inventory which recreates the items on default
581539
* @return the object for chaining
@@ -609,9 +567,9 @@ public M updateTitle(String title) {
609567

610568
protected void recreateItems() {
611569
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();
615573
if (item == null || item.getType() == AIR) {
616574
inventory.setItem(i, null);
617575
continue;
@@ -620,7 +578,6 @@ protected void recreateItems() {
620578
}
621579
}
622580

623-
624581
/**
625582
* Open the inventory for the provided player.
626583
* @apiNote Will not work if the player is sleeping.
@@ -689,6 +646,7 @@ public void updateItem(final int slot, @NotNull final ItemStack itemStack) {
689646
* @param itemStack The {@link ItemStack} to replace in the original one in the {@link MenuItem}.
690647
*/
691648
public void updateItem(@NotNull Slot slot, @NotNull final ItemStack itemStack) {
649+
if (!validSlot(slot)) return;
692650
final int slotNum = slot.getSlot();
693651
final MenuItem guiItem = itemMap.get(slotNum);
694652

@@ -709,6 +667,7 @@ public void updateItem(@NotNull Slot slot, @NotNull final ItemStack itemStack) {
709667
* @param itemStack The {@link ItemStack} to replace in the original one in the {@link MenuItem}.
710668
*/
711669
public void updateItem(@NotNull Slot slot, @NotNull final MenuItem itemStack) {
670+
if (!validSlot(slot)) return;
712671
final int slotNum = slot.getSlot();
713672
final MenuItem guiItem = itemMap.get(slotNum);
714673

@@ -720,4 +679,8 @@ public void updateItem(@NotNull Slot slot, @NotNull final MenuItem itemStack) {
720679
guiItem.setItemStack(itemStack.getItemStack());
721680
itemMap.put(slotNum, guiItem);
722681
}
682+
683+
protected boolean validSlot(Slot slot) {
684+
return slot.isSlot();
685+
}
723686
}

0 commit comments

Comments
 (0)