Targets, entities and events, oh my!

Targets updated to have boxzone
Entities dont require a model and are effectively points with on spawn on despawn
onplayerloaded and onplayeriunloaded now pass source
This commit is contained in:
gononono64 committed 2025-06-02 22:19:42 -05:00
1 parent 3a141cb8f2
commit 34d24cb6a9
11 files changed
+61 -352

No files matched your search

-154
View File
@@ -1,154 +0,0 @@
local Tables = Require('use/tables.lua')
local Ids = Require("use/ids.lua")
local AllInstances = {}
---@class Class
Class = {}
Class.ClassName = "Class"
Class.__call = function(this, ...)
return this:new(...)
end
Builders = {}
function Class.SetInstance(instance)
AllInstances[instance.id] = instance
end
function Class.GetInstance(id)
return AllInstances[id]
end
--- Default (empty) constructor
--- @param self Class
--- @vararg any
function Class:init(...) end
--- Creates a subclass of the current class.
--- This function copies the properties and methods of the current class to a new object.
--- It also sets up metatables to allow for object-oriented behavior, such as method calls and property access.
--- @param obj object | nil: The table to extend. If nil, a new table will be created.
--- @return obj object: The extended object with properties and methods from the given class obj.
function Class:extend(obj)
local className = obj.ClassName or "Class"
local old = Tables.DeepClone(self, {}, {components = true})
local new = Tables.DeepClone(obj, old, {ClassName = true})
new.components = self.components or {}
new.components[className] = obj
if not new.id then new.id = Ids.CreateUniqueId(new.components) end
-- create new objects directly, like o = Object()
self = setmetatable(new, Class)
if not Class.GetInstance(new.id) then Class.SetInstance(self) end
return self
end
--- Sets a property on the object. NOTE: This is mostly for internal purposes.
--- @param prop string|table: The property name or a table of properties to set.
--- @param value any: The value to set the property to.
function Class:set(prop, value)
if not value and type(prop) == "table" then
for k, v in pairs(prop) do
rawset(self._, k, v)
end
else
rawset(self._, prop, value)
end
end
--- This function is used to create a new instance of the class.
--- It calls the init function if it exists.
--- @vararg any: The arguments to pass to the init
--- @return object: The new instance of the class.
function Class:new(...)
local obj = self:extend({})
if obj.init then obj:init(...) end
return obj
end
--- This function is used to create a new instance of the class.
--- It calls the struct function if it exists.
--- @vararg object(s): List of objects to extend the class with.
--- @return object: The new instance of the class with the data and functions from the objects passed.
function class(className, ...)
local newObj = Class:extend({})
local components = table.pack(...)
newObj.ClassName = className
if components then
for k, v in pairs(components) do
local clName = v.ClassName
if not Builders[clName] then -- builders are used to reconstruct an object usually when pulled from server to client or vice versa
Builders[clName] = Tables.DeepClone(newObj)
end
end
end
if components.n > 0 then
for k, v in pairs(components) do
if k ~= "n" then
local newNewObject = nil
if v.Struct and type(v.Struct) == "function" then
newNewObject = v:Struct()
end
newObj = newObj:extend(newNewObject or v)
end
end
else
return newObj
end
return newObj
end
function reconstruct(obj)
local newTable = {}
local tbl = obj.components or {}
for k, v in pairs(tbl) do
local builder = Builders[k]
if builder then
newTable[k] = builder
end
end
return class(table.unpack(newTable))
end
--- Overwrites the type(self) value to return "Class".
--- @return string: The string "Class".
function Class:__type()
return self.ClassName or "Class"
end
--- Overwrites the call operator to call the new function. Ex: Class()
--- @vararg any: The arguments to pass to the new function.
--- @return object: The new instance of the class.
function Class:__call(...)
return self:new(...)
end
--- Checks to see if the class is an instance of the given class.
--- @param cls Class: The class to check against.
--- @return boolean: True if the class is an instance of the given class, false otherwise.
function Class:IsA(cls)
local p = promise.new()
CreateThread(function()
local m = getmetatable(self)
while m do
if m == cls then
return p:resolve(true)
end
m = getmetatable(m)
Wait(0)
end
return p:resolve(false)
end)
return Citizen.Await(p)
end
return {Class, class}
-38
View File
@@ -1,38 +0,0 @@
local classModule = Require("components/class.lua")
local _, class = table.unpack(classModule)
-- Registry to store singletons for classes
local SingletonRegistry = {}
--- @class Singleton
local Singleton = class()
Singleton.ClassName = "Singleton"
--- Ensures the class instance is a singleton.
--- If an instance already exists, it returns the existing one.
--- If not, it initializes and registers a new singleton.
function Singleton:Struct()
local className = self.ClassName or "UnnamedClass"
-- Check if the singleton already exists
if SingletonRegistry[className] then
self = SingletonRegistry[className]
return self
end
-- Register this instance as the singleton for its class
SingletonRegistry[className] = self
return self
end
--- Clears the singleton instance for this class (if needed).
function Singleton:ClearSingleton()
local destroy = self.Destroy
if destroy and type(destroy) == "function" then self:Destroy() end
local className = self.ClassName or "UnnamedClass"
SingletonRegistry[className] = nil
end
--- Returns the existing singleton for a class.
--- @param className string: The name of the class to check.
--- @return any: The singleton instance, or nil if none exists.
function Singleton.Get(className)
return SingletonRegistry[className]
end
return Singleton
-103
View File
@@ -1,103 +0,0 @@
local classModule = Require("components/class.lua")
local _, class = table.unpack(classModule)
local ids = Require("use/ids.lua")
local SyncObjects = {}
function GetSyncObject(syndId)
return SyncObjects[syndId]
end
function GetSyncObjects()
return SyncObjects
end
function SetSyncObject(syncId, sync)
SyncObjects[syncId] = sync
end
--- @class Sync
local Sync = class("Sync")
--- Initializes the Sync object.
function Sync:Struct()
self.syncId = ids.CreateUniqueId(SyncObjects)
self.sync = {}
self.updateWaitTime = 500
SyncObjects[self.syncId] = self
return self
end
--- Retrieves the value associated with the given key in the sync table.
--- @param key any: The key to retrieve the value for.
--- @return any: The value associated with the key.
function Sync:GetSyncKey(key)
return self.sync[key]
end
--- Called when a sync key is initialized.
--- @param key any: The key being initialized.
--- @param value any: The value being set.
function Sync:OnSyncKeyInit(key, value) end
--- Called when a sync key is updated.
--- @param key any: The key being updated.
--- @param oldValue any: The old value of the key.
--- @param newValue any: The new value of the key.
function Sync:OnSyncKeyUpdate(key, oldValue, newValue) end
--- Called when a sync key is removed.
--- @param key any: The key being removed.
function Sync:OnSyncKeyRemove(key) end
-- Server
if not IsDuplicityVersion() then goto client end
--- Sets a sync key and triggers a client event to update the sync table.
--- @param key any: The key to set.
--- @param value any: The value to set for the key.
function Sync:SetSynckey(key, value)
self.sync[key] = value
CreateThread(function()
SetTimeout(self.updateWaitTime, function()
TriggerClientEvent("Sync:Setup", -1, self.syncId, self.sync)
end)
end)
end
-- Client
if IsDuplicityVersion() then return Sync end
::client::
--- Compares the current sync table with a new table and updates accordingly.
--- @param newTable table: The new sync table to compare with.
function Sync:_CompareAndUpdate(newTable)
local oldTable = self.sync
for key, oldValue in pairs(oldTable) do
local newValue = newTable[key]
if newValue == nil then
self:OnSyncKeyRemove(key)
elseif oldValue ~= newValue then
self:OnSyncKeyUpdate(key, oldValue, newValue)
end
end
for key, newValue in pairs(newTable) do
if oldTable[key] == nil then
self:OnSyncKeyInit(key, newValue)
end
end
self.sync = newTable
end
RegisterNetEvent("Sync:Setup", function(syncId, sync)
local syncObj = GetSyncObject(syncId)
if not syncObj then
syncObj = reconstruct(Sync)
assert(syncObj, "Failed to sync: ", syncId)
syncObj.syncId = syncId
SetSyncObject(syncId, syncObj)
end
syncObj:_CompareAndUpdate(sync)
end)
return table.pack({Sync})
+14 -20
View File
@@ -25,7 +25,6 @@ function Anim.RequestDict(animDict)
end
--- This will start the animation update loop.
--- @return void
function Anim.Start()
if Anim.isUpdateLoopRunning then return end
Anim.isUpdateLoopRunning = true
@@ -37,15 +36,13 @@ function Anim.Start()
end
if #idsToProcess == 0 then
Wait(750)
Wait(750)
else
for _, id in ipairs(idsToProcess) do
local animData = Anim.Active[id]
if animData then
local animData = Anim.Active[id]
if animData then
local entity = animData.entity
local onComplete = animData.onComplete
local onComplete = animData.onComplete
if not DoesEntityExist(entity) then
if onComplete then onComplete(false, "despawned") end
Anim.Active[id] = nil
@@ -54,10 +51,8 @@ function Anim.Start()
animData.startTime = GetGameTimer()
animData.animEndTime = animData.duration > 0 and (animData.startTime + animData.duration) or -1
animData.status = "playing"
elseif animData.status == "playing" then
local animationCompletedNaturally = false
if animData.duration == -1 then
if not IsEntityPlayingAnim(entity, animData.animDict, animData.animName, 3) and GetEntityAnimCurrentTime(entity, animData.animDict, animData.animName) > 0.8 then
animationCompletedNaturally = true
@@ -65,17 +60,16 @@ function Anim.Start()
elseif animData.animEndTime ~= -1 and GetGameTimer() >= animData.animEndTime then
animationCompletedNaturally = true
end
if animationCompletedNaturally then
if onComplete then onComplete(true, "completed") end
Anim.Active[id] = nil
end
end
end
end
Wait(100)
end
end
end
end
Wait(100)
end
end
end)
end
@@ -92,7 +86,7 @@ end
--- @param onComplete function | nil
--- @return string | nil
function Anim.Play(id, entity, animDict, animName, blendIn, blendOut, duration, flag, playbackRate, onComplete)
local newId = id or Ids.CreateUniqueId(Anim.Active)
local newId = id or Ids.CreateUniqueId(Anim.Active)
if Anim.Active[newId] then
if onComplete then
onComplete(false, "id_in_use")
@@ -111,7 +105,7 @@ function Anim.Play(id, entity, animDict, animName, blendIn, blendOut, duration,
if onComplete then
onComplete(false, "dict_load_failed")
end
return nil
return nil
end
Anim.Active[newId] = {
@@ -124,7 +118,7 @@ function Anim.Play(id, entity, animDict, animName, blendIn, blendOut, duration,
flag = flag or 1,
playbackRate = playbackRate or 0.0,
onComplete = onComplete,
status = "pending_task",
status = "pending_task",
startTime = 0,
animEndTime = 0
}
@@ -139,7 +133,7 @@ function Anim.Stop(id)
end
local animData = Anim.Active[id]
if animData.entity and DoesEntityExist(animData.entity) and IsEntityAPed(animData.entity) then
if animData.status == "playing" or animData.status == "pending_task" then
StopAnimTask(animData.entity, animData.animDict, animData.animName, 1.0)
@@ -150,7 +144,7 @@ function Anim.Stop(id)
animData.onComplete(false, "stopped_by_id")
end
Anim.Active[id] = nil
Anim.Active[id] = nil
local anyLeft = Anim.Active and next(Anim.Active) ~= nil
if not anyLeft then
Anim.isUpdateLoopRunning = false
+10 -13
View File
@@ -13,7 +13,7 @@ local function SpawnEntity(entityData)
-- print(string.format("SpawnEntity %s: %s", k, v))
-- end
local model = entityData.model and type(entityData.model) == 'string' and GetHashKey(entityData.model) or entityData.model
if not Utility.LoadModel(model) then
if model and not Utility.LoadModel(model) then
print(string.format("[ClientEntity] Failed to load model %s for entity %s", entityData.model, entityData.id))
return
end
@@ -30,23 +30,20 @@ local function SpawnEntity(entityData)
-- Apply ped specific settings from entityData.meta if needed
elseif entityData.entityType == 'vehicle' then
entity = CreateVehicle(model, coords.x, coords.y, coords.z, type(rotation) == 'number' and rotation or rotation.z, false, false)
-- Apply vehicle specific settings from entityData.meta if needed
else
print(string.format("[ClientEntity] Unknown entity type '%s' for entity %s", entityData.entityType, entityData.id))
end
if entity then
-- print(string.format("[ClientEntity] Spawned %s entity %s (GameID: %s)", entityData.entityType, entityData.id, entity))
if entity and model then
entityData.spawned = entity
SetModelAsNoLongerNeeded(model)
SetEntityAsMissionEntity(entity, true, true) -- Keep entity from being deleted by game engine
FreezeEntityPosition(entity, true) -- Optional freeze based on meta
if entityData.OnSpawn then
entityData.OnSpawn(entityData)
end
-- print(string.format("[ClientEntity] Spawned %s entity %s (GameID: %s)", entityData.entityType, entityData.id, entity))
SetEntityAsMissionEntity(entity, true, true)
FreezeEntityPosition(entity, true)
else
SetModelAsNoLongerNeeded(model)
end
if entityData.OnSpawn then
entityData.OnSpawn(entityData)
end
end
local function RemoveEntity(entityData)
@@ -83,8 +80,8 @@ function ClientEntity.Unregister(id)
local entityData = Entities[id]
if not entityData then return end
Point.Remove(id)
RemoveEntity(entityData)
Point.Remove(id)
RemoveEntity(entityData)
Entities[id] = nil
end
@@ -193,7 +190,7 @@ end)
RegisterNetEvent("community_bridge:client:TriggerEntityActions", function(entityId, actions, endPosition)
local entityData = Entities[entityId]
if entityData then
for _, actionData in pairs(actions) do
for _, actionData in pairs(actions) do
local actionName = actionData.name
local actionParams = actionData.params
if actionName == "Stop" then
+4 -4
View File
@@ -432,13 +432,13 @@ Framework.RegisterUsableItem = function(itemName, cb)
ESX.RegisterUsableItem(itemName, func)
end
RegisterNetEvent("esx:playerLoaded", function()
local src = source
RegisterNetEvent("esx:playerLoaded", function(src)
src = src or source
TriggerEvent("community_bridge:Server:OnPlayerLoaded", src)
end)
RegisterNetEvent("esx:playerLogout", function()
local src = source
RegisterNetEvent("esx:playerLogout", function(src)
src = src or source
TriggerEvent("community_bridge:Server:OnPlayerUnload", src)
end)
+4 -4
View File
@@ -562,13 +562,13 @@ Framework.RegisterUsableItem = function(itemName, cb)
QBCore.Functions.CreateUseableItem(itemName, func)
end
RegisterNetEvent("QBCore:Server:OnPlayerLoaded", function()
local src = source
RegisterNetEvent("QBCore:Server:OnPlayerLoaded", function(src)
src = src or source
TriggerEvent("community_bridge:Server:OnPlayerLoaded", src)
end)
RegisterNetEvent("QBCore:Server:OnPlayerUnload", function(source)
local src = source
RegisterNetEvent("QBCore:Server:OnPlayerUnload", function(src)
src = src or source
TriggerEvent("community_bridge:Server:OnPlayerUnload", src)
end)
+4 -4
View File
@@ -471,13 +471,13 @@ Framework.RegisterUsableItem = function(itemName, cb)
return QBox:CreateUseableItem(itemName, func)
end
RegisterNetEvent("QBCore:Server:OnPlayerLoaded", function()
local src = source
RegisterNetEvent("QBCore:Server:OnPlayerLoaded", function(src)
src = src or source
TriggerEvent("community_bridge:Server:OnPlayerLoaded", src)
end)
RegisterNetEvent("QBCore:Server:OnPlayerUnload", function()
local src = source
RegisterNetEvent("QBCore:Server:OnPlayerUnload", function(src)
src = src or source
TriggerEvent("community_bridge:Server:OnPlayerUnload", src)
end)
+16 -3
View File
@@ -102,19 +102,32 @@ end
---@param size table
---@param heading number
---@param options table
Target.AddBoxZone = function(name, coords, size, heading, options)
options = Target.FixOptions(options)
Target.AddBoxZone = function(name, coords, size, heading, options, debug)
options = Target.FixOptions(options or {})
local target = ox_target:addBoxZone({
coords = coords,
size = size,
rotation = heading,
debug = targetDebug,
debug = debug or targetDebug,
options = options,
})
table.insert(targetZones, { name = name, id = target, creator = GetInvokingResource() })
return target
end
-- RegisterCommand('boxzone:test', function(source, args, rawCommand)
-- local coords = GetEntityCoords(PlayerPedId())
-- Target.AddBoxZone('test_zone', coords, { 2.0, 2.0, 2.0 }, 0.0, {
-- {
-- label = 'Test Option',
-- icon = 'fas fa-check',
-- onSelect = function()
-- print('Test option selected!')
-- end
-- }
-- }, true)
-- 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.
---@param name string
---@param coords table
+5 -5
View File
@@ -66,7 +66,7 @@ Target.AddGlobalVehicle = function(options)
})
end
---This will remove target options from all vehicles.
---This will remove target options from all vehicles.
---@param options table
Target.RemoveGlobalVehicle = function(options)
local assembledLables = {}
@@ -116,14 +116,14 @@ end
---@param size table
---@param heading number
---@param options table
Target.AddBoxZone = function(name, coords, size, heading, options)
Target.AddBoxZone = function(name, coords, size, heading, options, debug)
options = Target.FixOptions(options)
qb_target:AddBoxZone(name, coords, size.x, size.y, {
name = name,
debugPoly = targetDebug,
debugPoly = debug or targetDebug,
heading = heading,
minZ = coords.z - (size.x * 0.5),
maxZ = coords.z + (size.x * 0.5),
minZ = coords.z - (size.z * 0.5),
maxZ = coords.z + (size.z * 0.5),
}, {
options = options,
distance = options.distance or 1.5,
+4 -4
View File
@@ -7,7 +7,7 @@
-- -- -- -- -- -- -- -- -- --
if not IsDuplicityVersion() then goto client end
exports.community_bridge:RegisterCallback('callback_test1_name_here', function(src, var1)
Bridge.Callbacks.Register('callback_test1_name_here', function(src, var1)
if var1 == "herpaderpa" then
print("this is a test")
end
@@ -15,7 +15,7 @@ exports.community_bridge:RegisterCallback('callback_test1_name_here', function(s
end)
RegisterCommand('callback_test2', function(source)
local someData, other = exports.community_bridge:TriggerCallback('callback_test2_name_here', source, "herpaderpa")
local someData, other = Bridge.Callbacks.Trigger('callback_test2_name_here', source, "herpaderpa")
print("Data From Client: ", someData, other)
end)
@@ -27,7 +27,7 @@ if IsDuplicityVersion() then return end
::client::
RegisterCommand('callback_test1', function()
local somerandomstring, someotherrandomstring = exports.community_bridge:TriggerCallback('callback_test1_name_here', "herpaderpa")
local somerandomstring, someotherrandomstring = Bridge.Callbacks.Trigger('callback_test1_name_here', "herpaderpa")
print("Data From Server: ", somerandomstring, someotherrandomstring)
end)
@@ -35,6 +35,6 @@ end)
-- print("this is a rebound to all players")
-- end)
exports.community_bridge:RegisterCallback('callback_test2_name_here', function(ftpcsftug)
Bridge.Callbacks.Register('callback_test2_name_here', function(ftpcsftug)
return ftpcsftug, 'TestyMcTesterton'
end)