Files
community_bridge/lib/entities/shared/behaviors.lua
T
Mr.Crowley c399fbaca4 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.
2025-08-31 20:50:46 -05:00

79 lines
2.9 KiB
Lua

Behaviors = {
All = {},
isSetup = false,
invoked = {}
}
function Behaviors.Create(behaviorId, behavior)
if not behaviorId or not behavior then return end
Behaviors.All = Behaviors.All or {}
local invoking = GetInvokingResource() or "community_bridge"
Behaviors.All[invoking] = Behaviors.All[invoking] or {}
Behaviors.All[invoking][behaviorId] = behavior
end
function Behaviors.Get(behaviorId)
local invoking = GetInvokingResource() or "community_bridge"
return Behaviors.All[invoking] and Behaviors.All[invoking][behaviorId]
end
function Behaviors.Remove(behaviorId)
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
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, ...)
if not success then
print(string.format("[ClientEntity] Behavior %s failed: %s", property, result))
end
end
end
end
function Behaviors.Inherit(behaviorId, clientEntityData, defaultData)
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
if not clientEntityData or not clientEntityData.id then
print("[ClientEntity] Invalid client entity data provided for inheritance")
return false
end
clientEntityData[behaviorId] = defaultData or {} -- Mark the entity as having this behavior
return true
end
function Behaviors.Has(behaviorId, clientEntityData)
return clientEntityData and clientEntityData[behaviorId]
end
function Behaviors.Cleanup(resourceName)
if not resourceName or not Behaviors.All[resourceName] then return end
for _, behaviorId in ipairs(Behaviors.All[resourceName]) do
Behaviors.Remove(behaviorId)
end
Behaviors.All[resourceName] = nil
end
AddEventHandler("onResourceStop", function(resourceName)
Behaviors.Cleanup(resourceName)
if resourceName ~= GetCurrentResourceName() then return end
for invoking, behaviors in pairs(Behaviors.All) do
for behaviorId, _ in pairs(behaviors) do
Behaviors.Remove(behaviorId)
end
end
end)
return Behaviors