Refactor item info retrieval in inventory modules

Updated Inventory.GetItemInfo to use the full items table for lookups instead of fetching individual items. Added Inventory.Items function to both client and server for consistent access to the items table. Improved handling of missing data and streamlined code for better maintainability.
This commit is contained in:
MrNewb committed 2025-09-13 18:04:05 -04:00
1 parent 41c90f9082
commit 582ce9e640
2 files changed
+40 -42

No files matched your search

+18 -19
View File
@@ -4,34 +4,33 @@ Inventory = Inventory or {}
local codem = exports['codem-inventory'] local codem = exports['codem-inventory']
---Return the item info in oxs format, {name, label, stack, weight, description, image}
---@param item string
---@return table
Inventory.GetItemInfo = function(item)
local itemData = codem:GetItemList(item)
if not itemData then return {} end
local image = itemData.image and Inventory.GetImagePath(itemData.image) or Inventory.GetImagePath(item)
return {
name = itemData.name or "Missing Name",
label = itemData.label or "Missing Label",
stack = itemData.unique or "false",
weight = itemData.weight or "0",
description = itemData.description or "none",
image = image,
}
end
---This will get the name of the in use resource. ---This will get the name of the in use resource.
---@return string ---@return string
Inventory.GetResourceName = function() Inventory.GetResourceName = function()
return "codem-inventory" return "codem-inventory"
end end
---This will return the entire items table from the inventory. ---This will return the entire items table from the inventory.
---@return table ---@return table
Inventory.Items = function() Inventory.Items = function()
return codem:GetItemList() return codem:GetItemList() or {}
end
---Return the item info in oxs format, {name, label, stack, weight, description, image}
---@param item string
---@return table
Inventory.GetItemInfo = function(item)
local itemData = Inventory.Items()
if not itemData or not itemData[item] then return {} end
local image = Inventory.GetImagePath(itemData[item].image) or Inventory.GetImagePath(item)
return {
name = itemData[item].name or "Missing Name",
label = itemData[item].label or "Missing Label",
stack = itemData[item].unique or "false",
weight = itemData[item].weight or "0",
description = itemData[item].description or "none",
image = image,
}
end end
---This will get the image path for this item, if not found will return placeholder. ---This will get the image path for this item, if not found will return placeholder.
+22 -23
View File
@@ -24,7 +24,6 @@ Inventory.GetResourceName = function()
return "codem-inventory" return "codem-inventory"
end end
---This will remove an item, and return true or false based on success ---This will remove an item, and return true or false based on success
---@param src number ---@param src number
---@param item string ---@param item string
@@ -37,28 +36,28 @@ Inventory.RemoveItem = function(src, item, count, slot, metadata)
return codem:RemoveItem(src, item, count, slot) return codem:RemoveItem(src, item, count, slot)
end end
---This will return the entire items table from the inventory.
---@return table
Inventory.Items = function()
return codem:GetItemList() or {}
end
---This will return a table with the item info, {name, label, stack, weight, description, image} ---This will return a table with the item info, {name, label, stack, weight, description, image}
---@param item string ---@param item string
---@return table ---@return table
Inventory.GetItemInfo = function(item) Inventory.GetItemInfo = function(item)
local itemData = codem:GetItemList(item) local itemData = Inventory.Items()
if not itemData then return {} end if not itemData or not itemData[item] then return {} end
return { return {
name = itemData.name or "Missing Name", name = itemData[item].name or "Missing Name",
label = itemData.label or "Missing Label", label = itemData[item].label or "Missing Label",
stack = itemData.unique or "false", stack = itemData[item].unique or "false",
weight = itemData.weight or "0", weight = itemData[item].weight or "0",
description = itemData.description or "none", description = itemData[item].description or "none",
image = itemData.image or Inventory.GetImagePath(item), image = itemData[item].image or Inventory.GetImagePath(item),
} }
end end
---This will return the entire items table from the inventory.
---@return table
Inventory.Items = function()
return codem:GetItemList()
end
---This wil return the players inventory. ---This wil return the players inventory.
---@param src number ---@param src number
---@return table ---@return table
@@ -86,14 +85,14 @@ Inventory.GetItemBySlot = function(src, slot)
local slotData = codem:GetItemBySlot(src, slot) local slotData = codem:GetItemBySlot(src, slot)
if not slotData then return {} end if not slotData then return {} end
return { return {
name = v.name, name = slotData.name,
label = v.label or v.name, label = slotData.label or slotData.name,
weight = v.weight, weight = slotData.weight,
slot = v.slot, slot = slotData.slot,
count = v.amount or v.count, count = slotData.amount or slotData.count,
metadata = v.info or v.metadata, metadata = slotData.info or slotData.metadata,
stack = v.unique, stack = slotData.unique,
description = v.description description = slotData.description
} }
end end