From 582ce9e6408b2cacf74216b51382af42d1ca61cf Mon Sep 17 00:00:00 2001 From: MrNewb <47620135+MrNewb@users.noreply.github.com> Date: Sat, 13 Sep 2025 18:04:05 -0400 Subject: [PATCH] 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. --- modules/inventory/codem-inventory/client.lua | 37 ++++++++-------- modules/inventory/codem-inventory/server.lua | 45 ++++++++++---------- 2 files changed, 40 insertions(+), 42 deletions(-) diff --git a/modules/inventory/codem-inventory/client.lua b/modules/inventory/codem-inventory/client.lua index 2fe7e66..870d09f 100644 --- a/modules/inventory/codem-inventory/client.lua +++ b/modules/inventory/codem-inventory/client.lua @@ -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. diff --git a/modules/inventory/codem-inventory/server.lua b/modules/inventory/codem-inventory/server.lua index a0b3e0a..b931a7f 100644 --- a/modules/inventory/codem-inventory/server.lua +++ b/modules/inventory/codem-inventory/server.lua @@ -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