mirror of
https://github.com/esx-framework/esx_core.git
synced 2026-08-28 17:01:14 +00:00
refactor(ox_inventory): validate module methods and fix hasItem return contract
This commit is contained in:
@@ -4,12 +4,31 @@ if Config.CustomInventory ~= "ox" then
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local requiredMethods = {
|
||||||
|
"GetItem",
|
||||||
|
"AddItem",
|
||||||
|
"RemoveItem",
|
||||||
|
"SetItem",
|
||||||
|
"CanCarryItem",
|
||||||
|
"CanSwapItem",
|
||||||
|
"SetMaxWeight",
|
||||||
|
}
|
||||||
|
|
||||||
---Stores the reference to the internal ox_inventory module.
|
---Stores the reference to the internal ox_inventory module.
|
||||||
---@param module table
|
---@param module table
|
||||||
---@return boolean
|
---@return boolean
|
||||||
|
---@return string? error
|
||||||
local function setOxInventory(module)
|
local function setOxInventory(module)
|
||||||
if type(module) ~= "table" then
|
if type(module) ~= "table" then
|
||||||
return false
|
return false, "module is not a table"
|
||||||
|
end
|
||||||
|
|
||||||
|
for i = 1, #requiredMethods do
|
||||||
|
local method = requiredMethods[i]
|
||||||
|
|
||||||
|
if type(module[method]) ~= "function" then
|
||||||
|
return false, ("missing method %s"):format(method)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
OxInventory = module
|
OxInventory = module
|
||||||
@@ -46,9 +65,11 @@ local function getOxInventory()
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
if not setOxInventory(module) then
|
local ok, err = setOxInventory(module)
|
||||||
|
if not ok then
|
||||||
error(
|
error(
|
||||||
"[es_extended] The Inventory export from ox_inventory returned an invalid module.",
|
("[es_extended] The Inventory export from ox_inventory returned an invalid module: %s")
|
||||||
|
:format(err),
|
||||||
2
|
2
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
@@ -58,8 +79,9 @@ end
|
|||||||
|
|
||||||
-- Standard method used when ox_inventory finishes loading..
|
-- Standard method used when ox_inventory finishes loading..
|
||||||
AddEventHandler("ox_inventory:loadInventory", function(module)
|
AddEventHandler("ox_inventory:loadInventory", function(module)
|
||||||
if not setOxInventory(module) then
|
local ok, err = setOxInventory(module)
|
||||||
print("^1[es_extended] ox_inventory:loadInventory returned an invalid module.^7")
|
if not ok then
|
||||||
|
print(("^1[es_extended] ox_inventory:loadInventory returned an invalid module: %s^7"):format(err))
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
@@ -315,11 +337,17 @@ Core.PlayerFunctionOverrides.OxInventory = {
|
|||||||
|
|
||||||
hasItem = function(self)
|
hasItem = function(self)
|
||||||
return function(name, metadata)
|
return function(name, metadata)
|
||||||
return getOxInventory().GetItem(
|
local item = getOxInventory().GetItem(
|
||||||
self.source,
|
self.source,
|
||||||
name,
|
name,
|
||||||
metadata
|
metadata
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if not item or not item.count or item.count < 1 then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
return item, item.count
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user