Skip to content

Commit

Permalink
feat(client/utils): support for "anyItem" on options
Browse files Browse the repository at this point in the history
See #60. PR code was somewhat messy and repeated most of the code
in the backing function (PlayerHasItems), mostly due to it being
defined in multiple locations.
  • Loading branch information
thelindat committed Feb 15, 2023
1 parent 13608d3 commit 2d6bd94
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ local function enableTargeting()
hide = true
end

if option.items and not PlayerHasItems(option.items) then
if option.items and not PlayerHasItems(option.items, option.anyItem) then
hide = true
end

Expand Down
24 changes: 19 additions & 5 deletions client/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,38 @@ if playerItems and GetResourceState('ox_inventory') ~= 'missing' then
})
end

function PlayerHasItems(filter)
function PlayerHasItems(filter, hasAny)
if not playerItems then return true end

local _type = type(filter)

if _type == 'string' then
if playerItems[filter] < 1 then return end
return playerItems[filter] and true
elseif _type == 'table' then
local tabletype = table.type(filter)

if tabletype == 'hash' then
for name, amount in pairs(filter) do
if playerItems[name] < amount then return end
local hasItem = (playerItems[name] or 0) < amount

if hasAny then
if hasItem then return true end
elseif not hasItem then
return false
end
end
elseif tabletype == 'array' then
for i = 1, #filter do
if playerItems[filter[i]] < 1 then return end
local hasItem = playerItems[filter[i]]

if hasAny then
if hasItem then return true end
elseif not hasItem then
return false
end
end
end
end

return true
return not hasAny
end

0 comments on commit 2d6bd94

Please sign in to comment.