Class Inventory

java.lang.Object
net.risingworld.api.objects.Inventory

public final class Inventory extends Object
Represents the inventory of a player (both online or offline players).
  • Method Details

    • getPlayerDbID

      public int getPlayerDbID()
      Gets the database ID of the player this inventory belongs to.
      Returns:
      the unique owner database ID.
    • getQuickslotFocus

      public int getQuickslotFocus()
      Gets the current quickslot (hotbar) focus (i.e the currently equipped item).
      Returns:
      the number of the currently selected quickslot.
    • getInventoryHashCode

      public int getInventoryHashCode()
      Gets a hashcode of this inventory. If the content of the inventory changes, the hashcode changes accordingly. Use this value to check if the content of the inventory has changed (although bear in mind that there is the risk of collisions, so there is a very small chance that two different inventories produce the same hash).
      Returns:
      the hashcode of this inventory.
    • getSlotCount

      public int getSlotCount(Inventory.SlotType slotType)
      Gets the amount of slots the particular inventory/slot type has.
      Parameters:
      slotType - the slot type.
      Returns:
      the total amount of slots.
    • getMaxSlotCount

      public int getMaxSlotCount(Inventory.SlotType slotType)
      Gets the max possible amount of slots the particular inventory/slot type can have.
      Parameters:
      slotType - the slot type.
      Returns:
      the max possible amount of slots.
    • getItems

      public Item[] getItems(Inventory.SlotType slotType)
      Gets a new array containing all items that are currently in the particular inventory type. Note that a new array is always created when calling this function. This array may contain null values if a particular slot in the inventory does not hold any items.
      Parameters:
      slotType - the inventory/slot type (i.e the category) you want to get the items from.
      Returns:
      a new array containing all items in the particular slot type.
    • getAllItems

      public Item[] getAllItems()
      Gets a new array containing all items in this inventory. Note that a new array is always created when calling this function. This array may contain null values if a particular slot in the inventory does not hold any items.
      Returns:
      a new array containing all items in this inventory.
    • getItem

      public Item getItem(int slot, Inventory.SlotType slotType)
      Gets the item from the specified slot and inventory type. Returns null if there is no item in this slot.
      Parameters:
      slot - the slot.
      slotType - the slot / inventory type.
      Returns:
      the item in the slot, or null if there is no item in the slot.
    • findItem

      public int findItem(short itemID, int variant, Inventory.SlotType slotType)
      Searches for an item with the provided type ID (and optional variant/texture). If such an item was found in the provided inventory/slot type, the slot index is returned, otherwise -1 is returned.
      Parameters:
      itemID - the item ID you're looking for.
      variant - the optional item variant/texture. -1 to ignore it.
      slotType - the slot / inventory type.
      Returns:
      the slot where such an item was found, or -1 if no suitable item was found.
    • findItemByGroup

      public int findItemByGroup(Items.Group group, Inventory.SlotType slotType)
    • findItemByType

      public int findItemByType(Items.Type type, Inventory.SlotType slotType)
    • findAllItems

      public int[] findAllItems(short itemID, int variant, Inventory.SlotType slotType)
      Searches for all items with the provided type ID (and optional variant/textures). The slots of each item that was found in the provided inventory/slot type are returned as a new array.
      Parameters:
      itemID - the item ID you're looking for.
      variant - the optional item variant/texture. -1 to ignore it.
      slotType - the slot / inventory type.
      Returns:
      a new array containing all slots where the found items are located.
    • addItem

      public Item addItem(short itemID, int variant, int stack)
      Creates and adds a new item to the inventory. The item will be added to the next suitable slot (this is either a slot containing an identical item it could be merged to, or a free slot). If there is no free slot, the player will drop the item and get a message about the inventory being full.
      Parameters:
      itemID - the item type ID.
      variant - the variant/texture of the item (only relevant for certain items, like blocks etc). Typically the the variant for most items is 0.
      stack - the stack size, i.e the amount of items.
      Returns:
      the newly created item. If there was already a compatible item in the inventory, that item will be returned instead. Returns null if the item could not be added to the inventory.
      Example: Add 4 gold ingots to a player inventory
      1Inventory inventory = player.getInventory();
      2
      3//Get gold ingot item definition
      4Items.ItemDefinition def = Definitions.getItemDefinition("goldingot");
      5
      6//Insert item
      7Item item = inventory.addItem(def.id, 0, 4);
      8
      9//If item is null, it could not be added to the inventory
      10if (item == null) {
      11 player.showStatusMessage("Could not add item to your inventory!", 1);
      12}
    • addItemToSlot

      public Item addItemToSlot(short itemID, int variant, int stack, int slot, Inventory.SlotType slotType)
      Creates and adds a new item to the inventory. The item will be added to the provided slot if possible.
      Parameters:
      itemID - the item type ID.
      variant - the variant/texture of the item (only relevant for certain items, like blocks etc). Typically the the variant for most items is 0.
      stack - the stack size, i.e the amount of items.
      slot - the target slot where you want to insert the item.
      slotType - the target slot type.
      Returns:
      the newly created item. If there was already a compatible item in the target slot, that item will be returned instead. Returns null if the item could not be added to the inventory.
      See Also:
    • addObjectItem

      public Item.ObjectItem addObjectItem(short objectID, int variant, int stack)
      Creates and adds a new object item to the inventory, i.e an item that represents an object (like furniture). The item will be added to the next suitable slot (this is either a slot containing an identical item it could be merged to, or a free slot). If there is no free slot, the player will drop the item and get a message about the inventory being full.
      Parameters:
      objectID - the object type ID.
      variant - the variant/texture of the object (only relevant for certain objects). Typically the the variant for most objects is 0.
      stack - the stack size, i.e the amount of items.
      Returns:
      the newly created object item. If there was already a compatible item in the inventory, that item will be returned instead. Returns null if the item could not be added to the inventory.
      Example: Add a workbench to a player inventory
      1Inventory inventory = player.getInventory();
      2
      3//Get workbench object definition
      4Objects.ObjectDefinition def = Definitions.getObjectDefinition("workbench");
      5
      6//Add object item
      7inventory.addObjectItem(def.id, 0, 1);
    • addObjectItemToSlot

      public Item.ObjectItem addObjectItemToSlot(short objectID, int variant, int stack, int slot, Inventory.SlotType slotType)
      Creates and adds a new object item to the inventory, i.e an item that represents an object (like furniture). The item will be added to the provided slot if possible.
      Parameters:
      objectID - the object type ID.
      variant - the variant/texture of the object (only relevant for certain objects). Typically the the variant for most objects is 0.
      stack - the stack size, i.e the amount of items.
      slot - the target slot where you want to insert the object item.
      slotType - the target slot type.
      Returns:
      the newly created object item. If there was already a compatible item in the target slot, that item will be returned instead. Returns null if the item could not be added to the inventory.
      See Also:
    • addConstructionItem

      public Item.ConstructionItem addConstructionItem(byte constructionID, int texture, int stack, int color)
      Creates and adds a new construction item to the inventory (e.g a block, window frame etc). The item will be added to the next suitable slot (this is either a slot containing an identical item it could be merged to, or a free slot). If there is no free slot, the player will drop the item and get a message about the inventory being full.
      Parameters:
      constructionID - the type ID of the construction element.
      texture - the texture of the construction element.
      stack - the stack size, i.e the amount of items.
      color - the optional RGBA color of the construction element (else 0).
      Returns:
      the newly created construction item. If there was already a compatible item in the inventory, that item will be returned instead. Returns null if the item could not be added to the inventory.
      Example: Add stack of blocks with texture ID 200 to a player inventory
      1Inventory inventory = player.getInventory();
      2
      3//Get block definition
      4Constructions.ConstructionDefinition def = Definitions.getConstructionDefinition("block");
      5
      6//Add 999x item (no color == 0)
      7inventory.addConstructionItem(def.id, 200, 999, 0);
    • addConstructionItemToSlot

      public Item.ConstructionItem addConstructionItemToSlot(byte constructionID, int texture, int stack, int color, int slot, Inventory.SlotType slotType)
      Creates and adds a new construction item to the inventory (e.g a block, window frame etc). The item will be added to the provided slot if possible.
      Parameters:
      constructionID - the type ID of the construction element.
      texture - the texture of the construction element.
      stack - the stack size, i.e the amount of items.
      color - the optional RGBA color of the construction element (else 0).
      slot - the target slot where you want to insert the object item.
      slotType - the target slot type.
      Returns:
      the newly created object item. If there was already a compatible item in the target slot, that item will be returned instead. Returns null if the item could not be added to the inventory.
      See Also:
    • addClothingItem

      public Item.ClothingItem addClothingItem(short clothingID, int variant, int stack, int color, long infoID)
      Creates and adds a new clothing item to the inventory. The item will be added to the next suitable slot (this is either a slot containing an identical item it could be merged to, or a free slot). If there is no free slot, the player will drop the item and get a message about the inventory being full.
      Parameters:
      clothingID - the clothing type ID.
      variant - the variant/texture of the object (only relevant for certain objects). Typically the the variant for most objects is 0.
      stack - the stack size, i.e the amount of items.
      color - optional RGBA color. Currently unused (set to 0).
      infoID - optional info ID. Currently unused (set to 0).
      Returns:
      the newly created object item. If there was already a compatible item in the inventory, that item will be returned instead. Returns null if the item could not be added to the inventory.
    • addClothingItemToSlot

      public Item.ClothingItem addClothingItemToSlot(short clothingID, int variant, int stack, int color, long infoID, int slot, Inventory.SlotType slotType)
      Creates and adds a new clothing item to the inventory. The item will be added to the provided slot if possible.
      Parameters:
      clothingID - the clothing type ID.
      variant - the variant/texture of the object (only relevant for certain objects). Typically the the variant for most objects is 0.
      stack - the stack size, i.e the amount of items.
      color - optional RGBA color. Currently unused (set to 0).
      infoID - optional info ID. Currently unused (set to 0).
      slot - the target slot where you want to insert the object item.
      slotType - the target slot type.
      Returns:
      the newly created object item. If there was already a compatible item in the target slot, that item will be returned instead. Returns null if the item could not be added to the inventory.
      See Also:
    • removeItem

      public boolean removeItem(int slot, Inventory.SlotType slotType)
      Removes an item from a particular slot in the inventory. After calling this method, the particular slot does no longer contain an item.
      Parameters:
      slot - the target slot you want to remove the item from.
      slotType - the target slot / inventory type.
      Returns:
      true if there was an item in the slot, false if not.
    • removeItem

      public boolean removeItem(int slot, Inventory.SlotType slotType, int amount)
      Removes an item (or more precisely, reduces the stack size of the item by the provided amount) from a particular slot in the inventory.
      Parameters:
      slot - the target slot you want to remove the item from.
      slotType - the target slot / inventory type.
      amount - the amount of items you want to remove from the stack.
      Returns:
      true if there was an item in the slot, false if not.
    • sort

      public void sort(boolean mergeStacks)
      Sorts all items in the inventory. Optionally also merges stacks.
      Parameters:
      mergeStacks - true if you also want to merge all compatible item stacks, false if you only want to sort them.
    • mergeStacks

      public void mergeStacks()
      Merges all compatible item stacks in this inventory.
      See Also:
    • clear

      public void clear()
      Clears the inventory, i.e removes all items from the inventory.