lib structure overhaul

restructured lib portion to match modules
also edited community bridges onplayerunload fire to accept the passed src as it was otherwise nil
This commit is contained in:
gononono64 committed 2025-03-30 21:06:58 -05:00
1 parent 0b67e4b029
commit 8fd5de2d4e
36 files changed
+459 -210

No files matched your search

+52
View File
@@ -0,0 +1,52 @@
-- Objects
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")
local Entities = {}
BaseEntity = {}
function BaseEntity.Create(id, model, coords, rotation, meta)
local self = meta or {}
if id and type(id) == "table" then
self = id
self.id = id.id or Ids.CreateUniqueId(Entities)
self.rotation = id.rotation or vector3(0.0, 0.0, 0.0)
else
self.id = id or Ids.CreateUniqueId(Entities)
self.model = model
self.coords = coords
self.rotation = rotation or vector3(0.0, 0.0, 0.0)
end
assert(self.id, "ID Failed to generate")
assert(self.model, "Model is required for entity creation")
assert(self.coords, "Coords are required for entity creation")
self.Spawn = self.Spawn or function() end
self.Remove = self.Remove or function() end
return Point.Register(self.id, self.coords, 50.0, self.Spawn, self.Remove, nil, self)
end
function BaseEntity.GetAll()
return Entities
end
function BaseEntity.Get(id)
return Entities[id]
end
function BaseEntity.Add(self)
Entities[self.id] = self
end
function BaseEntity.Remove(id)
Entities[id] = nil
end
function BaseEntity.LoadModel(model)
return Utility.LoadModel(model)
end
return BaseEntity
+71
View File
@@ -0,0 +1,71 @@
BaseEntity = BaseEntity or Require("lib/entities/client/entity_base.lua")
local Objects = {}
Object = {}
function Object.Create(id, model, coords, rotation, meta)
local base = meta or {}
if id and type(id) == "table" then
base = id
base.id = id.id or Ids.CreateUniqueId(Objects)
base.rotation = id.rotation or vector3(0.0, 0.0, 0.0)
else
base.id = id or Ids.CreateUniqueId(Objects)
base.model = model
base.coords = coords
base.rotation = rotation or vector3(0.0, 0.0, 0.0)
end
assert(base.id, "ID Failed to generate")
assert(base.model, "Model is required for entity creation")
assert(base.coords, "Coords are required for entity creation")
base.Spawn = function(self)
print("Spawning object", self)
if not BaseEntity.LoadModel(self.model) then return end
self.entity = CreateObject(self.model, self.coords.x, self.coords.y, self.coords.z, false, false, false)
SetEntityRotation(self.entity, self.rotation.x, self.rotation.y, self.rotation.z, 2, false)
SetEntityAsMissionEntity(self.entity, true, true)
SetModelAsNoLongerNeeded(self.model)
if self.OnSpawn then self.OnSpawn(self, self.entity, self.coords, self.rotation) end
return self.entity
end
base.Remove = function(self)
DeleteEntity(self.entity)
if self.OnRemove then self.OnRemove(self, self.entity) end
self.entity = nil
end
Object.Add(base)
return BaseEntity.Create(base)
end
function Object.GetAll()
return Objects
end
function Object.Get(id)
return Objects[id]
end
function Object.Add(self)
Objects[self.id] = self
end
function Object.Remove(id)
Objects[id] = nil
end
function Object.SetOnSpawn(id, callback)
local obj = Object.Get(id)
if not obj then return end
obj.OnSpawn = callback
return true
end
function Object.SetOnRemove(id, callback)
local obj = Object.Get(id)
if not obj then return end
obj.OnRemove = callback
return true
end
return Object
+65
View File
@@ -0,0 +1,65 @@
-- Objects
Ids = Ids or Require("lib/utility/shared/ids.lua")
local Entities = {}
EntityRelay = {}
function EntityRelay.Create(id, model, coords, rotation, meta)
local self = meta or {}
if id and type(id) == "table" then
self = id
self.id = id.id or Ids.CreateUniqueId(Entities)
self.rotation = id.rotation or vector3(0.0, 0.0, 0.0)
else
self.id = id or Ids.CreateUniqueId(Entities)
self.model = model
self.coords = coords
self.rotation = rotation or vector3(0.0, 0.0, 0.0)
end
assert(self.id, "ID Failed to generate")
assert(self.model, "Model is required for entity creation")
assert(self.coords, "Coords are required for entity creation")
EntityRelay.Add(self)
TriggerClientEvent(GetCurrentResourceName() .. "client:CreateEntity", -1, self)
end
function EntityRelay.GetAll()
return Entities
end
function EntityRelay.Get(id)
return Entities[id]
end
function EntityRelay.Add(self)
Entities[self.id] = self
end
function EntityRelay.Remove(id)
Entities[id] = nil
end
function EntityRelay.LoadModel(model)
return Utility.LoadModel(model)
end
return EntityRelay
-- RegisterCommand("entityobj", function()
-- local entity = Bridge.Object.Create({
-- id = "something", -- optional
-- model = "prop_beach_fire",
-- coords = GetEntityCoords(PlayerPedId()),
-- rotation = vector3(0.0, 0.0, 0.0),
-- })
-- Bridge.Object.SetOnSpawn("something", function(self, coords, rotation)
-- print("Entity spawned")
-- end)
-- Bridge.Object.SetOnRemove("something", function(self, entity)
-- print("Entity removed")
-- end)
-- end)
+49
View File
@@ -0,0 +1,49 @@
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