From 19911d91734329823d5d507cefad631d4e8c51e6 Mon Sep 17 00:00:00 2001 From: MrNewb <47620135+MrNewb@users.noreply.github.com> Date: Wed, 15 Oct 2025 18:05:56 -0400 Subject: [PATCH] Refactor target system client logic and zone functions Simplifies debug flag initialization and resource state checks in both ox_target and qb-target client scripts. Updates Target.FixOptions to streamline option handling and canInteract logic. Target.AddBoxZone and Target.AddSphereZone now return the zone name for improved usability. --- modules/target/ox_target/client.lua | 26 +++++++------------------- modules/target/qb-target/client.lua | 22 +++++++--------------- 2 files changed, 14 insertions(+), 34 deletions(-) diff --git a/modules/target/ox_target/client.lua b/modules/target/ox_target/client.lua index f98c26b..3f7d90d 100644 --- a/modules/target/ox_target/client.lua +++ b/modules/target/ox_target/client.lua @@ -2,33 +2,23 @@ local resourceName = "ox_target" if GetResourceState(resourceName) == 'missing' then return end -local targetDebug = false -local function detectDebugEnabled() - if BridgeSharedConfig.DebugLevel == 2 then - targetDebug = true - end -end - -detectDebugEnabled() - -local ox_target = exports.ox_target -local targetZones = {} - Target = Target or {} +local targetDebug = BridgeSharedConfig and BridgeSharedConfig.DebugLevel == 2 or false +local targetZones = {} +local ox_target = exports.ox_target ---This is an internal function that is used to fix the options passed to fit alternative target systems, for example qb-ox or ox-qb etc. ---@param options table ---@return table Target.FixOptions = function(options) - for k, v in pairs(options) do + for _, v in pairs(options) do local action = v.onSelect or v.action - if not action then + if not action then local _type = v.type if _type and _type == "server" then v.serverEvent = v.event v.event = nil end - else local select = function(entityOrData) if type(entityOrData) == 'table' then @@ -37,17 +27,15 @@ Target.FixOptions = function(options) return action(entityOrData) end v.onSelect = select - end v.groups = v.job or v.groups - local optionsCanInteract = v.canInteract - if optionsCanInteract then + local optionsCanInteract = v.canInteract + if optionsCanInteract then local id = Target.CreateCanInteract(optionsCanInteract) v.canInteract = function(...) return Target.CanInteract(id, ...) end end - -- print(json.encode(v)) end return options end diff --git a/modules/target/qb-target/client.lua b/modules/target/qb-target/client.lua index 6dc9adb..c153977 100644 --- a/modules/target/qb-target/client.lua +++ b/modules/target/qb-target/client.lua @@ -1,20 +1,11 @@ ---@diagnostic disable: duplicate-set-field local resourceName = "qb-target" if GetResourceState(resourceName) == 'missing' then return end -if GetResourceState("ox_target") == 'started' then return end - -local targetDebug = false -local function detectDebugEnabled() - if BridgeSharedConfig.DebugLevel == 2 then - targetDebug = true - end -end - -detectDebugEnabled() - -local targetZones = {} +if GetResourceState("ox_target") ~= 'missing' then return end Target = Target or {} +local targetDebug = BridgeSharedConfig and BridgeSharedConfig.DebugLevel == 2 or false +local targetZones = {} local qb_target = exports['qb-target'] function GetLargestDistance(data) @@ -27,7 +18,6 @@ function GetLargestDistance(data) return largestDistance ~= -1 and largestDistance or 2.0 end - ---This is an internal function that is used to fix the options passed to fit alternative target systems, for example qb-ox or ox-qb etc. ---@param options table ---@return table @@ -50,8 +40,8 @@ Target.FixOptions = function(options) options[k].action = select options[k].job = v.job or v.groups options[k].jobType = v.jobType - local optionsCanInteract = v.canInteract - if optionsCanInteract then + local optionsCanInteract = v.canInteract + if optionsCanInteract then local id = Target.CreateCanInteract(optionsCanInteract) v.canInteract = function(...) return Target.CanInteract(id, ...) @@ -188,6 +178,7 @@ Target.AddBoxZone = function(name, coords, size, heading, options, debug) distance = GetLargestDistance(options), }) table.insert(targetZones, { name = name, creator = GetInvokingResource() }) + return name end ---This will add a circle zone to the target system. This is useful for when you want to add target options to a specific area. @@ -205,6 +196,7 @@ Target.AddSphereZone = function(name, coords, radius, options, debug) distance = GetLargestDistance(options), }) table.insert(targetZones, { name = name, creator = GetInvokingResource() }) + return name end ---This will remove target options from a specific zone.