From a87640db692da18b99042616c72f8ffb7b877a7c Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Tue, 16 Jan 2024 11:36:32 +1100 Subject: [PATCH] feat(client/api): check for and remove existing named options I would rather not, but qb devs are wild with their loops. --- client/api.lua | 61 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/client/api.lua b/client/api.lua index 85856c9..39a5971 100644 --- a/client/api.lua +++ b/client/api.lua @@ -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 @@ -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 @@ -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 local peds = {}