Files

234 lines
8.2 KiB
Lua
Raw Permalink Normal View History

---@diagnostic disable: duplicate-set-field
2025-03-04 22:28:34 -05:00
local resourceName = "ox_target"
2025-05-18 23:45:37 -04:00
if GetResourceState(resourceName) == 'missing' then return end
2025-01-08 06:33:31 -05:00
2025-02-24 11:07:52 -05:00
Target = Target or {}
local targetDebug = BridgeSharedConfig and BridgeSharedConfig.DebugLevel == 2 or false
local ox_target = exports.ox_target
local targetZones = {}
local resourceTargets = {}
resourceTargets.models = {}
resourceTargets.entities = {}
resourceTargets.networkedEntities = {}
Target.GetResourceName = function()
return "ox_target"
end
2025-01-08 06:33:31 -05:00
2025-04-08 19:27:34 -04:00
---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
function Target.FixOptions(options)
for _, v in pairs(options) do
2025-02-05 17:42:50 -06:00
local action = v.onSelect or v.action
2026-08-14 17:38:09 -05:00
if not action then
2025-07-29 05:22:55 -05:00
local _type = v.type
if _type and _type == "server" then
v.serverEvent = v.event
v.event = nil
end
2025-07-24 05:03:07 -05:00
else
local select = function(entityOrData)
if type(entityOrData) == 'table' then
return action(entityOrData.entity)
end
return action(entityOrData)
2025-02-05 17:42:50 -06:00
end
2025-07-29 05:22:55 -05:00
v.onSelect = select
2025-02-05 17:42:50 -06:00
end
2025-07-29 05:22:55 -05:00
v.groups = v.job or v.groups
local optionsCanInteract = v.canInteract
if optionsCanInteract then
2025-08-20 22:23:07 -05:00
local id = Target.CreateCanInteract(optionsCanInteract)
v.canInteract = function(...)
return Target.CanInteract(id, ...)
end
end
2025-01-14 10:55:14 -06:00
end
2025-02-05 17:42:50 -06:00
return options
end
---This will toggle the targeting system on or off. This is useful for when you want to disable the targeting system for a specific player entirely.
---@param bool boolean
function Target.DisableTargeting(bool)
ox_target:disableTargeting(bool)
end
2025-04-08 19:27:34 -04:00
---This will add target options to players.
2025-03-03 01:37:29 -05:00
---@param options table
function Target.AddGlobalPlayer(options)
2025-02-05 17:42:50 -06:00
options = Target.FixOptions(options)
2025-01-08 06:33:31 -05:00
ox_target:addGlobalPlayer(options)
end
2025-04-08 19:27:34 -04:00
---This will remove target options from all players.
function Target.RemoveGlobalPlayer()
2025-04-08 19:27:34 -04:00
ox_target:removeGlobalPlayer()
end
---This will add target options to all specified models. This is useful for when you want to add target options to all models of a specific type.
---@param options table
function Target.AddGlobalPed(options)
options = Target.FixOptions(options)
ox_target:addGlobalPed(options)
end
---This will remove target options from all peds. This is useful for when you want to remove target options from all peds.
---@param options any
function Target.RemoveGlobalPed(options)
ox_target:removeGlobalPed(options)
end
2025-04-08 19:27:34 -04:00
---This will add taget options to all vehicles.
2025-03-03 01:37:29 -05:00
---@param options table
function Target.AddGlobalVehicle(options)
2025-02-05 17:42:50 -06:00
options = Target.FixOptions(options)
2025-01-25 17:59:19 -05:00
ox_target:addGlobalVehicle(options)
end
2025-06-04 15:29:21 -05:00
---This will remove target options from all vehicles.
2025-03-03 01:37:29 -05:00
---@param options table
function Target.RemoveGlobalVehicle(options)
2025-02-20 14:50:27 -05:00
local assembledLables = {}
for k, v in pairs(options) do
table.insert(assembledLables, v.name)
end
ox_target:removeGlobalVehicle(assembledLables)
end
2025-04-08 19:27:34 -04:00
---This will generate targets on non networked entites with the passed options.
---@param entities number | table
2025-03-03 01:37:29 -05:00
---@param options table
function Target.AddLocalEntity(entities, options)
2025-02-05 17:42:50 -06:00
options = Target.FixOptions(options)
2025-01-08 06:33:31 -05:00
ox_target:addLocalEntity(entities, options)
2026-08-14 17:38:09 -05:00
local resource = GetInvokingResource() or GetCurrentResourceName()
resourceTargets.entities[resource] = resourceTargets.entities[resource] or {}
table.insert(resourceTargets.entities[resource], { entities = entities})
2025-01-08 06:33:31 -05:00
end
2025-04-08 19:27:34 -04:00
---This will remove the target options from a local entity. This is useful for when you want to remove target options from a specific entity.
2025-06-11 21:59:58 -04:00
---@param entities table
---@param optionNames string | table | nil
function Target.RemoveLocalEntity(entities, optionNames)
ox_target:removeLocalEntity(entities, optionNames)
2025-04-08 19:27:34 -04:00
end
---This will add a networked entity to the target system.
---@param netids table | number
---@param options table
function Target.AddNetworkedEntity(netids, options)
options = Target.FixOptions(options)
ox_target:addEntity(netids, options)
2026-08-14 17:38:09 -05:00
local resource = GetInvokingResource() or GetCurrentResourceName()
resourceTargets.networkedEntities[resource] = resourceTargets.networkedEntities[resource] or {}
table.insert(resourceTargets.networkedEntities[resource], { netids = netids})
end
---This will remove a networked entity from the target system.
---@param netids table | number
---@param optionNames string
function Target.RemoveNetworkedEntity(netids, optionNames)
ox_target:removeEntity(netids, optionNames)
end
2025-06-11 21:59:58 -04:00
---This will add target options to all specified models. This is useful for when you want to add target options to all models of a specific type.
2025-04-08 19:27:34 -04:00
---@param models number | table
2025-03-03 01:37:29 -05:00
---@param options table
function Target.AddModel(models, options)
2025-02-05 17:42:50 -06:00
options = Target.FixOptions(options)
2025-01-08 06:33:31 -05:00
ox_target:addModel(models, options)
2026-08-14 17:38:09 -05:00
local resource = GetInvokingResource() or GetCurrentResourceName()
resourceTargets.models[resource] = resourceTargets.models[resource] or {}
table.insert(resourceTargets.models[resource], { models = models})
2025-01-08 06:33:31 -05:00
end
2025-04-08 19:27:34 -04:00
---This will remove target options from all specified models.
---@param model number
function Target.RemoveModel(model)
2025-04-08 19:27:34 -04:00
ox_target:removeModel(model)
end
---This will add a box zone to the target system. This is useful for when you want to add target options to a specific area.
2025-03-03 01:37:29 -05:00
---@param name string
---@param coords table
---@param size table
---@param heading number
---@param options table
function Target.AddBoxZone(name, coords, size, heading, options, debug)
2025-06-02 22:19:42 -05:00
options = Target.FixOptions(options or {})
2025-06-04 15:29:21 -05:00
if not next(options) then return end
2025-01-08 06:33:31 -05:00
local target = ox_target:addBoxZone({
coords = coords,
size = size,
rotation = heading,
2025-06-02 22:19:42 -05:00
debug = debug or targetDebug,
2025-01-08 06:33:31 -05:00
options = options,
})
2026-08-14 17:38:09 -05:00
table.insert(targetZones, { name = name, id = target, creator = GetInvokingResource() or GetCurrentResourceName() })
2025-04-05 20:58:13 +13:00
return target
end
2025-04-08 19:27:34 -04:00
---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.
2025-04-05 20:58:13 +13:00
---@param name string
2025-04-08 19:27:34 -04:00
---@param coords table
2025-04-05 20:58:13 +13:00
---@param radius number
---@param options table
function Target.AddSphereZone(name, coords, radius, options, debug)
2025-04-05 20:58:13 +13:00
options = Target.FixOptions(options)
local target = ox_target:addSphereZone({
coords = coords,
radius = radius,
name = name,
2025-07-28 00:10:51 -05:00
debug = targetDebug or debug,
2025-04-05 20:58:13 +13:00
options = options
})
2026-08-14 17:38:09 -05:00
table.insert(targetZones, { name = name, id = target, creator = GetInvokingResource() or GetCurrentResourceName() })
2025-01-08 06:33:31 -05:00
return target
end
2025-04-08 19:27:34 -04:00
---This will remove target options from a specific zone.
2025-03-03 01:37:29 -05:00
---@param name string
function Target.RemoveZone(name)
2025-04-08 19:27:34 -04:00
if not name then return end
2025-01-08 06:33:31 -05:00
for _, data in pairs(targetZones) do
if data.name == name then
ox_target:removeZone(data.id)
table.remove(targetZones, _)
break
end
end
end
AddEventHandler('onResourceStop', function(resource)
2025-04-08 19:27:34 -04:00
for _, target in pairs(targetZones) do
if target.creator == resource then
ox_target:removeZone(target.id)
2025-01-08 06:33:31 -05:00
end
end
2025-04-08 19:27:34 -04:00
targetZones = {}
if resourceTargets.entities[resource] and #resourceTargets.entities[resource] > 0 then
for _, data in pairs(resourceTargets.entities[resource]) do
Target.RemoveLocalEntity(data.entities)
end
end
resourceTargets.entities[resource] = nil
if resourceTargets.networkedEntities[resource] and #resourceTargets.networkedEntities[resource] > 0 then
2026-08-14 17:38:09 -05:00
for _, data in pairs(resourceTargets.networkedEntities[resource]) do
Target.RemoveNetworkedEntity(data.netids)
end
end
resourceTargets.networkedEntities[resource] = nil
if resourceTargets.models[resource] and #resourceTargets.models[resource] > 0 then
for _, data in pairs(resourceTargets.models[resource]) do
for _, model in pairs(data.models) do
ox_target:removeModel(model)
end
end
end
resourceTargets.models[resource] = nil
2025-01-08 06:33:31 -05:00
end)
return Target