mirror of
https://github.com/qbcore-fivem/qb-core.git
synced 2026-09-01 10:48:56 +00:00
feat(server/functions): server side hasitem check
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user