Skip to content

Inventories

Greg edited this page Feb 19, 2024 · 12 revisions

Inventories

Inventories are a fixed size grid of slots capable of holding items. Each slot can hold one type and an amount of the item. e.g. Item("bucket_noted", 15). Inventories are used by players for the backpack (confusingly known as the inventory), equipment etc.. and by npcs for Shops.

Item stack behaviour

Items come in two categories: stackable and non-stackable. Stackable items can have up to 2,147 million items within one slot. Non-stackable items can only have 1 item per slot.

This means having two non-stackable items like a bronze_dagger will take up two slots, where two stackable bronze_dagger_noted will take up only one slot.

Screenshot 2024-02-19 174140

Stack modes

Each inventory has a stack mode which can change the stacking behaviour, banks for example will always stack items even if they are non-stackable.

bank:
  id: 95
  stack: Always

Screenshot 2024-02-19 175815

Stack mode Behaviour
DependentOnItem The default; stacks stackable items, doesn't stack non-stackable items
Always Always stacks items regardless if they are stackable or non-stackable
Never Never stacks items even if they are stackable

Note

Custom stacking rules can also be created, see ItemStackingRule.kt.

An item can be checked if stackable against a certain inventory using the stackable() function:

val stackable = player.bank.stackable("bronze_dagger") // true

Getting an inventory

Player's have a map of inventories connected to them, you can access one by calling:

val equipment = player.inventories.inventory("worn_equipment")

Tip

A lot of the commonly used player inventories have helper functions for easy access e.g. player.equipment

val Player.equipment: Inventory
    get() = inventories.inventory("worn_equipment")

Inventory Modifications

Adding items

Adding a single item or a stack of items is simple with the add helper function

player.inventory.add("coins", 100)

If an inventory is full the item won't be added and add will return false allowing you to run different code depending on the outcome.

if (player.inventory.add("silverlight")) {
    npc<Furious>("Yes, I know, someone returned it to me. Take better care of it this time.")
} else {
    npc<Furious>("Yes, I know, someone returned it to me. I'll keep it until you have free inventory space.")
}

Note

Adding two non-stackable items to a normal inventory will add the items to two empty slots

Removing items

Removing items is much the same as adding, the remove item will return true when the required number of items was removed, and false when the inventory doesn't have the required amount.

player.inventory.remove("coins", 123)
Clone this wiki locally