From e5316f45aaea7b8557f40a9772d6a128add230e1 Mon Sep 17 00:00:00 2001 From: Jim Shield Date: Fri, 19 Jan 2024 13:00:50 +0000 Subject: [PATCH] Rewrite + Optimize hasItem() & stashhasItem() --- crafting.lua | 221 +++++++++++++++++---------------------------------- 1 file changed, 71 insertions(+), 150 deletions(-) diff --git a/crafting.lua b/crafting.lua index 5ccc6c9..a80c809 100644 --- a/crafting.lua +++ b/crafting.lua @@ -225,149 +225,66 @@ function openShop(data) end -- Client & Server side -function hasItem(items, amount, src) local amount, count = amount and amount or 1, 0 - if src then - if GetResourceState(OXInv):find("start") then - local item = exports[OXInv]:GetItem(src, items) - local amount = (amount or 1) - if item.count >= amount then - 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 - - elseif GetResourceState(QSInv):find("start") then - for _, itemData in pairs(exports[QSInv]:GetInventory(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 - - elseif GetResourceState(CoreInv):find("start") then - local item = exports[CoreInv]:getItems('primary-'..src, items) - - if item.amount >= amount then - 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 - - elseif GetResourceState(CodeMInv):find("start") then - local success = exports["codem-inventory"]:CheckItemValid(src, 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 - - 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 +function hasItem(items, amount, src) local amount = amount and amount or 1 + local grabInv = nil + local foundInv = "" + if type(items) ~= "table" then items = { [items] = amount and amount or 1, } end + if GetResourceState(OXInv):find("start") then + foundInv = OXInv + if src then grabInv = exports.ox_inventory:GetInventoryItems(src) + else grabInv = exports[OXInv]:GetPlayerItems() end + + elseif GetResourceState(QSInv):find("start") then + foundInv = QSInv + if src then grabInv = exports[QSInv]:GetInventory(src) + else grabInv = exports[QSInv]:getUserInventory() + end + + elseif GetResourceState(CoreInv):find("start") then + foundInv = CoreInv + if src then grabInv = exports['core_inventory']:getInventory('primary-'..src) + else grabInv = exports[CoreInv]:getInventory('primary-'..GetPlayerServerId(PlayerPedId())) + end + + elseif GetResourceState(CodeMInv):find("start") then + foundInv = CodeMInv + if src then grabInv = exports[CodeMInv]:GetUserInventory(src) + else grabInv = exports[CodeMInv]:GetClientPlayerInventory() + end + + elseif GetResourceState(QBInv):find("start") then + foundInv = QBInv + if src then grabInv = Core.Functions.GetPlayer(src).PlayerData.items + else grabInv = Core.Functions.GetPlayerData().items + 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 + print("^4ERROR^7: ^2No Inventory detected ^7- ^2Check ^3exports^1.^2lua^7") + 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) + 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 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 + if Config.System.Debug then print("^6Bridge^7: ^3HasItem^7: ^5FOUND^7 ^3"..count.."^7/^3"..amount.." "..tostring(item).."^7", foundInv) end + hasTable[item] = { hasItem = true, count = count, } 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) + if Config.System.Debug then + print("^6Bridge^7: ^3HasItem^7: ^2"..tostring(item).." ^1NOT FOUND^7", foundInv) end + hasTable[item] = { hasItem = false, count = count, } 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 - - elseif GetResourceState(QBInv):find("start") then - --if tonumber(GetResourceMetadata(QBInv, 'version')) <= 1.2 then - -- return exports[QBInv]:HasItem(items, amount) - --else - for _, itemData in pairs(Core.Functions.GetPlayerData().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 end + for k, v in pairs(hasTable) do if v.hasItem == false then return false, hasTable end end + return true, hasTable end end @@ -485,22 +402,26 @@ function stashRemoveItem(stashItems, stashName, items) local amount = amount and end RegisterNetEvent(GetCurrentResourceName()..":server:stashRemoveItem", stashRemoveItem) -function stashhasItem(stashItems, item, amount) local amount, count = amount and amount or 1, 0 - for k, itemData in pairs(stashItems) do - if itemData and (itemData.name == item) then - --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)") - --end - count += (itemData.amount or 1) - end - end - if count >= amount then - if Config.System.Debug then - print("^6Bridge^7: ^3stashHasItem^7: ^2Items ^3"..item.." ^5FOUND^7 x^3"..count.."^7/^3"..amount.."^7") end - return - true - else - if Config.System.Debug then print("^6Bridge^7: ^3stashHasItem^7: ^2Items ^1NOT FOUND^7 "..json.encode(item)) end - return false +function stashhasItem(stashItems, items, amount) + local foundInv = "" + if type(items) ~= "table" then items = { [items] = amount and amount or 1, } end + + local hasTable = {} + 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) + end + end + if count >= amount then + if Config.System.Debug then print("^6Bridge^7: ^3stashHasItem^7: ^2Items ^3"..item.." ^5FOUND^7 x^3"..count.."^7/^3"..amount.."^7") end + hasTable[item] = { hasItem = true, count = count, } + else + if Config.System.Debug then print("^6Bridge^7: ^3stashHasItem^7: ^2Items ^1NOT FOUND^7 "..json.encode(item)) end + hasTable[item] = { hasItem = false, count = count, } + end end + for k, v in pairs(hasTable) do if v.hasItem == false then return false, hasTable end end + return true, hasTable end \ No newline at end of file