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.
This commit is contained in:
MrNewb committed 2025-10-15 18:05:56 -04:00
1 parent 05bee5e743
commit 19911d9173
2 files changed
+14 -34

No files matched your search

+7 -19
View File
@@ -2,33 +2,23 @@
local resourceName = "ox_target" local resourceName = "ox_target"
if GetResourceState(resourceName) == 'missing' then return end 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 {} 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. ---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 ---@param options table
---@return table ---@return table
Target.FixOptions = function(options) Target.FixOptions = function(options)
for k, v in pairs(options) do for _, v in pairs(options) do
local action = v.onSelect or v.action local action = v.onSelect or v.action
if not action then if not action then
local _type = v.type local _type = v.type
if _type and _type == "server" then if _type and _type == "server" then
v.serverEvent = v.event v.serverEvent = v.event
v.event = nil v.event = nil
end end
else else
local select = function(entityOrData) local select = function(entityOrData)
if type(entityOrData) == 'table' then if type(entityOrData) == 'table' then
@@ -37,17 +27,15 @@ Target.FixOptions = function(options)
return action(entityOrData) return action(entityOrData)
end end
v.onSelect = select v.onSelect = select
end end
v.groups = v.job or v.groups v.groups = v.job or v.groups
local optionsCanInteract = v.canInteract local optionsCanInteract = v.canInteract
if optionsCanInteract then if optionsCanInteract then
local id = Target.CreateCanInteract(optionsCanInteract) local id = Target.CreateCanInteract(optionsCanInteract)
v.canInteract = function(...) v.canInteract = function(...)
return Target.CanInteract(id, ...) return Target.CanInteract(id, ...)
end end
end end
-- print(json.encode(v))
end end
return options return options
end end
+7 -15
View File
@@ -1,20 +1,11 @@
---@diagnostic disable: duplicate-set-field ---@diagnostic disable: duplicate-set-field
local resourceName = "qb-target" local resourceName = "qb-target"
if GetResourceState(resourceName) == 'missing' then return end if GetResourceState(resourceName) == 'missing' then return end
if GetResourceState("ox_target") == 'started' then return end if GetResourceState("ox_target") ~= 'missing' then return end
local targetDebug = false
local function detectDebugEnabled()
if BridgeSharedConfig.DebugLevel == 2 then
targetDebug = true
end
end
detectDebugEnabled()
local targetZones = {}
Target = Target or {} Target = Target or {}
local targetDebug = BridgeSharedConfig and BridgeSharedConfig.DebugLevel == 2 or false
local targetZones = {}
local qb_target = exports['qb-target'] local qb_target = exports['qb-target']
function GetLargestDistance(data) function GetLargestDistance(data)
@@ -27,7 +18,6 @@ function GetLargestDistance(data)
return largestDistance ~= -1 and largestDistance or 2.0 return largestDistance ~= -1 and largestDistance or 2.0
end 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. ---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 ---@param options table
---@return table ---@return table
@@ -50,8 +40,8 @@ Target.FixOptions = function(options)
options[k].action = select options[k].action = select
options[k].job = v.job or v.groups options[k].job = v.job or v.groups
options[k].jobType = v.jobType options[k].jobType = v.jobType
local optionsCanInteract = v.canInteract local optionsCanInteract = v.canInteract
if optionsCanInteract then if optionsCanInteract then
local id = Target.CreateCanInteract(optionsCanInteract) local id = Target.CreateCanInteract(optionsCanInteract)
v.canInteract = function(...) v.canInteract = function(...)
return Target.CanInteract(id, ...) return Target.CanInteract(id, ...)
@@ -188,6 +178,7 @@ Target.AddBoxZone = function(name, coords, size, heading, options, debug)
distance = GetLargestDistance(options), distance = GetLargestDistance(options),
}) })
table.insert(targetZones, { name = name, creator = GetInvokingResource() }) table.insert(targetZones, { name = name, creator = GetInvokingResource() })
return name
end 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. ---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), distance = GetLargestDistance(options),
}) })
table.insert(targetZones, { name = name, creator = GetInvokingResource() }) table.insert(targetZones, { name = name, creator = GetInvokingResource() })
return name
end end
---This will remove target options from a specific zone. ---This will remove target options from a specific zone.