Inventory class
Part of Inventory
The inventory class is used to manage a player's items, transferring them from one player or object to another, using the same interface and functionality.
Summary
Methods
Methods
Add an item object to an inventory.
Variants
Inventory:add_item(item_obj, x, y)- item_obj Item
- x Number
- y Number
Inventory:add_item(item_obj)In this case finds the best position for the item.
- item_obj Item
Returns
- Boolean was the item added successfully
- String text of the error that occurred
Inventory:add_item_by_id(instance_id, x, y)
server plugins/inventory/plugin/lib/classes/sh_inventory.lua:669Add an item to an inventory by its instance id.
Variants
Inventory:add_item_by_id(instance_id, x, y)- instance_id Number
- x Number
- y Number
Inventory:add_item_by_id(instance_id)In this case finds the best position for the item.
- instance_id Number
Returns
- Boolean was the item added successfully
- String text of the error that occurred
Add a new receiver to the inventory. Does nothing if the player is a receiver already.
Parameters
- receiver Player
Inventory:can_be_viewed_by(receiver)
server plugins/inventory/plugin/lib/classes/sh_inventory.lua:1117Checks whether a player is entitled to have the inventory open. The owner always is. The inside of a container item is for whoever carries the item, or stands within reach of it while it lies in the world. Any other inventory that belongs to an entity is for those within reach of that entity, and an inventory without an owner is for anyone. The server closes an inventory for the receivers that fail this check.
Parameters
- receiver Player
Returns
- Boolean
See also
Inventory:can_stack(item_obj, stack_item)
shared plugins/inventory/plugin/lib/classes/sh_inventory.lua:453Check if an item may be put onto the stack of another item of the inventory.
Parameters
- item_obj Item
- stack_item Item an item in this inventory; items elsewhere never take a stack
Returns
- Boolean
Internal
This function is internal. It is not part of the public API and may change without notice.
Check the size of the inventory and resize it if needed.
Creates a panel for the inventory. It will update automatically every time the inventory synchronizes itself.
local hotbar = PLAYER:get_inventory('hotbar'):create_panel()
hotbar:set_slot_size(math.scale(80))
hotbar:set_slot_padding(math.scale(8))
hotbar:SizeToContents()
hotbar:rebuild()
Parameters
- parent Panel optional, defaults to
nil
Returns
- Panel
Find free space in the inventory.
Parameters
- w Number
- h Number
Returns
- Number x
- Number y
Find a specified item object by its id.
Parameters
- id String
Returns
- Item first item found, or nil if there is none
Find specified item objects by their id.
Parameters
- id String
Returns
- List<Item> items
Inventory:find_position(item_obj, w, h)
shared plugins/inventory/plugin/lib/classes/sh_inventory.lua:408Find the best position for the item to be placed.
Parameters
- item_obj Item
- w Number width of the item
- h Number height of the item
Returns
- Number x
- Number y
- Boolean is a rotation needed
Inventory:find_stack(item_obj, _w, _h)
shared plugins/inventory/plugin/lib/classes/sh_inventory.lua:437Find the stack position for the item.
Parameters
- item_obj Item
- _w Number width of the item
- _h Number height of the item
Returns
- Number x
- Number y
- Boolean is a rotation needed
Get the first item id that is located in the specified slot.
Parameters
- x Number
- y Number
Returns
- Number instance id, or nil if the slot is empty
Gets the Y axis size of the inventory in a number of slots.
Returns
- Number
Get the item size based on inventory and item params.
Parameters
- item_obj Item
Returns
- Number width
- Number height
Get item objects that the inventory contains. Also includes items from the containers.
Returns
- List<Item> items
Get the amount of items by their id.
Parameters
- id String
Returns
- Number
Get item ids that the inventory contains.
Returns
- List<Number> items ids
Get the entity this inventory belongs to.
Returns
- Entity
Get the players that currently receive the inventory data.
Returns
- List<Player> players
Gets the X and Y axes size of the inventory in a number of slots.
Returns
- Number
- Number
Get the items ids that are located in the specified slot.
Parameters
- x Number column of the slot, starting from 1
- y Number row of the slot, starting from 1
Returns
- List<Number> items ids, or nil if the slot is out of the inventory bounds
Get the slots grid.
Returns
- Map slots, indexed by y and then by x; every slot is an array of instance ids
Get the type of the inventory.
Returns
- String
Gets the X axis size of the inventory in a number of slots.
Returns
- Number
Inventory:give_item(id, amount, data)
server plugins/inventory/plugin/lib/classes/sh_inventory.lua:686Create an item and add it to an inventory.
local success, error_text = inventory:give_item('test_item', 5, { name = 'Test Item #2' })
if success then
-- The changes are not sent to the clients until the inventory is synchronized.
inventory:sync()
end
Parameters
- id String
- amount Number optional, defaults to
1 - data Map optional, defaults to
nilfields to override on the created items
Returns
- Boolean was the item given successfully
- String text of the error that occurred
Check if the inventory contains an item by its id.
Parameters
- id String
Returns
- Boolean
- Item found item
Inventory:has_item_by_id(instance_id)
shared plugins/inventory/plugin/lib/classes/sh_inventory.lua:384Check if the inventory contains an item by its instance id.
Parameters
- instance_id Number
Returns
- Boolean
- Item found item
Check if the inventory contains items by their id.
Parameters
- id String
- amount Number optional, defaults to
1amount of items the inventory has to contain
Returns
- Boolean
- List<Item> found items
Checks if the player currently receives the inventory data, which is the case for their own inventories and for the inventories that have been opened for them.
Parameters
- receiver Player
Returns
- Boolean
Initializes the new inventory class and loads it to the server cache.
-- Creating a new inventory
local inventory = Inventory.new()
inventory.title = 'Test inventory'
inventory:set_size(4, 4)
inventory.type = 'testing_inventory'
inventory.multislot = false
Parameters
- id Number optional, defaults to
nilid of the inventory; client-side only, the server assigns ids itself
Checks if the inventory is default. If there's no certain inventory specified, the default one is used for it.
Returns
- Boolean
See also
Checks if the inventory is disabled.
Returns
- Boolean
Checks if the inventory is empty.
if target:get_inventory('main_inventory'):is_empty() then
target:notify('Your main inventory is empty!')
end
Returns
- Boolean
Checks if the inventory has infinite height.
Returns
- Boolean
Checks if the inventory is multislot.
Returns
- Boolean
Checks if the inventory has infinite width.
Returns
- Boolean
Inventory:move_item(instance_id, x, y, was_rotated)
server internal plugins/inventory/plugin/lib/classes/sh_inventory.lua:810Internal
This function is internal. It is not part of the public API and may change without notice.
Move the item inside the inventory.
Parameters
- instance_id Number
- x Number
- y Number
- was_rotated Boolean
Returns
- Boolean was the item moved successfully
- String text of the error that occurred
Inventory:move_stack(instance_ids, x, y, was_rotated)
server internal plugins/inventory/plugin/lib/classes/sh_inventory.lua:1037Internal
This function is internal. It is not part of the public API and may change without notice.
Move the whole stack inside the inventory.
Parameters
- instance_ids List<Number> instance ids
- x Number
- y Number
- was_rotated Boolean
Returns
- Boolean have the items been moved successfully
- String text of the error that occurred
Inventory:overlaps_itself(instance_id, x, y, w, h)
shared plugins/inventory/plugin/lib/classes/sh_inventory.lua:535Check if the item overlaps itself. The part of the area that lies outside of the inventory is ignored, so the position does not have to be within its bounds.
Parameters
- instance_id Number
- x Number
- y Number
- w Number
- h Number
Returns
- Boolean
Inventory:overlaps_only_itself(instance_id, x, y, w, h)
shared plugins/inventory/plugin/lib/classes/sh_inventory.lua:559Check if the item overlaps only itself.
Parameters
- instance_id Number
- x Number
- y Number
- w Number
- h Number
Returns
- Boolean
Inventory:overlaps_stack(item_obj, x, y, w, h)
shared plugins/inventory/plugin/lib/classes/sh_inventory.lua:510Check if the item overlaps another stackable item and return adjusted data.
Parameters
- item_obj Item
- x Number
- y Number
- w Number
- h Number
Returns
- Boolean
- Number x
- Number y
- Boolean is a rotation needed
Internal
This function is internal. It is not part of the public API and may change without notice.
Rebuilds the inventory slots hash.
Inventory:remove_receiver(receiver)
server plugins/inventory/plugin/lib/classes/sh_inventory.lua:1097Remove the receiver from the inventory.
Parameters
- receiver Player
Disables the inventory, preventing the manipulation of items inside it.
Parameters
- disabled Boolean
Sets the height of the inventory and rebuilds its slots.
Parameters
- height Number height in a number of slots
Sets the width and height of the inventory and rebuilds its slots.
Parameters
- width Number width in a number of slots
- height Number height in a number of slots
Sets the width of the inventory and rebuilds its slots.
Parameters
- width Number width in a number of slots
Check if the specified slots are empty.
Parameters
- x Number
- y Number
- w Number
- h Number
Returns
- Boolean
Send inventory data to its receivers.
Take an item object from the inventory based on its id.
Parameters
- id String
Returns
- Boolean was the item taken successfully
- String text of the error that occurred
Inventory:take_item_by_id(instance_id)
server plugins/inventory/plugin/lib/classes/sh_inventory.lua:799Take an item object from the inventory based on its instance id.
Parameters
- instance_id Number
Returns
- Boolean was the item taken successfully
- String text of the error that occurred
Inventory:take_item_table(item_obj)
server plugins/inventory/plugin/lib/classes/sh_inventory.lua:715Take an item object from the inventory. An item that another inventory holds, such as one inside of a container item in this inventory, is taken from that inventory, which is synchronized then.
Parameters
- item_obj Item
Returns
- Boolean was the item taken successfully
- String text of the error that occurred; false also if the item is in no inventory
Take a certain amount of items from the inventory based on their id.
Parameters
- id String
- amount Number
Returns
- Boolean was the item taken successfully
- String text of the error that occurred
Returns the values of the inventory that will be sent to the client.
Returns
- Map
Inventory:transfer_item(instance_id, inventory, x, y, was_rotated)
server internal plugins/inventory/plugin/lib/classes/sh_inventory.lua:906Internal
This function is internal. It is not part of the public API and may change without notice.
Move the item to another inventory.
Parameters
- instance_id Number
- inventory Inventory
- x Number
- y Number
- was_rotated Boolean
Returns
- Boolean was the item transferred successfully
- String text of the error that occurred
Inventory:transfer_stack(instance_ids, inventory, x, y, was_rotated)
server internal plugins/inventory/plugin/lib/classes/sh_inventory.lua:1067Internal
This function is internal. It is not part of the public API and may change without notice.
Move the whole stack to another inventory.
Parameters
- instance_ids List<Number> instance ids
- inventory Inventory
- x Number
- y Number
- was_rotated Boolean
Returns
- Boolean have the items been transferred successfully
- String text of the error that occurred