Rewrite + Optimize hasItem() & stashhasItem()

This commit is contained in:
Jim Shield
2024-01-19 13:00:50 +00:00
committed by GitHub
parent 02c202d075
commit e5316f45aa

View File

@@ -225,149 +225,66 @@ function openShop(data)
end end
-- Client & Server side -- Client & Server side
function hasItem(items, amount, src) local amount, count = amount and amount or 1, 0 function hasItem(items, amount, src) local amount = amount and amount or 1
if src then local grabInv = nil
local foundInv = ""
if type(items) ~= "table" then items = { [items] = amount and amount or 1, } end
if GetResourceState(OXInv):find("start") then if GetResourceState(OXInv):find("start") then
local item = exports[OXInv]:GetItem(src, items) foundInv = OXInv
local amount = (amount or 1) if src then grabInv = exports.ox_inventory:GetInventoryItems(src)
if item.count >= amount then else grabInv = exports[OXInv]:GetPlayerItems()
if Config.System.Debug then print("^6Bridge^7: ^3HasItem^7: ^5FOUND^7 ^3"..item.count.."^7/^3"..amount.." "..tostring(items)) end
return true
else if Config.System.Debug then
print("^6Bridge^7: ^3HasItem^7: ^2"..tostring(items).." ^1NOT FOUND^7") end
return false
end end
elseif GetResourceState(QSInv):find("start") then elseif GetResourceState(QSInv):find("start") then
for _, itemData in pairs(exports[QSInv]:GetInventory(src)) do foundInv = QSInv
if itemData and (itemData.name == items) then if src then grabInv = exports[QSInv]:GetInventory(src)
--if Config.System.Debug then print("^6Bridge^7: ^3HasItem^7: ^2Item^7: '^3"..tostring(items).."^7' ^2Slot^7: ^3"..itemData.slot.." ^7x(^3"..tostring(itemData.amount).."^7)") end else grabInv = exports[QSInv]:getUserInventory()
count += (itemData.amount or 1)
end
end
if count >= amount then
if Config.System.Debug then print("^6Bridge^7: ^3HasItem^7: ^5FOUND^7 ^3"..count.."^7/^3"..amount.." "..tostring(items)) end
return true
else
if Config.System.Debug then print("^6Bridge^7: ^3HasItem^7: ^2"..tostring(items).." ^1NOT FOUND^7") end
return false
end end
elseif GetResourceState(CoreInv):find("start") then elseif GetResourceState(CoreInv):find("start") then
local item = exports[CoreInv]:getItems('primary-'..src, items) foundInv = CoreInv
if src then grabInv = exports['core_inventory']:getInventory('primary-'..src)
if item.amount >= amount then else grabInv = exports[CoreInv]:getInventory('primary-'..GetPlayerServerId(PlayerPedId()))
if Config.System.Debug then print("^6Bridge^7: ^3HasItem^7: ^5FOUND^7 ^3"..item.amount.."^7/^3"..amount.." "..tostring(items)) end
return true
else if Config.System.Debug then
print("^6Bridge^7: ^3HasItem^7: ^2"..tostring(items).." ^1NOT FOUND^7") end
return false
end end
elseif GetResourceState(CodeMInv):find("start") then elseif GetResourceState(CodeMInv):find("start") then
local success = exports["codem-inventory"]:CheckItemValid(src, items, amount) foundInv = CodeMInv
if success then if src then grabInv = exports[CodeMInv]:GetUserInventory(src)
if Config.System.Debug then print("^6Bridge^7: ^3HasItem^7: ^5FOUND^7 ^3"..amount.." "..tostring(items)) end else grabInv = exports[CodeMInv]:GetClientPlayerInventory()
return true
end
elseif GetResourceState(CodeMInv):find("start") then
for _, itemData in pairs(exports[CodeMInv]:GetUserInventory(src)) do
if itemData and (itemData.name == items) then
if Config.System.Debug then print("^6Bridge^7: ^3HasItem^7: ^2Item^7: '^3"..tostring(items).."^7' ^2Slot^7: ^3"..itemData.slot.." ^7x(^3"..tostring(itemData.amount).."^7)") end
count += (itemData.amount or 1)
end
end
if count >= amount then
if Config.System.Debug then print("^6Bridge^7: ^3HasItem^7: ^5FOUND^7 ^3"..count.."^7/^3"..amount.." "..tostring(items)) end
return true
else
if Config.System.Debug then print("^6Bridge^7: ^3HasItem^7: ^2"..tostring(items).." ^1NOT FOUND^7") end
return false
end
else
for _, itemData in pairs(Core.Functions.GetPlayer(src).PlayerData.items) do
if itemData and (itemData.name == items) then
if Config.System.Debug then print("^6Bridge^7: ^3HasItem^7: ^2Item^7: '^3"..tostring(items).."^7' ^2Slot^7: ^3"..itemData.slot.." ^7x(^3"..tostring(itemData.amount).."^7)") end
count += (itemData.amount or 1)
end
end
if count >= amount then
if Config.System.Debug then print("^6Bridge^7: ^3HasItem^7: ^5FOUND^7 ^3"..count.."^7/^3"..amount.." "..tostring(items)) end
return true
else
if Config.System.Debug then print("^6Bridge^7: ^3HasItem^7: ^2"..tostring(items).." ^1NOT FOUND^7") end
return false
end
end
else
if GetResourceState(OXInv):find("start") then
local count = tonumber(exports[OXInv]:Search('count', items))
local amount = (amount or 1)
if count >= amount then if Config.System.Debug then print("^6Bridge^7: ^3HasItem^7: ^5FOUND^7 ^3"..count.."^7/^3"..amount.." "..tostring(items)) end return true
else if Config.System.Debug then print("^6Bridge^7: ^3HasItem^7: ^2"..tostring(items).." ^1NOT FOUND^7") end return false end
elseif GetResourceState(QSInv):find("start") then
for _, itemData in pairs(exports[QSInv]:getUserInventory()) do
if itemData and (itemData.name == items) then
if Config.System.Debug then print("^6Bridge^7: ^3HasItem^7: ^2Item^7: '^3"..tostring(items).."^7' ^2Slot^7: ^3"..itemData.slot.." ^7x(^3"..tostring(itemData.amount).."^7)") end
count += (itemData.amount or 1)
end
end
if count >= amount then
if Config.System.Debug then print("^6Bridge^7: ^3HasItem^7: ^5FOUND^7 ^3"..count.."^7/^3"..amount.." "..tostring(items)) end
return true
else
if Config.System.Debug then print("^6Bridge^7: ^3HasItem^7: ^2"..tostring(items).." ^1NOT FOUND^7") end
return false
end
elseif GetResourceState(CoreInv):find("start") then
for _, itemData in pairs(exports[CoreInv]:getInventory('primary-'..GetPlayerServerId(PlayerPedId()))) do
if itemData and (itemData.name == items) then
if Config.System.Debug then print("^6Bridge^7: ^3HasItem^7: ^2Item^7: '^3"..tostring(items).."^7' ^2Slot^7: ^3"..itemData.slot.." ^7x(^3"..tostring(itemData.amount).."^7)") end
count += (itemData.amount or 1)
end
end
if count >= amount then
if Config.System.Debug then print("^6Bridge^7: ^3HasItem^7: ^5FOUND^7 ^3"..count.."^7/^3"..amount.." "..tostring(items)) end
return true
else
if Config.System.Debug then print("^6Bridge^7: ^3HasItem^7: ^2"..tostring(items).." ^1NOT FOUND^7") end
return false
end
elseif GetResourceState(CodeMInv):find("start") then
local success = exports[CodeMInv]:CheckItemValid(items, amount)
if success then
if Config.System.Debug then print("^6Bridge^7: ^3HasItem^7: ^5FOUND^7 ^3"..amount.." "..tostring(items)) end
return true
end end
elseif GetResourceState(QBInv):find("start") then elseif GetResourceState(QBInv):find("start") then
--if tonumber(GetResourceMetadata(QBInv, 'version')) <= 1.2 then foundInv = QBInv
-- return exports[QBInv]:HasItem(items, amount) if src then grabInv = Core.Functions.GetPlayer(src).PlayerData.items
--else else grabInv = Core.Functions.GetPlayerData().items
for _, itemData in pairs(Core.Functions.GetPlayerData().items) do end
if itemData and (itemData.name == items) then
if Config.System.Debug then print("^6Bridge^7: ^3HasItem^7: ^2Item^7: '^3"..tostring(items).."^7' ^2Slot^7: ^3"..itemData.slot.." ^7x(^3"..tostring(itemData.amount).."^7)") end else
count += (itemData.amount or 1) print("^4ERROR^7: ^2No Inventory detected ^7- ^2Check ^3exports^1.^2lua^7")
end
if grabInv then
local hasTable = {}
for item, amount in pairs(items) do
local count = 0
for _, itemData in pairs(grabInv) do
if itemData and (itemData.name == item) then
--if Config.System.Debug then print("^6Bridge^7: ^3HasItem^7: ^2Item^7: '^3"..tostring(item).."^7' ^2Slot^7: ^3"..itemData.slot.." ^7x(^3"..tostring(itemData.count or itemData.amount).."^7)", foundInv) end
count += ((itemData.count or itemData.amount) or 1)
end end
end end
if count >= amount then if count >= amount then
if Config.System.Debug then if Config.System.Debug then print("^6Bridge^7: ^3HasItem^7: ^5FOUND^7 ^3"..count.."^7/^3"..amount.." "..tostring(item).."^7", foundInv) end
print("^6Bridge^7: ^3HasItem^7: ^5FOUND^7 ^3"..count.."^7/^3"..amount.." "..tostring(items)) hasTable[item] = { hasItem = true, count = count, }
end
return true
else else
if Config.System.Debug then if Config.System.Debug then
print("^6Bridge^7: ^3HasItem^7: ^2"..tostring(items).." ^1NOT FOUND^7") print("^6Bridge^7: ^3HasItem^7: ^2"..tostring(item).." ^1NOT FOUND^7", foundInv)
end end
return false hasTable[item] = { hasItem = false, count = count, }
end end
-- end
end end
for k, v in pairs(hasTable) do if v.hasItem == false then return false, hasTable end end
return true, hasTable
end end
end end
@@ -485,22 +402,26 @@ function stashRemoveItem(stashItems, stashName, items) local amount = amount and
end end
RegisterNetEvent(GetCurrentResourceName()..":server:stashRemoveItem", stashRemoveItem) RegisterNetEvent(GetCurrentResourceName()..":server:stashRemoveItem", stashRemoveItem)
function stashhasItem(stashItems, item, amount) local amount, count = amount and amount or 1, 0 function stashhasItem(stashItems, items, amount)
for k, itemData in pairs(stashItems) do local foundInv = ""
if itemData and (itemData.name == item) then if type(items) ~= "table" then items = { [items] = amount and amount or 1, } end
--if Config.System.Debug then
-- print("^6Bridge^7: ^3stashHasItem^7: ^2Item^7: '^3"..tostring(item).."^7' ^2Slot^7: ^3"..itemData.slot.." ^7x(^3"..tostring(itemData.amount).."^7)") local hasTable = {}
--end for item, amount in pairs(items) do
local count = 0
for _, itemData in pairs(stashItems) do
if itemData and (itemData.name == k) then
count += (itemData.amount or 1) count += (itemData.amount or 1)
end end
end end
if count >= amount then if count >= amount then
if Config.System.Debug then if Config.System.Debug then print("^6Bridge^7: ^3stashHasItem^7: ^2Items ^3"..item.." ^5FOUND^7 x^3"..count.."^7/^3"..amount.."^7") end
print("^6Bridge^7: ^3stashHasItem^7: ^2Items ^3"..item.." ^5FOUND^7 x^3"..count.."^7/^3"..amount.."^7") end hasTable[item] = { hasItem = true, count = count, }
return
true
else else
if Config.System.Debug then print("^6Bridge^7: ^3stashHasItem^7: ^2Items ^1NOT FOUND^7 "..json.encode(item)) end if Config.System.Debug then print("^6Bridge^7: ^3stashHasItem^7: ^2Items ^1NOT FOUND^7 "..json.encode(item)) end
return false hasTable[item] = { hasItem = false, count = count, }
end end
end
for k, v in pairs(hasTable) do if v.hasItem == false then return false, hasTable end end
return true, hasTable
end end