Inventory
Inventory management system for the Lilia framework.
Overview
getData(key, default)
Purpose
Retrieves a stored data value on the inventory.
When Called
Use whenever reading custom inventory metadata.
Parameters
Returns
any Stored value or the provided default.
Example Usage
local owner = inv:getData("char")
getItemsByUniqueID(uniqueID, onlyMain)
Purpose
Returns all items in the inventory matching a uniqueID.
When Called
Use when finding all copies of a specific item type.
Parameters
string uniqueID Item unique identifier.
boolean onlyMain Restrict search to main inventory when true.
Returns
table Array of matching item instances.
Example Usage
local meds = inv:getItemsByUniqueID("medkit")
register(typeID)
Purpose
Registers this inventory type with the system.
When Called
Invoke once per subclass to set type ID and defaults.
Parameters
string typeID Unique identifier for this inventory type.
Example Usage
Inventory:register("bag")
new()
Purpose
Creates a new instance of this inventory type.
When Called
Use when a character or container needs a fresh inventory.
Returns
table Deferred inventory instance creation.
Example Usage
local inv = Inventory:new()
tostring()
Purpose
Formats the inventory as a readable string with its ID.
When Called
Use for logging or debugging output.
Returns
string Localized class name and ID.
Example Usage
print(inv:tostring())
getType()
Purpose
Returns the inventory type definition table.
When Called
Use when accessing type-level configuration.
Returns
table Registered inventory type data.
Example Usage
local typeData = inv:getType()
getItems()
Purpose
Returns the table of item instances in this inventory.
When Called
Use when iterating all items.
Returns
table Item instances keyed by item ID.
Example Usage
for id, itm in pairs(inv:getItems()) do end
getID()
Purpose
Returns the numeric identifier for this inventory.
When Called
Use when networking, saving, or comparing inventories.
Returns
number Inventory ID.
Example Usage
local id = inv:getID()
addItem(item, noReplicate)
Purpose
Inserts an item into this inventory and persists its invID.
When Called
Use when adding an item to the inventory on the server.
Parameters
Returns
Inventory The inventory for chaining.
Example Usage
inv:addItem(item)
syncItemAdded(item)
Purpose
Notifies clients about an item newly added to this inventory.
When Called
Invoked after addItem to replicate state.
Parameters
Item item Item instance already inserted.
Example Usage
inv:syncItemAdded(item)
initializeStorage(initialData)
Purpose
Creates a database record for a new inventory and its data.
When Called
Use during initial inventory creation.
Parameters
table initialData Key/value pairs to seed invdata rows; may include char.
Returns
Promise Resolves with new inventory ID.
Example Usage
inv:initializeStorage({char = charID})
removeItem(itemID, preserveItem)
Purpose
Removes an item from this inventory and updates clients/DB.
When Called
Use when deleting or moving items out of the inventory.
Parameters
number itemID ID of the item to remove.
boolean preserveItem Keep the instance and DB row when true.
Returns
Promise Resolves after removal finishes.
Example Usage
inv:removeItem(itemID)
setData(key, value)
Purpose
Updates inventory data, persists it, and notifies listeners.
When Called
Use to change stored metadata such as character assignment.
Parameters
Returns
Inventory The inventory for chaining.
Example Usage
inv:setData("locked", true)
canAccess(action, context)
addAccessRule(rule, priority)
Purpose
Inserts an access rule into the rule list.
When Called
Use when configuring permissions for this inventory type.
Parameters
function rule Function returning decision and reason.
number priority optional Optional insert position.
Returns
Inventory The inventory for chaining.
Example Usage
inv:addAccessRule(myRule, 1)
getRecipients()
Purpose
Determines which players should receive inventory replication.
When Called
Use before sending inventory data to clients.
Returns
table List of player recipients allowed by access rules.
Example Usage
local recips = inv:getRecipients()
loadItems()
Purpose
Loads item instances from the database into this inventory.
When Called
Use during inventory initialization to restore contents.
Returns
Promise Resolves with the loaded items table.
Example Usage
inv:loadItems():next(function(items) end)
onItemsLoaded(items)
Purpose
Hook called after items are loaded into the inventory.
When Called
Override to run logic after contents are ready.
Parameters
table items Loaded items table.
Example Usage
function Inventory:onItemsLoaded(items) end
onItemsLoaded(items)
Purpose
Hook called after items are loaded into the inventory.
When Called
Override to run logic after contents are ready.
Parameters
table items Loaded items table.
Example Usage
function Inventory:onItemsLoaded(items) end
instance(initialData)
Purpose
Creates and registers an inventory instance with initial data.
When Called
Use to instantiate a server-side inventory of this type.
Parameters
table initialData Data used during creation (e.g., char assignment).
Returns
Promise Resolves with the new inventory instance.
Example Usage
Inventory:instance({char = charID})
syncData(key, recipients)
Purpose
Sends a single inventory data key to recipients.
When Called
Use after setData to replicate a specific field.
Parameters
string key Data key to send.
Player|table recipients optional Targets to notify; defaults to recipients with access.
Example Usage
inv:syncData("locked")
sync(recipients)
Purpose
Sends full inventory state and contained items to recipients.
When Called
Use when initializing or resyncing an inventory for clients.
Parameters
Player|table recipients optional Targets to receive the update; defaults to access list.
Example Usage
inv:sync(ply)