Inventory base function updates - UQdeco2800/2022-studio-2 GitHub Wiki

In sprint 3, inventory native functions are updated to incorporate all items that can be picked up. It is more strictly typed and perform faster operations after optimisation.

Sprint 3 updated methods

Adding items

AddItem() now only take Entity as parameter. Due to the nature of inventory implementation, each Entity will have a quantity attribute meaning that all items in the inventory will be unique and stacked.

public void addItem(Entity item) {
    if (inventory.size() == inventorySize) {
        logger.info("Inventory is full");
    } else if (!hasItem(item, inventory)) {
        if ((item.checkEntityType(EntityTypes.WEAPON)
                || item.checkEntityType(EntityTypes.ARMOUR))) {
                ) {
                inventory.add(item);
                ++itemQuantity[inventory.indexOf(item)];
            } else if (item.checkEntityType(EntityTypes.POTION)
                    || item.checkEntityType(EntityTypes.CRAFTABLE)) {
                inventory.add(item);
            }
        }
        if (getItemIndex(item, inventory) != -1
                && getItemQuantity(item) < 9
                && (item.checkEntityType(EntityTypes.POTION)
                || (!item.checkEntityType(EntityTypes.WEAPON)
                && item.checkEntityType(EntityTypes.CRAFTABLE)))) {
            ++itemQuantity[getItemIndex(item, inventory)];
        }
    }

Before adding the Entity, hasItem() will run through the current inventory to check if there is an existing same type of Entity to stack.
Documentation of hasItem()

Remove items

In the previous removeItem implementation, looking up an item in the inventory uses ArrayList.indexOf() which can potentially cause problems.

In sprint 3, indexOf() is replaced by a custom look up method getItemIndex() to minimise possibility of encounter NullPointerExceptions.
Documentation of getItemIndex()

Back to Inventory Page

Author

  • Li-Sung Ou
  • GitHub: @PeterOu8
  • Discord: Secret Agent Randy Beans#6754
  • Slack: Li-Sung Ou
⚠️ **GitHub.com Fallback** ⚠️