Skip to content

Inventory

Base inventory class. The inventory object stores items, typically in a grid via the GridInv plugin. Alternatively, a simple inventory, without a grid system, is available with the SimpleInv plugin. Typically, each Character has their own inventory, however an inventory can also be tied to a world object/prop (such as with the storage plugin), or an item (such as bags).

Methods

Inventory:show (parent)
Inventory:show

Displays the inventory in a UI panel. Delegates to the global nut.inventory.show function.

Parameters:

Panel parent Optional parent panel to attach the inventory UI to

Returns:

panel The created inventory panel

See also:

Inventory:addItem (item)
Inventory:addItem

Given an item type string, creates an instance of that item type and adds it to this inventory. A promise is returned containing the newly created item after it has been added to the inventory.

Parameters:

Item item The item to add

Returns:

inventory The inventory instance

Inventory:add (item)
Inventory:add

Sample implementation of Inventory:add - delegates to addItem

Parameters:

Item item The item to add

Returns:

inventory The inventory instance

See also:

Inventory:syncItemAdded (item)
Inventory:syncItemAdded

Syncs a single added item with clients.

Parameters:

Item item The item to sync

Inventory:initializeStorage (initialData)
Inventory:initializeStorage

Called to handle the logic for creating the data storage for this inventory. Returns a promise that is resolved after the storing is done.

Parameters:

table initialData Table of keys and values to persist

Returns:

number Promise that resolves to the inventory ID

Inventory:restoreFromStorage (id)
Inventory:restoreFromStorage

Called when some inventory with a certain ID needs to be loaded. If this type is responsible for loading that inventory ID in particular, then a promise that resolves to an inventory should be returned. This allows for custom data storage of inventories.

Parameters:

integer id The inventory ID to restore

Inventory:removeItem (itemID, preserveItem)
Inventory:removeItem

Removes an item corresponding to the given item ID if it is in this inventory. If the item belongs to this inventory, it is then deleted. A promise is returned which is resolved after removal from this. Deletes the item or clears its association based on preserveItem.

Parameters:

integer itemID The ID of the item to remove

boolean preserveItem optional. default: false Whether to keep the item in the DB

Returns:

promise Promise resolved when removal completes

Inventory:setData (key, value)
Inventory:setData

Stores arbitrary data that can later be looked up using the given key.

Parameters:

string key Metadata key

vararg value Value to store

Returns:

inventory The inventory instance

Inventory:canAccess (action, context)
Inventory:canAccess

Whether or not a client can interact with this inventory. Iterates through registered access rules.

Parameters:

string action The type of access

table context Optional context (e.g. player)

Returns:

bool Whether access is allowed

string Reason if denied

Inventory:addAccessRule (rule, priority)
Inventory:addAccessRule

Changes the canAccess method to also return the result of the rule where the rule of a function of (inventory, player, action) -> boolean.

Parameters:

function rule Access rule function (inventory, action, context)

integer priority optional Insert position

Returns:

inventory The inventory instance

Inventory:removeAccessRule (rule)
Inventory:removeAccessRule

Removes the first instance of a specific access rule from the inventory.

Parameters:

function rule The rule function to remove

Returns:

inventory The inventory instance

Inventory:getRecipients ()
Inventory:getRecipients

Returns a list of players who can interact with this inventory.

Returns:

table List of players

Inventory:onInstanced ()
Inventory:onInstanced

Called after this inventory has first been created and loaded.

Inventory:onLoaded ()
Inventory:onLoaded

Called after this inventory has first been loaded, not including right after it has been created.

Inventory:loadItems ()
Inventory:loadItems

Loads the items contained in this inventory.

Returns:

table Mapping of item IDs to item objects

Inventory:onItemsLoaded (items)
Inventory:onItemsLoaded

Called after items have been loaded into the inventory.

Parameters:

table items The loaded items

Inventory:instance (initialData)
Inventory:instance

Creates an inventory instance via the global inventory system.

Parameters:

table initialData Table of initialization parameters

Returns:

inventory The new inventory instance

See also:

Inventory:syncData (key, recipients)
Inventory:syncData

Sends a key-value pair to all clients who can access this inventory.

Parameters:

string key The key to replicate

Client recipients optional Override recipients

Inventory:sync (recipients)
Inventory:sync

Sends the full inventory and all contained items information to clients.

Parameters:

Client recipients optional Override recipients

Inventory:delete ()
Inventory:delete

Deletes the inventory using the global deletion handler.

See also:

Inventory:destroy ()
Inventory:destroy

Destroys all items and removes the inventory instance.

Inventory:getData (key, default)
Inventory:getData

Returns the value of the stored key if it exists, the default otherwise. If no default is given, then nil is returned.

Parameters:

integer key The key to look up data with

vararg default optional. default: nil The value that should be returned if no such data was found. By default this is nil

Returns:

A value corresponding to the key

Inventory:extend (className)
Inventory:extend

Creates an inventory object whose base class is the callee. Use this to create subclasses of a specific inventory type. A starting point is to extend the nut.Inventory class.

Parameters:

string className the className of the base class to extend

Inventory:configure (config)
Inventory:configure

Called when the inventory can first be configured. You can call edit the inventory configuration in here.

Parameters:

vararg config A reference to the inventory configuration table

Inventory:addDataProxy (key, onChange)
Inventory:addDataProxy

Adds a callback function for data changes whose key matches the given one. This allows you to add additional behavior when data is changed. Note that this only runs if the default behavior for Inventory:onDataChanged has not been modified.

Parameters:

string key A string containing the data key that needs to be changed for the callback to run

function onChange A function with oldValue and newValue as parameters that is called when the data is changed

Inventory:register (typeID)
Inventory:register

Sets the type ID for this inventory class and registers it as a valid type. This basically sets up configurations for this inventory and registers the type.

Parameters:

string typeID A string containing a key to later access the type

See also:

Inventory:new ()
Inventory:new

Creates an instance of this inventory type.

Returns:

An inventory instance

See also:

Inventory:getType ()
Inventory:getType

Returns the inventory type of this inventory.

Returns:

An inventory object

Inventory:onDataChanged (key, oldValue, newValue)
Inventory:onDataChanged

Called when a data value has been changed for this inventory. You can use this to add different behaviors for certain keys changing.

Parameters:

vararg key The key whose value was changed

vararg oldValue The previous value corresponding to the key

vararg newValue The value the key is being set to

Inventory:getItems ()
Inventory:getItems

Returns a list of all the items in this inventory

Returns:

A table containing items

Inventory:getItemsOfType (itemType)
Inventory:getItemsOfType

Returns a list of items in this inventory with matching item type.

Parameters:

string itemType A string containing the desired type of item

Returns:

table A table containing items whose type matches

Inventory:getFirstItemOfType (itemType)
Inventory:getFirstItemOfType

Returns an item in this inventory of a specific type, or nil if not found.

Parameters:

string itemType A string containing the desired type of item

Returns:

An item instance if one was found, nil otherwise.

Inventory:hasItem (itemType)
Inventory:hasItem

Returns whether or not this inventory contains at least one item of the given type.

Parameters:

string itemType The desired type of item

Returns:

bool True if there is such an item, false otherwise

Inventory:getItemCount (itemType)
Inventory:getItemCount

Returns the amount of items of a given type are in the inventory.

Parameters:

string itemType The desired type of item

Returns:

int the number of relevant items in the inventory

Inventory:getID ()
Inventory:getID

Returns the inventory's ID.

Returns:

number Inventory ID

Metamethods

Inventory:__tostring ()
Inventory:__tostring

Internal

This is used internally - although you're able to use it you probably shouldn't.

A string representation of this inventory.

Returns:

string A string containing a nice representation of this inventory

Inventory:__eq (other)
Inventory:__eq

Check whether the inventory is the same as another

Parameters:

inv other The other inventory

Returns:

bool True if equal, otherwise False