Skip to content

Commit

Permalink
feat(client/api): check for and remove existing named options
Browse files Browse the repository at this point in the history
I would rather not, but qb devs are wild with their loops.
  • Loading branch information
thelindat committed Jan 16, 2024
1 parent 2d77599 commit a87640d
Showing 1 changed file with 41 additions and 20 deletions.
61 changes: 41 additions & 20 deletions client/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,30 @@ local function typeError(variable, expected, received)
error(("expected %s to have type '%s' (received %s)"):format(variable, expected, received))
end

---@param target table
---@param remove string | string[]
---@param resource string
---@param showWarning? boolean
local function removeTarget(target, remove, resource, showWarning)
if type(remove) ~= 'table' then remove = { remove } end

for i = #target, 1, -1 do
local option = target[i]

if option.resource == resource then
for j = #remove, 1, -1 do
if option.name == remove[j] then
table.remove(target, i)

if showWarning then
utils.warn(("Replacing existing target option '%s'."):format(option.name))
end
end
end
end
end
end

---@param target table
---@param options OxTargetOption | OxTargetOption[]
---@param resource string
Expand All @@ -90,11 +114,27 @@ local function addTarget(target, options, resource)

---@cast options OxTargetOption[]

local checkNames = {}

resource = resource or 'ox_target'

for i = 1, #options do
local option = options[i]
option.resource = resource

if option.name then
checkNames[#checkNames + 1] = option.name
end
end

if checkNames[1] then
removeTarget(target, checkNames, resource, true)
end

local num = #target

for i = 1, #options do
local option = options[i]
option.resource = resource or 'ox_target'

if not resource then
if option.canInteract then
Expand All @@ -111,25 +151,6 @@ local function addTarget(target, options, resource)
end
end

---@param target table
---@param remove string | string[]
---@param resource string
local function removeTarget(target, remove, resource)
if type(remove) ~= 'table' then remove = { remove } end

for i = #target, 1, -1 do
local option = target[i]

if option.resource == resource then
for j = #remove, 1, -1 do
if option.name == remove[j] then
table.remove(target, i)
end
end
end
end
end

---@type table<number, OxTargetOption[]>
local peds = {}

Expand Down

0 comments on commit a87640d

Please sign in to comment.