ESX Fallback functions are very basic.

There aren't many native functions for what the script can do, if anyone has any better ideas

- go to `itemcontrol.lua`
- search `ESX` and tell me what could be done better/right
This commit is contained in:
Jim Shield
2025-10-04 01:31:14 +01:00
parent 0bc3c0f9b4
commit 63bcb86ac1

View File

@@ -1304,14 +1304,26 @@ function hasItem(items, amount, src)
-- Fallback to default hasItem function -- Fallback to default hasItem function
if not hasTable or not next(hasTable) then if not hasTable or not next(hasTable) then
for item, amt in pairs(items) do if isStarted(ESXExport) then
local count = 0 for item, amt in pairs(items) do
for _, itemData in pairs(grabInv) do local count = 0
if itemData and itemData.name == item then for itemName, amount in pairs(grabInv) do
count += (itemData.amount or itemData.count or 1) if itemName and itemName == item then
count += (amount or 1)
end
end end
hasTable[item] = { hasItem = count >= amt, count = count }
end
else
for item, amt in pairs(items) do
local count = 0
for _, itemData in pairs(grabInv) do
if itemData and itemData.name == item then
count += (itemData.amount or itemData.count or 1)
end
end
hasTable[item] = { hasItem = count >= amt, count = count }
end end
hasTable[item] = { hasItem = count >= amt, count = count }
end end
end end
@@ -1362,8 +1374,7 @@ function getPlayerInv(src)
local xPlayer = ESX.GetPlayerFromId(src) local xPlayer = ESX.GetPlayerFromId(src)
grabInv = xPlayer and xPlayer.getInventory() or {} grabInv = xPlayer and xPlayer.getInventory() or {}
else else
local xPlayer = ESX.GetPlayerData() grabInv = triggerCallback(getScript()..":GetESXInv")
grabInv = xPlayer and xPlayer.inventory or {}
end end
end end
--jsonPrint(grabInv) --jsonPrint(grabInv)
@@ -1375,6 +1386,15 @@ function getPlayerInv(src)
return grabInv, foundInv return grabInv, foundInv
end end
if isServer() then
createCallback(getScript()..":GetESXInv", function(source)
local src = source
local xPlayer = ESX.GetPlayerFromId(src)
local inv = xPlayer.getInventory()
return xPlayer.getInventory(src)
end)
end
function isInventoryOpen() function isInventoryOpen()
return IsNuiFocused() return IsNuiFocused()
@@ -1559,14 +1579,21 @@ end
RegisterNetEvent(getScript()..":server:toggleItem", function(give, item, amount, newsrc, info, slot, token) RegisterNetEvent(getScript()..":server:toggleItem", function(give, item, amount, newsrc, info, slot, token)
local excludeRes = {
[QBExport] = true,
[ESXExport] = true,
[VorpExport] = true,
}
local invokingRes = GetInvokingResource()
--debugPrint(GetInvokingResource()) --debugPrint(GetInvokingResource())
if GetInvokingResource() and GetInvokingResource() ~= getScript() and GetInvokingResource() ~= "qb-core" then if invokingRes and invokingRes ~= getScript() and not excludeRes[invokingRes] then
debugPrint("^1Error^7: ^1Possible exploit^7, ^1vital function was called from an external resource^7") debugPrint("^1Error^7: ^1Possible exploit^7, ^1vital function was called from an external resource^7")
return return
end end
if not Items[item] then if not doesItemExist(item) then
print("^6Bridge^7: ^1Error^7 - ^2Tried to "..(tostring(give) == "true" and "add" or "remove").." '^3"..item.."^7' but it doesn't exist") print("^1Error^7 - ^2Tried to "..(tostring(give) == "true" and "add" or "remove").." '^3"..item.."^7' but it doesn't exist")
return return
end end
@@ -1960,8 +1987,14 @@ function getCurrentInvWeight(src)
-- fallback function -- fallback function
if weight == 0 then if weight == 0 then
local itemcheck = getPlayerInv(src) local itemcheck = getPlayerInv(src)
for _, v in pairs(itemcheck) do if isStarted(ESXExport) then
weight += ((v.weight * (v.amount or v.count)) or 0) for _, amount in pairs(itemcheck) do
weight += (amount or 0)
end
else
for _, v in pairs(itemcheck) do
weight += ((v.weight * (v.amount or v.count)) or 0)
end
end end
end end
return weight return weight