Skip to content
16 changes: 11 additions & 5 deletions src/client/java/minicraft/entity/furniture/Chest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import minicraft.entity.mob.Player;
import minicraft.gfx.SpriteLinker.LinkedSprite;
import minicraft.gfx.SpriteLinker.SpriteType;
import minicraft.item.BoundedInventory;
import minicraft.item.FixedInventory;
import minicraft.item.Inventory;
import minicraft.item.Item;
import minicraft.item.Items;
Expand All @@ -19,24 +21,28 @@
import java.util.Random;

public class Chest extends Furniture implements ItemHolder {
private Inventory inventory; // Inventory of the chest
protected static final LinkedSprite defaultSprite = new LinkedSprite(SpriteType.Item, "chest");
protected final Inventory inventory; // Inventory of the chest

public Chest() {
this("Chest");
}

public Chest(String name) {
this(name, new LinkedSprite(SpriteType.Item, "chest"));
this(name, defaultSprite);
}

/**
* Creates a chest with a custom name.
* @param name Name of chest.
*/
public Chest(String name, LinkedSprite itemSprite) {
super(name, new LinkedSprite(SpriteType.Entity, "chest"), 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 LinkedSprite(SpriteType.Entity, "chest"), itemSprite, 3, 3); // Name of the chest
this.inventory = inventory; // Initialize the inventory.
}

/**
Expand Down Expand Up @@ -84,7 +90,7 @@ public Inventory getInventory() {
@Override
public void die() {
if (level != null) {
List<Item> items = inventory.getItems();
List<Item> items = inventory.getItemsView();
level.dropItem(x, y, items.toArray(new Item[0]));
}
super.die();
Expand Down
19 changes: 4 additions & 15 deletions src/client/java/minicraft/entity/furniture/DeathChest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import minicraft.gfx.SpriteLinker.SpriteType;
import minicraft.item.Inventory;
import minicraft.item.Item;
import minicraft.item.StackableItem;
import minicraft.item.UnlimitedInventory;

public class DeathChest extends Chest {
private static LinkedSprite normalSprite = new LinkedSprite(SpriteType.Entity, "chest");
Expand All @@ -22,15 +22,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 LinkedSprite(SpriteType.Item, "dungeon_chest"));
super(new UnlimitedInventory(), "Death Chest", new LinkedSprite(SpriteType.Item, "dungeon_chest"));
this.sprite = normalSprite;

/// Set the expiration time based on the world difficulty.
Expand All @@ -47,7 +44,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());
}
}
Expand Down Expand Up @@ -95,14 +92,11 @@ public boolean use(Player player) {
return false;
} // can't open it, just walk into it.

public void take(Player player) {
} // can't grab a death chest.

@Override
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.notifications.add("Your inventory is full!");
return;
Expand All @@ -115,9 +109,4 @@ public void touchedBy(Entity other) {
Game.notifications.add(Localization.getLocalized("minicraft.notification.death_chest_retrieved"));
}
}

@Override
public Inventory getInventory() {
return inventory;
}
}
38 changes: 38 additions & 0 deletions src/client/java/minicraft/entity/furniture/RewardChest.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
14 changes: 5 additions & 9 deletions src/client/java/minicraft/entity/mob/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
import minicraft.gfx.SpriteLinker.LinkedSprite;
import minicraft.gfx.SpriteLinker.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;
Expand Down Expand Up @@ -89,7 +91,7 @@ public class Player extends Mob implements ItemHolder, ClientTickable {
public static LinkedSprite[][] sprites;
public static LinkedSprite[][] carrySprites;

private final Inventory inventory;
private final BoundedInventory inventory;

public Item activeItem;
Item attackItem; // attackItem is useful again b/c of the power glove.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<>();
Expand Down Expand Up @@ -1236,7 +1232,7 @@ public void remove() {
}

@Override
public Inventory getInventory() {
public BoundedInventory getInventory() {
return inventory;
}

Expand Down
65 changes: 65 additions & 0 deletions src/client/java/minicraft/item/BoundedInventory.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
25 changes: 25 additions & 0 deletions src/client/java/minicraft/item/FixedInventory.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading