Player class
Part of Inventory
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
Methods
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:538Give 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
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>
Closes an inventory that was opened for the player with open_inventory.
Parameters
- inventory Inventory
See also
Closes the inventories of another player that were opened with open_player_inventory.
Parameters
- target Player
See also
Internal
This function is internal. It is not part of the public API and may change without notice.
Creates the player's default inventories.
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.
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
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
Get the item of the weapon that the player is holding in their hands.
Returns
- Item
Get the first instance_id from a certain slot of the specified inventory.
Parameters
- x Number
- y Number
- inv_type String
Returns
- Number
Get all the player's inventories.
Returns
- Map inventories
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
Get the item object by the weapon class.
Parameters
- weapon_class String
Returns
- Item
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
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
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
Get instance_ids from a certain slot of the specified inventory.
Parameters
- x Number
- y Number
- inv_type String
Returns
- List<Number> numbers
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:556Give 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
nilfields 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
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:294Checking 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
Checking if the player has a certain item equipped by its id.
Parameters
- id String
Returns
- Boolean
- Item found item
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.
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
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
Synchronize all the inventories that the player has.
Synchronize only the specified inventory.
Parameters
- inv_type String
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:645Takes 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
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:683Transfers 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
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
falsedo it on the next tick, so that what is saved along with the character in the meantime is not lost