Class InventoryComponent

java.lang.Object
com.csse3200.game.components.Component
com.csse3200.game.components.player.InventoryComponent

public class InventoryComponent extends Component
A component to track and manage a player's inventory, including stacking items. Inventory is set up as a multiset of item ids (stack counts).
  • Constructor Details

    • InventoryComponent

      public InventoryComponent()
    • InventoryComponent

      public InventoryComponent(InventoryComponent other)
  • Method Details

    • getInventory

      public Map<String,Integer> getInventory()
      Read only view of the inventory for UI rendering
    • addItem

      public void addItem(String itemId)
      Adds one instance of the given item to the inventory.

      If the item's config has autoConsume == true, its effects are applied immediately and the item is not stored. Otherwise the item is added to the inventory stack; if it is not yet present, a new stack is created with count 1.

      Parameters:
      itemId - non-null identifier (e.g., "key:red").
    • addItems

      public void addItems(String itemId, int amount)
      Adds amount instances of the given item (stacking).

      If autoConsume == true, effects are applied once per instance and nothing is stored. Otherwise the inventory count is increased by amount, creating a new stack if needed.

      Parameters:
      itemId - non-null identifier (e.g., "key:red").
      amount - number of items to add (must be ≥ 1).
      Throws:
      IllegalArgumentException - if amount < 1.
    • hasItem

      public boolean hasItem(String itemId)
      Returns whether at least one instance of itemId exists in the inventory.
      Parameters:
      itemId - non-null identifier of item.
      Returns:
      true if the count is > 0; otherwise false.
    • getItemCount

      public int getItemCount(String itemId)
      Gets the current stack count for itemId, where the stack count is the number of instances of an item in the inventory.
      Parameters:
      itemId - non-null identifier of item.
      Returns:
      the stored count, or 0 if the item is absent
    • getTotalItemCount

      public int getTotalItemCount()
      Gets the count for all instances of items in the inventory.
      Returns:
      the stored count, or 0 if the inventory is empty
    • removeItem

      public void removeItem(String itemId)
      Removes all items with itemId from the inventory.
      Parameters:
      itemId - non-null identifier to remove.
    • useItem

      public void useItem(String itemId)
      Uses one instance of itemId.

      Decrements inventory item count by 1 if the item is present in the inventory and the number of items is > 0. Also applies its effects.s

      Parameters:
      itemId - non-null identifier of item to decrement.
    • useItems

      public void useItems(String itemId, int amount)
      Consumes a specified number of items from the inventory.

      This method will decrement the count of the given itemId until either the requested amount has been used or the available quantity is depleted. If the item does not exist in the inventory or has a count of zero, no changes occur. Looks up the item's config and applies its effects. If no effect can be applied (e.g., using a heart at full HP), the item is not consumed.

      Parameters:
      itemId - the identifier of the item to use
      amount - the number of items to attempt to consume; if greater than the available count, all available items are consumed