Package net.risingworld.api.objects
Class Inventory
java.lang.Object
net.risingworld.api.objects.Inventory
Represents the inventory of a player (both online or offline players).
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
Determines the "inventory type" or "slot type", i.e the category in the inventory (regular inventory items, hotbar, equipment etc.) -
Method Summary
Modifier and TypeMethodDescriptionaddClothingItem
(short clothingID, int variant, int stack, int color, long infoID) Creates and adds a new clothing item to the inventory.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.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).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).addItem
(short itemID, int variant, int stack) Creates and adds a new item to the inventory.Adds an item by name.addItemToSlot
(short itemID, int variant, int stack, int slot, Inventory.SlotType slotType) Creates and adds a new item to the inventory.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).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).void
clear()
Clears the inventory, i.e removes all items from the inventory.int[]
findAllItems
(short itemID, int variant, Inventory.SlotType slotType) Searches for all items with the provided type ID (and optional variant/textures).int
findItem
(short itemID, int variant, Inventory.SlotType slotType) Searches for an item with the provided type ID (and optional variant/texture).int
findItemByGroup
(Items.Group group, Inventory.SlotType slotType) Searches for a particular item group in the provided inventory slot type.int
findItemByType
(Items.Type type, Inventory.SlotType slotType) Searches for a particular item type in the provided inventory slot type.Item[]
Gets a new array containing all items in this inventory.int
Gets the slot of the currently equipped item.Gets the slot type of the currently equipped item.int
Gets a hashcode of this inventory.getItem
(int slot, Inventory.SlotType slotType) Gets the item from the specified slot and inventory type.Item[]
getItems
(Inventory.SlotType slotType) Gets a new array containing all items that are currently in the particular inventory type.int
getMaxSlotCount
(Inventory.SlotType slotType) Gets the max possible amount of slots the particular inventory/slot type can have.int
Gets the database ID of the player this inventory belongs to.int
Gets the currently selected quickslot / hotbar slot.int
getSlotCount
(Inventory.SlotType slotType) Gets the amount of slots the particular inventory/slot type has.static Inventory.SlotType
getSlotType
(int index) Converts a total index to the according inventory slot type.static int
indexToSlot
(int index) Converts a total index to the according inventory slot.void
Merges all compatible item stacks in this inventory.boolean
removeItem
(int index, int amount) Removes an item from a total index in inventory (or more precisely, reduces the stack size of the item by the provided amount).boolean
removeItem
(int slot, Inventory.SlotType slotType) Removes an item from a particular slot in the inventory.boolean
removeItem
(int slot, Inventory.SlotType slotType, int amount) Removes an item from a particular slot in the inventory (or more precisely, reduces the stack size of the item by the provided amount).static int
slotToIndex
(int slot, Inventory.SlotType slotType) Converts a slot to a total index.void
sort
(boolean mergeStacks) Sorts all items in the inventory.void
Force-syncs the entire inventory with the client.
-
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 currently selected quickslot / hotbar slot.- Returns:
- the currently selected quickslot.
- See Also:
-
getEquippedItemSlot
public int getEquippedItemSlot()Gets the slot of the currently equipped item.- Returns:
- the currently equipped item slot.
- See Also:
-
getEquippedItemSlotType
Gets the slot type of the currently equipped item. Usually it's theSlotType.Quickslot
, but there is no guarantee because the player is able to equip any item in the inventory (rightclick -> equip item).- Returns:
- the currently equipped item slot type.
- See Also:
-
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
Gets the amount of slots the particular inventory/slot type has.- Parameters:
slotType
- the slot type.- Returns:
- the total amount of slots.
-
getMaxSlotCount
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
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
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
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
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
Searches for a particular item group in the provided inventory slot type.- Parameters:
group
- the item group you're looking for.slotType
- the inventory slot type.- Returns:
- the slot of the item, or -1 if no suitable item was found.
-
findItemByType
Searches for a particular item type in the provided inventory slot type.- Parameters:
type
- the item type you're looking for.slotType
- the inventory slot type.- Returns:
- the slot of the item, or -1 if no suitable item was found.
-
findAllItems
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
Adds an item by name. This supports all types of items - regular items, objects (like furniture), construction elements or clothing items.- Parameters:
item
- the internal item/object name, e.g "pickaxe" or "workbench".variant
- the item variant/texture.stack
- the amount of items you want to add.- Returns:
- the newly created item. Depending on the item type, this could be either a regular
Item
, or anItem.ObjectItem
(if it's an object), or aItem.ConstructionItem
(if it's a block), or aItem.ClothingItem
(if it's a clothing piece). 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.
-
addItem
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 with, or a free slot). If there is no free slot, the player will drop the item and receive 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
-
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
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 with, or a free slot). If there is no free slot, the player will drop the item and receive 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
-
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 with, or a free slot). If there is no free slot, the player will drop the item and receive 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
-
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 with, or a free slot). If there is no free slot, the player will drop the item and receive a message about the inventory being full.- Parameters:
clothingID
- the clothing type ID.variant
- the variant/texture of the cloth (only relevant for certain clothes). Typically the the variant for most clothes 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 clothing 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 cloth (only relevant for certain clothes). Typically the the variant for most clothes 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 clothing item.slotType
- the target slot type.- Returns:
- the newly created clothing 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
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
Removes an item from a particular slot in the inventory (or more precisely, reduces the stack size of the item by the provided amount).- 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.
-
removeItem
public boolean removeItem(int index, int amount) Removes an item from a total index in inventory (or more precisely, reduces the stack size of the item by the provided amount).- Parameters:
index
- the total index in inventory (not to be confused with the slot!)amount
- the amount of items you want to remove from the stack. Set to -1 to remove the item entirely.- Returns:
- true if there was an item in that index, false if not.
- See Also:
-
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:
-
syncWithClient
public void syncWithClient()Force-syncs the entire inventory with the client. -
clear
public void clear()Clears the inventory, i.e removes all items from the inventory. -
indexToSlot
public static int indexToSlot(int index) Converts a total index to the according inventory slot.- Parameters:
index
- the total index in inventory.- Returns:
- the related slot.
- See Also:
-
slotToIndex
Converts a slot to a total index. This is useful, for example, if you want to work with the items array (getAllItems()
).- Parameters:
slot
- the slot.slotType
- the slot type.- Returns:
- the related slot.
- See Also:
-
getSlotType
Converts a total index to the according inventory slot type.- Parameters:
index
- the total index in inventory.- Returns:
- the related slot type.
- See Also:
-