Class InventoryComponent

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

public class InventoryComponent extends Component
Tracks and manages a player's possessions, split into three logical bags: - INVENTORY : regular items picked up during gameplay (keys, etc.) - UPGRADES : upgrade tokens/items - OBJECTIVES : quest/goal items Each bag is a multiset (item id -> stack count).
  • Constructor Details

    • InventoryComponent

      public InventoryComponent()
      Creates a new, empty component with three empty bags.
    • InventoryComponent

      public InventoryComponent(InventoryComponent other)
      Copy constructor. Creates deep copies of the three internal maps.
      Parameters:
      other - another InventoryComponent to copy
      Throws:
      NullPointerException - if other is null
  • Method Details

    • getInventory

      public Map<String,Integer> getInventory()
      Returns:
      unmodifiable view of the INVENTORY bag
    • getUpgrades

      public Map<String,Integer> getUpgrades()
      Returns:
      unmodifiable view of the UPGRADES bag
    • getObjectives

      public Map<String,Integer> getObjectives()
      Returns:
      unmodifiable view of the OBJECTIVES bag
    • getInventoryCopy

      public Map<String,Integer> getInventoryCopy()
      Returns:
      copy of the INVENTORY bag
    • getUpgradesCopy

      public Map<String,Integer> getUpgradesCopy()
      Returns:
      copy of the UPGRADES bag
    • setInventory

      public void setInventory(Map<String,Integer> inventory)
      Add all items from a passed inventory.
      Parameters:
      inventory - - passed inventory.
    • setUpgrades

      public void setUpgrades(Map<String,Integer> upgrades)
      Add all passed upgrades.
      Parameters:
      upgrades - - passed upgrades.
    • addItem

      public void addItem(String itemId)
      Adds one instance of the given item to the inventory via addItems
      Parameters:
      itemId - non-null item identifier (e.g., "key:door")
      Throws:
      NullPointerException - if itemId is null
    • addItems

      public void addItems(String itemId, int amount)
      Adds amount instances of itemId to the specified bag.
      Parameters:
      itemId - non-null item identifier
      amount - number of instances to add (must be >= 0)
      Throws:
      NullPointerException - if bag or itemId is null
      IllegalArgumentException - if amount is negative
    • addItem

      public void addItem(String itemId, Map<String,Map<String,String>> effectParams)
      Adds one instance of the given item and passes per-instance effect params. If the item is auto-consumable, effects are applied with the provided params. If non-auto, it’s stored normally.
      Parameters:
      itemId - item identifier (e.g., "objective")
      effectParams - effectType -> (key -> value), e.g. {"objective":{"target":"dash"}}
    • addItem

      @Deprecated public void addItem(InventoryComponent.Bag bag, String itemId)
      Deprecated.
      Add a single item to a specific bag.
    • addItems

      @Deprecated public void addItems(InventoryComponent.Bag bag, String itemId, int amount)
      Deprecated.
      Add multiple items to a specific bag.

      If the config is non-auto-consumable, items are stored in the given bag. If the config is auto-consumable, effects are applied immediately (the bag is ignored), exactly amount times.

      Parameters:
      bag - destination bag (must not be null) for non-auto-consumables
      itemId - item id (must not be null)
      amount - number of items to add (must be > 0)
      Throws:
      NullPointerException - if bag or itemId is null
      IllegalArgumentException - if amount /le 0 or no config is found
    • addDirect

      public void addDirect(InventoryComponent.Bag bag, String itemId, int amount)
      Adds the specified item directly into the given bag without applying any collectable effects or config lookups.

      Intended for internal use by effects (e.g., upgrades) that must place tokens in inventory without re-triggering the effect pipeline.

      Parameters:
      bag - the inventory bag to add the item to
      itemId - the identifier of the item to add
      amount - how many items to add (must be > 0)
      Throws:
      NullPointerException - if itemId is null
      IllegalArgumentException - if amount <= 0
    • hasItem

      public boolean hasItem(InventoryComponent.Bag bag, String itemId)
      Parameters:
      bag - which bag to query
      itemId - non-null identifier
      Returns:
      true if the bag contains at least one instance
      Throws:
      NullPointerException - if bag or itemId is null
    • getItemCount

      public int getItemCount(InventoryComponent.Bag bag, String itemId)
      Returns the stack count for itemId in the given bag.
      Parameters:
      bag - which bag to query
      itemId - non-null identifier
      Returns:
      count (0 if absent)
      Throws:
      NullPointerException - if bag or itemId is null
    • getTotalCount

      public int getTotalCount(InventoryComponent.Bag bag)
      Parameters:
      bag - which bag to query
      Returns:
      total count of all instances stored in the bag
      Throws:
      NullPointerException - if bag is null
    • removeItem

      public void removeItem(InventoryComponent.Bag bag, String itemId)
      Removes all instances of itemId from the specified bag.
      Parameters:
      bag - which bag to modify
      itemId - non-null identifier to remove
      Throws:
      NullPointerException - if bag or itemId is null
    • resetBag

      public void resetBag(InventoryComponent.Bag bag)
      Clears all items from the specified bag. 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 effect(s)

      Parameters:
      bag - which bag to clear
      Throws:
      NullPointerException - if bag is null
    • useItem

      public boolean useItem(InventoryComponent.Bag bag, String itemId)
      Consumes one instance of itemId from the specified bag (if present).
      Parameters:
      bag - which bag to modify
      itemId - non-null identifier to decrement
      Returns:
      true if one instance was consumed; false if none were available
      Throws:
      NullPointerException - if bag or itemId is null
    • useItems

      public int useItems(InventoryComponent.Bag bag, String itemId, int amount)
      Consumes up to "amount" instances of itemId from the specified bag 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:
      bag - which bag to modify
      itemId - non-null identifier to decrement
      amount - desired number to consume (must be >= 0)
      Returns:
      the number of instances actually consumed (0..amount)
      Throws:
      NullPointerException - if bag or itemId is null
      IllegalArgumentException - if amount is negative
    • existsAnywhere

      public boolean existsAnywhere(String itemId)
      Returns true if the item exists in ANY bag (count > 0).
      Parameters:
      itemId - non-null identifier
      Returns:
      true if present in inventory OR upgrades OR objectives
      Throws:
      NullPointerException - if itemId is null
    • getGrandTotalCount

      public int getGrandTotalCount()
      Returns:
      total count across all bags (inventory + upgrades + objectives)
    • hasItem

      @Deprecated public boolean hasItem(String itemId)
      Deprecated.
      Prefer bagged version: hasItem(Bag.INVENTORY, itemId).
    • getItemCount

      @Deprecated public int getItemCount(String itemId)
      Deprecated.
      Prefer bagged version: getItemCount(Bag.INVENTORY, itemId).
    • getTotalItemCount

      @Deprecated public int getTotalItemCount()
      Deprecated.
      Prefer bagged version: getTotalCount(Bag.INVENTORY).
    • removeItem

      @Deprecated public void removeItem(String itemId)
      Deprecated.
      Prefer bagged version: removeItem(Bag.INVENTORY, itemId).
    • useItem

      @Deprecated public void useItem(String itemId)
      Deprecated.
      Prefer bagged version: useItem(Bag.INVENTORY, itemId).
    • useItems

      @Deprecated public void useItems(String itemId, int amount)
      Deprecated.
      Prefer bagged version: useItems(Bag.INVENTORY, itemId, amount).
    • printItems

      public String printItems()
      Turns the contents of the bags of the inventory into a readable string - used for shell commands.
      Returns:
      String - The constructed string of bag contents