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:
1 parent
41c90f9082
commit
582ce9e640
2 files changed
+40
-42
No files matched your search
@@ -4,34 +4,33 @@ Inventory = Inventory or {}
|
||||
|
||||
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.
|
||||
---@return string
|
||||
Inventory.GetResourceName = function()
|
||||
return "codem-inventory"
|
||||
end
|
||||
|
||||
|
||||
---This will return the entire items table from the inventory.
|
||||
---@return table
|
||||
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
|
||||
|
||||
---This will get the image path for this item, if not found will return placeholder.
|
||||
|
||||
@@ -24,7 +24,6 @@ Inventory.GetResourceName = function()
|
||||
return "codem-inventory"
|
||||
end
|
||||
|
||||
|
||||
---This will remove an item, and return true or false based on success
|
||||
---@param src number
|
||||
---@param item string
|
||||
@@ -37,28 +36,28 @@ Inventory.RemoveItem = function(src, item, count, slot, metadata)
|
||||
return codem:RemoveItem(src, item, count, slot)
|
||||
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}
|
||||
---@param item string
|
||||
---@return table
|
||||
Inventory.GetItemInfo = function(item)
|
||||
local itemData = codem:GetItemList(item)
|
||||
if not itemData then return {} end
|
||||
local itemData = Inventory.Items()
|
||||
if not itemData or not itemData[item] then return {} end
|
||||
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 = itemData.image or Inventory.GetImagePath(item),
|
||||
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 = itemData[item].image or Inventory.GetImagePath(item),
|
||||
}
|
||||
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.
|
||||
---@param src number
|
||||
---@return table
|
||||
@@ -86,14 +85,14 @@ Inventory.GetItemBySlot = function(src, slot)
|
||||
local slotData = codem:GetItemBySlot(src, slot)
|
||||
if not slotData then return {} end
|
||||
return {
|
||||
name = v.name,
|
||||
label = v.label or v.name,
|
||||
weight = v.weight,
|
||||
slot = v.slot,
|
||||
count = v.amount or v.count,
|
||||
metadata = v.info or v.metadata,
|
||||
stack = v.unique,
|
||||
description = v.description
|
||||
name = slotData.name,
|
||||
label = slotData.label or slotData.name,
|
||||
weight = slotData.weight,
|
||||
slot = slotData.slot,
|
||||
count = slotData.amount or slotData.count,
|
||||
metadata = slotData.info or slotData.metadata,
|
||||
stack = slotData.unique,
|
||||
description = slotData.description
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
Reference in new issue
Block a user