Player extensions of the Inventory plugin: everything a player can do with their inventories and the items in them. A player has one inventory per type ('main_inventory', 'hotbar', 'pockets' and so on), built by the CreatePlayerInventories hook when their character becomes active. The inventories belong to the character, and the items of the character are loaded from the database into them right afterward. The functions that look items up work on both the server and the client; those that take an optional inv_type go through all inventories of the player when it is omitted. On the server a player can also be given items, have them taken or moved to another of their inventories, and be shown an inventory. These functions synchronize the inventories they change right away.

The file also holds the registry of all inventories, Inventories.all and Inventories.find.

Summary

Methods

Give the player a certain item.
Give the player a certain item by its instance id.
Closes inventories that were opened for the player: removes the player from their receivers, tells the client to remove their windows and runs the 'OnInventoryClosed' hook once, with the first of them.
Closes an inventory that was opened for the player with open_inventory.
Closes the inventories of another player that were opened with open_player_inventory.
create_inventories()server internal
Creates the player's default inventories.
delete_inventories()server internal
Deletes the player's inventories from the server cache.
Get the first item with a certain id from the specified inventory or from all of them.
Get all items with a certain id from the specified inventory or from all of them.
Get the item of the weapon that the player is holding in their hands.
Get the first instance_id from a certain slot of the specified inventory.
Get all the player's inventories.
Get one of the player's inventories by type.
Get the item object by the weapon class.
Get the items table from a certain inventory or from all of them.
Get the amount of items with a certain id from the specified inventory or from all of them.
Get only instance_ids from a certain inventory or from all of them.
Get instance_ids from a certain slot of the specified inventory.
give_default_items()server internal
Gives the player's active character its starting items through the 'AddDefaultItems' hook, if it has not had them yet.
Give the player a certain item(s) by id.
Checking if the player has a certain item by its id.
Checking if the player has a certain item by its instance id.
Checking if the player has a certain item equipped by its id.
load_inventories()server internal
Loads the items of the player's active character from the database into the inventories of the player, which may take a moment.
Opens an inventory window for the player and makes them a receiver of the inventory.
Opens all the inventories the other player has.
Synchronize all the inventories that the player has.
Synchronize only the specified inventory.
Takes one item from the player.
Takes one specified item from the player.
Takes the specified amount of items from the player.
Transfers an item to a specified inventory of the player.
unload_items(defer)server internal
Writes the changes of the items in the player's inventories, and of the items of the character that could not be put into them, and makes the server forget them.

Methods

Player:add_item(item_obj, inv_type)

server plugins/inventory/plugin/lib/sh_inventory.lua:525

Give the player a certain item.

Parameters

  • item_obj Item
  • inv_type String optional, defaults to self.default_inventory or 'main_inventory'

Returns

  • Boolean was the item added successfully
  • String text of the error that occurred

Player:add_item_by_id(instance_id, inv_type)

server plugins/inventory/plugin/lib/sh_inventory.lua:538

Give the player a certain item by its instance id.

Parameters

  • instance_id Number
  • inv_type String optional, defaults to self.default_inventory or 'main_inventory'

Returns

  • Boolean was the item added successfully
  • String text of the error that occurred

Player:close_inventories(inventories)

server plugins/inventory/plugin/lib/sh_inventory.lua:746

Closes inventories that were opened for the player: removes the player from their receivers, tells the client to remove their windows and runs the 'OnInventoryClosed' hook once, with the first of them. The player's own inventories and inventories that are not open for the player are skipped.

Parameters

  • inventories List<Inventory>

Player:close_inventory(inventory)

server plugins/inventory/plugin/lib/sh_inventory.lua:767

Closes an inventory that was opened for the player with open_inventory.

Parameters

  • inventory Inventory

See also

Player:close_player_inventory(target)

server plugins/inventory/plugin/lib/sh_inventory.lua:774

Closes the inventories of another player that were opened with open_player_inventory.

Parameters

  • target Player

See also

Player:create_inventories()

server internal plugins/inventory/plugin/lib/sh_inventory.lua:351

Internal

This function is internal. It is not part of the public API and may change without notice.

Creates the player's default inventories.

Player:delete_inventories()

server internal plugins/inventory/plugin/lib/sh_inventory.lua:500

Internal

This function is internal. It is not part of the public API and may change without notice.

Deletes the player's inventories from the server cache.

Player:find_item(id, inv_type)

shared plugins/inventory/plugin/lib/sh_inventory.lua:223

Get the first item with a certain id from the specified inventory or from all of them.

Variants

  • Player:find_item(id, inv_type)

    Will return the item only from the specified inventory.

    • id String
    • inv_type String
  • Player:find_item(id)

    Will return the item from all the inventories that the player has.

    • id String

Returns

  • Item

Player:find_items(id, inv_type)

shared plugins/inventory/plugin/lib/sh_inventory.lua:246

Get all items with a certain id from the specified inventory or from all of them.

Variants

  • Player:find_items(id, inv_type)

    Will return items only from the specified inventory.

    • id String
    • inv_type String
  • Player:find_items(id)

    Will return items from all the inventories that the player has.

    • id String

Returns

  • List<Item> items

Player:get_active_weapon_item()

shared plugins/inventory/plugin/lib/sh_inventory.lua:340

Get the item of the weapon that the player is holding in their hands.

Returns

  • Item

Player:get_first_in_slot(x, y, inv_type)

shared plugins/inventory/plugin/lib/sh_inventory.lua:187

Get the first instance_id from a certain slot of the specified inventory.

Parameters

  • x Number
  • y Number
  • inv_type String

Returns

  • Number

Player:get_inventories()

shared plugins/inventory/plugin/lib/sh_inventory.lua:127

Get all the player's inventories.

Returns

  • Map inventories

Player:get_inventory(inv_type)

shared plugins/inventory/plugin/lib/sh_inventory.lua:121

Get one of the player's inventories by type.

Parameters

  • inv_type String

Returns

  • Inventory, or nil if the player has no such inventory, as a player without a character has none

Player:get_item_from_weapon(weapon_class)

shared plugins/inventory/plugin/lib/sh_inventory.lua:326

Get the item object by the weapon class.

Parameters

  • weapon_class String

Returns

  • Item

Player:get_items(inv_type)

shared plugins/inventory/plugin/lib/sh_inventory.lua:138

Get the items table from a certain inventory or from all of them.

Variants

  • Player:get_items(inv_type)

    Will return items from the specified inventory.

    • inv_type String
  • Player:get_items()

    Will return items from all the inventories that the player has.

Returns

  • List<Item> items

Player:get_items_count(id, inv_type)

shared plugins/inventory/plugin/lib/sh_inventory.lua:200

Get the amount of items with a certain id from the specified inventory or from all of them.

Variants

  • Player:get_items_count(id, inv_type)

    Will return the items count only from the specified inventory.

    • id String
    • inv_type String
  • Player:get_items_count(id)

    Will return the items count from all the inventories that the player has.

    • id String

Returns

  • Number

Player:get_items_ids(inv_type)

shared plugins/inventory/plugin/lib/sh_inventory.lua:159

Get only instance_ids from a certain inventory or from all of them.

Variants

  • Player:get_items_ids(inv_type)

    Will return item ids only from the specified inventory.

    • inv_type String
  • Player:get_items_ids()

    Will return item ids from all the inventories that the player has.

Returns

  • List<Number> numbers

Player:get_slot(x, y, inv_type)

shared plugins/inventory/plugin/lib/sh_inventory.lua:178

Get instance_ids from a certain slot of the specified inventory.

Parameters

  • x Number
  • y Number
  • inv_type String

Returns

  • List<Number> numbers

Player:give_default_items()

server internal plugins/inventory/plugin/lib/sh_inventory.lua:435

Internal

This function is internal. It is not part of the public API and may change without notice.

Gives the player's active character its starting items through the 'AddDefaultItems' hook, if it has not had them yet. Called the first time a new character is loaded, once its inventories exist and its items are there. The character is saved right afterward, so that it gets them only once.

Player:give_item(id, amount, data, inv_type)

server plugins/inventory/plugin/lib/sh_inventory.lua:556

Give the player a certain item(s) by id.

-- Adds 10 test items to the player's inventory and sets their name to 'Some Item'.
local success, error_text = target:give_item('test_item', 10, { name = 'Some Item' })
if !success then
  -- Notifies the player of an error.
  target:notify(error_text)
end

Parameters

  • id String
  • amount Number optional, defaults to 1
  • data Map optional, defaults to nil fields to override on the created items
  • inv_type String optional, defaults to self.default_inventory or 'main_inventory'

Returns

  • Boolean was the item added successfully
  • String text of the error that occurred

Player:has_item(id, inv_type)

shared plugins/inventory/plugin/lib/sh_inventory.lua:269

Checking if the player has a certain item by its id.

Variants

  • Player:has_item(id, inv_type)

    Will check only the specified inventory.

    • id String
    • inv_type String
  • Player:has_item(id)

    Will check all the inventories that the player has.

    • id String

Returns

  • Boolean
  • Item found item

Player:has_item_by_id(instance_id, inv_type)

shared plugins/inventory/plugin/lib/sh_inventory.lua:294

Checking if the player has a certain item by its instance id.

Variants

  • Player:has_item_by_id(instance_id, inv_type)

    Will check only the specified inventory.

    • instance_id Number
    • inv_type String
  • Player:has_item_by_id(instance_id)

    Will check all the inventories that the player has.

    • instance_id Number

Returns

  • Boolean
  • Item found item

Player:has_item_equipped(id)

shared plugins/inventory/plugin/lib/sh_inventory.lua:313

Checking if the player has a certain item equipped by its id.

Parameters

  • id String

Returns

  • Boolean
  • Item found item

Player:load_inventories()

server internal plugins/inventory/plugin/lib/sh_inventory.lua:399

Internal

This function is internal. It is not part of the public API and may change without notice.

Loads the items of the player's active character from the database into the inventories of the player, which may take a moment. Until the items are there, loading_items is true on the player. Once they are, the inventories are synchronized and the 'OnCharacterItemsLoaded' hook is run. Items that turn up after the player has switched to another character are dropped without being used.

Player:open_inventory(inventory)

server plugins/inventory/plugin/lib/sh_inventory.lua:717

Opens an inventory window for the player and makes them a receiver of the inventory. The server closes it again as soon as Inventory:can_be_viewed_by fails for the player, for example when they walk away from the entity the inventory belongs to.

-- Creating new inventory
local inventory = Inventory.new()
inventory.title = 'Test inventory'
inventory:set_size(4, 4)
inventory.type = 'testing_inventory'
inventory.multislot = false

-- Creating an inventory window for a player
target:open_inventory(inventory)

Parameters

  • inventory Inventory

Player:open_player_inventory(target)

server plugins/inventory/plugin/lib/sh_inventory.lua:728

Opens all the inventories the other player has. The server closes them again as soon as the other player is out of reach; call close_player_inventory to close them earlier.

Parameters

  • target Player

See also

Player:sync_inventories()

server plugins/inventory/plugin/lib/sh_inventory.lua:509

Synchronize all the inventories that the player has.

Player:sync_inventory(inv_type)

server plugins/inventory/plugin/lib/sh_inventory.lua:517

Synchronize only the specified inventory.

Parameters

  • inv_type String

Player:take_item(id, inv_type)

server plugins/inventory/plugin/lib/sh_inventory.lua:574

Takes one item from the player.

Variants

  • Player:take_item(id, inv_type)

    Takes the item only from the specified inventory.

    • id String
    • inv_type String
  • Player:take_item(id)

    Takes the item from the inventory that has it.

    • id String

Returns

  • Boolean was the item taken successfully
  • String text of the error that occurred

Player:take_item_by_id(instance_id, inv_type)

server plugins/inventory/plugin/lib/sh_inventory.lua:645

Takes one specified item from the player.

Variants

  • Player:take_item_by_id(instance_id, inv_type)

    Takes the item only from the specified inventory.

    • instance_id Number
    • inv_type String
  • Player:take_item_by_id(instance_id)

    Takes the item from the inventory that has it.

    • instance_id Number

Returns

  • Boolean was the item taken successfully
  • String text of the error that occurred

Player:take_items(id, amount, inv_type)

server plugins/inventory/plugin/lib/sh_inventory.lua:608

Takes the specified amount of items from the player.

Variants

  • Player:take_items(id, amount, inv_type)

    Takes items only from the specified inventory.

    • id String
    • amount Number
    • inv_type String
  • Player:take_items(id, amount)

    Takes items from the inventory that has them.

    • id String
    • amount Number

Returns

  • Boolean have the items been taken successfully
  • String text of the error that occurred

Player:transfer_item(instance_id, inv_type)

server plugins/inventory/plugin/lib/sh_inventory.lua:683

Transfers an item to a specified inventory of the player. Takes the item only from the specified inventory. Does nothing and returns nothing if the item is in that inventory already.

-- Puts the item on the player's hotbar.
local success, error_text = target:transfer_item(item_obj.instance_id, 'hotbar')

if success == false then
  target:notify(error_text)
end

Parameters

  • instance_id Number
  • inv_type String

Returns

  • Boolean was the item transferred successfully
  • String text of the error that occurred

Player:unload_items(defer)

server internal plugins/inventory/plugin/lib/sh_inventory.lua:467

Internal

This function is internal. It is not part of the public API and may change without notice.

Writes the changes of the items in the player's inventories, and of the items of the character that could not be put into them, and makes the server forget them. Called when the character is no longer in use. The inventories of the container items among them are closed and deleted from the server cache; the player's own inventories are left alone.

Parameters

  • defer Boolean optional, defaults to false do it on the next tick, so that what is saved along with the character in the meantime is not lost

Defined in