Files

105 lines
3.5 KiB
Lua
Raw Permalink Normal View History

2026-08-18 00:13:37 -07:00
2025-04-23 14:10:19 -05:00
local Entities = {}
ServerEntity = {} -- Renamed from EntityRelay
2025-08-27 06:13:02 -05:00
local Invoked = {}
2025-04-23 14:10:19 -05:00
--- 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-08-27 06:13:02 -05:00
function ServerEntity.New(id, entityType, model, coords, rotation, args)
assert(coords, "Coords are required for entity creation")
local self = args or {}
2026-08-17 03:49:37 -07:00
self.id = id or lib.string.random('Aa1A', 8) -- Generate a random ID if not provided
2025-04-23 14:10:19 -05:00
self.entityType = entityType
self.model = model
self.coords = coords
2025-08-27 06:13:02 -05:00
self.rotation = rotation or vector3(0.0, 0.0, 0.0)
local invoking = GetInvokingResource() or "community_bridge"
self.invoked = invoking
2025-08-27 06:13:02 -05:00
Invoked[invoking] = Invoked[invoking] or {}
table.insert(Invoked[invoking], self)
2025-04-23 14:10:19 -05:00
ServerEntity.Add(self)
2025-06-22 17:40:41 -05:00
return self
end
2025-08-27 06:13:02 -05:00
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
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
2026-08-17 03:49:37 -07:00
local id = entityData.id or lib.string.random('Aa1A', 8)
2025-08-27 06:13:02 -05:00
local entity = ServerEntity.New(id, entityData.entityType, entityData.model, entityData.coords, entityData.rotation, entityData)
2025-06-22 17:40:41 -05:00
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.
2025-08-27 06:13:02 -05:00
function ServerEntity.Destroy(id)
if not Entities[id] then return end
ServerEntity.Remove(id)
2026-01-11 18:36:07 -06:00
TriggerClientEvent("community_bridge:client:DeleteEntity", -1, id)
2025-04-23 14:10:19 -05:00
end
2025-08-27 06:13:02 -05:00
ServerEntity.Delete = ServerEntity.Destroy
2025-04-23 14:10:19 -05:00
2025-08-27 06:13:02 -05:00
function ServerEntity.DestroyBulk(entityDatas)
for _, entityData in pairs(entityDatas) do
ServerEntity.Remove(entityData.id)
2025-05-04 11:11:29 -05:00
end
2025-08-27 06:13:02 -05:00
print("[ServerEntity] DestroyBulk: Removed", #entityDatas, "entities")
TriggerClientEvent("community_bridge:client:DeleteBulk", -1, entityDatas)
2025-04-23 14:10:19 -05:00
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
2025-08-27 05:47:44 -05:00
--- 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.Set(id, data)
local entity = Entities[id]
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
2025-08-27 06:13:02 -05:00
2025-04-23 14:10:19 -05:00
-- Clean up entities associated with a stopped resource
AddEventHandler('onResourceStop', function(resourceName)
2025-08-27 06:13:02 -05:00
if Invoked[resourceName] then
ServerEntity.DestroyBulk(Invoked[resourceName])
Invoked[resourceName] = nil
2025-04-23 14:10:19 -05:00
end
end)
2025-08-27 06:13:02 -05:00
AddEventHandler('community_bridge:Server:OnPlayerLoaded', function(source)
2025-08-30 03:43:01 -04:00
-- print("Player loaded:", source)
2025-08-27 06:13:02 -05:00
TriggerClientEvent("community_bridge:client:CreateEntities", source, Entities)
end)
2025-04-23 14:10:19 -05:00
2025-08-30 19:12:56 -05:00
return ServerEntity