FrameworkZ 10.8.3
Provides a framework for Project Zomboid with various systems.
Loading...
Searching...
No Matches
Inventories.lua
Go to the documentation of this file.
1--! \page global_variables Global Variables
2--! \section Inventories Inventories
3--! FrameworkZ.Inventories\n
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.
9
10FrameworkZ = FrameworkZ or {}
11
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",
20 Vehicle = "Vehicle"
22FrameworkZ.Inventories = FrameworkZ.Foundation:NewModule(FrameworkZ.Inventories, "Inventories")
24--! \brief Inventory class for FrameworkZ.
25--! \class INVENTORY
26local INVENTORY = {}
27INVENTORY.__index = INVENTORY
28
29--! \brief Initialize an inventory.
30--! \return \string The inventory's ID.
31function INVENTORY:Initialize()
32 return FrameworkZ.Inventories:Initialize(self.id, self)
33end
34
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
41
42 item["inventoryIndex"] = inventoryIndex
43 self.items[inventoryIndex] = item
44end
45
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
49
50 self.items[item.inventoryIndex] = nil
51
52 return true, "Item removed from inventory #" .. self.id
53end
54
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)
63 end
64end
66function INVENTORY:GetItems()
67 return self.items
68end
69
70function INVENTORY:GetItemByUniqueID(uniqueID)
71 if not uniqueID or uniqueID == "" then return false, "No unique ID provided." end
72
73 for _key, item in pairs(self:GetItems()) do
74 if item.uniqueID == uniqueID then
75 return item
76 end
77 end
78
79 return false, "No item found with unique ID: " .. uniqueID
80end
81
82function INVENTORY:GetItemCountByID(uniqueID)
83 if not uniqueID or uniqueID == "" then return false, "No unique ID provided." end
85 local count = 0
87 for _key, item in pairs(self:GetItems()) do
88 if item.uniqueID == uniqueID then
89 count = count + 1
90 end
91 end
93 return count
94end
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"
100end
102function INVENTORY:GetSaveableData()
103 return FrameworkZ.Foundation:ProcessSaveableData(self, {}, {})
104end
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)
112 if not id then
113 FrameworkZ.Inventories.List[#FrameworkZ.Inventories.List + 1] = {} -- Reserve space to avoid inconsistencies.
114 id = #FrameworkZ.Inventories.List
115 end
116
117 local object = {
118 id = id,
119 owner = username or "",
120 type = type or FrameworkZ.Inventories.Types.Character,
121 name = "Someone's Inventory",
122 description = "No description available.",
123 items = {}
124 }
126 setmetatable(object, INVENTORY)
128 return object
129end
130
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
137
138 return id
139end
140
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
148 return inventory
149end
150
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
154
155 local inventoryOrSuccess, inventoryMessage = self:GetInventoryByID(inventoryID)
156
157 if not inventoryOrSuccess then return inventoryOrSuccess, inventoryMessage end
158
159 local itemOrSuccess, itemMessage = inventoryOrSuccess:GetItemByUniqueID(uniqueID)
160
161 return itemOrSuccess, itemMessage
162end
163
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
167
168 local inventoryOrSuccess, inventoryMessage = self:GetInventoryByID(inventoryID)
169
170 if not inventoryOrSuccess then return inventoryOrSuccess, inventoryMessage end
171
172 local countOrSuccess, countMessage = inventoryOrSuccess:GetItemCountByID(uniqueID)
173
174 return countOrSuccess, countMessage
175end
176
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
184
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 })
189
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
195 item[key] = {}
196 end
197
198 rebuildAndInherit(item[key], value)
199 elseif type(value) == "function" then
200 -- Ensure functions are inherited and retain their object context
201 item[key] = value
202 elseif item[key] == nil then
203 -- Copy over non-function and non-table fields if missing
204 item[key] = value
205 end
206 end
207 end
208
209 -- Rebuild an individual item
210 local function rebuildItem(item)
211 if type(item) ~= "table" then return end -- Ensure item is a table
212
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
216
217 -- Rebuild fields and inherit methods
218 rebuildAndInherit(item, itemDefinition)
219
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)
224
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 {}
239 }
240
241 -- Link the world item to the instance data
242 FrameworkZ.Items:LinkWorldItemToInstanceData(worldItem, instanceData)
243
244 -- Add the item instance to the inventory
245 inventory:AddItem(itemInstance)
246
247 -- Call OnInstance if it exists
248 if item.OnInstance then
249 item:OnInstance(isoPlayer, inventory, worldItem)
250 end
251 end
252 end
253
254 -- Iterate through and rebuild each item
255 for _, item in pairs(items) do
256 rebuildItem(item)
257 end
258
259 return true, "Inventory rebuilt.", inventory
260end
261
262FrameworkZ.Foundation:RegisterModule(FrameworkZ.Inventories)
void elseif type value()
void id()
void shouldConsume()
void local success
void uniqueID()
void customFields()
void items()
void local object()
void local instanceID
void weight()
void description()
void useTime()
void item()
void category()
void local itemDefinition()
void local worldItem()
void itemID()
void owner()
void local instanceData()
void type()
void name()
void local itemInstance()
void useAction()
void FrameworkZ()
void local message
void self username()
void key()
void isoPlayer()
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 GetItems()
void GetItemCountByID(uniqueID)
void AddItems(uniqueID, quantity)
Add multiple items to the inventory.
void GetSaveableData()
string GetName()
Get the inventory's name.
INVENTORY __index
string Initialize()
Initialize an inventory.
void RemoveItem(item)