From 495b24e53fd7aedc273274683b350b49a8a9cb37 Mon Sep 17 00:00:00 2001 From: "Mr.Crowley" Date: Wed, 27 Aug 2025 06:13:02 -0500 Subject: [PATCH] fixed last mess --- lib/entities/client/behaviors/particles.lua | 56 ---------- lib/entities/client/behaviors/targets.lua | 54 ---------- lib/entities/client/client_entity.lua | 19 +++- lib/entities/server/server_entity.lua | 112 +++++++------------- lib/entities/shared/actions.lua | 49 --------- lib/init.lua | 1 + 6 files changed, 55 insertions(+), 236 deletions(-) delete mode 100644 lib/entities/client/behaviors/particles.lua delete mode 100644 lib/entities/client/behaviors/targets.lua delete mode 100644 lib/entities/shared/actions.lua diff --git a/lib/entities/client/behaviors/particles.lua b/lib/entities/client/behaviors/particles.lua deleted file mode 100644 index 418569b..0000000 --- a/lib/entities/client/behaviors/particles.lua +++ /dev/null @@ -1,56 +0,0 @@ -Utility = Utility or Require("lib/utility/client/utility.lua") -local ParticlesBehaviors = { - tag = "particles", - default = { - dict = "cut_exile3", - ptfx = "cs_ex3_wheel_spin", - offset = vector3(0, 0, 0), - rotation = vector3(0, 0, 0), - size = 1.0, - color = vector3(255, 255, 255), - loopLength = 5000, - } - -} - -function ParticlesBehaviors.OnSpawn(entityData) - if not entityData.particles then return end - if entityData.particles.dict then -- single particle - entityData.particles = {entityData.particles} - end - - for _, particleData in ipairs(entityData.particles) do - local position = vector3(entityData.coords.x, entityData.coords.y, entityData.coords.z) - local rotation = vector3(entityData.rotation.x, entityData.rotation.y, entityData.rotation.z) - position = position + (particleData.offset?.xyz or vector3(0, 0, 0)) - rotation = rotation + (particleData.rotation?.xyz or vector3(0, 0, 0)) - particleData.playing = Particle.Play( - particleData.dict or ParticlesBehaviors.default.dict, - particleData.ptfx or ParticlesBehaviors.default.ptfx, - position, - rotation, - particleData.size or ParticlesBehaviors.default.size, - particleData.color or ParticlesBehaviors.default.color, - particleData.looped, - particleData.loopLength or ParticlesBehaviors.default.loopLength - ) - end -end - - -function ParticlesBehaviors.OnRemove(entityData) - if not entityData.particles then return end - for _, particleData in ipairs(entityData.particles) do - if particleData.playing then - Particle.Stop(particleData.playing) - particleData.playing = nil - end - end -end - --- function ParticlesBehaviors.OnUpdate(entityData) --- if not entityData.particles then return end - --- end - -return ParticlesBehaviors diff --git a/lib/entities/client/behaviors/targets.lua b/lib/entities/client/behaviors/targets.lua deleted file mode 100644 index 81135c2..0000000 --- a/lib/entities/client/behaviors/targets.lua +++ /dev/null @@ -1,54 +0,0 @@ - -local Targets = { - tag = "targets", - default = { - label = "Default Target Label", - distance = 2, - } -} - - -function Targets.OnSpawn(entityData) - if not entityData.spawned or not entityData.target then return end - if entityData.targets?.label then - entityData.newTargets = {entityData.targets} - else - entityData.newTargets = entityData.targets or {} - end - for k, v in pairs(entityData.newTargets) do - local onSelect = v.onSelect - if onSelect then - v.onSelect = function(entity) - onSelect(entityData, entity) - end - end - end - Target.AddLocalEntity(entityData.spawned, entityData.newTargets) - entityData.oldTargets = entityData.newTargets -end - -function Targets.OnRemove(entityData) - if not entityData.spawned or not entityData.newTargets then return end - Target.RemoveLocalEntity(entityData.spawned) -end - -function Targets.OnUpdate(entityData) - if not entityData.spawned or not entityData.newTargets then return end - for i, target in ipairs(entityData.oldTargets) do - local newTarget = entityData.targets.label - if newTarget then - entityData.newTargets = {entityData.targets} - end - newTarget = entityData.newTargets[i] - if newTarget - and target.label ~= newTarget.label - or target.distance ~= newTarget.distance - or target.description ~= newTarget.description - then - Targets.OnRemove(entityData) - Targets.OnSpawn(entityData) - end - end -end - -return Targets \ No newline at end of file diff --git a/lib/entities/client/client_entity.lua b/lib/entities/client/client_entity.lua index 22fa91b..0c3f8a5 100644 --- a/lib/entities/client/client_entity.lua +++ b/lib/entities/client/client_entity.lua @@ -1,9 +1,11 @@ Utility = Utility or Require("lib/utility/client/utility.lua") Ids = Ids or Require("lib/utility/shared/ids.lua") Point = Point or Require("lib/points/client/points.lua") -Behaviors = Behaviors or Require("lib/entities/client/behaviors.lua") +Behaviors = Behaviors or Require("lib/entities/shared/behaviors.lua") local Entities = {} -- Stores entity data received from server -ClientEntity = {} -- Renamed from BaseEntity +ClientEntity = { + Invoked = {} +} -- Renamed from BaseEntity ClientEntity.Behaviors = Behaviors @@ -113,6 +115,9 @@ function ClientEntity.Create(entityData) local entityPoint = Point.Register(entityData.id, entityData.coords, entityData.spawnDistance or 50.0, entityData, SpawnEntity, RemoveEntity, UpdateEntity) Entities[entityData.id] = entityPoint + entityData.invoked = GetInvokingResource() or "community_bridge" + ClientEntity.Invoked[entityData.invoked] = ClientEntity.Invoked[entityData.invoked] or {} + ClientEntity.Invoked[entityData.invoked][entityData.id] = entityData Behaviors.Trigger("OnCreate", entityPoint) return entityPoint end @@ -146,7 +151,9 @@ function ClientEntity.Destroy(id) end ClientEntity.Unregister = ClientEntity.Destroy - +function ClientEntity.RegisterBehavior(id, behavior) + Behaviors.Create(id, behavior) +end function ClientEntity.Set(id, key, value) local entityData = ClientEntity.Get(id) @@ -307,6 +314,12 @@ AddEventHandler('onResourceStop', function(resourceName) RemoveEntity(entityData) -- Clean up spawned game entity end Entities = {} -- Clear local cache + else + for id, entityData in pairs(ClientEntity.Invoked[resourceName] or {}) do + Point.Remove(id) -- Clean up point registration + RemoveEntity(entityData) -- Clean up spawned game entity + Entities[id] = nil + end end end) diff --git a/lib/entities/server/server_entity.lua b/lib/entities/server/server_entity.lua index 487a809..28d0ccb 100644 --- a/lib/entities/server/server_entity.lua +++ b/lib/entities/server/server_entity.lua @@ -1,7 +1,10 @@ Ids = Ids or Require("lib/utility/shared/ids.lua") +Behaviors = Behaviors or Require("lib/entities/shared/behaviors.lua") local Entities = {} ServerEntity = {} -- Renamed from EntityRelay +ServerEntity.Behaviors = Behaviors +local Invoked = {} --- Creates a server-side representation of an entity and notifies clients. -- @param entityType string 'object', 'ped', or 'vehicle' @@ -10,27 +13,28 @@ ServerEntity = {} -- Renamed from EntityRelay -- @param rotation vector3|number Heading for peds/vehicles, rotation for objects -- @param meta table Optional additional data -- @return table The created entity data -function ServerEntity.New(id, entityType, model, coords, rotation, meta) - local self = meta or {} +function ServerEntity.New(id, entityType, model, coords, rotation, args) + assert(entityType, "EntityType is required") + assert(model, "Model is required for entity creation") + assert(coords, "Coords are required for entity creation") + local self = args or {} self.id = id or Ids.CreateUniqueId(Entities) self.entityType = entityType self.model = model self.coords = coords - self.rotation = rotation or (entityType == 'object' and vector3(0.0, 0.0, 0.0) or 0.0) -- Default rotation or heading - self.resource = GetInvokingResource() + self.rotation = rotation or vector3(0.0, 0.0, 0.0) + local invoking = GetInvokingResource() or "community_bridge" + self.invoking = invoking + Invoked[invoking] = Invoked[invoking] or {} + table.insert(Invoked[invoking], self) - assert(self.id, "ID Failed to generate") - assert(self.entityType, "EntityType is required") - assert(self.model, "Model is required for entity creation") - assert(self.coords, "Coords are required for entity creation") ServerEntity.Add(self) return self end -function ServerEntity.Create(id, entityType, model, coords, rotation, meta) - local self = ServerEntity.New(id, entityType, model, coords, rotation, meta) - if not self then - return nil - end +function ServerEntity.Create(data) + local self = ServerEntity.New(data.id, data.entityType, data.model, data.coords, data.rotation or vector3(0.0, 0.0, data.heading or 0.0), data) + if not self then return nil end + Behaviors.Trigger("OnCreate", self) TriggerClientEvent("community_bridge:client:CreateEntity", -1, self) return self end @@ -39,15 +43,9 @@ function ServerEntity.CreateBulk(entities) local createdEntities = {} for _, entityData in pairs(entities) do local id = entityData.id or Ids.CreateUniqueId(Entities) - local entity = ServerEntity.New( - id, - entityData.entityType, - entityData.model, - entityData.coords, - entityData.rotation, - entityData.meta - ) + local entity = ServerEntity.New(id, entityData.entityType, entityData.model, entityData.coords, entityData.rotation, entityData) createdEntities[id] = entity + Behaviors.Trigger("OnCreate", entity) end TriggerClientEvent("community_bridge:client:CreateEntities", -1, createdEntities) return createdEntities @@ -55,54 +53,19 @@ end --- Deletes a server-side entity representation and notifies clients. -- @param id string|number The ID of the entity to delete. -function ServerEntity.Delete(id) - if Entities[id] then - ServerEntity.Remove(id) - TriggerClientEvent("community_bridge:client:DeleteEntity", -1, id) - end +function ServerEntity.Destroy(id) + if not Entities[id] then return end + ServerEntity.Remove(id) + TriggerClientEvent("community_bridge:client:DeleteEntity", -1, id) end +ServerEntity.Delete = ServerEntity.Destroy ---- Updates data for a server-side entity and notifies clients. --- @param id string|number The ID of the entity to update. --- @param data table The data fields to update. -function ServerEntity.Update(id, data) - local entity = Entities[id] - -- print("Updating entity: ", id, entity) - if not entity then return false end - - for key, value in pairs(data) do - entity[key] = value +function ServerEntity.DestroyBulk(entityDatas) + for _, entityData in pairs(entityDatas) do + ServerEntity.Remove(entityData.id) end - TriggerClientEvent("community_bridge:client:UpdateEntity", -1, id, data) - return true -end - ---- Triggers a specific action on the client-side entity. --- Clients will only execute the action if the entity is currently spawned for them. --- @param entityId string|number The ID of the entity. --- @param actionName string The name of the action to trigger (must match a function in ClientEntityActions). --- @param ... any Additional arguments for the action function. -function ServerEntity.TriggerAction(entityId, actionName, endPosition, ...) - -- print("Triggering action: ", entityId, actionName, ...) - local entity = Entities[entityId] - if not entity then - print(string.format("[ServerEntity] Attempted to trigger action '%s' on non-existent entity %s", actionName, entityId)) - return - end - TriggerClientEvent("community_bridge:client:TriggerEntityAction", -1, entityId, actionName, endPosition, ...) -end - -function ServerEntity.TriggerActions(entityId, actions, endPosition) - local entity = Entities[entityId] - if not entity then - print(string.format("[ServerEntity] Attempted to trigger actions on non-existent entity %s", entityId)) - return - end - TriggerClientEvent("community_bridge:client:TriggerEntityActions", -1, entityId, actions, endPosition) -end - -function ServerEntity.GetAll() - return Entities + print("[ServerEntity] DestroyBulk: Removed", #entityDatas, "entities") + TriggerClientEvent("community_bridge:client:DeleteBulk", -1, entityDatas) end function ServerEntity.Get(id) @@ -130,18 +93,19 @@ function ServerEntity.Set(id, data) return true end + -- Clean up entities associated with a stopped resource AddEventHandler('onResourceStop', function(resourceName) - local toDelete = {} - for id, entity in pairs(Entities) do - if entity.resource == resourceName then - table.insert(toDelete, id) - end - end - for _, id in pairs(toDelete) do - ServerEntity.Delete(id) + if Invoked[resourceName] then + ServerEntity.DestroyBulk(Invoked[resourceName]) + Invoked[resourceName] = nil end end) +AddEventHandler('community_bridge:Server:OnPlayerLoaded', function(source) + print("Player loaded:", source) + TriggerClientEvent("community_bridge:client:CreateEntities", source, Entities) +end) + return ServerEntity diff --git a/lib/entities/shared/actions.lua b/lib/entities/shared/actions.lua deleted file mode 100644 index 1725820..0000000 --- a/lib/entities/shared/actions.lua +++ /dev/null @@ -1,49 +0,0 @@ -local Actions = {} -Action = {} - - -if not IsDuplicityVersion() then goto client end - -function Action.Fire(id, players, ...) - local action = Actions[id] - if not action then return end - if type(players) == "table" then - for _, player in ipairs(players) do - TriggerClientEvent(GetCurrentResourceName() .. "client:Action", tonumber(player), id, ...) - end - return - end - TriggerClientEvent(GetCurrentResourceName() .. "client:Action", tonumber(players or -1), id, ...) -end - -if IsDuplicityVersion() then return Actions end -::client:: - -function Action.Create(id, action) - assert(type(id) == "string", "id must be a string") - assert(type(action) == "function", "action must be a function") - Actions[id] = action -end - -function Action.Remove(id) - Actions[id] = nil -end - -function Action.Get(id) - return Actions[id] -end - -function Action.GetAll() - return Actions -end - -RegisterNetEvent(GetCurrentResourceName() .. "client:Action", function(id, ...) - local action = Actions[id] - if not action then return end - action(...) -end) - -exports("Action", Action) -return Action - - diff --git a/lib/init.lua b/lib/init.lua index b95fee8..e8676e0 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -59,6 +59,7 @@ cLib.Cache = Cache or Require("lib/cache/shared/cache.lua") cLib.Marker = Marker or Require("lib/markers/server/server.lua") cLib.Particle = Particle or Require("lib/particles/server/particles.lua") cLib.Shell = Shells or Require("lib/shells/server/shells.lua") +cLib.ServerEntity = ServerEntity or Require("lib/entities/server/server_entity.lua") if IsDuplicityVersion() then return cLib end ::client::