From c40addea3845db6f1357d072cd76373ce610adc5 Mon Sep 17 00:00:00 2001 From: BenCheung0422 <74168521+BenCheung0422@users.noreply.github.com> Date: Sat, 17 Feb 2024 02:06:26 +0800 Subject: [PATCH 1/4] Simplify Inventory#add --- .../entity/furniture/DeathChest.java | 6 +- .../java/minicraft/entity/mob/Player.java | 77 +++++++++---------- .../java/minicraft/item/BucketItem.java | 4 +- src/client/java/minicraft/item/Inventory.java | 68 +++++++--------- src/client/java/minicraft/item/Recipe.java | 2 +- .../java/minicraft/saveload/LegacyLoad.java | 6 +- src/client/java/minicraft/saveload/Load.java | 8 +- .../minicraft/screen/ContainerDisplay.java | 24 +++--- .../java/minicraft/screen/InventoryMenu.java | 17 +++- .../minicraft/screen/PlayerInvDisplay.java | 34 ++++---- 10 files changed, 113 insertions(+), 133 deletions(-) diff --git a/src/client/java/minicraft/entity/furniture/DeathChest.java b/src/client/java/minicraft/entity/furniture/DeathChest.java index d9528da25..c80f3d89a 100644 --- a/src/client/java/minicraft/entity/furniture/DeathChest.java +++ b/src/client/java/minicraft/entity/furniture/DeathChest.java @@ -103,11 +103,7 @@ public void touchedBy(Entity other) { if (other instanceof Player) { Inventory playerInv = ((Player) other).getInventory(); for (Item i : inventory.getItems()) { - int total = 1; - if (i instanceof StackableItem) total = ((StackableItem) i).count; - - int returned = playerInv.add(i); - if (returned < total) { + if (playerInv.add(i) != null) { Game.notifications.add("Your inventory is full!"); return; } diff --git a/src/client/java/minicraft/entity/mob/Player.java b/src/client/java/minicraft/entity/mob/Player.java index b9a74e976..4735058f6 100644 --- a/src/client/java/minicraft/entity/mob/Player.java +++ b/src/client/java/minicraft/entity/mob/Player.java @@ -167,8 +167,8 @@ public Item remove(int idx) { } @Override - public int add(int slot, Item item) { - int res = super.add(slot, item); + public @Nullable Item add(Item item) { + Item res = super.add(item); triggerTrigger(); return res; } @@ -518,15 +518,7 @@ public void tick() { } if (input.inputPressed("menu") && activeItem != null) { - int returned = inventory.add(0, activeItem); - if (activeItem instanceof StackableItem) { - StackableItem stackable = (StackableItem) activeItem; - if (stackable.count > 0) { - getLevel().dropItem(x, y, stackable.copy()); - } - } else if (returned <= 0) { - getLevel().dropItem(x, y, activeItem); - } + tryAddToInvOrDrop(activeItem); activeItem = null; if (isFishing) { @@ -585,14 +577,7 @@ public void tick() { public void resolveHeldItem() { if (!(activeItem instanceof PowerGloveItem)) { // If you are now holding something other than a power glove... if (prevItem != null) { // and you had a previous item that we should care about... - int returned = inventory.add(0, prevItem); // Then add that previous item to your inventory so it isn't lost. - if (prevItem instanceof StackableItem) { - if (((StackableItem) prevItem).count > 0) { - getLevel().dropItem(x, y, prevItem.copy()); - } - } else if (returned == 0) { - getLevel().dropItem(x, y, prevItem); - } + tryAddToInvOrDrop(prevItem); // Then add that previous item to your inventory so it isn't lost. } // If something other than a power glove is being held, but the previous item is null, then nothing happens; nothing added to inventory, and current item remains as the new one. } else activeItem = prevItem; // Otherwise, if you're holding a power glove, then the held item didn't change, so we can remove the power glove and make it what it was before. @@ -998,24 +983,41 @@ public void render(Screen screen) { } } - /** - * What happens when the player interacts with a itemEntity - */ + /** What happens when the player interacts with a itemEntity */ public void pickupItem(ItemEntity itemEntity) { - int picked = 0; - int total = 1; + boolean successful = false; // If there is any item successfully added to the player + boolean remove = false; // Whether to remove the item entity (when empty) if (itemEntity.item instanceof StackableItem && ((StackableItem) itemEntity.item).stacksWith(activeItem)) { // Picked up item equals the one in your hand - ((StackableItem) activeItem).count += ((StackableItem) itemEntity.item).count; - picked = ((StackableItem) itemEntity.item).count; - } else { - if (itemEntity.item instanceof StackableItem) total = ((StackableItem) itemEntity.item).count; - picked = inventory.add(itemEntity.item); // Add item to inventory + int toAdd = Math.min(((StackableItem) activeItem).count + ((StackableItem) itemEntity.item).count, ((StackableItem) activeItem).maxCount) + - ((StackableItem) activeItem).count; + if (toAdd > 0) { + ((StackableItem) activeItem).count += toAdd; + ((StackableItem) itemEntity.item).count -= toAdd; + successful = true; + } + if (((StackableItem) itemEntity.item).count == 0) { // Empty + remove = true; // Remove the item entity + } } - if (picked == total) { - Sound.play("pickup"); + if (!(itemEntity.item instanceof StackableItem && ((StackableItem) itemEntity.item).count == 0)) { + // Add item to inventory + Item remaining; + if (itemEntity.item instanceof StackableItem) { + int orig = ((StackableItem) itemEntity.item).count; + remaining = inventory.add(itemEntity.item); + if (remaining != null && ((StackableItem) remaining).count != orig) { + successful = true; + } + } else remaining = inventory.add(itemEntity.item); + if (remaining == null) { + successful = remove = true; + } + } - itemEntity.remove(); + if (remove) itemEntity.remove(); + if (successful) { + Sound.play("pickup"); addScore(1); } } @@ -1248,17 +1250,12 @@ public String getDebugHunger() { } /** - * Trying to add item(s) to the player inventory. - * If no more item(s) can be added to the inventory, drop the item(s) near the player. + * Trying to add a stack of item(s) to the top of player inventory. + * If there is/are no more item(s) can be added to the inventory, drop the item(s) near the player. */ public void tryAddToInvOrDrop(@Nullable Item item) { if (item != null) { - int returned = inventory.add(0, item); - if (item instanceof StackableItem) { - if (((StackableItem) item).count > 0) { - getLevel().dropItem(x, y, item); - } - } else if (returned == 0) { + if (inventory.add(item) != null) { getLevel().dropItem(x, y, item); } } diff --git a/src/client/java/minicraft/item/BucketItem.java b/src/client/java/minicraft/item/BucketItem.java index 244c4bf40..608c6e8c2 100644 --- a/src/client/java/minicraft/item/BucketItem.java +++ b/src/client/java/minicraft/item/BucketItem.java @@ -91,9 +91,7 @@ private BucketItem editBucket(Player player, Fill newFill) { // This item object is a stack of buckets. count--; - if (player.getInventory().add(new BucketItem(newFill)) == 0) { - player.getLevel().dropItem(player.x, player.y, new BucketItem(newFill)); - } + player.tryAddToInvOrDrop(new BucketItem(newFill)); return this; } diff --git a/src/client/java/minicraft/item/Inventory.java b/src/client/java/minicraft/item/Inventory.java index c0ca6b756..b62258c99 100644 --- a/src/client/java/minicraft/item/Inventory.java +++ b/src/client/java/minicraft/item/Inventory.java @@ -2,6 +2,7 @@ import minicraft.entity.furniture.Furniture; import minicraft.util.Logging; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.ArrayList; @@ -56,58 +57,47 @@ public Item remove(int idx) { return items.remove(idx); } - /** - * Adds an item to the inventory - */ - public int add(@Nullable Item item) { - if (item != null) - return add(items.size(), item); // Adds the item to the end of the inventory list - return 0; - } - /** * Adds several copies of the same item to the end of the inventory. * * @param item Item to be added. * @param num Amount of items to add. + * @return the remaining item not being added; empty if whole stack of items has been added successfully */ - public int add(Item item, int num) { - int total = 0; - for (int i = 0; i < num; i++) - total += add(item.copy()); - return total; + public List add(@NotNull Item item, int num) { + ArrayList remaining = new ArrayList<>(); + for (int i = 0; i < num; i++) { + Item remain = add(item.copy()); + if (remain != null) remaining.add(remain); + } + return remaining; } /** - * Adds an item to a specific spot in the inventory. - * - * @param slot Index to place item at. + * Adds an item at the end of the inventory. * @param item Item to be added. - * @return The number of items added. + * @return the remaining item not being added; {@code null} if whole stack of items has been added successfully */ - public int add(int slot, Item item) { - + public @Nullable Item add(@Nullable Item item) { + if (item == null) return null; // Do not add to inventory if it is a PowerGlove if (item instanceof PowerGloveItem) { Logging.INVENTORY.warn("Tried to add power glove to inventory. stack trace:", new Exception()); - return 0; + return null; } if (item instanceof StackableItem) { // If the item is a item... StackableItem toTake = (StackableItem) item; // ...convert it into a StackableItem object. - int total = toTake.count; - for (Item value : items) { if (toTake.stacksWith(value)) { StackableItem stack = (StackableItem) value; - if (!unlimited) { if (stack.count < stack.maxCount) { int r = stack.maxCount - stack.count; if (r >= toTake.count) { // Matching implies that the other item is stackable, too. stack.count += toTake.count; - return total; + return null; } else { toTake.count -= r; stack.count += r; @@ -115,42 +105,40 @@ public int add(int slot, Item item) { } } else { stack.count += toTake.count; - return total; + return null; } } } if (!unlimited) { if (items.size() < maxItem) { - int c = (int) Math.ceil(toTake.count / 100.0); - for (int i = 0; i < c; i++) { + while (toTake.count > 0) { + if (items.size() == maxItem) return toTake; StackableItem adding = toTake.copy(); - adding.count = i + 1 == c && toTake.count % 100 > 0 ? toTake.count % 100 : 100; - if (adding.count == 0) break; - if (items.size() == maxItem) return total - toTake.count; + adding.count = Math.min(toTake.count, toTake.maxCount); items.add(adding); // Add the item to the items list toTake.count -= adding.count; } - return total; + return null; } else { - return total - toTake.count; + return toTake; } } else { - items.add(slot, toTake); - return total; + items.add(toTake); + return null; } } if (!unlimited) { if (items.size() < maxItem) { - items.add(slot, item); // Add the item to the items list - return 1; + items.add(item); // Add the item to the items list + return null; } else { - return 0; + return item; } } else { - items.add(slot, item); - return 1; + items.add(item); + return null; } } diff --git a/src/client/java/minicraft/item/Recipe.java b/src/client/java/minicraft/item/Recipe.java index 630191c51..d50cdfe3e 100644 --- a/src/client/java/minicraft/item/Recipe.java +++ b/src/client/java/minicraft/item/Recipe.java @@ -89,7 +89,7 @@ public boolean craft(Player player) { // Rdd the crafted items. for (int i = 0; i < amount; i++) { Item product = getProduct(); - if (player.getInventory().add(product) == 0) + if (player.getInventory().add(product) != null) player.getLevel().dropItem(player.x, player.y, product); } diff --git a/src/client/java/minicraft/saveload/LegacyLoad.java b/src/client/java/minicraft/saveload/LegacyLoad.java index b902c3775..cdd492bcf 100644 --- a/src/client/java/minicraft/saveload/LegacyLoad.java +++ b/src/client/java/minicraft/saveload/LegacyLoad.java @@ -344,11 +344,7 @@ public void loadItemToInventory(String item, Inventory inventory) { } private void loadItem(Inventory inventory, Item item) { - int total = 1; - if (item instanceof StackableItem) total = ((StackableItem) item).count; - int loaded = inventory.add(item); - - if (loaded < total) { + if (inventory.add(item) != null) { deathChest.getInventory().add(item.copy()); } } diff --git a/src/client/java/minicraft/saveload/Load.java b/src/client/java/minicraft/saveload/Load.java index efc17809a..d5cc2f24c 100644 --- a/src/client/java/minicraft/saveload/Load.java +++ b/src/client/java/minicraft/saveload/Load.java @@ -764,7 +764,7 @@ public void loadPlayer(Player player, List origData) { if (worldVer.compareTo(new Version("2.0.4-dev7")) < 0) { int arrowCount = Integer.parseInt(data.remove(0)); if (worldVer.compareTo(new Version("2.0.1-dev1")) < 0) - player.getInventory().add(Items.get("arrow"), arrowCount); + player.getInventory().add(Items.get("arrow"), arrowCount).forEach(deathChest.getInventory()::add); } Game.currentLevel = Integer.parseInt(data.remove(0)); @@ -916,11 +916,7 @@ public void loadInventory(Inventory inventory, List data) { } private void loadItem(Inventory inventory, Item item) { - int total = 1; - if (item instanceof StackableItem) total = ((StackableItem) item).count; - int loaded = inventory.add(item); - - if (loaded < total) { + if (inventory.add(item) != null) { deathChest.getInventory().add(item.copy()); } } diff --git a/src/client/java/minicraft/screen/ContainerDisplay.java b/src/client/java/minicraft/screen/ContainerDisplay.java index 21b71f2b7..c10ebd647 100644 --- a/src/client/java/minicraft/screen/ContainerDisplay.java +++ b/src/client/java/minicraft/screen/ContainerDisplay.java @@ -19,13 +19,10 @@ public class ContainerDisplay extends Display { private Chest chest; public ContainerDisplay(Player player, Chest chest) { - super( - new InventoryMenu(player, player.getInventory(), "minicraft.display.menus.inventory", RelPos.LEFT), - new InventoryMenu(chest, chest.getInventory(), chest.name, RelPos.RIGHT) - ); - - //pInv = player.getInventory(); - //cInv = chest.getInventory(); + menus = new Menu[] { + new InventoryMenu(chest, chest.getInventory(), chest.name, RelPos.LEFT, this::update), + new InventoryMenu(player, player.getInventory(), "minicraft.display.menus.inventory", RelPos.RIGHT, this::update) + }; this.player = player; this.chest = chest; @@ -140,12 +137,11 @@ public void tick(InputHandler input) { if (!transferAll) { ((StackableItem) toItem).count = 1; } else { - move = ((StackableItem) fromItem).count; + move = ((StackableItem) toItem).count; } - int moved = to.add(toSel, toItem); - if (moved < move) { - ((StackableItem) fromItem).count -= moved; + if (to.add(toItem) != null) { + ((StackableItem)fromItem).count -= move - ((StackableItem) toItem).count; } else if (!transferAll) { ((StackableItem) fromItem).count--; } else { @@ -153,8 +149,7 @@ public void tick(InputHandler input) { } update(); } else { - int moved = to.add(toSel, toItem); - if (moved == 1) { + if (to.add(toItem) == null) { from.remove(fromSel); update(); } @@ -162,6 +157,9 @@ public void tick(InputHandler input) { } } + /** @deprecated This method is no longer in use by the removal of multiplayer system. + * Also, the game is paused when the display is shown, so it is not possible for the player to pickup items during this period. */ + @Deprecated public void onInvUpdate(ItemHolder holder) { if (holder == player || holder == chest) { update(); diff --git a/src/client/java/minicraft/screen/InventoryMenu.java b/src/client/java/minicraft/screen/InventoryMenu.java index b924b9879..3f1367b2d 100644 --- a/src/client/java/minicraft/screen/InventoryMenu.java +++ b/src/client/java/minicraft/screen/InventoryMenu.java @@ -1,28 +1,37 @@ package minicraft.screen; +import minicraft.core.Action; import minicraft.core.io.InputHandler; import minicraft.entity.Entity; import minicraft.item.Inventory; import minicraft.item.Item; import minicraft.item.StackableItem; import minicraft.screen.entry.ItemEntry; +import org.jetbrains.annotations.Nullable; class InventoryMenu extends ItemListMenu { private final Inventory inv; private final Entity holder; - protected boolean creativeInv = false; + private final boolean creativeInv; + private final @Nullable Action onStackUpdateListener; // The length of the entry shown may change when there is an update to the stack. - InventoryMenu(Entity holder, Inventory inv, String title, RelPos entryPos) { + InventoryMenu(Entity holder, Inventory inv, String title, RelPos entryPos, @Nullable Action onStackUpdateListener) { this(holder, inv, title, entryPos, false, onStackUpdateListener); } + InventoryMenu(Entity holder, Inventory inv, String title, RelPos entryPos, boolean creativeInv) { this(holder, inv, title, entryPos, creativeInv, null); } + InventoryMenu(Entity holder, Inventory inv, String title, RelPos entryPos, boolean creativeInv, @Nullable Action onStackUpdateListener) { super(ItemListMenu.getBuilder(entryPos), ItemEntry.useItems(inv.getItems()), title); this.inv = inv; this.holder = holder; + this.creativeInv = creativeInv; + this.onStackUpdateListener = onStackUpdateListener; } InventoryMenu(InventoryMenu model) { super(ItemListMenu.getBuilder(), ItemEntry.useItems(model.inv.getItems()), model.getTitle()); this.inv = model.inv; this.holder = model.holder; + this.creativeInv = model.creativeInv; + this.onStackUpdateListener = model.onStackUpdateListener; setSelection(model.getSelection()); } @@ -47,6 +56,10 @@ public void tick(InputHandler input) { // drop the whole item. removeSelectedEntry(); } + + if (onStackUpdateListener != null) { + onStackUpdateListener.act(); + } } if (holder.getLevel() != null) { diff --git a/src/client/java/minicraft/screen/PlayerInvDisplay.java b/src/client/java/minicraft/screen/PlayerInvDisplay.java index c5a709854..6df045d32 100644 --- a/src/client/java/minicraft/screen/PlayerInvDisplay.java +++ b/src/client/java/minicraft/screen/PlayerInvDisplay.java @@ -14,6 +14,7 @@ import minicraft.item.Items; import minicraft.item.StackableItem; import minicraft.screen.entry.StringEntry; +import minicraft.util.Logging; public class PlayerInvDisplay extends Display { @@ -28,7 +29,7 @@ public class PlayerInvDisplay extends Display { private final Inventory creativeInv; public PlayerInvDisplay(Player player) { - super(new InventoryMenu(player, player.getInventory(), "minicraft.display.menus.inventory", RelPos.LEFT)); + InventoryMenu invMenu = new InventoryMenu(player, player.getInventory(), "minicraft.display.menus.inventory", RelPos.LEFT, this::update); this.player = player; descriptionMenuBuilder = new Menu.Builder(true, 3, RelPos.TOP_LEFT); creativeMode = Game.isMode("minicraft.settings.mode.creative"); @@ -40,20 +41,18 @@ public PlayerInvDisplay(Player player) { if (creativeMode) { creativeInv = Items.getCreativeModeInventory(); menus = new Menu[]{ - menus[0], - new InventoryMenu(player, creativeInv, "minicraft.displays.player_inv.container_title.items", RelPos.RIGHT) {{ - creativeInv = true; - }}, + invMenu, + new InventoryMenu(player, creativeInv, "minicraft.displays.player_inv.container_title.items", RelPos.RIGHT, true), descriptionMenu }; - menus[1].translate(menus[0].getBounds().getWidth() + padding, 0); + menus[1].translate(invMenu.getBounds().getWidth() + padding, 0); update(); - if (menus[0].getNumOptions() == 0) onSelectionChange(0, 1); + if (invMenu.getNumOptions() == 0) onSelectionChange(0, 1); } else { creativeInv = null; - menus = new Menu[]{menus[0], descriptionMenu}; + menus = new Menu[]{invMenu, descriptionMenu}; } onScreenKeyboardMenu = OnScreenKeyboardMenu.checkAndCreateMenu(); @@ -129,7 +128,6 @@ public void tick(InputHandler input) { } from = player.getInventory(); - to = creativeInv; int fromSel = curMenu.getSelection(); Item fromItem = from.get(fromSel); @@ -159,17 +157,19 @@ public void tick(InputHandler input) { Item fromItem = from.get(fromSel); boolean transferAll; - if (input.inputPressed("attack")) { // If stack limit is available, this can transfer whole stack - transferAll = !(fromItem instanceof StackableItem) || ((StackableItem) fromItem).count == 1; + if (input.getMappedKey("SHIFT-SELECT").isClicked()) { + transferAll = true; + } else if (input.inputPressed("SELECT")) { // If stack limit is available, this can transfer whole stack + transferAll = !(fromItem instanceof StackableItem); } else return; Item toItem = fromItem.copy(); + if (toItem instanceof StackableItem && transferAll) + ((StackableItem) toItem).count = ((StackableItem) toItem).maxCount; - if (!transferAll) { - ((StackableItem) toItem).count = 1; - } + if (to.add(toItem) != null) + Logging.PLAYER.trace("Item {} cannot be added to the player inventory because max slot reached.", toItem); - to.add(toSel, toItem); update(); } @@ -228,9 +228,7 @@ private int getOtherIdx() { private void update() { menus[0] = new InventoryMenu((InventoryMenu) menus[0]); - menus[1] = new InventoryMenu((InventoryMenu) menus[1]) {{ - creativeInv = true; - }}; + menus[1] = new InventoryMenu((InventoryMenu) menus[1]); menus[1].translate(menus[0].getBounds().getWidth() + padding, 0); onSelectionChange(0, selection); itemDescription = getDescription(); From 28f8b3440ff83ec157ea2891dae1788f7b0adc28 Mon Sep 17 00:00:00 2001 From: BenCheung0422 <74168521+BenCheung0422@users.noreply.github.com> Date: Tue, 27 Feb 2024 21:03:40 +0800 Subject: [PATCH 2/4] Move to check drop-stack first --- src/client/java/minicraft/entity/mob/Player.java | 10 +++++----- src/client/java/minicraft/screen/InventoryMenu.java | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/client/java/minicraft/entity/mob/Player.java b/src/client/java/minicraft/entity/mob/Player.java index 4735058f6..fc16414ff 100644 --- a/src/client/java/minicraft/entity/mob/Player.java +++ b/src/client/java/minicraft/entity/mob/Player.java @@ -495,16 +495,16 @@ public void tick() { if (activeItem != null && (input.inputPressed("drop-one") || input.inputPressed("drop-stack"))) { Item drop = activeItem.copy(); - if (input.inputPressed("drop-one") && drop instanceof StackableItem && ((StackableItem) drop).count > 1) { - // Drop one from stack - ((StackableItem) activeItem).count--; - ((StackableItem) drop).count = 1; - } else { + if (!input.inputPressed("drop-stack") || !(drop instanceof StackableItem) || ((StackableItem) drop).count <= 1) { activeItem = null; // Remove it from the "inventory" if (isFishing) { isFishing = false; fishingTicks = maxFishingTicks; } + } else { + // Drop one from stack + ((StackableItem) activeItem).count--; + ((StackableItem) drop).count = 1; } level.dropItem(x, y, drop); diff --git a/src/client/java/minicraft/screen/InventoryMenu.java b/src/client/java/minicraft/screen/InventoryMenu.java index 3f1367b2d..4c02912be 100644 --- a/src/client/java/minicraft/screen/InventoryMenu.java +++ b/src/client/java/minicraft/screen/InventoryMenu.java @@ -48,13 +48,13 @@ public void tick(InputHandler input) { Item drop = invItem.copy(); if (!creativeInv) { - if (dropOne && drop instanceof StackableItem && ((StackableItem) drop).count > 1) { + if (!dropOne || !(drop instanceof StackableItem) || ((StackableItem) drop).count <= 1) { + // drop the whole item. + removeSelectedEntry(); + } else { // just drop one from the stack ((StackableItem) drop).count = 1; ((StackableItem) invItem).count--; - } else { - // drop the whole item. - removeSelectedEntry(); } if (onStackUpdateListener != null) { From 8dfb3ef1b937638d18e02f13584ad2c03e4522c5 Mon Sep 17 00:00:00 2001 From: BenCheung0422 <74168521+BenCheung0422@users.noreply.github.com> Date: Tue, 27 Feb 2024 21:20:07 +0800 Subject: [PATCH 3/4] Resolve unresolved AIOOBE on menus in PlayerInv --- src/client/java/minicraft/screen/PlayerInvDisplay.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/client/java/minicraft/screen/PlayerInvDisplay.java b/src/client/java/minicraft/screen/PlayerInvDisplay.java index 6df045d32..923d8183b 100644 --- a/src/client/java/minicraft/screen/PlayerInvDisplay.java +++ b/src/client/java/minicraft/screen/PlayerInvDisplay.java @@ -29,7 +29,7 @@ public class PlayerInvDisplay extends Display { private final Inventory creativeInv; public PlayerInvDisplay(Player player) { - InventoryMenu invMenu = new InventoryMenu(player, player.getInventory(), "minicraft.display.menus.inventory", RelPos.LEFT, this::update); + menus = new Menu[] { new InventoryMenu(player, player.getInventory(), "minicraft.display.menus.inventory", RelPos.LEFT, this::update) }; this.player = player; descriptionMenuBuilder = new Menu.Builder(true, 3, RelPos.TOP_LEFT); creativeMode = Game.isMode("minicraft.settings.mode.creative"); @@ -41,18 +41,18 @@ public PlayerInvDisplay(Player player) { if (creativeMode) { creativeInv = Items.getCreativeModeInventory(); menus = new Menu[]{ - invMenu, + menus[0], new InventoryMenu(player, creativeInv, "minicraft.displays.player_inv.container_title.items", RelPos.RIGHT, true), descriptionMenu }; - menus[1].translate(invMenu.getBounds().getWidth() + padding, 0); + menus[1].translate(menus[0].getBounds().getWidth() + padding, 0); update(); - if (invMenu.getNumOptions() == 0) onSelectionChange(0, 1); + if (menus[0].getNumOptions() == 0) onSelectionChange(0, 1); } else { creativeInv = null; - menus = new Menu[]{invMenu, descriptionMenu}; + menus = new Menu[]{menus[0], descriptionMenu}; } onScreenKeyboardMenu = OnScreenKeyboardMenu.checkAndCreateMenu(); From 7c8cfdee7966f2c674b1d6e052168969001a367f Mon Sep 17 00:00:00 2001 From: BenCheung0422 <74168521+BenCheung0422@users.noreply.github.com> Date: Tue, 23 Apr 2024 03:47:01 +0800 Subject: [PATCH 4/4] Fix unmatching change to the container menus --- src/client/java/minicraft/screen/ContainerDisplay.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client/java/minicraft/screen/ContainerDisplay.java b/src/client/java/minicraft/screen/ContainerDisplay.java index 638df9801..9c108984a 100644 --- a/src/client/java/minicraft/screen/ContainerDisplay.java +++ b/src/client/java/minicraft/screen/ContainerDisplay.java @@ -20,8 +20,8 @@ public class ContainerDisplay extends Display { public ContainerDisplay(Player player, Chest chest) { menus = new Menu[] { - new InventoryMenu(chest, chest.getInventory(), chest.name, RelPos.LEFT, this::update), - new InventoryMenu(player, player.getInventory(), "minicraft.display.menus.inventory", RelPos.RIGHT, this::update) + new InventoryMenu(player, player.getInventory(), "minicraft.display.menus.inventory", RelPos.RIGHT, this::update), + new InventoryMenu(chest, chest.getInventory(), chest.name, RelPos.LEFT, this::update) }; this.player = player; this.chest = chest;