Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorganize Inventory code and add Reward Chest #13

Merged
merged 12 commits into from
Jan 20, 2025
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.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;
Expand All @@ -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
Expand Down Expand Up @@ -81,7 +87,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
15 changes: 4 additions & 11 deletions src/client/java/minicraft/entity/furniture/DeathChest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.
Expand All @@ -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());
}
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}
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 @@ -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;
Expand Down Expand Up @@ -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;

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 @@ -1176,7 +1172,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