Skip to content

Commit

Permalink
Update inventory implementation style
Browse files Browse the repository at this point in the history
  • Loading branch information
BenCheung0422 committed Sep 3, 2024
1 parent bb50a10 commit e40db13
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 27 deletions.
5 changes: 3 additions & 2 deletions src/client/java/minicraft/entity/furniture/Chest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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 Down Expand Up @@ -36,7 +37,7 @@ public Chest(String name) {
* @param name Name of chest.
*/
public Chest(String name, LinkedSprite itemSprite) {
this(new BoundedInventory(), name, itemSprite); // Default with bounded inventory
this(new FixedInventory(), name, itemSprite); // Default with bounded inventory
}

protected Chest(Inventory inventory, String name, LinkedSprite itemSprite) {
Expand Down Expand Up @@ -89,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
6 changes: 2 additions & 4 deletions src/client/java/minicraft/entity/furniture/DeathChest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
import minicraft.gfx.Screen;
import minicraft.gfx.SpriteLinker.LinkedSprite;
import minicraft.gfx.SpriteLinker.SpriteType;
import minicraft.item.BoundedInventory;
import minicraft.item.Inventory;
import minicraft.item.Item;
import minicraft.item.StackableItem;
import minicraft.item.UnlimitedInventory;

public class DeathChest extends Chest {
Expand Down Expand Up @@ -46,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 @@ -98,7 +96,7 @@ public boolean use(Player player) {
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 Down
3 changes: 2 additions & 1 deletion src/client/java/minicraft/entity/mob/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
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 @@ -147,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 BoundedInventory() { // 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
22 changes: 9 additions & 13 deletions src/client/java/minicraft/item/BoundedInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@
import minicraft.util.Logging;
import org.jetbrains.annotations.Nullable;

public class BoundedInventory extends Inventory {
public static final int DEFAULT_SIZE = 27;

protected final int maxItem;

public BoundedInventory() { this(DEFAULT_SIZE); }
public BoundedInventory(int maxItem) {
this.maxItem = maxItem;
}

public int getMaxSlots() {
return maxItem;
}
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) {
Expand All @@ -26,6 +21,7 @@ public int getMaxSlots() {
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) {
Expand Down
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;
}
}
16 changes: 13 additions & 3 deletions src/client/java/minicraft/item/Inventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,28 @@
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
* An item storage space for general purposes, based on the maximum number of item stacks,
* and the default item maximum count for stackable items.
* <p>
* 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.
* </p>
*/
public abstract class Inventory {
protected final List<Item> 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<Item> getItems() {
return new ArrayList<>(items);
public List<Item> getItemsView() {
return Collections.unmodifiableList(items);
}

public void clearInv() {
Expand Down
3 changes: 3 additions & 0 deletions src/client/java/minicraft/item/UnlimitedInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
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) {
Expand Down
4 changes: 2 additions & 2 deletions src/client/java/minicraft/screen/InventoryMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/client/java/minicraft/util/AdvancementElement.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package minicraft.util;

import minicraft.core.World;
import minicraft.entity.furniture.Chest;
import minicraft.entity.furniture.RewardChest;
import minicraft.item.BoundedInventory;
import minicraft.item.Item;
Expand Down Expand Up @@ -925,7 +924,7 @@ public static class InventoryChangedTriggerConditions extends AdvancementTrigger
private final int maxSlots;

public InventoryChangedTriggerConditions(BoundedInventory inventory) {
items.addAll(inventory.getItems());
items.addAll(inventory.getItemsView());
maxSlots = inventory.getMaxSlots();
}
}
Expand Down

0 comments on commit e40db13

Please sign in to comment.