Improve logic for HasItem

This commit is contained in:
TheiLLeniumStudios
2022-05-13 06:16:21 +00:00
parent 193e988875
commit e484ea0d48
+21 -33
View File
@@ -207,41 +207,29 @@ QBCore.Functions.CreateCallback('QBCore:HasItem', function(source, cb, items, am
local retval = false local retval = false
local Player = QBCore.Functions.GetPlayer(source) local Player = QBCore.Functions.GetPlayer(source)
if not Player then return cb(false) end if not Player then return cb(false) end
if type(items) == 'table' then local isTable = type(items) == 'table'
local count = 0 local isArray = isTable and table.type(items) == 'array' or false
local finalcount = #items local totalItems = 0
for k, v in pairs(items) do local count = 0
if type(k) == 'string' then if isTable then for _ in pairs(items) do totalItems += 1 end else totalItems = #items end
local item = Player.Functions.GetItemByName(k) local kvIndex
if item and item.amount >= v then if isArray then kvIndex = 2 else kvIndex = 1 end
count += 1 if isTable then
end for k, v in pairs(items) do
else local itemKV = {k, v}
local item = Player.Functions.GetItemByName(v) local item = Player.Functions.GetItemByName(itemKV[kvIndex])
if item then if item and (not amount or (amount and item.amount >= amount)) then
if amount then count += 1
if item.amount >= amount then
count += 1
end
else
count += 1
end
end
end end
end end
if count == finalcount then if count == totalItems then
retval = false
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 retval = true
end end
else end
local item = Player.Functions.GetItemByName(items)
if not item then return cb(false) end
if amount then
if item.amount >= amount then
retval = true
end
else
retval = true
end
end
cb(retval) cb(retval)
end) end)