From 156182dd937e5dd690218ec9608ef62f6a641f5a Mon Sep 17 00:00:00 2001 From: Kakarot <57848836+GhzGarage@users.noreply.github.com> Date: Fri, 26 Aug 2022 08:49:22 -0500 Subject: [PATCH] Update events.lua --- server/events.lua | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/server/events.lua b/server/events.lua index 9fedad9..a9ccbbc 100644 --- a/server/events.lua +++ b/server/events.lua @@ -237,3 +237,39 @@ QBCore.Functions.CreateCallback('QBCore:Server:CreateVehicle', function(source, if warp then TaskWarpPedIntoVehicle(GetPlayerPed(source), veh, -1) end cb(NetworkGetNetworkIdFromEntity(veh)) end) + +-- Callback (maintain backwards compatibility) + +QBCore.Functions.CreateCallback('QBCore:HasItem', function(source, cb, items, amount) + local retval = false + local Player = QBCore.Functions.GetPlayer(source) + if not Player then return cb(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 ((amount and item.amount >= amount) or (not amount and not isArray and item.amount >= v) or (not amount and isArray)) then + count += 1 + end + end + if count == totalItems then + retval = 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 + retval = true + end + end + cb(retval) +end)