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 toReturns:
panel The created inventory panelSee 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 addReturns:
inventory The inventory instanceInventory:add (item)
Inventory:add
Sample implementation of Inventory:add - delegates to addItem
Parameters:
Item item The item to addReturns:
inventory The inventory instanceSee also:
Inventory:syncItemAdded (item)
Inventory:syncItemAdded
Syncs a single added item with clients.
Parameters:
Item item The item to syncInventory: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 persistReturns:
number Promise that resolves to the inventory IDInventory: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 restoreInventory: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 removeboolean
preserveItem
optional. default
: false
Whether to keep the item in the DB
Returns:
promise Promise resolved when removal completesInventory:setData (key, value)
Inventory:setData
Stores arbitrary data that can later be looked up using the given key.
Parameters:
string key Metadata keyvararg value Value to store
Returns:
inventory The inventory instanceInventory: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 accesstable context Optional context (e.g. player)
Returns:
bool Whether access is allowedstring 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 instanceInventory:removeAccessRule (rule)
Inventory:removeAccessRule
Removes the first instance of a specific access rule from the inventory.
Parameters:
function rule The rule function to removeReturns:
inventory The inventory instanceInventory:getRecipients ()
Inventory:getRecipients
Returns a list of players who can interact with this inventory.
Returns:
table List of playersInventory: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 objectsInventory:onItemsLoaded (items)
Inventory:onItemsLoaded
Called after items have been loaded into the inventory.
Parameters:
table items The loaded itemsInventory:instance (initialData)
Inventory:instance
Creates an inventory instance via the global inventory system.
Parameters:
table initialData Table of initialization parametersReturns:
inventory The new inventory instanceSee 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 replicateClient
recipients
optional
Override recipients
Inventory:sync (recipients)
Inventory:sync
Sends the full inventory and all contained items information to clients.
Parameters:
Client recipientsoptional
Override recipients
Inventory:delete ()
Inventory:delete
Deletes the inventory using the global deletion handler.
See also:
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 withvararg
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 keyInventory: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 extendInventory: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 tableInventory: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 runfunction 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 typeSee also:
Inventory:new ()
Inventory:new
Creates an instance of this inventory type.
Returns:
An inventory instanceSee also:
Inventory:getType ()
Inventory:getType
Returns the inventory type of this inventory.
Returns:
An inventory objectInventory: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 changedvararg 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 itemsInventory: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 itemReturns:
table A table containing items whose type matchesInventory: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 itemReturns:
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 itemReturns:
bool True if there is such an item, false otherwiseInventory:getItemCount (itemType)
Inventory:getItemCount
Returns the amount of items of a given type are in the inventory.
Parameters:
string itemType The desired type of itemReturns:
int the number of relevant items in the inventoryMetamethods
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.