From 483f14d66a6d66beac6780e301a0c86c207f386e Mon Sep 17 00:00:00 2001 From: BerkieBb <82737367+BerkieBb@users.noreply.github.com> Date: Fri, 17 Jun 2022 01:09:50 +0200 Subject: [PATCH 1/2] feat(client/functions): client side hasitem check --- client/functions.lua | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/client/functions.lua b/client/functions.lua index 1744593..60df9f2 100644 --- a/client/functions.lua +++ b/client/functions.lua @@ -11,12 +11,35 @@ function QBCore.Functions.GetCoords(entity) return vector4(GetEntityCoords(entity), GetEntityHeading(entity)) end -function QBCore.Functions.HasItem(item) - local p = promise.new() - QBCore.Functions.TriggerCallback('QBCore:HasItem', function(result) - p:resolve(result) - end, item) - return Citizen.Await(p) +function QBCore.Functions.HasItem(items, amount) + local isTable = type(items) == 'table' + local isArray = isTable and table.type(items) == 'array' or false + local totalItems = #items + local count = 0 + local kvIndex = 2 + if isTable and not isArray then + totalItems = 0 + for _ in pairs(items) do totalItems += 1 end + kvIndex = 1 + end + for _, itemData in pairs(QBCore.PlayerData.items) do + if isTable then + for k, v in pairs(items) do + local itemKV = {k, v} + if itemData and itemData.name == itemKV[kvIndex] and ((not amount and not isArray and itemData.amount >= v) or (isArray and amount and itemData.amount >= amount) or (not amount and isArray)) then + count += 1 + end + end + if count == totalItems then + return true + end + else -- Single item as string + if itemData and itemData.name == items and (not amount or (amount and itemData.amount >= amount)) then + return true + end + end + end + return false end -- Utility From cc0cf6a942fc3568d79779f02afc139fe990c782 Mon Sep 17 00:00:00 2001 From: BerkieBb <82737367+BerkieBb@users.noreply.github.com> Date: Fri, 17 Jun 2022 01:10:16 +0200 Subject: [PATCH 2/2] feat(server/functions): server side hasitem check --- server/functions.lua | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/server/functions.lua b/server/functions.lua index aaf0c45..a81004d 100644 --- a/server/functions.lua +++ b/server/functions.lua @@ -366,3 +366,38 @@ function QBCore.Functions.IsLicenseInUse(license) end return false end + +-- Utility functions + +function QBCore.Functions.HasItem(source, items, amount) + local Player = QBCore.Functions.GetPlayer(source) + if not Player then return false end + local isTable = type(items) == 'table' + local isArray = isTable and table.type(items) == 'array' or false + local totalItems = #items + local count = 0 + local kvIndex = 2 + if isTable and not isArray then + totalItems = 0 + for _ in pairs(items) do totalItems += 1 end + kvIndex = 1 + end + if isTable then + for k, v in pairs(items) do + local itemKV = {k, v} + local item = Player.Functions.GetItemByName(itemKV[kvIndex]) + if item and ((not amount and not isArray and item.amount >= v) or (isArray and amount and item.amount >= amount) or (not amount and isArray)) then + count += 1 + end + end + if count == totalItems then + return true + end + else -- Single item as string + local item = Player.Functions.GetItemByName(items) + if item and (not amount or (amount and item.amount >= amount)) then + return true + end + end + return false +end