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
+43 -334

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})
-6
View File
@@ -25,7 +25,6 @@ function Anim.RequestDict(animDict)
end end
--- This will start the animation update loop. --- This will start the animation update loop.
--- @return void
function Anim.Start() function Anim.Start()
if Anim.isUpdateLoopRunning then return end if Anim.isUpdateLoopRunning then return end
Anim.isUpdateLoopRunning = true Anim.isUpdateLoopRunning = true
@@ -41,11 +40,9 @@ function Anim.Start()
else else
for _, id in ipairs(idsToProcess) do for _, id in ipairs(idsToProcess) do
local animData = Anim.Active[id] local animData = Anim.Active[id]
if animData then if animData then
local entity = animData.entity local entity = animData.entity
local onComplete = animData.onComplete local onComplete = animData.onComplete
if not DoesEntityExist(entity) then if not DoesEntityExist(entity) then
if onComplete then onComplete(false, "despawned") end if onComplete then onComplete(false, "despawned") end
Anim.Active[id] = nil Anim.Active[id] = nil
@@ -54,10 +51,8 @@ function Anim.Start()
animData.startTime = GetGameTimer() animData.startTime = GetGameTimer()
animData.animEndTime = animData.duration > 0 and (animData.startTime + animData.duration) or -1 animData.animEndTime = animData.duration > 0 and (animData.startTime + animData.duration) or -1
animData.status = "playing" animData.status = "playing"
elseif animData.status == "playing" then elseif animData.status == "playing" then
local animationCompletedNaturally = false local animationCompletedNaturally = false
if animData.duration == -1 then if animData.duration == -1 then
if not IsEntityPlayingAnim(entity, animData.animDict, animData.animName, 3) and GetEntityAnimCurrentTime(entity, animData.animDict, animData.animName) > 0.8 then if not IsEntityPlayingAnim(entity, animData.animDict, animData.animName, 3) and GetEntityAnimCurrentTime(entity, animData.animDict, animData.animName) > 0.8 then
animationCompletedNaturally = true animationCompletedNaturally = true
@@ -65,7 +60,6 @@ function Anim.Start()
elseif animData.animEndTime ~= -1 and GetGameTimer() >= animData.animEndTime then elseif animData.animEndTime ~= -1 and GetGameTimer() >= animData.animEndTime then
animationCompletedNaturally = true animationCompletedNaturally = true
end end
if animationCompletedNaturally then if animationCompletedNaturally then
if onComplete then onComplete(true, "completed") end if onComplete then onComplete(true, "completed") end
Anim.Active[id] = nil Anim.Active[id] = nil
+7 -10
View File
@@ -13,7 +13,7 @@ local function SpawnEntity(entityData)
-- print(string.format("SpawnEntity %s: %s", k, v)) -- print(string.format("SpawnEntity %s: %s", k, v))
-- end -- end
local model = entityData.model and type(entityData.model) == 'string' and GetHashKey(entityData.model) or entityData.model 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)) print(string.format("[ClientEntity] Failed to load model %s for entity %s", entityData.model, entityData.id))
return return
end end
@@ -30,23 +30,20 @@ local function SpawnEntity(entityData)
-- Apply ped specific settings from entityData.meta if needed -- Apply ped specific settings from entityData.meta if needed
elseif entityData.entityType == 'vehicle' then elseif entityData.entityType == 'vehicle' then
entity = CreateVehicle(model, coords.x, coords.y, coords.z, type(rotation) == 'number' and rotation or rotation.z, false, false) 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 else
print(string.format("[ClientEntity] Unknown entity type '%s' for entity %s", entityData.entityType, entityData.id)) print(string.format("[ClientEntity] Unknown entity type '%s' for entity %s", entityData.entityType, entityData.id))
end end
if entity then if entity and model then
-- print(string.format("[ClientEntity] Spawned %s entity %s (GameID: %s)", entityData.entityType, entityData.id, entity))
entityData.spawned = entity entityData.spawned = entity
SetModelAsNoLongerNeeded(model) SetModelAsNoLongerNeeded(model)
SetEntityAsMissionEntity(entity, true, true) -- Keep entity from being deleted by game engine SetEntityAsMissionEntity(entity, true, true)
FreezeEntityPosition(entity, true) -- Optional freeze based on meta FreezeEntityPosition(entity, true)
if entityData.OnSpawn then
entityData.OnSpawn(entityData)
end
-- print(string.format("[ClientEntity] Spawned %s entity %s (GameID: %s)", entityData.entityType, entityData.id, entity))
else else
SetModelAsNoLongerNeeded(model) SetModelAsNoLongerNeeded(model)
end end
if entityData.OnSpawn then
entityData.OnSpawn(entityData)
end
end end
local function RemoveEntity(entityData) local function RemoveEntity(entityData)
+4 -4
View File
@@ -432,13 +432,13 @@ Framework.RegisterUsableItem = function(itemName, cb)
ESX.RegisterUsableItem(itemName, func) ESX.RegisterUsableItem(itemName, func)
end end
RegisterNetEvent("esx:playerLoaded", function() RegisterNetEvent("esx:playerLoaded", function(src)
local src = source src = src or source
TriggerEvent("community_bridge:Server:OnPlayerLoaded", src) TriggerEvent("community_bridge:Server:OnPlayerLoaded", src)
end) end)
RegisterNetEvent("esx:playerLogout", function() RegisterNetEvent("esx:playerLogout", function(src)
local src = source src = src or source
TriggerEvent("community_bridge:Server:OnPlayerUnload", src) TriggerEvent("community_bridge:Server:OnPlayerUnload", src)
end) end)
+4 -4
View File
@@ -562,13 +562,13 @@ Framework.RegisterUsableItem = function(itemName, cb)
QBCore.Functions.CreateUseableItem(itemName, func) QBCore.Functions.CreateUseableItem(itemName, func)
end end
RegisterNetEvent("QBCore:Server:OnPlayerLoaded", function() RegisterNetEvent("QBCore:Server:OnPlayerLoaded", function(src)
local src = source src = src or source
TriggerEvent("community_bridge:Server:OnPlayerLoaded", src) TriggerEvent("community_bridge:Server:OnPlayerLoaded", src)
end) end)
RegisterNetEvent("QBCore:Server:OnPlayerUnload", function(source) RegisterNetEvent("QBCore:Server:OnPlayerUnload", function(src)
local src = source src = src or source
TriggerEvent("community_bridge:Server:OnPlayerUnload", src) TriggerEvent("community_bridge:Server:OnPlayerUnload", src)
end) end)
+4 -4
View File
@@ -471,13 +471,13 @@ Framework.RegisterUsableItem = function(itemName, cb)
return QBox:CreateUseableItem(itemName, func) return QBox:CreateUseableItem(itemName, func)
end end
RegisterNetEvent("QBCore:Server:OnPlayerLoaded", function() RegisterNetEvent("QBCore:Server:OnPlayerLoaded", function(src)
local src = source src = src or source
TriggerEvent("community_bridge:Server:OnPlayerLoaded", src) TriggerEvent("community_bridge:Server:OnPlayerLoaded", src)
end) end)
RegisterNetEvent("QBCore:Server:OnPlayerUnload", function() RegisterNetEvent("QBCore:Server:OnPlayerUnload", function(src)
local src = source src = src or source
TriggerEvent("community_bridge:Server:OnPlayerUnload", src) TriggerEvent("community_bridge:Server:OnPlayerUnload", src)
end) end)
+16 -3
View File
@@ -102,19 +102,32 @@ end
---@param size table ---@param size table
---@param heading number ---@param heading number
---@param options table ---@param options table
Target.AddBoxZone = function(name, coords, size, heading, options) Target.AddBoxZone = function(name, coords, size, heading, options, debug)
options = Target.FixOptions(options) options = Target.FixOptions(options or {})
local target = ox_target:addBoxZone({ local target = ox_target:addBoxZone({
coords = coords, coords = coords,
size = size, size = size,
rotation = heading, rotation = heading,
debug = targetDebug, debug = debug or targetDebug,
options = options, options = options,
}) })
table.insert(targetZones, { name = name, id = target, creator = GetInvokingResource() }) table.insert(targetZones, { name = name, id = target, creator = GetInvokingResource() })
return target return target
end 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. ---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 name string
---@param coords table ---@param coords table
+4 -4
View File
@@ -116,14 +116,14 @@ end
---@param size table ---@param size table
---@param heading number ---@param heading number
---@param options table ---@param options table
Target.AddBoxZone = function(name, coords, size, heading, options) Target.AddBoxZone = function(name, coords, size, heading, options, debug)
options = Target.FixOptions(options) options = Target.FixOptions(options)
qb_target:AddBoxZone(name, coords, size.x, size.y, { qb_target:AddBoxZone(name, coords, size.x, size.y, {
name = name, name = name,
debugPoly = targetDebug, debugPoly = debug or targetDebug,
heading = heading, heading = heading,
minZ = coords.z - (size.x * 0.5), minZ = coords.z - (size.z * 0.5),
maxZ = coords.z + (size.x * 0.5), maxZ = coords.z + (size.z * 0.5),
}, { }, {
options = options, options = options,
distance = options.distance or 1.5, distance = options.distance or 1.5,
+4 -4
View File
@@ -7,7 +7,7 @@
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
if not IsDuplicityVersion() then goto client end 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 if var1 == "herpaderpa" then
print("this is a test") print("this is a test")
end end
@@ -15,7 +15,7 @@ exports.community_bridge:RegisterCallback('callback_test1_name_here', function(s
end) end)
RegisterCommand('callback_test2', function(source) 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) print("Data From Client: ", someData, other)
end) end)
@@ -27,7 +27,7 @@ if IsDuplicityVersion() then return end
::client:: ::client::
RegisterCommand('callback_test1', function() 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) print("Data From Server: ", somerandomstring, someotherrandomstring)
end) end)
@@ -35,6 +35,6 @@ end)
-- print("this is a rebound to all players") -- print("this is a rebound to all players")
-- end) -- end)
exports.community_bridge:RegisterCallback('callback_test2_name_here', function(ftpcsftug) Bridge.Callbacks.Register('callback_test2_name_here', function(ftpcsftug)
return ftpcsftug, 'TestyMcTesterton' return ftpcsftug, 'TestyMcTesterton'
end) end)