1--! \page global_variables Global Variables
2--! \section Inventories Inventories
4--! See Inventories
for the
module on inventories.\n\n
5--! FrameworkZ.Inventories.List\n
6--! A list of all instanced inventories in the game.\n\n
7--! FrameworkZ.Inventories.Types\n
8--! The types of inventories that can be created.
10FrameworkZ = FrameworkZ or {}
12--! \brief The Inventories module for FrameworkZ. Defines and interacts with INVENTORY object.
13--! \class FrameworkZ.Inventories
14FrameworkZ.Inventories = {}
15FrameworkZ.Inventories.__index = FrameworkZ.Inventories
16FrameworkZ.Inventories.List = {}
17FrameworkZ.Inventories.Types = {
18 Character = "Character",
19 Container = "Container",
22FrameworkZ.Inventories = FrameworkZ.Foundation:NewModule(FrameworkZ.Inventories, "Inventories")
24--! \brief Inventory class for FrameworkZ.
27INVENTORY.__index = INVENTORY
29--! \brief Initialize an inventory.
30--! \return \string The inventory's ID.
31function INVENTORY:Initialize()
32 return FrameworkZ.Inventories:Initialize(self.id, self)
35--! \brief Add an item to the inventory.
36--! \details Note: This does not add a world item, it simply adds it to the inventory's object. Please use CHARACTER::GiveItem(uniqueID) to add an item to a character's inventory along with the world item.
37--! \param item \string The item's ID.
38--! \see CHARACTER::GiveItem(uniqueID)
39function INVENTORY:AddItem(item)
40 local inventoryIndex = #self.items + 1
42 item["inventoryIndex"] = inventoryIndex
43 self.items[inventoryIndex] = item
46function INVENTORY:RemoveItem(item)
47 if not item then return false, "No item provided." end
48 if not item.inventoryIndex then return false, "Item does not have an inventory index." end
50 self.items[item.inventoryIndex] = nil
52 return true, "Item removed from inventory #" .. self.id
55--! \brief Add multiple items to the inventory.
56--! \details Note: This does not add a world item, it simply adds it to the inventory's object. Please use CHARACTER::GiveItems(uniqueID) to add an items to a character's inventory along with the world item.
57--! \param uniqueID \string The item's ID.
58--! \param quantity \integer The quantity of the item to add.
59--! \see CHARACTER::GiveItems(uniqueID)
60function INVENTORY:AddItems(uniqueID, quantity)
61 for i = 1, quantity do
62 self:AddItem(uniqueID)
66function INVENTORY:GetItems()
70function INVENTORY:GetItemByUniqueID(uniqueID)
71 if not uniqueID or uniqueID == "" then return false, "No unique ID provided." end
73 for _key, item in pairs(self:GetItems()) do
74 if item.uniqueID == uniqueID then
79 return false, "No item found with unique ID: " .. uniqueID
82function INVENTORY:GetItemCountByID(uniqueID)
83 if not uniqueID or uniqueID == "" then return false, "No unique ID provided." end
87 for _key, item in pairs(self:GetItems()) do
88 if item.uniqueID == uniqueID then
96--! \brief Get the inventory's name.
97--! \return \string The inventory's name.
98function INVENTORY:GetName()
99 return self.name or "Someone's Inventory"
102function INVENTORY:GetSaveableData()
103 return FrameworkZ.Foundation:ProcessSaveableData(self, {}, {})
106--! \brief Create a new inventory object.
107--! \param username \string The owner's username. Can be nil for no owner.
108--! \param type \string The type of inventory. Can be nil, but creates a character inventory type by default. Refer to FrameworkZ.Inventories.Types table for available types.
109--! \param id \string The inventory's ID. Can be nil for an auto generated ID (recommended).
110--! \return \table The new inventory object.
111function FrameworkZ.Inventories:New(username, type, id)
113 FrameworkZ.Inventories.List[#FrameworkZ.Inventories.List + 1] = {} -- Reserve space to avoid inconsistencies.
114 id = #FrameworkZ.Inventories.List
119 owner = username or "",
120 type = type or FrameworkZ.Inventories.Types.Character,
121 name = "Someone's Inventory",
122 description = "No description available.",
126 setmetatable(object, INVENTORY)
131--! \brief Initialize an inventory.
132--! \param id \table The inventory's id.
133--! \param object \table The inventory's object.
134--! \return \integer The inventory's ID.
135function FrameworkZ.Inventories:Initialize(id, object)
136 FrameworkZ.Inventories.List[id] = object
141function FrameworkZ.Inventories:GetInventoryByID(id)
142 if not id then return false, "No inventory ID provided." end
144 local inventory = self.List[id] or nil
146 if not inventory then return false, "No inventory found with ID: " .. id end
151function FrameworkZ.Inventories:GetItemByUniqueID(inventoryID, uniqueID)
152 if not inventoryID then return false, "No inventory ID provided." end
153 if not uniqueID or uniqueID == "" then return false, "No unique ID provided." end
155 local inventoryOrSuccess, inventoryMessage = self:GetInventoryByID(inventoryID)
157 if not inventoryOrSuccess then return inventoryOrSuccess, inventoryMessage end
159 local itemOrSuccess, itemMessage = inventoryOrSuccess:GetItemByUniqueID(uniqueID)
161 return itemOrSuccess, itemMessage
164function FrameworkZ.Inventories:GetItemCountByID(inventoryID, uniqueID)
165 if not inventoryID then return false, "No inventory ID provided." end
166 if not uniqueID or uniqueID == "" then return false, "No unique ID provided." end
168 local inventoryOrSuccess, inventoryMessage = self:GetInventoryByID(inventoryID)
170 if not inventoryOrSuccess then return inventoryOrSuccess, inventoryMessage end
172 local countOrSuccess, countMessage = inventoryOrSuccess:GetItemCountByID(uniqueID)
174 return countOrSuccess, countMessage
177--! \brief Recursively traverses the inventory table for missing data while referencing the item definitions to rebuild the inventory.
178--! \param inventory \table The inventory to rebuild.
179--! \return \table The rebuilt inventory.
180function FrameworkZ.Inventories:Rebuild(isoPlayer, inventory, items)
181 if not isoPlayer then return false, "No ISO Player to add items to." end
182 if not inventory then return false, "No inventory to rebuild." end
183 if not items then return false, "No items to add to inventory." end
185 -- Recursive function to rebuild fields and inherit methods
186 local function rebuildAndInherit(item, definition)
187 -- Ensure item inherits methods and properties from the definition
188 setmetatable(item, { __index = definition })
190 -- Recursively rebuild all fields
191 for key, value in pairs(definition) do
192 if type(value) == "table" then
193 -- Ensure item[key] exists and is a table, then recurse
194 if item[key] == nil then
198 rebuildAndInherit(item[key], value)
199 elseif type(value) == "function" then
200 -- Ensure functions are inherited and retain their object context
202 elseif item[key] == nil then
203 -- Copy over non-function and non-table fields if missing
209 -- Rebuild an individual item
210 local function rebuildItem(item)
211 if type(item) ~= "table" then return end -- Ensure item is a table
213 -- Fetch the item definition
214 local itemDefinition = FrameworkZ.Items:GetItemByUniqueID(item.uniqueID)
215 if not itemDefinition then return end -- Exit if no definition is found
217 -- Rebuild fields and inherit methods
218 rebuildAndInherit(item, itemDefinition)
220 -- Create and link the world item
221 local success, message, worldItem = FrameworkZ.Items:CreateWorldItem(isoPlayer, item.itemID)
222 if success and worldItem then
223 local instanceID, itemInstance = FrameworkZ.Items:AddInstance(item, isoPlayer, worldItem)
225 -- Define instance data
226 local instanceData = {
227 uniqueID = itemInstance.uniqueID,
228 itemID = worldItem:getFullType(),
229 instanceID = instanceID,
230 owner = isoPlayer:getUsername(),
231 name = itemInstance.name or "Unknown",
232 description = itemInstance.description or "No description available.",
233 category = itemInstance.category or "Uncategorized",
234 shouldConsume = itemInstance.shouldConsume or false,
235 weight = itemInstance.weight or 1,
236 useAction = itemInstance.useAction or nil,
237 useTime = itemInstance.useTime or nil,
238 customFields = itemInstance.customFields or {}
241 -- Link the world item to the instance data
242 FrameworkZ.Items:LinkWorldItemToInstanceData(worldItem, instanceData)
244 -- Add the item instance to the inventory
245 inventory:AddItem(itemInstance)
247 -- Call OnInstance if it exists
248 if item.OnInstance then
249 item:OnInstance(isoPlayer, inventory, worldItem)
254 -- Iterate through and rebuild each item
255 for _, item in pairs(items) do
259 return true, "Inventory rebuilt.", inventory
262FrameworkZ.Foundation:RegisterModule(FrameworkZ.Inventories)
void local itemDefinition()
void local instanceData()
void local itemInstance()
The Inventories module for FrameworkZ. Defines and interacts with INVENTORY object.
FrameworkZ Inventories Types
FrameworkZ Inventories __index
FrameworkZ Inventories List
Inventory class for FrameworkZ.
void GetItemByUniqueID(uniqueID)
void AddItem(item)
Add an item to the inventory.
void GetItemCountByID(uniqueID)
void AddItems(uniqueID, quantity)
Add multiple items to the inventory.
string GetName()
Get the inventory's name.
string Initialize()
Initialize an inventory.