From d0bcb3674b3ffc649fa087fb1db23142d0e1354d Mon Sep 17 00:00:00 2001 From: Ihsan <122683406+rwixy@users.noreply.github.com> Date: Thu, 6 Aug 2026 21:17:52 +0530 Subject: [PATCH] refactor(oxinventory): improve error handling and streamline inventory functions --- .../server/classes/overrides/oxinventory.lua | 319 +++++++++++++----- 1 file changed, 228 insertions(+), 91 deletions(-) diff --git a/[core]/es_extended/server/classes/overrides/oxinventory.lua b/[core]/es_extended/server/classes/overrides/oxinventory.lua index c3be26a8..cdfeaff3 100644 --- a/[core]/es_extended/server/classes/overrides/oxinventory.lua +++ b/[core]/es_extended/server/classes/overrides/oxinventory.lua @@ -1,11 +1,85 @@ -local Inventory +local OxInventory -if Config.CustomInventory ~= "ox" then return end +if Config.CustomInventory ~= "ox" then + return +end +---Stores the reference to the internal ox_inventory module. +---@param module table +---@return boolean +local function setOxInventory(module) + if type(module) ~= "table" then + return false + end + + OxInventory = module + return true +end + +---Returns the internal ox_inventory module. +---If es_extended missed the load event, it will attempt to retrieve it via the export. +---@return table +local function getOxInventory() + if OxInventory then + return OxInventory + end + + local state = GetResourceState("ox_inventory") + + if state ~= "started" then + error( + ("[es_extended] ox_inventory not started; current status: %s") + :format(tostring(state)), + 2 + ) + end + + local success, module = pcall(function() + return exports.ox_inventory:Inventory() + end) + + if not success then + error( + ("[es_extended] Failed to execute exports.ox_inventory:Inventory(): %s") + :format(tostring(module)), + 2 + ) + end + + if not setOxInventory(module) then + error( + "[es_extended] The Inventory export from ox_inventory returned an invalid module.", + 2 + ) + end + + return OxInventory +end + +-- Standard method used when ox_inventory finishes loading.. AddEventHandler("ox_inventory:loadInventory", function(module) - Inventory = module + if not setOxInventory(module) then + print("^1[es_extended] ox_inventory:loadInventory returned an invalid module.^7") + end end) +-- Standard method used when ox_inventory is stopped. +AddEventHandler("onResourceStop", function(resourceName) + if resourceName == "ox_inventory" then + OxInventory = nil + end +end) + +local function emptyMethod() + return function() end +end + +local function falseMethod() + return function() + return false + end +end + Core.PlayerFunctionOverrides.OxInventory = { getInventory = function(self) return function(minimal) @@ -13,26 +87,26 @@ Core.PlayerFunctionOverrides.OxInventory = { return self.inventory end - local minimalInventory = {} + local result = {} - for k, v in pairs(self.inventory) do - if v.count and v.count > 0 then - local metadata = v.metadata + for slot, item in pairs(self.inventory) do + if item.count and item.count > 0 then + local metadata = item.metadata - if v.metadata and next(v.metadata) == nil then + if type(metadata) == "table" and next(metadata) == nil then metadata = nil end - minimalInventory[#minimalInventory + 1] = { - name = v.name, - count = v.count, - slot = k, + result[#result + 1] = { + name = item.name, + count = item.count, + slot = slot, metadata = metadata, } end end - return minimalInventory + return result end end, @@ -45,18 +119,33 @@ Core.PlayerFunctionOverrides.OxInventory = { setAccountMoney = function(self) return function(accountName, money, reason) reason = reason or "unknown" - if money < 0 then return end + + if money < 0 then + return + end + local account = self.getAccount(accountName) - if not account then return end + if not account then + return + end money = account.round and ESX.Math.Round(money) or money self.accounts[account.index].money = money self.triggerEvent("esx:setAccountMoney", account) - TriggerEvent("esx:setAccountMoney", self.source, accountName, money, reason) - if Inventory.accounts[accountName] then - Inventory.SetItem(self.source, accountName, money) + TriggerEvent( + "esx:setAccountMoney", + self.source, + accountName, + money, + reason + ) + + local inventory = getOxInventory() + + if inventory.accounts[accountName] then + inventory.SetItem(self.source, accountName, money) end end end, @@ -64,17 +153,34 @@ Core.PlayerFunctionOverrides.OxInventory = { addAccountMoney = function(self) return function(accountName, money, reason) reason = reason or "unknown" - if money < 1 then return end + + if money < 1 then + return + end local account = self.getAccount(accountName) - if not account then return end + + if not account then + return + end money = account.round and ESX.Math.Round(money) or money - self.accounts[account.index].money = self.accounts[account.index].money + money + self.accounts[account.index].money = + self.accounts[account.index].money + money + self.triggerEvent("esx:setAccountMoney", account) - TriggerEvent("esx:addAccountMoney", self.source, accountName, money, reason) - if Inventory.accounts[accountName] then - Inventory.AddItem(self.source, accountName, money) + TriggerEvent( + "esx:addAccountMoney", + self.source, + accountName, + money, + reason + ) + + local inventory = getOxInventory() + + if inventory.accounts[accountName] then + inventory.AddItem(self.source, accountName, money) end end end, @@ -82,54 +188,103 @@ Core.PlayerFunctionOverrides.OxInventory = { removeAccountMoney = function(self) return function(accountName, money, reason) reason = reason or "unknown" - if money < 1 then return end + + if money < 1 then + return + end local account = self.getAccount(accountName) - if not account then return end + + if not account then + return + end money = account.round and ESX.Math.Round(money) or money - self.accounts[account.index].money = self.accounts[account.index].money - money + self.accounts[account.index].money = + self.accounts[account.index].money - money + self.triggerEvent("esx:setAccountMoney", account) - TriggerEvent("esx:removeAccountMoney", self.source, accountName, money, reason) - if Inventory.accounts[accountName] then - Inventory.RemoveItem(self.source, accountName, money) + TriggerEvent( + "esx:removeAccountMoney", + self.source, + accountName, + money, + reason + ) + + local inventory = getOxInventory() + + if inventory.accounts[accountName] then + inventory.RemoveItem(self.source, accountName, money) end end end, getInventoryItem = function(self) return function(name, metadata) - return Inventory.GetItem(self.source, name, metadata) + return getOxInventory().GetItem( + self.source, + name, + metadata + ) end end, addInventoryItem = function(self) return function(name, count, metadata, slot) - return Inventory.AddItem(self.source, name, count or 1, metadata, slot) + return getOxInventory().AddItem( + self.source, + name, + count or 1, + metadata, + slot + ) end end, removeInventoryItem = function(self) return function(name, count, metadata, slot) - return Inventory.RemoveItem(self.source, name, count or 1, metadata, slot) + return getOxInventory().RemoveItem( + self.source, + name, + count or 1, + metadata, + slot + ) end end, setInventoryItem = function(self) return function(name, count, metadata) - return Inventory.SetItem(self.source, name, count, metadata) + return getOxInventory().SetItem( + self.source, + name, + count, + metadata + ) end end, canCarryItem = function(self) return function(name, count, metadata) - return Inventory.CanCarryItem(self.source, name, count, metadata) + return getOxInventory().CanCarryItem( + self.source, + name, + count, + metadata + ) end end, canSwapItem = function(self) return function(firstItem, firstItemCount, testItem, testItemCount) - return Inventory.CanSwapItem(self.source, firstItem, firstItemCount, testItem, testItemCount) + return getOxInventory().CanSwapItem( + self.source, + firstItem, + firstItemCount, + testItem, + testItemCount + ) end end, @@ -137,83 +292,65 @@ Core.PlayerFunctionOverrides.OxInventory = { return function(newWeight) self.maxWeight = newWeight self.triggerEvent("esx:setMaxWeight", self.maxWeight) - return Inventory.SetMaxWeight(self.source, newWeight) + + return getOxInventory().SetMaxWeight( + self.source, + newWeight + ) end end, - addWeapon = function() - return function() end - end, + addWeapon = emptyMethod, + addWeaponComponent = emptyMethod, + addWeaponAmmo = emptyMethod, + updateWeaponAmmo = emptyMethod, + setWeaponTint = emptyMethod, + getWeaponTint = emptyMethod, + removeWeapon = emptyMethod, + removeWeaponComponent = emptyMethod, + removeWeaponAmmo = emptyMethod, - addWeaponComponent = function() - return function() end - end, - - addWeaponAmmo = function() - return function() end - end, - - updateWeaponAmmo = function() - return function() end - end, - - setWeaponTint = function() - return function() end - end, - - getWeaponTint = function() - return function() end - end, - - removeWeapon = function() - return function() end - end, - - removeWeaponComponent = function() - return function() end - end, - - removeWeaponAmmo = function() - return function() end - end, - - hasWeaponComponent = function() - return function() - return false - end - end, - - hasWeapon = function() - return function() - return false - end - end, + hasWeaponComponent = falseMethod, + hasWeapon = falseMethod, hasItem = function(self) return function(name, metadata) - return Inventory.GetItem(self.source, name, metadata) + return getOxInventory().GetItem( + self.source, + name, + metadata + ) end end, - getWeapon = function() - return function() end - end, + getWeapon = emptyMethod, syncInventory = function(self) return function(weight, maxWeight, items, money) - self.weight, self.maxWeight = weight, maxWeight + self.weight = weight + self.maxWeight = maxWeight self.inventory = items - if not money then return end + if not money then + return + end + for accountName, amount in pairs(money) do local account = self.getAccount(accountName) if account and ESX.Math.Round(account.money) ~= amount then account.money = amount + self.triggerEvent("esx:setAccountMoney", account) - TriggerEvent("esx:setAccountMoney", self.source, accountName, amount, "Sync account with item") + TriggerEvent( + "esx:setAccountMoney", + self.source, + accountName, + amount, + "Sync account with item" + ) end end end end, -} +} \ No newline at end of file