Class AbstractItem

java.lang.Object
com.csse3200.game.inventory.items.AbstractItem
Direct Known Subclasses:
ConsumableItem

public abstract class AbstractItem extends Object
Abstract base class for items that can be used by a player.

This class provides a common implementation for an item including default behavior for common properties like name and limit. Subclasses must provide their own implementation for the useItem method, which defines how the item can be used and must define what ItemUsageContext should be input to useItem.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected String
     
    protected final int
     
    protected final int
     
    protected String
     
    protected boolean
     
    protected boolean
     
    protected int
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    AbstractItem(String name, int itemCode)
    Constructs an AbstractItem with the specified name and item code.
    protected
    AbstractItem(String name, int itemCode, int limit, int quantity)
    Constructs an AbstractItem with the specified name, item code, limit and initial quantity.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(int n)
    Increases the current quantity of the item by n.
    Returns a description of how the current item can be used and what it does
    int
    Gets the item code of the item.
    int
    Gets the maximum number of this item that can be held in a `stack`.
    Gets the name of the item.
    int
    Gets the quantity of the item currently stacked.
    Retrieves the texture of the object.
    boolean
    Returns whether the quantity of this item has reached 0 (ie is consumed).
    int
    Checks the number of this item that can be further stacked.
    boolean
    Whether an item can only be used in combat
    boolean
    Whether an item can only be used on the map
    void
    setDescription(String description)
    If what item can do, update the description of current item
    protected void
    setTexturePath(String texturePath)
    Sets the path to the texture of the object using the provided file path.
    abstract void
    Uses the item with the provided ItemUsageContext and returns a result.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • name

      protected String name
    • itemCode

      protected final int itemCode
    • limit

      protected final int limit
    • quantity

      protected int quantity
    • description

      protected String description
    • onlyCombatItem

      protected boolean onlyCombatItem
    • onlyMapItem

      protected boolean onlyMapItem
  • Constructor Details

    • AbstractItem

      protected AbstractItem(String name, int itemCode)
      Constructs an AbstractItem with the specified name and item code. Defaults to single non-stackable item.
      Parameters:
      name - name of the item
      itemCode - unique item code (up to name)

      Note - all concrete subclasses must provide a unique itemCode

    • AbstractItem

      protected AbstractItem(String name, int itemCode, int limit, int quantity)
      Constructs an AbstractItem with the specified name, item code, limit and initial quantity.
      Parameters:
      name - name of the item
      itemCode - unique item code (up to name)
      limit - the maximum size of the item stack
      quantity - the initial starting quantity of the item
      Throws:
      IllegalArgumentException - if limit or quantity is negative, or quantity > limit

      Note - all concrete subclasses must provide a unique itemCode

  • Method Details

    • getName

      public String getName()
      Gets the name of the item.
      Returns:
      the name of the item
    • onlyCombatItem

      public boolean onlyCombatItem()
      Whether an item can only be used in combat
    • onlyMapItem

      public boolean onlyMapItem()
      Whether an item can only be used on the map
    • getItemCode

      public int getItemCode()
      Gets the item code of the item.
      Returns:
      the code of the item
    • getLimit

      public int getLimit()
      Gets the maximum number of this item that can be held in a `stack`.
      Returns:
      the stack limit of the item
    • getQuantity

      public int getQuantity()
      Gets the quantity of the item currently stacked.
      Returns:
      the quantity of the item
    • getDescription

      public String getDescription()
      Returns a description of how the current item can be used and what it does
      Returns:
      description of item
    • setDescription

      public void setDescription(String description)
      If what item can do, update the description of current item
      Parameters:
      description - description of item
    • numAddable

      public int numAddable()
      Checks the number of this item that can be further stacked.
      Returns:
      how much the quantity of this item can increase by
    • add

      public void add(int n)
      Increases the current quantity of the item by n.
      Parameters:
      n - - number of item to add
      Throws:
      IllegalArgumentException - if the number of items cannot be increased by n
    • useItem

      public abstract void useItem(ItemUsageContext context)
      Uses the item with the provided ItemUsageContext and returns a result.

      This method must be implemented by subclasses to define the specific behavior of the item when used. The result type is defined by the generic parameters of the subclass. The input type

      Parameters:
      context - the context for this item usage
    • isEmpty

      public boolean isEmpty()
      Returns whether the quantity of this item has reached 0 (ie is consumed).
      Returns:
      whether there are none of this item remaining
    • setTexturePath

      protected void setTexturePath(String texturePath)
      Sets the path to the texture of the object using the provided file path.
      Parameters:
      texturePath - The file path of the texture to set.
    • getTexturePath

      public String getTexturePath() throws IllegalAccessError
      Retrieves the texture of the object.
      Returns:
      The path to the texture associated with this object.
      Throws:
      IllegalAccessError - if the texture has not been set prior to calling this method.