diff --git a/src/client/java/minicraft/entity/furniture/Chest.java b/src/client/java/minicraft/entity/furniture/Chest.java index 7f1289ac..317aa867 100644 --- a/src/client/java/minicraft/entity/furniture/Chest.java +++ b/src/client/java/minicraft/entity/furniture/Chest.java @@ -7,6 +7,8 @@ import minicraft.entity.mob.Player; import minicraft.gfx.SpriteManager.SpriteLink; import minicraft.gfx.SpriteManager.SpriteType; +import minicraft.item.BoundedInventory; +import minicraft.item.FixedInventory; import minicraft.item.Inventory; import minicraft.item.Item; import minicraft.item.Items; @@ -19,23 +21,27 @@ import java.util.Random; public class Chest extends Furniture implements ItemHolder { - private Inventory inventory; // Inventory of the chest + protected static final LinkedSprite defaultSprite = new SpriteLink.SpriteLinkBuilder(SpriteType.Item, "chest").createSpriteLink(); + protected final Inventory inventory; // Inventory of the chest public Chest() { this("Chest"); } public Chest(String name) { - this(name, new SpriteLink.SpriteLinkBuilder(SpriteType.Item, "chest").createSpriteLink()); + this(name, defaultSprite); } /** * Creates a chest with a custom name. * @param name Name of chest. */ public Chest(String name, SpriteLink itemSprite) { - super(name, new SpriteLink.SpriteLinkBuilder(SpriteType.Entity, "chest").createSpriteLink(), itemSprite, 3, 3); // Name of the chest + this(new FixedInventory(), name, itemSprite); // Default with bounded inventory + } - inventory = new Inventory(); // Initialize the inventory. + protected Chest(Inventory inventory, String name, LinkedSprite itemSprite) { + super(name, new SpriteLink.SpriteLinkBuilder(SpriteType.Entity, "chest").createSpriteLink(), itemSprite, 3, 3); // Name of the chest + this.inventory = inventory; // Initialize the inventory. } @Override @@ -81,7 +87,7 @@ public Inventory getInventory() { @Override public void die() { if (level != null) { - List items = inventory.getItems(); + List items = inventory.getItemsView(); level.dropItem(x, y, items.toArray(new Item[0])); } super.die(); diff --git a/src/client/java/minicraft/entity/furniture/DeathChest.java b/src/client/java/minicraft/entity/furniture/DeathChest.java index 60cefe0d..d23cebb7 100644 --- a/src/client/java/minicraft/entity/furniture/DeathChest.java +++ b/src/client/java/minicraft/entity/furniture/DeathChest.java @@ -14,6 +14,7 @@ import minicraft.gfx.SpriteManager.SpriteType; import minicraft.item.Inventory; import minicraft.item.Item; +import minicraft.item.UnlimitedInventory; import org.jetbrains.annotations.Nullable; public class DeathChest extends Chest { @@ -23,15 +24,12 @@ public class DeathChest extends Chest { public int time; // Time passed (used for death chest despawn) private int redtick = 0; //This is used to determine the shade of red when the chest is about to expire. private boolean reverse; // What direction the red shade (redtick) is changing. - private Inventory inventory = new Inventory() {{ - unlimited = true; - }}; // Implement the inventory locally instead. /** * Creates a custom chest with the name Death Chest */ public DeathChest() { - super("Death Chest", new SpriteLink.SpriteLinkBuilder(SpriteType.Item, "dungeon_chest").createSpriteLink()); + super(new UnlimitedInventory(), "Death Chest", new SpriteLink.SpriteLinkBuilder(SpriteType.Item, "dungeon_chest").createSpriteLink()); this.sprite = normalSprite; /// Set the expiration time based on the world difficulty. @@ -48,7 +46,7 @@ public DeathChest(Player player) { this(); this.x = player.x; this.y = player.y; - for (Item i : player.getInventory().getItems()) { + for (Item i : player.getInventory().getItemsView()) { inventory.add(i.copy()); } } @@ -100,7 +98,7 @@ public boolean use(Player player, @Nullable Item item, Direction attackDir) { public void touchedBy(Entity other) { if (other instanceof Player) { Inventory playerInv = ((Player) other).getInventory(); - for (Item i : inventory.getItems()) { + for (Item i : inventory.getItemsView()) { if (playerInv.add(i) != null) { Game.inGameNotifications.add("Your inventory is full!"); return; @@ -113,9 +111,4 @@ public void touchedBy(Entity other) { Game.inGameNotifications.add(Localization.getLocalized("minicraft.notification.death_chest_retrieved")); } } - - @Override - public Inventory getInventory() { - return inventory; - } } diff --git a/src/client/java/minicraft/entity/furniture/RewardChest.java b/src/client/java/minicraft/entity/furniture/RewardChest.java new file mode 100644 index 00000000..e732d30e --- /dev/null +++ b/src/client/java/minicraft/entity/furniture/RewardChest.java @@ -0,0 +1,38 @@ +package minicraft.entity.furniture; + +import minicraft.item.Inventory; +import minicraft.item.Item; +import minicraft.item.UnlimitedInventory; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Collection; + +public class RewardChest extends Chest { + /** + * Creates a custom chest with the name Rewards + */ + public RewardChest(@Nullable Collection<@NotNull Item> items) { + super(new RewardChestInventory(), "Rewards", defaultSprite); + if (items != null) items.forEach(((RewardChestInventory) inventory)::add0); + } + + private final static class RewardChestInventory extends UnlimitedInventory { + @Override + public @Nullable Item add(@Nullable Item item) { + return item; // Items cannot be added by player + } + + private void add0(@NotNull Item item) { // But internally + super.add(item); + } + } + + @Override + public void tick() { + super.tick(); + if (inventory.invSize() == 0) { + remove(); + } + } +} diff --git a/src/client/java/minicraft/entity/mob/Player.java b/src/client/java/minicraft/entity/mob/Player.java index f12c95cd..5bd776d6 100644 --- a/src/client/java/minicraft/entity/mob/Player.java +++ b/src/client/java/minicraft/entity/mob/Player.java @@ -27,8 +27,10 @@ import minicraft.gfx.SpriteManager.SpriteLink; import minicraft.gfx.SpriteManager.SpriteType; import minicraft.item.ArmorItem; +import minicraft.item.BoundedInventory; import minicraft.item.FishingData; import minicraft.item.FishingRodItem; +import minicraft.item.FixedInventory; import minicraft.item.FurnitureItem; import minicraft.item.Inventory; import minicraft.item.Item; @@ -92,7 +94,7 @@ public class Player extends Mob implements ItemHolder, ClientTickable { public static SpriteLink[][] sprites; public static SpriteLink[][] carrySprites; - private final Inventory inventory; + private final BoundedInventory inventory; public @Nullable Item activeItem; @@ -146,7 +148,7 @@ public Player(@Nullable Player previousInstance, InputHandler input) { y = 24; this.input = input; // Since this implementation will be deleted by Better Creative Mode Inventory might not implemented correctly - inventory = new Inventory() { // Registering all triggers to InventoryChanged. + inventory = new FixedInventory() { // Registering all triggers to InventoryChanged. private void triggerTrigger() { AdvancementElement.AdvancementTrigger.InventoryChangedTrigger.INSTANCE.trigger( new AdvancementElement.AdvancementTrigger.InventoryChangedTrigger.InventoryChangedTriggerConditionHandler.InventoryChangedTriggerConditions(this) @@ -178,12 +180,6 @@ public void removeItems(Item given, int count) { super.removeItems(given, count); triggerTrigger(); } - - @Override - public void updateInv(String items) { - super.updateInv(items); - triggerTrigger(); - } }; potioneffects = new HashMap<>(); @@ -1176,7 +1172,7 @@ public void remove() { } @Override - public Inventory getInventory() { + public BoundedInventory getInventory() { return inventory; } diff --git a/src/client/java/minicraft/item/BoundedInventory.java b/src/client/java/minicraft/item/BoundedInventory.java new file mode 100644 index 00000000..e73354ed --- /dev/null +++ b/src/client/java/minicraft/item/BoundedInventory.java @@ -0,0 +1,65 @@ +package minicraft.item; + +import minicraft.util.Logging; +import org.jetbrains.annotations.Nullable; + +public abstract class BoundedInventory extends Inventory { + /** + * Gets the current maximum capacity of inventory. + * This value is capable to inventory expanding (e.g. upgrades), but not changing by other + * conditions such as the contents. + * @return current value of maximum capacity of general slots + */ + public abstract int getMaxSlots(); + + @Override + 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 null; + } + + int maxItem = getMaxSlots(); + if (item instanceof StackableItem) { // If the item is a item... + StackableItem toTake = (StackableItem) item; // ...convert it into a StackableItem object. + for (Item value : items) { + if (toTake.stacksWith(value)) { + StackableItem stack = (StackableItem) value; + 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 null; + } else { + toTake.count -= r; + stack.count += r; + } + } + } + } + + if (items.size() < maxItem) { + while (toTake.count > 0) { + if (items.size() == maxItem) return toTake; + StackableItem adding = toTake.copy(); + adding.count = Math.min(toTake.count, toTake.maxCount); + items.add(adding); // Add the item to the items list + toTake.count -= adding.count; + } + return null; + } else { + return toTake; + } + } + + if (items.size() < maxItem) { + items.add(item); // Add the item to the items list + return null; + } else { + return item; + } + } +} diff --git a/src/client/java/minicraft/item/FixedInventory.java b/src/client/java/minicraft/item/FixedInventory.java new file mode 100644 index 00000000..e424c8bd --- /dev/null +++ b/src/client/java/minicraft/item/FixedInventory.java @@ -0,0 +1,25 @@ +/* + * SPDX-FileCopyrightText: 2024 Minicraft+ contributors + * SPDX-License-Identifier: GPL-3.0-only + */ + +package minicraft.item; + +/** + * A general inventory implementation with fixed size (number of slots). + */ +public class FixedInventory extends BoundedInventory { + public static final int DEFAULT_SIZE = 27; + + protected final int maxSlots; + + public FixedInventory() { this(DEFAULT_SIZE); } + public FixedInventory(int maxSlots) { + this.maxSlots = maxSlots; + } + + @Override + public int getMaxSlots() { + return maxSlots; + } +} diff --git a/src/client/java/minicraft/item/Inventory.java b/src/client/java/minicraft/item/Inventory.java index ee6ed0b9..48e86705 100644 --- a/src/client/java/minicraft/item/Inventory.java +++ b/src/client/java/minicraft/item/Inventory.java @@ -5,25 +5,28 @@ import org.jetbrains.annotations.Nullable; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Random; -public class Inventory { - private final List items = new ArrayList<>(); // The list of items that is in the inventory. - - protected int maxItem = 27; - protected boolean unlimited = false; - - public int getMaxSlots() { - return maxItem; - } +/** + * An item storage space for general purposes, based on the maximum number of item stacks, + * and the default item maximum count for stackable items. + *

+ * For special conditional storage space for items, like custom maximum count of items and + * storage space that is variable to other conditions, slots or a custom implementation + * is recommended instead. + *

+ */ +public abstract class Inventory { + protected final List items = new ArrayList<>(); // The list of items that is in the inventory. /** * Returns all the items which are in this inventory. - * @return ArrayList containing all the items in the inventory. + * @return a read-only view of the internal list containing all the items in the inventory. */ - public List getItems() { - return new ArrayList<>(items); + public List getItemsView() { + return Collections.unmodifiableList(items); } public void clearInv() { @@ -72,74 +75,12 @@ public List add(@NotNull Item item, int num) { * @param item Item to be added. * @return the remaining item not being added; {@code null} if whole stack of items has been added successfully */ - 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 null; - } - - if (item instanceof StackableItem) { // If the item is a item... - StackableItem toTake = (StackableItem) item; // ...convert it into a StackableItem object. - 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 null; - } else { - toTake.count -= r; - stack.count += r; - } - } - } else { - stack.count += toTake.count; - return null; - } - } - } - - if (!unlimited) { - if (items.size() < maxItem) { - while (toTake.count > 0) { - if (items.size() == maxItem) return toTake; - StackableItem adding = toTake.copy(); - adding.count = Math.min(toTake.count, toTake.maxCount); - items.add(adding); // Add the item to the items list - toTake.count -= adding.count; - } - return null; - } else { - return toTake; - } - } else { - items.add(toTake); - return null; - } - } - - if (!unlimited) { - if (items.size() < maxItem) { - items.add(item); // Add the item to the items list - return null; - } else { - return item; - } - } else { - items.add(item); - return null; - } - } + public abstract @Nullable Item add(@Nullable Item item); /** * Removes items from your inventory; looks for stacks, and removes from each until reached count. returns amount removed. */ - private int removeFromStack(StackableItem given, int count) { + protected int removeFromStack(StackableItem given, int count) { int removed = 0; // To keep track of amount removed. for (int i = 0; i < items.size(); i++) { if (!(items.get(i) instanceof StackableItem)) continue; @@ -201,7 +142,7 @@ public void removeItems(Item given, int count) { } /** - * Returns the how many of an item you have in the inventory. + * Returns how many of an item you have in the inventory. */ public int count(Item given) { if (given == null) return 0; // null requests get no items. :) @@ -235,19 +176,6 @@ public String getItemData() { return itemdata.toString(); } - /** - * Replaces all the items in the inventory with the items in the string. - * @param items String representation of an inventory. - */ - public void updateInv(String items) { - clearInv(); - - if (items.length() == 0) return; // There are no items to add. - - for (String item : items.split(":")) // This still generates a 1-item array when "items" is blank... [""]. - add(Items.get(item)); - } - /** * Tries to add an item to the inventory. * @param random The {@code Random} number generator. diff --git a/src/client/java/minicraft/item/Items.java b/src/client/java/minicraft/item/Items.java index 2752d1f8..1c4ecbb3 100644 --- a/src/client/java/minicraft/item/Items.java +++ b/src/client/java/minicraft/item/Items.java @@ -140,10 +140,9 @@ public static CreativeModeInventory getCreativeModeInventory() { return new CreativeModeInventory(); } - public static class CreativeModeInventory extends Inventory { + public static class CreativeModeInventory extends UnlimitedInventory { CreativeModeInventory() { - unlimited = true; - items.forEach(i -> { + Items.items.forEach(i -> { if (!(i instanceof PowerGloveItem)) add(i.copy()); }); } @@ -153,4 +152,3 @@ public static Set getRegisteredItemKeys() { return items.stream().map(Item::getName).collect(Collectors.toCollection(TreeSet::new)); } } - diff --git a/src/client/java/minicraft/item/UnlimitedInventory.java b/src/client/java/minicraft/item/UnlimitedInventory.java new file mode 100644 index 00000000..c932261d --- /dev/null +++ b/src/client/java/minicraft/item/UnlimitedInventory.java @@ -0,0 +1,35 @@ +package minicraft.item; + +import minicraft.util.Logging; +import org.jetbrains.annotations.Nullable; + +/** + * A general inventory implementation basically without size limit (maximum number of slots). + */ +public class UnlimitedInventory extends Inventory { + @Override + 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 null; + } + + if (item instanceof StackableItem) { // If the item is a item... + StackableItem toTake = (StackableItem) item; // ...convert it into a StackableItem object. + for (Item value : items) { + if (toTake.stacksWith(value)) { + ((StackableItem) value).count += toTake.count; + return null; + } + } + + items.add(toTake); + return null; + } + + items.add(item); + return null; + } +} diff --git a/src/client/java/minicraft/saveload/LegacyLoad.java b/src/client/java/minicraft/saveload/LegacyLoad.java index 11dd6a4d..cc970a7e 100644 --- a/src/client/java/minicraft/saveload/LegacyLoad.java +++ b/src/client/java/minicraft/saveload/LegacyLoad.java @@ -11,6 +11,7 @@ import minicraft.entity.furniture.DeathChest; import minicraft.entity.furniture.DungeonChest; import minicraft.entity.furniture.Lantern; +import minicraft.entity.furniture.RewardChest; import minicraft.entity.furniture.Spawner; import minicraft.entity.furniture.Tnt; import minicraft.entity.mob.AirWizard; @@ -32,7 +33,6 @@ import minicraft.item.Items; import minicraft.item.PotionItem; import minicraft.item.PotionType; -import minicraft.item.StackableItem; import minicraft.level.Level; import minicraft.level.tile.Tiles; import minicraft.screen.LoadingDisplay; @@ -46,6 +46,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashSet; import java.util.List; /// This class is simply a way to seperate all the old, compatibility complications into a seperate file. @@ -60,12 +61,13 @@ public class LegacyLoad { public boolean hasloadedbigworldalready; @Nullable Version worldVer = null; - private DeathChest deathChest; + private final HashSet overflowingItems; { data = new ArrayList<>(); extradata = new ArrayList<>(); hasloadedbigworldalready = false; + overflowingItems = new HashSet<>(); } public LegacyLoad(String worldname) { @@ -80,9 +82,9 @@ public LegacyLoad(String worldname) { loadEntities("Entities", Game.player); LoadingDisplay.setPercentage(0); // reset - if (deathChest != null && deathChest.getInventory().invSize() > 0) { - Game.player.getLevel().add(deathChest, Game.player.x, Game.player.y); - Logging.SAVELOAD.debug("Added DeathChest which contains exceed items."); + if (!overflowingItems.isEmpty()) { + Game.player.getLevel().add(new RewardChest(overflowingItems), Game.player.x, Game.player.y); + Logging.SAVELOAD.debug("Added a RewardChest containing inventory-overflowing items."); } } @@ -262,7 +264,6 @@ public void loadPlayer(String filename, Player player) { } public void loadInventory(String filename, Inventory inventory) { - deathChest = new DeathChest(); loadFromFile(location + filename + extension); inventory.clearInv(); @@ -295,7 +296,7 @@ public void loadItemToInventory(String item, Inventory inventory) { private void loadItem(Inventory inventory, Item item) { if (inventory.add(item) != null) { - deathChest.getInventory().add(item.copy()); + overflowingItems.add(item.copy()); } } diff --git a/src/client/java/minicraft/saveload/Load.java b/src/client/java/minicraft/saveload/Load.java index ea9fe64c..a94215b4 100644 --- a/src/client/java/minicraft/saveload/Load.java +++ b/src/client/java/minicraft/saveload/Load.java @@ -21,6 +21,7 @@ import minicraft.entity.furniture.KnightStatue; import minicraft.entity.furniture.Lantern; import minicraft.entity.furniture.RepairBench; +import minicraft.entity.furniture.RewardChest; import minicraft.entity.furniture.Spawner; import minicraft.entity.furniture.Tnt; import minicraft.entity.mob.AirWizard; @@ -98,6 +99,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Stack; @@ -121,11 +123,12 @@ public class Load { private @Nullable Version worldVer = null; - private DeathChest deathChest; + private final HashSet overflowingItems; { data = new ArrayList<>(); extradata = new ArrayList<>(); + overflowingItems = new HashSet<>(); } public Load(String worldname) @@ -265,9 +268,9 @@ public Load(String worldname, boolean loadGame) loadInventory("Inventory", Game.player.getInventory()); loadPlayer("Player", Game.player); - if (deathChest != null && deathChest.getInventory().invSize() > 0) { - Game.player.getLevel().add(deathChest, Game.player.x, Game.player.y); - Logging.SAVELOAD.debug("Added DeathChest which contains exceed items."); + if (!overflowingItems.isEmpty()) { + Game.player.getLevel().add(new RewardChest(overflowingItems), Game.player.x, Game.player.y); + Logging.SAVELOAD.debug("Added a RewardChest containing inventory-overflowing items."); } if (worldVer.compareTo(new Version("2.2.0-dev3")) < 0) { @@ -968,7 +971,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).forEach(deathChest.getInventory()::add); + overflowingItems.addAll(player.getInventory().add(Items.get("arrow"), arrowCount)); } Game.currentLevel = Integer.parseInt(data.remove(0)); @@ -1103,7 +1106,6 @@ protected static String subOldName(String name, Version worldVer) { } public void loadInventory(String filename, Inventory inventory) { - deathChest = new DeathChest(); loadFromFile(location + filename + extension); loadInventory(inventory, data); } @@ -1146,7 +1148,7 @@ public void loadInventory(Inventory inventory, List data) { private void loadItem(Inventory inventory, Item item) { if (inventory.add(item) != null) { - deathChest.getInventory().add(item.copy()); + overflowingItems.add(item.copy()); } } @@ -1289,6 +1291,7 @@ public static Entity loadEntity(String entityData, Version worldVer, boolean isL List chestInfo = info.subList(2, info.size() - 1); int endIdx = chestInfo.size() - (isDeathChest || isDungeonChest ? 1 : 0); + ArrayList chestItems = new ArrayList<>(); for (int idx = 0; idx < endIdx; idx++) { String itemData = subOldName(chestInfo.get(idx), worldVer); @@ -1296,7 +1299,13 @@ public static Entity loadEntity(String entityData, Version worldVer, boolean isL if (itemData.contains("Totem of Wind")) continue; Item item = Items.get(itemData); - chest.getInventory().add(item); + chestItems.add(item); + } + + if (newEntity instanceof RewardChest) { + newEntity = new RewardChest(chestItems); + } else { + chestItems.forEach(chest.getInventory()::add); } if (isDeathChest) { @@ -1306,8 +1315,6 @@ public static Entity loadEntity(String entityData, Version worldVer, boolean isL if (((DungeonChest) chest).isLocked()) World.levels[Integer.parseInt(info.get(info.size() - 1))].chestCount++; } - - newEntity = chest; } else if (newEntity instanceof Spawner) { MobAi mob = (MobAi) getEntity(info.get(2).substring(info.get(2).lastIndexOf(".") + 1), Integer.parseInt(info.get(3))); if (mob != null) @@ -1395,6 +1402,8 @@ private static Entity getEntity(String string, int mobLevel) { return new DeathChest(); case "DungeonChest": return new DungeonChest(null); + case "RewardChest": + return new RewardChest(null); case "Anvil": return new Crafter(Crafter.Type.Anvil); case "Enchanter": diff --git a/src/client/java/minicraft/screen/ContainerDisplay.java b/src/client/java/minicraft/screen/ContainerDisplay.java index e30ffb20..d78d1483 100644 --- a/src/client/java/minicraft/screen/ContainerDisplay.java +++ b/src/client/java/minicraft/screen/ContainerDisplay.java @@ -12,6 +12,7 @@ import minicraft.gfx.Rectangle; import minicraft.gfx.Screen; import minicraft.gfx.SpriteManager; +import minicraft.item.BoundedInventory; import minicraft.item.Inventory; import minicraft.item.Item; import minicraft.item.StackableItem; @@ -105,30 +106,33 @@ public void render(Screen screen) { 0, 4, 5, capLeft, Color.GRAY); } - // RHS is not focused - Rectangle boundsRight = menus[1].getBounds(); - int sizeRight = chest.getInventory().invSize(); - int capRight = chest.getInventory().getMaxSlots(); - // Minimized counter - if (sizeRight < 10) { // no worry yet, really - // Background - screen.render(null, boundsRight.getLeft() + 4, boundsRight.getTop() - 1, - 0, 12, 4, 9, counterSheet); - // Skips the middle part as that is for more digits - screen.render(null, boundsRight.getLeft() + 8, boundsRight.getTop() - 1, - 8, 12, 4, 9, counterSheet); - - // Digits - renderCounterNumber(screen, boundsRight.getLeft() + 4 + 2, boundsRight.getTop() + 1, - 0, 4, 5, sizeRight, fadeColor(colorByHeaviness(calculateHeaviness(sizeRight, capRight), false))); - } else { - // Background - screen.render(null, boundsRight.getLeft() + 4, boundsRight.getTop() - 1, - 0, 12, 12, 9, counterSheet); + // Later when more configs can be made into chest inventory, this can be linked directly. + if (chest.getInventory() instanceof BoundedInventory) { // At the moment, simplify the situation + // RHS is not focused + Rectangle boundsRight = menus[1].getBounds(); + int sizeRight = chest.getInventory().invSize(); + int capRight = ((BoundedInventory) chest.getInventory()).getMaxSlots(); + // Minimized counter + if (sizeRight < 10) { // no worry yet, really + // Background + screen.render(null, boundsRight.getLeft() + 4, boundsRight.getTop() - 1, + 0, 12, 4, 9, counterSheet); + // Skips the middle part as that is for more digits + screen.render(null, boundsRight.getLeft() + 8, boundsRight.getTop() - 1, + 8, 12, 4, 9, counterSheet); + + // Digits + renderCounterNumber(screen, boundsRight.getLeft() + 4 + 2, boundsRight.getTop() + 1, + 0, 4, 5, sizeRight, fadeColor(colorByHeaviness(calculateHeaviness(sizeRight, capRight), false))); + } else { + // Background + screen.render(null, boundsRight.getLeft() + 4, boundsRight.getTop() - 1, + 0, 12, 12, 9, counterSheet); - // Digits - renderCounterNumber(screen, boundsRight.getLeft() + 4 + 2, boundsRight.getTop() + 1, - 0, 4, 5, sizeRight, fadeColor(colorByHeaviness(calculateHeaviness(sizeRight, capRight), false))); + // Digits + renderCounterNumber(screen, boundsRight.getLeft() + 4 + 2, boundsRight.getTop() + 1, + 0, 4, 5, sizeRight, fadeColor(colorByHeaviness(calculateHeaviness(sizeRight, capRight), false))); + } } } else { // assert selection == 1 // LHS is not focused @@ -157,34 +161,36 @@ public void render(Screen screen) { 0, 4, 5, sizeLeft, fadeColor(colorByHeaviness(calculateHeaviness(sizeLeft, capLeft), false))); } - // RHS is focused - Rectangle boundsRight = menus[1].getBounds(); - int sizeRight = chest.getInventory().invSize(); - int capRight = chest.getInventory().getMaxSlots(); - // Expanded counter (background horizontally mirrored) - if (sizeRight < 10) { - // Background - screen.render(null, boundsRight.getLeft() - 2 + (20 - 5), boundsRight.getTop() - 3, - 12, 12, 3, 13, counterSheet, 1); - // Skips the middle part as that is for more digits - screen.render(null, boundsRight.getLeft() - 2, boundsRight.getTop() - 3, - 20, 12, 15, 13, counterSheet, 1); - - // Digits - renderCounterNumber(screen, boundsRight.getLeft() - 2 + 11, boundsRight.getTop() - 1, - 5, 5, 7, sizeRight, colorByHeaviness(calculateHeaviness(sizeRight, capRight), true)); - renderCounterNumber(screen, boundsRight.getLeft(), boundsRight.getTop() + 3, - 0, 4, 5, capRight, Color.GRAY); - } else { - // Background - screen.render(null, boundsRight.getLeft() - 2, boundsRight.getTop() - 3, - 12, 12, 23, 13, counterSheet, 1); - - // Digits - renderCounterNumber(screen, boundsRight.getLeft() - 2 + 11, boundsRight.getTop() - 1, - 5, 5, 7, sizeRight, colorByHeaviness(calculateHeaviness(sizeRight, capRight), true)); - renderCounterNumber(screen, boundsRight.getLeft(), boundsRight.getTop() + 3, - 0, 4, 5, capRight, Color.GRAY); + if (chest.getInventory() instanceof BoundedInventory) { // At the moment, simplify the situation + // RHS is focused + Rectangle boundsRight = menus[1].getBounds(); + int sizeRight = chest.getInventory().invSize(); + int capRight = ((BoundedInventory) chest.getInventory()).getMaxSlots(); + // Expanded counter (background horizontally mirrored) + if (sizeRight < 10) { + // Background + screen.render(null, boundsRight.getLeft() - 2 + (20 - 5), boundsRight.getTop() - 3, + 12, 12, 3, 13, counterSheet, 1); + // Skips the middle part as that is for more digits + screen.render(null, boundsRight.getLeft() - 2, boundsRight.getTop() - 3, + 20, 12, 15, 13, counterSheet, 1); + + // Digits + renderCounterNumber(screen, boundsRight.getLeft() - 2 + 11, boundsRight.getTop() - 1, + 5, 5, 7, sizeRight, colorByHeaviness(calculateHeaviness(sizeRight, capRight), true)); + renderCounterNumber(screen, boundsRight.getLeft(), boundsRight.getTop() + 3, + 0, 4, 5, capRight, Color.GRAY); + } else { + // Background + screen.render(null, boundsRight.getLeft() - 2, boundsRight.getTop() - 3, + 12, 12, 23, 13, counterSheet, 1); + + // Digits + renderCounterNumber(screen, boundsRight.getLeft() - 2 + 11, boundsRight.getTop() - 1, + 5, 5, 7, sizeRight, colorByHeaviness(calculateHeaviness(sizeRight, capRight), true)); + renderCounterNumber(screen, boundsRight.getLeft(), boundsRight.getTop() + 3, + 0, 4, 5, capRight, Color.GRAY); + } } } } diff --git a/src/client/java/minicraft/screen/InventoryMenu.java b/src/client/java/minicraft/screen/InventoryMenu.java index 34fa9c92..debb68f2 100644 --- a/src/client/java/minicraft/screen/InventoryMenu.java +++ b/src/client/java/minicraft/screen/InventoryMenu.java @@ -21,7 +21,7 @@ class InventoryMenu extends ItemListMenu { 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); + super(ItemListMenu.getBuilder(entryPos), ItemEntry.useItems(inv.getItemsView()), title); this.inv = inv; this.holder = holder; this.title = title; @@ -31,7 +31,7 @@ class InventoryMenu extends ItemListMenu { } InventoryMenu(InventoryMenu model) { - super(ItemListMenu.getBuilder(model.entryPos), ItemEntry.useItems(model.inv.getItems()), model.title); + super(ItemListMenu.getBuilder(model.entryPos), ItemEntry.useItems(model.inv.getItemsView()), model.title); this.inv = model.inv; this.holder = model.holder; this.creativeInv = model.creativeInv; diff --git a/src/client/java/minicraft/util/AdvancementElement.java b/src/client/java/minicraft/util/AdvancementElement.java index 5006577f..9c51495a 100644 --- a/src/client/java/minicraft/util/AdvancementElement.java +++ b/src/client/java/minicraft/util/AdvancementElement.java @@ -1,8 +1,8 @@ package minicraft.util; import minicraft.core.World; -import minicraft.entity.furniture.Chest; -import minicraft.item.Inventory; +import minicraft.entity.furniture.RewardChest; +import minicraft.item.BoundedInventory; import minicraft.item.Item; import minicraft.item.Items; import minicraft.item.Recipe; @@ -351,16 +351,12 @@ public void update() { protected void sendRewards() { if (rewards != null) { ArrayList items = rewards.getItems(); - if (items.size() > 0) { - Chest chest = new Chest("Rewards"); - chest.x = World.player.x; - chest.y = World.player.y; - for (Item item : items) chest.getInventory().add(item); - World.levels[World.currentLevel].add(chest); + if (!items.isEmpty()) { + World.levels[World.currentLevel].add(new RewardChest(items), World.player.x, World.player.y); } ArrayList recipes = rewards.getRecipe(); - if (recipes.size() > 0) { + if (!recipes.isEmpty()) { recipes.forEach(CraftingDisplay::unlockRecipe); } } @@ -927,8 +923,8 @@ public static class InventoryChangedTriggerConditions extends AdvancementTrigger private final ArrayList items = new ArrayList<>(); private final int maxSlots; - public InventoryChangedTriggerConditions(Inventory inventory) { - items.addAll(inventory.getItems()); + public InventoryChangedTriggerConditions(BoundedInventory inventory) { + items.addAll(inventory.getItemsView()); maxSlots = inventory.getMaxSlots(); } }