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

Add an item object to an inventory.
Add an item to an inventory by its instance id.
Add a new receiver to the inventory.
Checks whether a player is entitled to have the inventory open.
Check if an item may be put onto the stack of another item of the inventory.
check_size()server internal
Check the size of the inventory and resize it if needed.
Creates a panel for the inventory.
Find free space in the inventory.
Find a specified item object by its id.
Find specified item objects by their id.
Find the best position for the item to be placed.
Find the stack position for the item.
Get the first item id that is located in the specified slot.
Gets the Y axis size of the inventory in a number of slots.
Get the item size based on inventory and item params.
Get item objects that the inventory contains.
Get the amount of items by their id.
Get item ids that the inventory contains.
Get the entity this inventory belongs to.
Get the players that currently receive the inventory data.
Gets the X and Y axes size of the inventory in a number of slots.
Get the items ids that are located in the specified slot.
Get the slots grid.
Get the type of the inventory.
Gets the X axis size of the inventory in a number of slots.
Create an item and add it to an inventory.
Check if the inventory contains an item by its id.
Check if the inventory contains an item by its instance id.
Check if the inventory contains items by their id.
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.
init(id)shared
Initializes the new inventory class and loads it to the server cache.
Checks if the inventory is default.
Checks if the inventory is disabled.
Checks if the inventory is empty.
Checks if the inventory has infinite height.
Checks if the inventory is multislot.
Checks if the inventory has infinite width.
Move the item inside the inventory.
Move the whole stack inside the inventory.
Check if the item overlaps itself.
Check if the item overlaps only itself.
Check if the item overlaps another stackable item and return adjusted data.
rebuild()shared internal
Rebuilds the inventory slots hash.
Remove the receiver from the inventory.
Disables the inventory, preventing the manipulation of items inside it.
Sets the height of the inventory and rebuilds its slots.
Sets the width and height of the inventory and rebuilds its slots.
Sets the width of the inventory and rebuilds its slots.
Check if the specified slots are empty.
sync()server
Send inventory data to its receivers.
Take an item object from the inventory based on its id.
Take an item object from the inventory based on its instance id.
Take an item object from the inventory.
Take a certain amount of items from the inventory based on their id.
Returns the values of the inventory that will be sent to the client.
Move the item to another inventory.
Move the whole stack to another inventory.

Methods

Inventory:add_item(item_obj, x, y)

server plugins/inventory/plugin/lib/classes/sh_inventory.lua:605

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:669

Add 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

Inventory:add_receiver(receiver)

server plugins/inventory/plugin/lib/classes/sh_inventory.lua:1087

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:1117

Checks 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:453

Check 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

Inventory:check_size()

server internal plugins/inventory/plugin/lib/classes/sh_inventory.lua:1172

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.

Inventory:create_panel(parent)

client plugins/inventory/plugin/lib/classes/sh_inventory.lua:1223

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

Inventory:find_empty_slot(w, h)

shared plugins/inventory/plugin/lib/classes/sh_inventory.lua:468

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:408

Find 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:437

Find 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

Inventory:get_first_in_slot(x, y)

shared plugins/inventory/plugin/lib/classes/sh_inventory.lua:278

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

Inventory:get_item_size(item_obj)

shared plugins/inventory/plugin/lib/classes/sh_inventory.lua:581

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

Inventory:get_items_count(id)

shared plugins/inventory/plugin/lib/classes/sh_inventory.lua:289

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:686

Create 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 nil fields 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:384

Check if the inventory contains an item by its instance id.

Parameters

  • instance_id Number

Returns

  • Boolean
  • Item found item

Inventory:has_items(id, amount)

shared plugins/inventory/plugin/lib/classes/sh_inventory.lua:369

Check if the inventory contains items by their id.

Parameters

  • id String
  • amount Number optional, defaults to 1 amount of items the inventory has to contain

Returns

  • Boolean
  • List<Item> found items

Inventory:has_receiver(receiver)

server plugins/inventory/plugin/lib/classes/sh_inventory.lua:1105

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 nil id 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

Inventory:is_height_infinite()

shared plugins/inventory/plugin/lib/classes/sh_inventory.lua:179

Checks if the inventory has infinite height.

Returns

  • Boolean

Checks if the inventory is multislot.

Returns

  • Boolean

Inventory:is_width_infinite()

shared plugins/inventory/plugin/lib/classes/sh_inventory.lua:173

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:810

Internal

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:1037

Internal

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:535

Check 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:559

Check 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:510

Check 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

Inventory:rebuild()

shared internal plugins/inventory/plugin/lib/classes/sh_inventory.lua:194

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:1097

Remove the receiver from the inventory.

Parameters

  • receiver Player

Inventory:set_disabled(disabled)

server plugins/inventory/plugin/lib/classes/sh_inventory.lua:1205

Disables the inventory, preventing the manipulation of items inside it.

Parameters

  • disabled Boolean

Inventory:set_height(height)

shared plugins/inventory/plugin/lib/classes/sh_inventory.lua:107

Sets the height of the inventory and rebuilds its slots.

Parameters

  • height Number height in a number of slots

Inventory:set_size(width, height)

shared plugins/inventory/plugin/lib/classes/sh_inventory.lua:116

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

Inventory:slots_empty(x, y, w, h)

shared plugins/inventory/plugin/lib/classes/sh_inventory.lua:486

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:799

Take 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:715

Take 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

Inventory:take_items(id, amount)

server plugins/inventory/plugin/lib/classes/sh_inventory.lua:784

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:906

Internal

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:1067

Internal

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

Defined in