Files

186 lines
5.4 KiB
Lua
Raw Permalink Normal View History

---@diagnostic disable: duplicate-set-field
2026-08-17 03:49:37 -07:00
---
2025-02-24 12:17:56 -05:00
Target = Target or {}
local InteractIds = {}
2025-02-24 12:17:56 -05:00
local function warnUser()
2025-03-03 19:09:06 -05:00
print("Currently Only Targeting Is Supported By Community Bridge, You Are Using A Resource That Requires The Target Module To Be Used.")
2025-02-24 12:17:56 -05:00
end
2025-08-20 22:23:07 -05:00
function Target.GetResourceName()
return "default"
end
function Target.GetCanInteract(id)
2025-08-20 22:23:07 -05:00
return InteractIds[id]
end
function Target.CreateCanInteract(cb)
2025-08-20 22:23:07 -05:00
if not cb then return end
2026-08-17 03:49:37 -07:00
local id = lib.string.random('Aa1A', 8) -- Generate a random ID if not provided
2025-08-20 22:23:07 -05:00
InteractIds[id] = {
id = id,
ableToInteract = -1,
onInteract = cb
}
return id
end
function Target.CanInteract(id, ...)
2025-08-20 22:23:07 -05:00
local interactData = InteractIds[id]
if not interactData then return true end
if interactData.ableToInteract == -1 then
local cb = interactData.onInteract
local canInteractStatus = cb(...)
interactData.ableToInteract = canInteractStatus and 1 or 0
SetTimeout(1000, function()
interactData.ableToInteract = -1
end)
end
return interactData.ableToInteract > 0
end
2025-02-24 12:17:56 -05:00
function Target.FixOptions(options)
2025-04-07 14:33:05 -04:00
for k, v in pairs(options) do
local action = v.onSelect or v.action
local select = function(entityOrData)
if type(entityOrData) == 'table' then
return action(entityOrData.entity)
end
return action(entityOrData)
end
options[k].onSelect = select
2025-08-20 22:23:07 -05:00
local optionsCanInteract = v.canInteract
if optionsCanInteract then
local id = Target.CreateCanInteract(optionsCanInteract)
v.canInteract = function(...)
return Target.CanInteract(id, ...)
end
end
2025-04-07 14:33:05 -04:00
end
return options
end
function Target.AddGlobalPlayer(options)
2025-02-24 12:17:56 -05:00
warnUser()
end
function Target.AddGlobalVehicle(options)
2025-02-24 12:17:56 -05:00
warnUser()
end
function Target.RemoveGlobalVehicle(options)
2025-02-24 12:17:56 -05:00
warnUser()
end
function Target.AddLocalEntity(entities, _options)
2025-04-07 14:33:05 -04:00
local fixedOptions = Target.FixOptions(_options)
if type(entities) == "string" or type(entities) == "number" then
entities = { entities }
end
for _, entity in pairs(entities) do
2026-08-17 03:49:37 -07:00
local id = lib.string.random('Aa1A', 8) -- Generate a random ID if not provided
2026-08-18 00:02:04 -07:00
local title = lib.locale("target.title")
local menuData = { id = id, title = title, options = {} }
2025-04-07 14:33:05 -04:00
for k, v in pairs(fixedOptions) do
table.insert(menuData.options, {
title = title .. " " .. k,
2026-08-18 00:02:04 -07:00
description = lib.locale("target.description"),
2025-04-07 14:33:05 -04:00
onSelect = function(selected, secondary, args)
if v.onSelect then
v.onSelect(selected, secondary, args)
end
end
})
end
Point.Register(id, entity, 5, nil, function()
2025-04-07 14:33:05 -04:00
local coords = GetEntityCoords(entity)
local sleep = 3000
2026-08-18 00:02:04 -07:00
local test = lib.locale("target.interact")
CreateThread(function()
while DoesEntityExist(entity) do
Wait(sleep)
local distance = #(coords - GetEntityCoords(PlayerPedId()))
if distance < 2 then
sleep = 0
Utility.Draw3DHelpText(coords, test, 0.35)
if IsControlJustPressed(0, 38) then
Menu.Open(menuData, false)
end
else
sleep = 1000
2025-04-07 14:33:05 -04:00
end
end
end)
2025-04-07 14:33:05 -04:00
end,
function()
Point.Remove(id)
end, function()
--No need for this in this one
end)
end
2025-02-24 12:17:56 -05:00
end
function Target.AddModel(models, options)
2025-02-24 12:17:56 -05:00
warnUser()
end
function Target.AddBoxZone(name, coords, size, heading, options)
local fixedOptions = Target.FixOptions(options)
2026-08-17 03:49:37 -07:00
local id = lib.string.random('Aa1A', 8) -- Generate a random ID if not provided
2026-08-18 00:02:04 -07:00
local title = lib.locale("target.title")
local menuData = { id = id, title = title, options = {} }
2025-04-07 14:33:05 -04:00
for k, v in pairs(fixedOptions) do
table.insert(menuData.options, {
title = title .. " " .. k,
2026-08-18 00:02:04 -07:00
description = lib.locale("target.description"),
2025-04-07 14:33:05 -04:00
onSelect = function(selected, secondary, args)
if v.onSelect then
v.onSelect(selected, secondary, args)
end
end
})
end
local inZone = false
Point.Register(id, coords, 5, nil,
function()
2025-04-07 14:33:05 -04:00
local sleep = 3000
while inZone do
Wait(sleep)
local distance = #(coords - GetEntityCoords(PlayerPedId()))
if distance < 10 then
sleep = 0
2026-08-18 00:02:04 -07:00
Utility.Draw3DHelpText(coords, lib.locale("target.interact"), 0.35)
if IsControlJustPressed(0, 38) then
Menu.Open(menuData, false)
2025-04-07 14:33:05 -04:00
end
end
end
2025-04-07 14:33:05 -04:00
end,
function()
inZone = false
2025-04-07 14:33:05 -04:00
Point.Remove(id)
end,
function()
2025-04-07 14:33:05 -04:00
--No need for this in this one
end)
2025-02-24 12:17:56 -05:00
end
function Target.RemoveGlobalPlayer()
2025-02-24 12:17:56 -05:00
warnUser()
end
function Target.RemoveLocalEntity(entity)
2025-02-24 12:17:56 -05:00
warnUser()
end
function Target.RemoveModel(model)
2025-02-24 12:17:56 -05:00
warnUser()
end
function Target.RemoveZone(name)
2025-02-24 12:17:56 -05:00
warnUser()
end
return Target