Files
community_bridge/lib/entities/server/server_entity.lua
T

135 lines
4.6 KiB
Lua
Raw Normal View History

2025-04-23 14:10:19 -05:00
Ids = Ids or Require("lib/utility/shared/ids.lua")
local Entities = {}
ServerEntity = {} -- Renamed from EntityRelay
--- Creates a server-side representation of an entity and notifies clients.
-- @param entityType string 'object', 'ped', or 'vehicle'
-- @param model string|number
-- @param coords vector3
-- @param rotation vector3|number Heading for peds/vehicles, rotation for objects
-- @param meta table Optional additional data
-- @return table The created entity data
2025-06-22 17:40:41 -05:00
function ServerEntity.New(id, entityType, model, coords, rotation, meta)
2025-04-23 14:10:19 -05:00
local self = meta 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()
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)
2025-06-22 17:40:41 -05:00
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
print("Failed to create entity with ID: " .. tostring(id))
return nil
end
2025-04-23 14:10:19 -05:00
TriggerClientEvent("community_bridge:client:CreateEntity", -1, self)
return self
end
2025-06-22 17:40:41 -05:00
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
)
createdEntities[id] = entity
end
TriggerClientEvent("community_bridge:client:CreateEntities", -1, createdEntities)
return createdEntities
end
2025-04-23 14:10:19 -05:00
--- 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
end
--- 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
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.
2025-04-28 22:51:54 -05:00
function ServerEntity.TriggerAction(entityId, actionName, endPosition, ...)
print("Triggering action: ", entityId, actionName, ...)
2025-04-23 14:10:19 -05:00
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
2025-04-28 22:51:54 -05:00
TriggerClientEvent("community_bridge:client:TriggerEntityAction", -1, entityId, actionName, endPosition, ...)
2025-04-23 14:10:19 -05:00
end
2025-05-04 11:11:29 -05:00
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
2025-04-23 14:10:19 -05:00
function ServerEntity.GetAll()
return Entities
end
function ServerEntity.Get(id)
return Entities[id]
end
function ServerEntity.Add(self)
Entities[self.id] = self
end
function ServerEntity.Remove(id)
Entities[id] = nil
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
2025-06-22 17:40:41 -05:00
ServerEntity.Delete(id)
2025-04-23 14:10:19 -05:00
end
end)
return ServerEntity