From c399fbaca456381e446d9e26d4c568a52b795db2 Mon Sep 17 00:00:00 2001 From: "Mr.Crowley" Date: Sun, 31 Aug 2025 20:50:46 -0500 Subject: [PATCH] Refactor behavior and entity invocation handling Standardizes the use of the 'invoked' property for entities and behaviors, ensuring behaviors are scoped per invoking resource. Updates behavior registration, retrieval, removal, and cleanup logic to support resource-specific behavior tables, improving modularity and preventing cross-resource conflicts. --- lib/entities/client/client_entity.lua | 4 +-- lib/entities/server/server_entity.lua | 2 +- lib/entities/shared/behaviors.lua | 37 +++++++++++++++------------ 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/lib/entities/client/client_entity.lua b/lib/entities/client/client_entity.lua index 187c253..3544dbb 100644 --- a/lib/entities/client/client_entity.lua +++ b/lib/entities/client/client_entity.lua @@ -112,10 +112,10 @@ function ClientEntity.Create(entityData) entityData.rotation = entityData.rotation or vector3(0.0, 0.0, entityData.heading or 0.0) entityData.oldCoords = entityData.oldCoords or vector3(0.0, 0.0, 0.0) entityData.oldRotation = entityData.rotation - + entityData.invoked = entityData.invoked or GetInvokingResource() or "community_bridge" 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) diff --git a/lib/entities/server/server_entity.lua b/lib/entities/server/server_entity.lua index d47a56e..c9950c0 100644 --- a/lib/entities/server/server_entity.lua +++ b/lib/entities/server/server_entity.lua @@ -24,7 +24,7 @@ function ServerEntity.New(id, entityType, model, coords, rotation, args) self.coords = coords self.rotation = rotation or vector3(0.0, 0.0, 0.0) local invoking = GetInvokingResource() or "community_bridge" - self.invoking = invoking + self.invoked = invoking Invoked[invoking] = Invoked[invoking] or {} table.insert(Invoked[invoking], self) diff --git a/lib/entities/shared/behaviors.lua b/lib/entities/shared/behaviors.lua index f152c8e..65f56a0 100644 --- a/lib/entities/shared/behaviors.lua +++ b/lib/entities/shared/behaviors.lua @@ -4,29 +4,31 @@ Behaviors = { invoked = {} } - function Behaviors.Create(behaviorId, behavior) if not behaviorId or not behavior then return end - if Behaviors.All[behaviorId] then return print(string.format("[ClientEntity] Behavior %s already exists", behaviorId)) end + Behaviors.All = Behaviors.All or {} local invoking = GetInvokingResource() or "community_bridge" - Behaviors.All[behaviorId] = behavior - Behaviors.invoked[invoking] = Behaviors.invoked[invoking] or {} - table.insert(Behaviors.invoked[invoking], behaviorId) + Behaviors.All[invoking] = Behaviors.All[invoking] or {} + Behaviors.All[invoking][behaviorId] = behavior end function Behaviors.Get(behaviorId) - return Behaviors.All[behaviorId] + local invoking = GetInvokingResource() or "community_bridge" + return Behaviors.All[invoking] and Behaviors.All[invoking][behaviorId] end function Behaviors.Remove(behaviorId) - if not Behaviors.All[behaviorId] then return end - Behaviors.All[behaviorId] = nil + local invoking = GetInvokingResource() or "community_bridge" + if not Behaviors.All[invoking] or not Behaviors.All[invoking][behaviorId] then return end + Behaviors.All[invoking][behaviorId] = nil return true end function Behaviors.Trigger(actionName, clientEntityData, ...) if not clientEntityData or not actionName then return end - for property, behavior in pairs(Behaviors.All) do + + local invoking = clientEntityData.invoked or GetInvokingResource() or "community_bridge" + for property, behavior in pairs(Behaviors.All[invoking] or {}) do local hasBehaviorArgs = Behaviors.Has(property, clientEntityData) -- this is everything that's contained inside the object's individual property if hasBehaviorArgs and behavior[actionName] then local success, result = pcall(behavior[actionName], clientEntityData, hasBehaviorArgs, ...) @@ -38,7 +40,8 @@ function Behaviors.Trigger(actionName, clientEntityData, ...) end function Behaviors.Inherit(behaviorId, clientEntityData, defaultData) - if not Behaviors.All[behaviorId] then + local invoking = GetInvokingResource() or "community_bridge" + if not Behaviors.All[invoking] or not Behaviors.All[invoking][behaviorId] then print(string.format("[ClientEntity] Behavior %s does not exist", behaviorId)) return false end @@ -55,19 +58,21 @@ function Behaviors.Has(behaviorId, clientEntityData) end function Behaviors.Cleanup(resourceName) - if not resourceName or not Behaviors.invoked[resourceName] then return end - for _, behaviorId in ipairs(Behaviors.invoked[resourceName]) do + if not resourceName or not Behaviors.All[resourceName] then return end + for _, behaviorId in ipairs(Behaviors.All[resourceName]) do Behaviors.Remove(behaviorId) end - Behaviors.invoked[resourceName] = nil + Behaviors.All[resourceName] = nil end AddEventHandler("onResourceStop", function(resourceName) Behaviors.Cleanup(resourceName) if resourceName ~= GetCurrentResourceName() then return end - for behaviorId, _ in pairs(Behaviors.All) do - Behaviors.Remove(behaviorId) + for invoking, behaviors in pairs(Behaviors.All) do + for behaviorId, _ in pairs(behaviors) do + Behaviors.Remove(behaviorId) + end end end) -return Behaviors \ No newline at end of file +return Behaviors