From 8346130b004a1e275b088c6452f5f53ac5acddd8 Mon Sep 17 00:00:00 2001 From: "Mr.Crowley" Date: Wed, 3 Sep 2025 00:28:17 -0500 Subject: [PATCH 1/7] Add modular entity behaviors and update entity logic Introduces modular behaviors for entities, including anim, attach, clothing, follow, particles, scenarios, shop, stash, targets, and weapon, each in their own files. Updates client_entity.lua and behaviors.lua to support dynamic behavior registration, initialization, and triggering. Refactors entity update logic to improve movement handling and behavior invocation. --- lib/entities/client/client_entity.lua | 24 +++--- lib/entities/shared/behaviors.lua | 35 ++++++++- lib/entities/shared/behaviors/anim.lua | 62 ++++++++++++++++ lib/entities/shared/behaviors/attach.lua | 50 +++++++++++++ lib/entities/shared/behaviors/clothing.lua | 82 +++++++++++++++++++++ lib/entities/shared/behaviors/follow.lua | 75 +++++++++++++++++++ lib/entities/shared/behaviors/particles.lua | 80 ++++++++++++++++++++ lib/entities/shared/behaviors/scenarios.lua | 64 ++++++++++++++++ lib/entities/shared/behaviors/shop.lua | 58 +++++++++++++++ lib/entities/shared/behaviors/stash.lua | 59 +++++++++++++++ lib/entities/shared/behaviors/targets.lua | 61 +++++++++++++++ lib/entities/shared/behaviors/weapon.lua | 54 ++++++++++++++ 12 files changed, 693 insertions(+), 11 deletions(-) create mode 100644 lib/entities/shared/behaviors/anim.lua create mode 100644 lib/entities/shared/behaviors/attach.lua create mode 100644 lib/entities/shared/behaviors/clothing.lua create mode 100644 lib/entities/shared/behaviors/follow.lua create mode 100644 lib/entities/shared/behaviors/particles.lua create mode 100644 lib/entities/shared/behaviors/scenarios.lua create mode 100644 lib/entities/shared/behaviors/shop.lua create mode 100644 lib/entities/shared/behaviors/stash.lua create mode 100644 lib/entities/shared/behaviors/targets.lua create mode 100644 lib/entities/shared/behaviors/weapon.lua diff --git a/lib/entities/client/client_entity.lua b/lib/entities/client/client_entity.lua index 3544dbb..cc32930 100644 --- a/lib/entities/client/client_entity.lua +++ b/lib/entities/client/client_entity.lua @@ -71,10 +71,21 @@ end local function UpdateEntity(_entityData) local entityData = ClientEntity.Get(_entityData.id) if not entityData then return end + if entityData.OnUpdate then + pcall(function (...) + return entityData.OnUpdate(entityData) + end) + end + Behaviors.Trigger("OnUpdate", entityData) if entityData.oldCoords and entityData.oldCoords ~= entityData.coords then local coords = vector3(entityData.coords.x, entityData.coords.y, entityData.coords.z) + entityData.oldCoords = vector3(entityData.oldCoords.x, entityData.oldCoords.y, entityData.oldCoords.z) local dist = #(coords.xyz - entityData.oldCoords.xyz) - if dist > 0.1 then + if dist > 0.05 then + if entityData.spawned then + print("Moving entity:", entityData.id) + SetEntityCoords(entityData.spawned, coords.x, coords.y, coords.z, false, false, false, true) + end -- ClientEntity.UpdateCoords(entityData.id, entityData.coords) if entityData.OnMove then pcall(function (...) @@ -96,12 +107,7 @@ local function UpdateEntity(_entityData) end entityData.oldRotation = entityData.rotation end - if entityData.OnUpdate then - pcall(function (...) - return entityData.OnUpdate(entityData) - end) - end - Behaviors.Trigger("OnUpdate", entityData) + end --- Registers an entity received from the server and sets up proximity spawning. @@ -258,9 +264,7 @@ function ClientEntity.UpdateCoords(id, coords) return end entityData.coords = coords - Point.UpdateCoords(id, coords) - if not entityData.spawned or not DoesEntityExist(entityData.spawned) then return end - SetEntityCoords(entityData.spawned, coords.x, coords.y, coords.z, false, false, false, true) + Point.UpdateCoords(id, coords) end function ClientEntity.ChangeModel(id, model) diff --git a/lib/entities/shared/behaviors.lua b/lib/entities/shared/behaviors.lua index 65f56a0..e30b3d2 100644 --- a/lib/entities/shared/behaviors.lua +++ b/lib/entities/shared/behaviors.lua @@ -4,6 +4,27 @@ Behaviors = { invoked = {} } +Behaviors.init = { + "anim", + "attach", + 'clothing', + 'follow', + 'particles', + 'scenarios', + 'shop', + 'stash', + 'targets', + 'weapon' +} + +function Behaviors.Init() + if Behaviors.isSetup then return end + Behaviors.isSetup = true + for k, v in pairs(Behaviors.init) do + Require(string.format("lib/entities/shared/behaviors/%s.lua", v)) + end +end + function Behaviors.Create(behaviorId, behavior) if not behaviorId or not behavior then return end Behaviors.All = Behaviors.All or {} @@ -26,7 +47,6 @@ 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 @@ -37,6 +57,15 @@ function Behaviors.Trigger(actionName, clientEntityData, ...) end end end + for property, behavior in pairs(Behaviors.All['community_bridge'] 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] and not Behaviors.All[invoking]?[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) @@ -75,4 +104,8 @@ AddEventHandler("onResourceStop", function(resourceName) end end) +AddEventHandler("onResourceStart", function(resourceName) + Behaviors.Init() +end) + return Behaviors diff --git a/lib/entities/shared/behaviors/anim.lua b/lib/entities/shared/behaviors/anim.lua new file mode 100644 index 0000000..ef8574e --- /dev/null +++ b/lib/entities/shared/behaviors/anim.lua @@ -0,0 +1,62 @@ +if IsDuplicityVersion() then return end +local Animation = { + property = "anim", + default = { + flags = 49, + duration = -1 + } +} +-- Anim.Play(id, entity, animDict, animName, blendIn, blendOut, duration, flag, playbackRate, onComplete) +function Animation.Play(entityData) + if entityData.entityType ~= "ped" then return end + local entity = entityData.spawned + if not entity then return end + local dict = entityData.anim?.dict + local name = entityData.anim?.name + local onComplete = entityData.anim?.onComplete and function() entityData.anim.onComplete(entityData) end + if not dict or not name then return end + local flags = entityData.anim.flags or Animation.default.flags + local duration = entityData.anim.duration or Animation.default.duration + entityData.anim.id = Bridge.Anim.Play(nil, entity, dict, name, 8.0, -8.0, duration, flags, 0.0, onComplete) + Bridge.ClientEntity.Set(entityData.id, "anim", entityData.anim) + entityData.oldAnim = entityData.anim + Bridge.ClientEntity.Set(entityData.id, "oldAnim", entityData.oldAnim) + -- SetTimeout(1000, function() + -- if entityData.anim then + -- entityData.anim.disable = false + -- Bridge.ClientEntity.Set(entityData.id, "anim", entityData.anim) + -- end + -- end) +end + +function Animation.OnSpawn(entityData) + print("Spawning animation for entity:", entityData.id) + if not entityData.spawned or not entityData.anim then return end + Animation.Play(entityData) + entityData.anim.disable = false + TaskSetBlockingOfNonTemporaryEvents(entityData.spawned, true) + SetBlockingOfNonTemporaryEvents(entityData.spawned, true) + Bridge.ClientEntity.Set(entityData.id, "anim", entityData.anim) +end + +function Animation.OnRemove(entityData) + if not entityData.spawned or not entityData.anim then return end + entityData.oldAnim = nil + entityData.anim.disable = true + Bridge.ClientEntity.Set(entityData.id, "anim", entityData.anim) + Bridge.ClientEntity.Set(entityData.id, "oldAnim", entityData.oldAnim) +end + +function Animation.OnUpdate(entityData) + if not entityData.spawned or not entityData.anim then return end + if entityData.anim.disable then return end + if not IsEntityPlayingAnim(entityData.spawned, entityData.anim.dict, entityData.anim.name, 3) then + Bridge.Anim.Stop(entityData.anim?.id) + ClearPedTasksImmediately(entityData.spawned) + Wait(100) + Animation.Play(entityData) + end +end + +Bridge.ClientEntity.RegisterBehavior("anim", Animation) +return Animation \ No newline at end of file diff --git a/lib/entities/shared/behaviors/attach.lua b/lib/entities/shared/behaviors/attach.lua new file mode 100644 index 0000000..291de33 --- /dev/null +++ b/lib/entities/shared/behaviors/attach.lua @@ -0,0 +1,50 @@ +if IsDuplicityVersion() then return end +local Attach = { + property = "attach", + default = { + bone = 0, + offset = vector3(0.0, 0.0, 0.0), + rotation = vector3(0.0, 0.0, 0.0) + } +} + + +function Attach.OnSpawn(entityData) + if not entityData.spawned or not entityData.attach then return end + local entity = entityData.spawned + local targetEntity = entityData.attach.target + if type(targetEntity) == "string" then + targetEntity = Bridge.ClientEntity.Get(targetEntity)?.spawned or targetEntity + else + local player = GetPlayerFromServerId(targetEntity) + if player and player ~= -1 then + targetEntity = GetPlayerPed(player) + end + end + targetEntity = tonumber(targetEntity) + if not targetEntity then return end + local bone = entityData.attach.bone or Attach.default.bone + local offset = entityData.attach.offset or Attach.default.offset + local rotation = entityData.attach.rotation or Attach.default.rotation + AttachEntityToEntity(entity, targetEntity, GetPedBoneIndex(targetEntity, bone), offset.x, offset.y, offset.z, rotation.x, rotation.y, rotation.z, true, true, false, true, 1, true) + Bridge.ClientEntity.Set(entityData.id, "attach", entityData.attach) +end + +function Attach.Detach(entityData) + if not entityData.spawned or not IsEntityAttached(entityData.spawned) then return end + DetachEntity(entityData.spawned, true, true) + Bridge.ClientEntity.Set(entityData.id, "attach", entityData.attach) +end + +function Attach.OnUpdate(entityData) + if not entityData.spawned then return end + if entityData.attach.disable then + return Attach.Detach(entityData) + end + local entity = entityData.spawned + if IsEntityAttached(entity) then return end + Attach.OnSpawn(entityData) +end + +Bridge.ClientEntity.RegisterBehavior("attach", Attach) +return Attach \ No newline at end of file diff --git a/lib/entities/shared/behaviors/clothing.lua b/lib/entities/shared/behaviors/clothing.lua new file mode 100644 index 0000000..1b3f605 --- /dev/null +++ b/lib/entities/shared/behaviors/clothing.lua @@ -0,0 +1,82 @@ +if IsDuplicityVersion() then return end +local Clothing = { + property = "clothing", + default = { + components = {}, + props = {} + } +} + +function Clothing.ChangeClothes(entityData) + if entityData.entityType ~= "ped" then return end + local entity = entityData.spawned + if not entity or not DoesEntityExist(entity) then return end + local clothing = entityData.clothing.target + if clothing then + if type(clothing) == "number" then + local player = GetPlayerFromServerId(clothing) + if player and player ~= -1 then + clothing = Bridge.Clothing.GetAppearance(GetPlayerPed(player)) + end + elseif type(clothing) == "string" then + local targetEntityData = Bridge.ClientEntity.Get(clothing) + if targetEntityData then + clothing = Bridge.Clothing.GetAppearance(targetEntityData.spawned) + end + end + else + clothing = entityData.clothing + end + + Bridge.Clothing.SetAppearance(entity, clothing) + Bridge.ClientEntity.Set(entityData.id, "clothing", clothing) + entityData.oldClothing = clothing + Bridge.ClientEntity.Set(entityData.id, "oldClothing", clothing) + +end + + +function Clothing.OnSpawn(entityData) + if not entityData.spawned or not entityData.clothing then return end + Clothing.ChangeClothes(entityData) + entityData.clothing.updating = false +end + +function Clothing.OnRemove(entityData) + if not entityData.spawned or not entityData.clothing then return end + entityData.oldClothing = nil + entityData.clothing.updating = true +end + +function Clothing.OnUpdate(entityData) + if not entityData.spawned or not entityData.clothing then return end + if entityData.clothing.updating then return end + entityData.clothing.updating = true + if not entityData.oldClothing then + Clothing.ChangeClothes(entityData) + return + end + for componentId, component in pairs(entityData.clothing.components or {}) do + local oldComponent = entityData.oldClothing.components and entityData.oldClothing.components[componentId] + if not oldComponent or oldComponent.drawable ~= component.drawable or oldComponent.texture ~= component.texture then + Clothing.ChangeClothes(entityData) + return + end + end + for propId, prop in pairs(entityData.clothing.props or {}) do + local oldProp = entityData.oldClothing.props and entityData.oldClothing.props[propId] + if not oldProp or oldProp.drawable ~= prop.drawable or oldProp.texture ~= prop.texture then + Clothing.ChangeClothes(entityData) + return + end + end + SetTimeout(1000, function() + if entityData.clothing then + entityData.clothing.updating = false + Bridge.ClientEntity.Set(entityData.id, "clothing", entityData.clothing) + end + end) +end + +Bridge.ClientEntity.RegisterBehavior("clothing", Clothing) +return Clothing \ No newline at end of file diff --git a/lib/entities/shared/behaviors/follow.lua b/lib/entities/shared/behaviors/follow.lua new file mode 100644 index 0000000..b5361a9 --- /dev/null +++ b/lib/entities/shared/behaviors/follow.lua @@ -0,0 +1,75 @@ +if IsDuplicityVersion() then return end +local Follow = { + All = {}, + property = "follow", + default = { + speed = 1.0, + distance = 2.0 + } +} + +function Follow.PedWalkToPos(entityData) + if not entityData?.follow?.target or entityData?.entityType ~= "ped" then return end + local entity = entityData.spawned + local targetCoords = entityData.follow.target + if not targetCoords then return end + if type(targetCoords) == "number" then + local player = GetPlayerFromServerId(targetCoords) + if player and player ~= -1 then + targetCoords = GetEntityCoords(GetPlayerPed(player)) + else + targetCoords = GetEntityCoords(targetCoords) + end + elseif type(targetCoords) == "string" then + local targetEntityData = Bridge.ClientEntity.Get(targetCoords) + if targetEntityData then + targetCoords = GetEntityCoords(targetEntityData.spawned) + end + end + local entityPos = GetEntityCoords(entity) + local distance = #(targetCoords - entityPos) + if distance > (entityData.follow.distance or Follow.default.distance) then + if entityData.anim then + entityData.anim.disable = true + Bridge.ClientEntity.Set(entityData.id, "anim", entityData.anim) + end + if entityData.scenarios then + entityData.scenarios.disable = true + Bridge.ClientEntity.Set(entityData.id, "scenarios", entityData.scenarios) + end + local speed = entityData.follow.speed or Follow.default.speed + local heading = GetHeadingFromVector_2d(targetCoords.x - entityPos.x, targetCoords.y - entityPos.y) + TaskGoStraightToCoord(entity, targetCoords.x, targetCoords.y, targetCoords.z, speed, -1, heading, 0.0) + entityData.coords = GetEntityCoords(entity) + entityData.follow.walking = true + Bridge.ClientEntity.Set(entityData.id, "coords", entityData.coords) + Bridge.ClientEntity.Set(entityData.id, "follow", entityData.follow) + if entityData.follow.OnExit then entityData.follow.OnExit(entityData) end + return false + end + if not entityData.follow.walking then return end + entityData.follow.walking = false + Bridge.ClientEntity.Set(entityData.id, "follow", entityData.follow) + if entityData.anim then + entityData.anim.disable = false + Bridge.ClientEntity.Set(entityData.id, "anim", entityData.anim) + end + if entityData.scenarios then + entityData.scenarios.disable = false + Bridge.ClientEntity.Set(entityData.id, "scenarios", entityData.scenarios) + end + ClearPedTasks(entity) + if entityData.follow.OnEnter then entityData.follow.OnEnter(entityData) end + return true +end + + +function Follow.OnUpdate(entityData) + assert(entityData?.id, "Entity ID is required") + local entity = entityData.spawned + if not entity or not DoesEntityExist(entity) then return end + return Follow.PedWalkToPos(entityData) +end + +Bridge.ClientEntity.RegisterBehavior("follow", Follow) +return Follow \ No newline at end of file diff --git a/lib/entities/shared/behaviors/particles.lua b/lib/entities/shared/behaviors/particles.lua new file mode 100644 index 0000000..fecc6b7 --- /dev/null +++ b/lib/entities/shared/behaviors/particles.lua @@ -0,0 +1,80 @@ +if IsDuplicityVersion() then return end +Utility = Utility or Bridge.Utility +local ParticlesBehaviors = { + property = "particles", + default = { + dict = "cut_exile3", + ptfx = "cs_ex3_wheel_spin", + offset = vector3(0, 0, 0), + rotation = vector3(0, 0, 0), + size = 1.0, + color = vector3(255, 255, 255), + loopLength = 5000, + } + +} + +function ParticlesBehaviors.Start(entityData) + for _, particleData in ipairs(entityData.particles) do + local position = vector3(entityData.coords.x, entityData.coords.y, entityData.coords.z) + local rotation = vector3(entityData.rotation.x, entityData.rotation.y, entityData.rotation.z) + position = position + (particleData.offset?.xyz or vector3(0, 0, 0)) + rotation = rotation + (particleData.rotation?.xyz or vector3(0, 0, 0)) + particleData.playing = Bridge.Particle.Play( + particleData.dict or ParticlesBehaviors.default.dict, + particleData.ptfx or ParticlesBehaviors.default.ptfx, + position, + rotation, + particleData.size or ParticlesBehaviors.default.size, + particleData.color or ParticlesBehaviors.default.color, + particleData.looped, + particleData.loopLength or ParticlesBehaviors.default.loopLength + ) + end +end + +function ParticlesBehaviors.OnSpawn(entityData) + if not entityData.particles then return end + if entityData.particles.dict then -- single particle + entityData.particles = {entityData.particles} + end + ParticlesBehaviors.Start(entityData) +end + + +function ParticlesBehaviors.OnRemove(entityData) + if not entityData.particles then return end + for _, particleData in ipairs(entityData.particles) do + if particleData.playing then + Bridge.Particle.Stop(particleData.playing) + particleData.playing = nil + end + end +end + +function ParticlesBehaviors.OnMove(entityData) + if not entityData.particles then return end + for _, particleData in ipairs(entityData.particles) do + if particleData.playing then + Particle.Stop(particleData.playing) + particleData.playing = nil + local position = vector3(entityData.coords.x, entityData.coords.y, entityData.coords.z) + local rotation = vector3(entityData.rotation.x, entityData.rotation.y, entityData.rotation.z) + position = position + (particleData.offset?.xyz or vector3(0, 0, 0)) + rotation = rotation + (particleData.rotation?.xyz or vector3(0, 0, 0)) + particleData.playing = Bridge.Particle.Play( + particleData.dict or ParticlesBehaviors.default.dict, + particleData.ptfx or ParticlesBehaviors.default.ptfx, + position, + rotation, + particleData.size or ParticlesBehaviors.default.size, + particleData.color or ParticlesBehaviors.default.color, + particleData.looped, + particleData.loopLength or ParticlesBehaviors.default.loopLength + ) + end + end +end + +Bridge.ClientEntity.RegisterBehavior("particles", ParticlesBehaviors) +return ParticlesBehaviors \ No newline at end of file diff --git a/lib/entities/shared/behaviors/scenarios.lua b/lib/entities/shared/behaviors/scenarios.lua new file mode 100644 index 0000000..20efb69 --- /dev/null +++ b/lib/entities/shared/behaviors/scenarios.lua @@ -0,0 +1,64 @@ + +if IsDuplicityVersion() then return end +local Scenarios = { + property = "scenarios", + default = { + introClip = true, + duration = -1 + } +} +-- scenarios.Play(id, entity, scenariosDict, scenariosName, blendIn, blendOut, duration, flag, playbackRate, onComplete) +function Scenarios.Play(entityData) + if entityData.entityType ~= "ped" then return end + local entity = entityData.spawned + if not entity then return end + local name = entityData.scenarios?.name + if not name then return end + local duration = entityData.scenarios?.duration or Scenarios.default.duration + local introClip = entityData.scenarios?.introClip or Scenarios.default.introClip + TaskStartScenarioInPlace(entity, name, duration, introClip) + if duration < 0 then return end + SetTimeout(duration, function() + if not entityData.scenarios then return end + ClearPedTasks(entity) + if not entityData.scenarios.onComplete then return end + entityData.scenarios.disabled = true + Bridge.ClientEntity.Set(entityData.id, "scenarios", entityData.scenarios) + entityData.scenarios.onComplete(entityData) + end) + +end + +function Scenarios.OnSpawn(entityData) + if not entityData.spawned or not entityData.scenarios then return end + Scenarios.Play(entityData) +end + +function Scenarios.OnRemove(entityData) + if not entityData.spawned or not entityData.scenarios then return end + ClearPedTasks(entityData.spawned) +end + +function Scenarios.OnUpdate(entityData) + if not entityData.spawned or not entityData.scenarios then return end + if entityData.scenarios.disable then return end + if IsPedUsingScenario(entityData.spawned, entityData.scenarios.name) then return end + local entities = GetGamePool('CObject') + for _, obj in ipairs(entities or {}) do + obj = tonumber(obj) + local entityCoords = GetEntityCoords(obj) + local coords = vector3(entityData.coords.x, entityData.coords.y, entityData.coords.z) + if entityCoords then + local distance = #(entityCoords - coords) + if distance < 2.0 then + SetEntityAsMissionEntity(obj, false, false) + DeleteObject(obj) + break + end + end + end + Scenarios.Play(entityData) +end + +Bridge.ClientEntity.RegisterBehavior("scenarios", Scenarios) +return Scenarios \ No newline at end of file diff --git a/lib/entities/shared/behaviors/shop.lua b/lib/entities/shared/behaviors/shop.lua new file mode 100644 index 0000000..048e786 --- /dev/null +++ b/lib/entities/shared/behaviors/shop.lua @@ -0,0 +1,58 @@ +local Shop = { + property = "shop", + default = { + label = "Default Shop Label", + items = { + {item = "water", price = 10} + }, + target = { + label = "Open Shop", + icon = "fa-solid fa-box" + } + } +} + +function Shop.OnCreate(entityData) + local shop = entityData.shop + if not shop then return end + if IsDuplicityVersion() then + local label = shop.label or Shop.default.label + local items = shop.items or Shop.default.items + local coords = entityData.coords + local groups = shop.groups + Bridge.Inventory.RegisterShop(label, items, nil, groups) + return + end + entityData.targets = entityData.targets or {} + print(json.encode(entityData.targets)) + table.insert(entityData.targets, + { + label = shop.target.label or "Shop", + icon = shop.target.icon or "fa-solid fa-box", + onSelect = function() + TriggerServerEvent("community_bridge:server:OpenShop", entityData.id) + end + } + ) +end + +if not IsDuplicityVersion() then return Bridge.ClientEntity.RegisterBehavior("shop", Shop) end + +RegisterNetEvent("community_bridge:server:OpenShop", function(id) + local src = source + if not src then return end + local entityData = Bridge.ServerEntity.Get(id) + if not entityData or not entityData.shop then + return print(string.format("[Shop] OpenShop: Entity %s does not exist or has no shop", id)) + end + local coords = entityData.coords + if not coords then return end + local distance = #(GetEntityCoords(GetPlayerPed(src)) - coords) + if distance > 3.0 then + return print(string.format("[Shop] OpenShop: Player %s is too far from entity %s", src, id)) + end + Bridge.Inventory.OpenShop(src, id) +end) + + +return Shop \ No newline at end of file diff --git a/lib/entities/shared/behaviors/stash.lua b/lib/entities/shared/behaviors/stash.lua new file mode 100644 index 0000000..c006a11 --- /dev/null +++ b/lib/entities/shared/behaviors/stash.lua @@ -0,0 +1,59 @@ +local Stash = { + property = "stash", + default = { + label = "default_stash_label", + slots = 20, + maxWeight = 100000, + target = { + label = "Open Stash", + icon = "fa-solid fa-box", + } + } +} + +function Stash.OnCreate(entityData) + if not entityData.stash then return end + if IsDuplicityVersion() then + local label = entityData.stash.label or Stash.default.label + local slots = entityData.stash.slots or Stash.default.slots + local weight = entityData.stash.maxWeight or Stash.default.maxWeight + local owner = entityData.stash.owner + local groups = entityData.stash.groups + Bridge.Inventory.RegisterStash(entityData.id, label, slots, weight, owner, groups) + else + entityData.targets = entityData.targets or {} + entityData.targets['stash'] = { + label = entityData.stash.target.label or Stash.default.target.label, + icon = entityData.stash.target.icon or Stash.default.target.icon, + canInteract = function(_) + local entData = Bridge.ClientEntity.Get(entityData.id) + return not entData.stash.disable + end, + onSelect = function() + TriggerServerEvent("community_bridge:server:OpenStash", entityData.id) + end + } + + Bridge.ClientEntity.Set(entityData.id, 'targets', entityData.targets) + end +end + +if not IsDuplicityVersion() then return Bridge.ClientEntity.RegisterBehavior("stash", Stash) end + +RegisterNetEvent("community_bridge:server:OpenStash", function(id) + local src = source + if not src then return end + local entityData = Bridge.ServerEntity.Get(id) + if not entityData or not entityData.stash then + return print(string.format("[Stash] OpenStash: Entity %s does not exist or has no stash", id)) + end + local coords = vector3(entityData.coords.x, entityData.coords.y, entityData.coords.z) + if not coords then return end + local distance = #(GetEntityCoords(GetPlayerPed(src)) - coords) + if distance > 3.0 then + return print(string.format("[Stash] OpenStash: Player %s is too far from entity %s", src, id)) + end + Bridge.Inventory.OpenStash(src, "stash", id) +end) + +Bridge.ServerEntity.RegisterBehavior("stash", Stash) \ No newline at end of file diff --git a/lib/entities/shared/behaviors/targets.lua b/lib/entities/shared/behaviors/targets.lua new file mode 100644 index 0000000..cb15ac3 --- /dev/null +++ b/lib/entities/shared/behaviors/targets.lua @@ -0,0 +1,61 @@ +if IsDuplicityVersion() then return end +local Targets = { + property = "targets", + default = { + label = "Default Target Label", + distance = 2, + } +} + +function Targets.OnSpawn(entityData) + if not entityData.spawned or not entityData.targets then return end + if entityData.targets?.label then + local temp = entityData.targets + entityData.targets = {temp} + end + local numberedTargets = {} + for k, v in pairs(entityData.targets) do + local onSelect = v.onSelect + if onSelect then + v.onSelect = function(entity) + onSelect(entityData, entity) + end + end + table.insert(numberedTargets, v) + end + table.sort(numberedTargets, function(a, b) return (a.label < b.label) end) + Bridge.Target.AddLocalEntity(entityData.spawned, numberedTargets) + entityData.oldTargets = entityData.targets + Bridge.ClientEntity.Set(entityData.id, "oldTargets", entityData.oldTargets) +end + +function Targets.OnRemove(entityData) + if not entityData.spawned or not entityData.oldTargets then return end + Bridge.Target.RemoveLocalEntity(entityData.spawned) +end + +function Targets.OnUpdate(entityData) + if not entityData.spawned or not entityData.oldTargets then return end + local doesntMatch = false + for k, v in pairs(entityData.targets) do + if not entityData.oldTargets or not entityData.oldTargets[k] then + doesntMatch = true + break + end + local old = entityData.oldTargets[k] + if old.label ~= v.label + or old.distance ~= v.distance + or old.description ~= v.description + then + doesntMatch = true + break + end + end + if doesntMatch then + Targets.OnRemove(entityData) + Wait(100) + Targets.OnSpawn(entityData) + end +end +Bridge.ClientEntity.RegisterBehavior("targets", Targets) +return Targets \ No newline at end of file diff --git a/lib/entities/shared/behaviors/weapon.lua b/lib/entities/shared/behaviors/weapon.lua new file mode 100644 index 0000000..d055ccd --- /dev/null +++ b/lib/entities/shared/behaviors/weapon.lua @@ -0,0 +1,54 @@ +if IsDuplicityVersion() then return end +local Weapon = { + property = "weapon", + default = { + name = "WEAPON_UNARMED", + ammo = 0 + } +} + +function Weapon.GiveWeapon(entityData) + if entityData.entityType ~= "ped" then return end + local entity = entityData.spawned + + if not entity or not DoesEntityExist(entity) then return end + if entityData.weapon?.name then + local ammo = entityData.weapon.ammo or 0 + local hash = GetHashKey(entityData.weapon.name) + GiveWeaponToPed(entity, hash, ammo, false, true) + TaskSwapWeapon(entity, true) + end + entityData.oldWeapon = entityData.weapon + Bridge.ClientEntity.Set(entityData.id, "oldWeapon", entityData.oldWeapon) +end + +function Weapon.OnSpawn(entityData) + if not entityData.spawned or not entityData.weapon then return end + Weapon.GiveWeapon(entityData) + entityData.weapon.updating = false +end + +function Weapon.OnRemove(entityData) + if not entityData.spawned or not entityData.weapon then return end + entityData.oldWeapon = nil + entityData.weapon.updating = true +end + +function Weapon.OnUpdate(entityData) + if not entityData.spawned or not entityData.weapon then return end + if entityData.weapon.updating then return end + entityData.weapon.updating = true + if not entityData.oldWeapon or entityData.oldWeapon.name ~= entityData.weapon.name then + Weapon.GiveWeapon(entityData) + end + SetTimeout(1000, function() + if entityData.weapon then + entityData.weapon.updating = false + Bridge.ClientEntity.Set(entityData.id, "weapon", entityData.weapon) + end + end) + Bridge.ClientEntity.Set(entityData.id, "weapon", entityData.weapon) +end + +Bridge.ClientEntity.RegisterBehavior("weapon", Weapon) +return Weapon \ No newline at end of file From dd08f058c35457eb9b10aaacf7018df2749193a4 Mon Sep 17 00:00:00 2001 From: "Mr.Crowley" Date: Wed, 3 Sep 2025 01:04:04 -0500 Subject: [PATCH 2/7] Refactor client entity management functions Added freeze position handling in UpdateEntity and ensured OnDestroy behavior is triggered on entity destruction. Removed unused and commented-out Update function, and improved code clarity in Set and Destroy methods. --- lib/entities/client/client_entity.lua | 70 +++++---------------------- 1 file changed, 11 insertions(+), 59 deletions(-) diff --git a/lib/entities/client/client_entity.lua b/lib/entities/client/client_entity.lua index cc32930..a4faabc 100644 --- a/lib/entities/client/client_entity.lua +++ b/lib/entities/client/client_entity.lua @@ -107,7 +107,10 @@ local function UpdateEntity(_entityData) end entityData.oldRotation = entityData.rotation end - + + if entityData.freeze ~= nil and entityData.spawned and IsEntityPositionFrozen(entityData.spawned) then + FreezeEntityPosition(entityData.spawned, entityData.freeze) + end end --- Registers an entity received from the server and sets up proximity spawning. @@ -150,9 +153,9 @@ ClientEntity.RegisterBulk = ClientEntity.CreateBulk function ClientEntity.Destroy(id) local entityData = Entities[id] if not entityData then return end - + Behaviors.Trigger("OnDestroy", entityData) Point.Remove(id) - RemoveEntity(entityData) + RemoveEntity(entityData) Entities[id] = nil end ClientEntity.Unregister = ClientEntity.Destroy @@ -161,68 +164,17 @@ function ClientEntity.RegisterBehavior(id, behavior) Behaviors.Create(id, behavior) end +--- Sets a key-value pair for a client entity. +---@param id any +---@param key any +---@param value any +---@return nil function ClientEntity.Set(id, key, value) local entityData = ClientEntity.Get(id) if not entityData then return print(string.format("[ClientEntity] SetKey: Entity %s does not exist", id)) end entityData[key] = value - -- TriggerServerEvent("community_bridge:server:UpdateEntity", id, {[key] = value}) end ---- Updates the data for a registered entity. --- @param id string|number The ID of the entity to update. --- @param data table The data fields to update. --- function ClientEntity.Update(id, data) --- local entityData = ClientEntity.Get(id) --- if not entityData then return end - --- local needsPointUpdate = false --- for key, value in pairs(data) do --- if key == 'coords' and #(entityData.coords - value) > 0.1 then --- needsPointUpdate = true --- end --- if key == 'spawnDistance' and entityData.spawnDistance ~= value then --- needsPointUpdate = true --- end --- entityData[key] = value --- end - --- -- If entity is currently spawned, apply updates --- if entityData.spawned and DoesEntityExist(entityData.spawned) then --- if data.coords then --- SetEntityCoords(entityData.spawned, entityData.coords.x, entityData.coords.y, entityData.coords.z, false, false, false, true) --- end --- if data.rotation then --- if entityData.entityType == 'object' then --- SetEntityRotation(entityData.spawned, entityData.rotation.x, entityData.rotation.y, entityData.rotation.z, 2, true) --- else -- Ped/Vehicle heading --- SetEntityHeading(entityData.spawned, type(entityData.rotation) == 'number' and entityData.rotation or entityData.rotation.z) --- end --- end --- if data.freeze ~= nil then --- FreezeEntityPosition(entityData.spawned, data.freeze) --- end --- -- Add other updatable properties as needed --- end - --- -- Update Point registration if coords or distance changed --- if needsPointUpdate then --- Point.Remove(id) --- Point.Register( --- entityData.id, --- entityData.coords, --- entityData.spawnDistance or 50.0, --- entityData, --- SpawnEntity, --- RemoveEntity, --- OnUpdateEntity --- ) --- end - --- if entityData.OnUpdate and type(entityData.OnUpdate) == 'function' then --- entityData.OnUpdate(entityData, data) --- end --- end - function ClientEntity.Get(id) return Entities[id] end From cb9dbdf61057e09189e7339308d2c0728644c42d Mon Sep 17 00:00:00 2001 From: "Mr.Crowley" Date: Wed, 3 Sep 2025 01:23:01 -0500 Subject: [PATCH 3/7] Adjust entity movement threshold and follow behavior Increased the movement threshold for entity updates from 0.05 to 1.0 to reduce unnecessary position updates. Modified follow behavior to offset the entity's coordinates by -1 on the Z axis when updating, possibly to account for ground level or animation adjustments. --- lib/entities/client/client_entity.lua | 5 +---- lib/entities/shared/behaviors/follow.lua | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/entities/client/client_entity.lua b/lib/entities/client/client_entity.lua index a4faabc..443baec 100644 --- a/lib/entities/client/client_entity.lua +++ b/lib/entities/client/client_entity.lua @@ -8,9 +8,6 @@ ClientEntity = { } -- Renamed from BaseEntity ClientEntity.Behaviors = Behaviors - - - -------------------- local function SpawnEntity(entityData) @@ -81,7 +78,7 @@ local function UpdateEntity(_entityData) local coords = vector3(entityData.coords.x, entityData.coords.y, entityData.coords.z) entityData.oldCoords = vector3(entityData.oldCoords.x, entityData.oldCoords.y, entityData.oldCoords.z) local dist = #(coords.xyz - entityData.oldCoords.xyz) - if dist > 0.05 then + if dist > 1.0 then if entityData.spawned then print("Moving entity:", entityData.id) SetEntityCoords(entityData.spawned, coords.x, coords.y, coords.z, false, false, false, true) diff --git a/lib/entities/shared/behaviors/follow.lua b/lib/entities/shared/behaviors/follow.lua index b5361a9..9a4c2c4 100644 --- a/lib/entities/shared/behaviors/follow.lua +++ b/lib/entities/shared/behaviors/follow.lua @@ -42,7 +42,7 @@ function Follow.PedWalkToPos(entityData) TaskGoStraightToCoord(entity, targetCoords.x, targetCoords.y, targetCoords.z, speed, -1, heading, 0.0) entityData.coords = GetEntityCoords(entity) entityData.follow.walking = true - Bridge.ClientEntity.Set(entityData.id, "coords", entityData.coords) + Bridge.ClientEntity.Set(entityData.id, "coords", entityData.coords - vector3(0,0,1)) Bridge.ClientEntity.Set(entityData.id, "follow", entityData.follow) if entityData.follow.OnExit then entityData.follow.OnExit(entityData) end return false From dff4622a943852590a2a8107c0afc2e5a02cb5d0 Mon Sep 17 00:00:00 2001 From: "Mr.Crowley" Date: Wed, 3 Sep 2025 04:06:31 -0500 Subject: [PATCH 4/7] Refactor entity behavior to use Bridge.Entity Replaces all usage of Bridge.ClientEntity and Bridge.ServerEntity with Bridge.Entity across shared behavior modules. Updates initialization in lib/init.lua to use cLib.Entity and marks cLib.ClientEntity and cLib.ServerEntity as deprecated for consistency and future maintainability. --- lib/entities/shared/behaviors/anim.lua | 14 +++++++------- lib/entities/shared/behaviors/attach.lua | 10 ++++++---- lib/entities/shared/behaviors/clothing.lua | 10 +++++----- lib/entities/shared/behaviors/follow.lua | 18 +++++++++--------- lib/entities/shared/behaviors/particles.lua | 2 +- lib/entities/shared/behaviors/scenarios.lua | 4 ++-- lib/entities/shared/behaviors/shop.lua | 4 ++-- lib/entities/shared/behaviors/stash.lua | 10 +++++----- lib/entities/shared/behaviors/targets.lua | 4 ++-- lib/entities/shared/behaviors/weapon.lua | 8 ++++---- lib/init.lua | 11 +++++++++-- 11 files changed, 52 insertions(+), 43 deletions(-) diff --git a/lib/entities/shared/behaviors/anim.lua b/lib/entities/shared/behaviors/anim.lua index ef8574e..ee6e40a 100644 --- a/lib/entities/shared/behaviors/anim.lua +++ b/lib/entities/shared/behaviors/anim.lua @@ -18,13 +18,13 @@ function Animation.Play(entityData) local flags = entityData.anim.flags or Animation.default.flags local duration = entityData.anim.duration or Animation.default.duration entityData.anim.id = Bridge.Anim.Play(nil, entity, dict, name, 8.0, -8.0, duration, flags, 0.0, onComplete) - Bridge.ClientEntity.Set(entityData.id, "anim", entityData.anim) + Bridge.Entity.Set(entityData.id, "anim", entityData.anim) entityData.oldAnim = entityData.anim - Bridge.ClientEntity.Set(entityData.id, "oldAnim", entityData.oldAnim) + Bridge.Entity.Set(entityData.id, "oldAnim", entityData.oldAnim) -- SetTimeout(1000, function() -- if entityData.anim then -- entityData.anim.disable = false - -- Bridge.ClientEntity.Set(entityData.id, "anim", entityData.anim) + -- Bridge.Entity.Set(entityData.id, "anim", entityData.anim) -- end -- end) end @@ -36,15 +36,15 @@ function Animation.OnSpawn(entityData) entityData.anim.disable = false TaskSetBlockingOfNonTemporaryEvents(entityData.spawned, true) SetBlockingOfNonTemporaryEvents(entityData.spawned, true) - Bridge.ClientEntity.Set(entityData.id, "anim", entityData.anim) + Bridge.Entity.Set(entityData.id, "anim", entityData.anim) end function Animation.OnRemove(entityData) if not entityData.spawned or not entityData.anim then return end entityData.oldAnim = nil entityData.anim.disable = true - Bridge.ClientEntity.Set(entityData.id, "anim", entityData.anim) - Bridge.ClientEntity.Set(entityData.id, "oldAnim", entityData.oldAnim) + Bridge.Entity.Set(entityData.id, "anim", entityData.anim) + Bridge.Entity.Set(entityData.id, "oldAnim", entityData.oldAnim) end function Animation.OnUpdate(entityData) @@ -58,5 +58,5 @@ function Animation.OnUpdate(entityData) end end -Bridge.ClientEntity.RegisterBehavior("anim", Animation) +Bridge.Entity.RegisterBehavior("anim", Animation) return Animation \ No newline at end of file diff --git a/lib/entities/shared/behaviors/attach.lua b/lib/entities/shared/behaviors/attach.lua index 291de33..8b3f66f 100644 --- a/lib/entities/shared/behaviors/attach.lua +++ b/lib/entities/shared/behaviors/attach.lua @@ -14,7 +14,7 @@ function Attach.OnSpawn(entityData) local entity = entityData.spawned local targetEntity = entityData.attach.target if type(targetEntity) == "string" then - targetEntity = Bridge.ClientEntity.Get(targetEntity)?.spawned or targetEntity + targetEntity = Bridge.Entity.Get(targetEntity)?.spawned or targetEntity else local player = GetPlayerFromServerId(targetEntity) if player and player ~= -1 then @@ -27,13 +27,13 @@ function Attach.OnSpawn(entityData) local offset = entityData.attach.offset or Attach.default.offset local rotation = entityData.attach.rotation or Attach.default.rotation AttachEntityToEntity(entity, targetEntity, GetPedBoneIndex(targetEntity, bone), offset.x, offset.y, offset.z, rotation.x, rotation.y, rotation.z, true, true, false, true, 1, true) - Bridge.ClientEntity.Set(entityData.id, "attach", entityData.attach) + Bridge.Entity.Set(entityData.id, "attach", entityData.attach) end function Attach.Detach(entityData) if not entityData.spawned or not IsEntityAttached(entityData.spawned) then return end DetachEntity(entityData.spawned, true, true) - Bridge.ClientEntity.Set(entityData.id, "attach", entityData.attach) + Bridge.Entity.Set(entityData.id, "attach", entityData.attach) end function Attach.OnUpdate(entityData) @@ -42,9 +42,11 @@ function Attach.OnUpdate(entityData) return Attach.Detach(entityData) end local entity = entityData.spawned + local coords = GetEntityCoords(entity) + Bridge.Entity.Set(entityData.id, "coords", vector3(coords.x, coords.y, coords.z)) if IsEntityAttached(entity) then return end Attach.OnSpawn(entityData) end -Bridge.ClientEntity.RegisterBehavior("attach", Attach) +Bridge.Entity.RegisterBehavior("attach", Attach) return Attach \ No newline at end of file diff --git a/lib/entities/shared/behaviors/clothing.lua b/lib/entities/shared/behaviors/clothing.lua index 1b3f605..4ae11db 100644 --- a/lib/entities/shared/behaviors/clothing.lua +++ b/lib/entities/shared/behaviors/clothing.lua @@ -19,7 +19,7 @@ function Clothing.ChangeClothes(entityData) clothing = Bridge.Clothing.GetAppearance(GetPlayerPed(player)) end elseif type(clothing) == "string" then - local targetEntityData = Bridge.ClientEntity.Get(clothing) + local targetEntityData = Bridge.Entity.Get(clothing) if targetEntityData then clothing = Bridge.Clothing.GetAppearance(targetEntityData.spawned) end @@ -29,9 +29,9 @@ function Clothing.ChangeClothes(entityData) end Bridge.Clothing.SetAppearance(entity, clothing) - Bridge.ClientEntity.Set(entityData.id, "clothing", clothing) + Bridge.Entity.Set(entityData.id, "clothing", clothing) entityData.oldClothing = clothing - Bridge.ClientEntity.Set(entityData.id, "oldClothing", clothing) + Bridge.Entity.Set(entityData.id, "oldClothing", clothing) end @@ -73,10 +73,10 @@ function Clothing.OnUpdate(entityData) SetTimeout(1000, function() if entityData.clothing then entityData.clothing.updating = false - Bridge.ClientEntity.Set(entityData.id, "clothing", entityData.clothing) + Bridge.Entity.Set(entityData.id, "clothing", entityData.clothing) end end) end -Bridge.ClientEntity.RegisterBehavior("clothing", Clothing) +Bridge.Entity.RegisterBehavior("clothing", Clothing) return Clothing \ No newline at end of file diff --git a/lib/entities/shared/behaviors/follow.lua b/lib/entities/shared/behaviors/follow.lua index 9a4c2c4..addefb3 100644 --- a/lib/entities/shared/behaviors/follow.lua +++ b/lib/entities/shared/behaviors/follow.lua @@ -21,7 +21,7 @@ function Follow.PedWalkToPos(entityData) targetCoords = GetEntityCoords(targetCoords) end elseif type(targetCoords) == "string" then - local targetEntityData = Bridge.ClientEntity.Get(targetCoords) + local targetEntityData = Bridge.Entity.Get(targetCoords) if targetEntityData then targetCoords = GetEntityCoords(targetEntityData.spawned) end @@ -31,32 +31,32 @@ function Follow.PedWalkToPos(entityData) if distance > (entityData.follow.distance or Follow.default.distance) then if entityData.anim then entityData.anim.disable = true - Bridge.ClientEntity.Set(entityData.id, "anim", entityData.anim) + Bridge.Entity.Set(entityData.id, "anim", entityData.anim) end if entityData.scenarios then entityData.scenarios.disable = true - Bridge.ClientEntity.Set(entityData.id, "scenarios", entityData.scenarios) + Bridge.Entity.Set(entityData.id, "scenarios", entityData.scenarios) end local speed = entityData.follow.speed or Follow.default.speed local heading = GetHeadingFromVector_2d(targetCoords.x - entityPos.x, targetCoords.y - entityPos.y) TaskGoStraightToCoord(entity, targetCoords.x, targetCoords.y, targetCoords.z, speed, -1, heading, 0.0) entityData.coords = GetEntityCoords(entity) entityData.follow.walking = true - Bridge.ClientEntity.Set(entityData.id, "coords", entityData.coords - vector3(0,0,1)) - Bridge.ClientEntity.Set(entityData.id, "follow", entityData.follow) + Bridge.Entity.Set(entityData.id, "coords", entityData.coords - vector3(0,0,1)) + Bridge.Entity.Set(entityData.id, "follow", entityData.follow) if entityData.follow.OnExit then entityData.follow.OnExit(entityData) end return false end if not entityData.follow.walking then return end entityData.follow.walking = false - Bridge.ClientEntity.Set(entityData.id, "follow", entityData.follow) + Bridge.Entity.Set(entityData.id, "follow", entityData.follow) if entityData.anim then entityData.anim.disable = false - Bridge.ClientEntity.Set(entityData.id, "anim", entityData.anim) + Bridge.Entity.Set(entityData.id, "anim", entityData.anim) end if entityData.scenarios then entityData.scenarios.disable = false - Bridge.ClientEntity.Set(entityData.id, "scenarios", entityData.scenarios) + Bridge.Entity.Set(entityData.id, "scenarios", entityData.scenarios) end ClearPedTasks(entity) if entityData.follow.OnEnter then entityData.follow.OnEnter(entityData) end @@ -71,5 +71,5 @@ function Follow.OnUpdate(entityData) return Follow.PedWalkToPos(entityData) end -Bridge.ClientEntity.RegisterBehavior("follow", Follow) +Bridge.Entity.RegisterBehavior("follow", Follow) return Follow \ No newline at end of file diff --git a/lib/entities/shared/behaviors/particles.lua b/lib/entities/shared/behaviors/particles.lua index fecc6b7..1160360 100644 --- a/lib/entities/shared/behaviors/particles.lua +++ b/lib/entities/shared/behaviors/particles.lua @@ -76,5 +76,5 @@ function ParticlesBehaviors.OnMove(entityData) end end -Bridge.ClientEntity.RegisterBehavior("particles", ParticlesBehaviors) +Bridge.Entity.RegisterBehavior("particles", ParticlesBehaviors) return ParticlesBehaviors \ No newline at end of file diff --git a/lib/entities/shared/behaviors/scenarios.lua b/lib/entities/shared/behaviors/scenarios.lua index 20efb69..760af6c 100644 --- a/lib/entities/shared/behaviors/scenarios.lua +++ b/lib/entities/shared/behaviors/scenarios.lua @@ -23,7 +23,7 @@ function Scenarios.Play(entityData) ClearPedTasks(entity) if not entityData.scenarios.onComplete then return end entityData.scenarios.disabled = true - Bridge.ClientEntity.Set(entityData.id, "scenarios", entityData.scenarios) + Bridge.Entity.Set(entityData.id, "scenarios", entityData.scenarios) entityData.scenarios.onComplete(entityData) end) @@ -60,5 +60,5 @@ function Scenarios.OnUpdate(entityData) Scenarios.Play(entityData) end -Bridge.ClientEntity.RegisterBehavior("scenarios", Scenarios) +Bridge.Entity.RegisterBehavior("scenarios", Scenarios) return Scenarios \ No newline at end of file diff --git a/lib/entities/shared/behaviors/shop.lua b/lib/entities/shared/behaviors/shop.lua index 048e786..306752c 100644 --- a/lib/entities/shared/behaviors/shop.lua +++ b/lib/entities/shared/behaviors/shop.lua @@ -36,12 +36,12 @@ function Shop.OnCreate(entityData) ) end -if not IsDuplicityVersion() then return Bridge.ClientEntity.RegisterBehavior("shop", Shop) end +if not IsDuplicityVersion() then return Bridge.Entity.RegisterBehavior("shop", Shop) end RegisterNetEvent("community_bridge:server:OpenShop", function(id) local src = source if not src then return end - local entityData = Bridge.ServerEntity.Get(id) + local entityData = Bridge.Entity.Get(id) if not entityData or not entityData.shop then return print(string.format("[Shop] OpenShop: Entity %s does not exist or has no shop", id)) end diff --git a/lib/entities/shared/behaviors/stash.lua b/lib/entities/shared/behaviors/stash.lua index c006a11..994a7d0 100644 --- a/lib/entities/shared/behaviors/stash.lua +++ b/lib/entities/shared/behaviors/stash.lua @@ -26,7 +26,7 @@ function Stash.OnCreate(entityData) label = entityData.stash.target.label or Stash.default.target.label, icon = entityData.stash.target.icon or Stash.default.target.icon, canInteract = function(_) - local entData = Bridge.ClientEntity.Get(entityData.id) + local entData = Bridge.Entity.Get(entityData.id) return not entData.stash.disable end, onSelect = function() @@ -34,16 +34,16 @@ function Stash.OnCreate(entityData) end } - Bridge.ClientEntity.Set(entityData.id, 'targets', entityData.targets) + Bridge.Entity.Set(entityData.id, 'targets', entityData.targets) end end -if not IsDuplicityVersion() then return Bridge.ClientEntity.RegisterBehavior("stash", Stash) end +if not IsDuplicityVersion() then return Bridge.Entity.RegisterBehavior("stash", Stash) end RegisterNetEvent("community_bridge:server:OpenStash", function(id) local src = source if not src then return end - local entityData = Bridge.ServerEntity.Get(id) + local entityData = Bridge.Entity.Get(id) if not entityData or not entityData.stash then return print(string.format("[Stash] OpenStash: Entity %s does not exist or has no stash", id)) end @@ -56,4 +56,4 @@ RegisterNetEvent("community_bridge:server:OpenStash", function(id) Bridge.Inventory.OpenStash(src, "stash", id) end) -Bridge.ServerEntity.RegisterBehavior("stash", Stash) \ No newline at end of file +Bridge.Entity.RegisterBehavior("stash", Stash) \ No newline at end of file diff --git a/lib/entities/shared/behaviors/targets.lua b/lib/entities/shared/behaviors/targets.lua index cb15ac3..610b2ca 100644 --- a/lib/entities/shared/behaviors/targets.lua +++ b/lib/entities/shared/behaviors/targets.lua @@ -26,7 +26,7 @@ function Targets.OnSpawn(entityData) table.sort(numberedTargets, function(a, b) return (a.label < b.label) end) Bridge.Target.AddLocalEntity(entityData.spawned, numberedTargets) entityData.oldTargets = entityData.targets - Bridge.ClientEntity.Set(entityData.id, "oldTargets", entityData.oldTargets) + Bridge.Entity.Set(entityData.id, "oldTargets", entityData.oldTargets) end function Targets.OnRemove(entityData) @@ -57,5 +57,5 @@ function Targets.OnUpdate(entityData) Targets.OnSpawn(entityData) end end -Bridge.ClientEntity.RegisterBehavior("targets", Targets) +Bridge.Entity.RegisterBehavior("targets", Targets) return Targets \ No newline at end of file diff --git a/lib/entities/shared/behaviors/weapon.lua b/lib/entities/shared/behaviors/weapon.lua index d055ccd..24c6b7c 100644 --- a/lib/entities/shared/behaviors/weapon.lua +++ b/lib/entities/shared/behaviors/weapon.lua @@ -19,7 +19,7 @@ function Weapon.GiveWeapon(entityData) TaskSwapWeapon(entity, true) end entityData.oldWeapon = entityData.weapon - Bridge.ClientEntity.Set(entityData.id, "oldWeapon", entityData.oldWeapon) + Bridge.Entity.Set(entityData.id, "oldWeapon", entityData.oldWeapon) end function Weapon.OnSpawn(entityData) @@ -44,11 +44,11 @@ function Weapon.OnUpdate(entityData) SetTimeout(1000, function() if entityData.weapon then entityData.weapon.updating = false - Bridge.ClientEntity.Set(entityData.id, "weapon", entityData.weapon) + Bridge.Entity.Set(entityData.id, "weapon", entityData.weapon) end end) - Bridge.ClientEntity.Set(entityData.id, "weapon", entityData.weapon) + Bridge.Entity.Set(entityData.id, "weapon", entityData.weapon) end -Bridge.ClientEntity.RegisterBehavior("weapon", Weapon) +Bridge.Entity.RegisterBehavior("weapon", Weapon) return Weapon \ No newline at end of file diff --git a/lib/init.lua b/lib/init.lua index e8676e0..48cbce8 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -59,7 +59,11 @@ cLib.Cache = Cache or Require("lib/cache/shared/cache.lua") cLib.Marker = Marker or Require("lib/markers/server/server.lua") cLib.Particle = Particle or Require("lib/particles/server/particles.lua") cLib.Shell = Shells or Require("lib/shells/server/shells.lua") -cLib.ServerEntity = ServerEntity or Require("lib/entities/server/server_entity.lua") +cLib.Entity = ServerEntity or Require("lib/entities/server/server_entity.lua") + +-- Depricated +cLib.ServerEntity = cLib.Entity + if IsDuplicityVersion() then return cLib end ::client:: @@ -71,10 +75,13 @@ cLib.Raycast = Raycast or Require("lib/raycast/client/raycast.lua") cLib.Point = Point or Require("lib/points/client/points.lua") cLib.Particle = Particle or Require("lib/particles/client/particles.lua") cLib.Cache = Cache or Require("lib/cache/client/cache.lua") -cLib.ClientEntity = ClientEntity or Require("lib/entities/client/client_entity.lua") cLib.Marker = Marker or Require("lib/markers/client/markers.lua") cLib.Anim = Anim or Require("lib/anim/client/client.lua") cLib.Cutscene = Cutscene or Require("lib/cutscenes/client/cutscene.lua") cLib.Particle = Particle or Require("lib/particles/client/particles.lua") +cLib.Entity = ClientEntity or Require("lib/entities/client/client_entity.lua") + +-- Deprecated +cLib.ClientEntity = cLib.Entity return cLib From 744369525424a7c4fcc7936d8f46de938a31e611 Mon Sep 17 00:00:00 2001 From: "Mr.Crowley" Date: Wed, 3 Sep 2025 08:57:39 -0500 Subject: [PATCH 5/7] Improve entity spawning and target behaviors Refactored client entity spawning to handle missing models and improved coordinate update sensitivity. Removed model assertion from server entity creation. Enhanced resource start event handling for both server and client. Updated stash and target behaviors to support additional properties and better zone handling for entities without spawned objects. --- lib/entities/client/client_entity.lua | 56 ++++++++++++----------- lib/entities/server/server_entity.lua | 2 - lib/entities/shared/behaviors.lua | 16 +++++-- lib/entities/shared/behaviors/stash.lua | 2 + lib/entities/shared/behaviors/targets.lua | 19 ++++++-- 5 files changed, 59 insertions(+), 36 deletions(-) diff --git a/lib/entities/client/client_entity.lua b/lib/entities/client/client_entity.lua index 443baec..bde8323 100644 --- a/lib/entities/client/client_entity.lua +++ b/lib/entities/client/client_entity.lua @@ -12,33 +12,35 @@ ClientEntity.Behaviors = Behaviors local function SpawnEntity(entityData) if entityData.spawned and DoesEntityExist(entityData.spawned) then return end -- Already spawned - local loaded, model = Utility.LoadModel(entityData.model) - if not loaded then - print(string.format("[ClientEntity] Failed to load model %s for entity %s", entityData.model, entityData.id)) - return - end + if entityData.model then + local loaded, model = Utility.LoadModel(entityData.model) + if not loaded then + print(string.format("[ClientEntity] Failed to load model %s for entity %s", entityData.model, entityData.id)) + return + end - local entity = nil - local coords = entityData.coords - local rotation = entityData.rotation or vector3(0.0, 0.0, entityData.heading or 0.0) -- Default rotation if not provided + local entity = nil + local coords = entityData.coords + local rotation = entityData.rotation or vector3(0.0, 0.0, entityData.heading or 0.0) -- Default rotation if not provided - if entityData.entityType == 'object' then - entity = CreateObject(model, coords.x, coords.y, coords.z, false, false, false) - SetEntityRotation(entity, rotation.x, rotation.y, rotation.z, 2, true) - elseif entityData.entityType == 'ped' then - entity = CreatePed(4, model, coords.x, coords.y, coords.z, type(rotation) == 'number' and rotation or rotation.z, false, false) - elseif entityData.entityType == 'vehicle' then - entity = CreateVehicle(model, coords.x, coords.y, coords.z, type(rotation) == 'number' and rotation or rotation.z, false, false) - else - print(string.format("[ClientEntity] Unknown entity type '%s' for entity %s", entityData.entityType, entityData.id)) - end - if entity and model then - entityData.spawned = entity - SetModelAsNoLongerNeeded(model) - SetEntityAsMissionEntity(entity, true, true) - FreezeEntityPosition(entity, true) - else - SetModelAsNoLongerNeeded(model) + if entityData.entityType == 'object' then + entity = CreateObject(model, coords.x, coords.y, coords.z, false, false, false) + SetEntityRotation(entity, rotation.x, rotation.y, rotation.z, 2, true) + elseif entityData.entityType == 'ped' then + entity = CreatePed(4, model, coords.x, coords.y, coords.z, type(rotation) == 'number' and rotation or rotation.z, false, false) + elseif entityData.entityType == 'vehicle' then + entity = CreateVehicle(model, coords.x, coords.y, coords.z, type(rotation) == 'number' and rotation or rotation.z, false, false) + else + print(string.format("[ClientEntity] Unknown entity type '%s' for entity %s", entityData.entityType, entityData.id)) + end + if entity and model then + entityData.spawned = entity + SetModelAsNoLongerNeeded(model) + SetEntityAsMissionEntity(entity, true, true) + FreezeEntityPosition(entity, true) + else + SetModelAsNoLongerNeeded(model) + end end if entityData.OnSpawn then pcall(function (...) @@ -78,9 +80,8 @@ local function UpdateEntity(_entityData) local coords = vector3(entityData.coords.x, entityData.coords.y, entityData.coords.z) entityData.oldCoords = vector3(entityData.oldCoords.x, entityData.oldCoords.y, entityData.oldCoords.z) local dist = #(coords.xyz - entityData.oldCoords.xyz) - if dist > 1.0 then + if dist > 0.5 then if entityData.spawned then - print("Moving entity:", entityData.id) SetEntityCoords(entityData.spawned, coords.x, coords.y, coords.z, false, false, false, true) end -- ClientEntity.UpdateCoords(entityData.id, entityData.coords) @@ -243,6 +244,7 @@ end) RegisterNetEvent("community_bridge:client:UpdateEntity", function(id, data) for k, v in pairs(data) do + print(json.encode({k, v})) ClientEntity.Set(id, k, v) end end) diff --git a/lib/entities/server/server_entity.lua b/lib/entities/server/server_entity.lua index c9950c0..c135432 100644 --- a/lib/entities/server/server_entity.lua +++ b/lib/entities/server/server_entity.lua @@ -14,8 +14,6 @@ local Invoked = {} -- @param meta table Optional additional data -- @return table The created entity data function ServerEntity.New(id, entityType, model, coords, rotation, args) - assert(entityType, "EntityType is required") - assert(model, "Model is required for entity creation") assert(coords, "Coords are required for entity creation") local self = args or {} self.id = id or Ids.CreateUniqueId(Entities) diff --git a/lib/entities/shared/behaviors.lua b/lib/entities/shared/behaviors.lua index e30b3d2..d85b4b1 100644 --- a/lib/entities/shared/behaviors.lua +++ b/lib/entities/shared/behaviors.lua @@ -104,8 +104,18 @@ AddEventHandler("onResourceStop", function(resourceName) end end) -AddEventHandler("onResourceStart", function(resourceName) - Behaviors.Init() -end) +if IsDuplicityVersion() then + RegisterNetEvent("onResourceStart", function(resourceName) + if resourceName ~= GetCurrentResourceName() then return end + Wait(500) + Behaviors.Init() + end) +else + RegisterNetEvent("onClientResourceStart", function(resource) + if resource ~= GetCurrentResourceName() then return end + Wait(500) + Behaviors.Init() + end) +end return Behaviors diff --git a/lib/entities/shared/behaviors/stash.lua b/lib/entities/shared/behaviors/stash.lua index 994a7d0..0c83b66 100644 --- a/lib/entities/shared/behaviors/stash.lua +++ b/lib/entities/shared/behaviors/stash.lua @@ -25,6 +25,8 @@ function Stash.OnCreate(entityData) entityData.targets['stash'] = { label = entityData.stash.target.label or Stash.default.target.label, icon = entityData.stash.target.icon or Stash.default.target.icon, + description = entityData.stash.target.description, + groups = entityData.stash.target.groups, canInteract = function(_) local entData = Bridge.Entity.Get(entityData.id) return not entData.stash.disable diff --git a/lib/entities/shared/behaviors/targets.lua b/lib/entities/shared/behaviors/targets.lua index 610b2ca..99f30d7 100644 --- a/lib/entities/shared/behaviors/targets.lua +++ b/lib/entities/shared/behaviors/targets.lua @@ -4,15 +4,17 @@ local Targets = { default = { label = "Default Target Label", distance = 2, + size = vector3(2.0, 2.0, 2.0), } } function Targets.OnSpawn(entityData) - if not entityData.spawned or not entityData.targets then return end + if not entityData.targets then return end if entityData.targets?.label then local temp = entityData.targets entityData.targets = {temp} end + print("Spawned targets:", entityData.targets) local numberedTargets = {} for k, v in pairs(entityData.targets) do local onSelect = v.onSelect @@ -24,18 +26,26 @@ function Targets.OnSpawn(entityData) table.insert(numberedTargets, v) end table.sort(numberedTargets, function(a, b) return (a.label < b.label) end) - Bridge.Target.AddLocalEntity(entityData.spawned, numberedTargets) + if not entityData.spawned then + local size = Targets.default.size + Bridge.Target.AddBoxZone(entityData.id, entityData.coords, entityData.size, entityData.heading, numberedTargets, entityData.debug) + else + Bridge.Target.AddLocalEntity(entityData.spawned, numberedTargets) + end entityData.oldTargets = entityData.targets Bridge.Entity.Set(entityData.id, "oldTargets", entityData.oldTargets) end function Targets.OnRemove(entityData) - if not entityData.spawned or not entityData.oldTargets then return end + if not entityData.oldTargets then return end + if not entityData.spawned then + return Bridge.Target.RemoveZone(entityData.id) + end Bridge.Target.RemoveLocalEntity(entityData.spawned) end function Targets.OnUpdate(entityData) - if not entityData.spawned or not entityData.oldTargets then return end + if not entityData.oldTargets then return end local doesntMatch = false for k, v in pairs(entityData.targets) do if not entityData.oldTargets or not entityData.oldTargets[k] then @@ -52,6 +62,7 @@ function Targets.OnUpdate(entityData) end end if doesntMatch then + print("ASDFASDFASDF") Targets.OnRemove(entityData) Wait(100) Targets.OnSpawn(entityData) From debfb062317cb6c81abcb1a003c4d8402d9de4c8 Mon Sep 17 00:00:00 2001 From: "Mr.Crowley" Date: Thu, 4 Sep 2025 08:21:48 -0500 Subject: [PATCH 6/7] Refactor stash and targets behavior logic Simplifies stash target assignment by returning early when registering a stash, ensuring consistent target setup. Enhances target update logic to check for removed targets and removes debug print statement. --- lib/entities/shared/behaviors/stash.lua | 36 +++++++++++------------ lib/entities/shared/behaviors/targets.lua | 8 ++++- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/lib/entities/shared/behaviors/stash.lua b/lib/entities/shared/behaviors/stash.lua index 0c83b66..badbebc 100644 --- a/lib/entities/shared/behaviors/stash.lua +++ b/lib/entities/shared/behaviors/stash.lua @@ -20,24 +20,24 @@ function Stash.OnCreate(entityData) local owner = entityData.stash.owner local groups = entityData.stash.groups Bridge.Inventory.RegisterStash(entityData.id, label, slots, weight, owner, groups) - else - entityData.targets = entityData.targets or {} - entityData.targets['stash'] = { - label = entityData.stash.target.label or Stash.default.target.label, - icon = entityData.stash.target.icon or Stash.default.target.icon, - description = entityData.stash.target.description, - groups = entityData.stash.target.groups, - canInteract = function(_) - local entData = Bridge.Entity.Get(entityData.id) - return not entData.stash.disable - end, - onSelect = function() - TriggerServerEvent("community_bridge:server:OpenStash", entityData.id) - end - } - - Bridge.Entity.Set(entityData.id, 'targets', entityData.targets) - end + return + end + entityData.targets = entityData.targets or {} + entityData.targets['stash'] = { + label = entityData.stash.target.label or Stash.default.target.label, + icon = entityData.stash.target.icon or Stash.default.target.icon, + description = entityData.stash.target.description, + groups = entityData.stash.target.groups, + canInteract = function(_) + local entData = Bridge.Entity.Get(entityData.id) + return not entData.stash.disable + end, + onSelect = function() + TriggerServerEvent("community_bridge:server:OpenStash", entityData.id) + end + } + + Bridge.Entity.Set(entityData.id, 'targets', entityData.targets) end if not IsDuplicityVersion() then return Bridge.Entity.RegisterBehavior("stash", Stash) end diff --git a/lib/entities/shared/behaviors/targets.lua b/lib/entities/shared/behaviors/targets.lua index 99f30d7..c11afc4 100644 --- a/lib/entities/shared/behaviors/targets.lua +++ b/lib/entities/shared/behaviors/targets.lua @@ -61,8 +61,14 @@ function Targets.OnUpdate(entityData) break end end + + for k, v in pairs(entityData.oldTargets) do + if not entityData.targets or not entityData.targets[k] then + doesntMatch = true + break + end + end if doesntMatch then - print("ASDFASDFASDF") Targets.OnRemove(entityData) Wait(100) Targets.OnSpawn(entityData) From 18748bd7754c19fd35792d2306012ace3c8c9d7d Mon Sep 17 00:00:00 2001 From: "Mr.Crowley" Date: Thu, 4 Sep 2025 11:42:59 -0500 Subject: [PATCH 7/7] Refactor entity movement and add Math.Nearest function Simplifies entity movement logic in client_entity.lua by improving coordinate comparison and updating behavior triggers. Adds a new Math.Nearest function in math.lua to round values to the nearest whole number. --- lib/entities/client/client_entity.lua | 33 ++++++++++++--------------- lib/utility/shared/math.lua | 9 +++++++- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/lib/entities/client/client_entity.lua b/lib/entities/client/client_entity.lua index bde8323..6a627df 100644 --- a/lib/entities/client/client_entity.lua +++ b/lib/entities/client/client_entity.lua @@ -76,24 +76,21 @@ local function UpdateEntity(_entityData) end) end Behaviors.Trigger("OnUpdate", entityData) - if entityData.oldCoords and entityData.oldCoords ~= entityData.coords then - local coords = vector3(entityData.coords.x, entityData.coords.y, entityData.coords.z) - entityData.oldCoords = vector3(entityData.oldCoords.x, entityData.oldCoords.y, entityData.oldCoords.z) - local dist = #(coords.xyz - entityData.oldCoords.xyz) - if dist > 0.5 then - if entityData.spawned then - SetEntityCoords(entityData.spawned, coords.x, coords.y, coords.z, false, false, false, true) - end - -- ClientEntity.UpdateCoords(entityData.id, entityData.coords) - if entityData.OnMove then - pcall(function (...) - return entityData.OnMove(entityData) - end) - end - Behaviors.Trigger("OnMove", entityData, entityData.oldCoords, entityData.coords) - end - entityData.oldCoords = entityData.coords - end + local coords = vector3(entityData.coords.x, entityData.coords.y, entityData.coords.z) + entityData.oldCoords = entityData.oldCoords and vector3(entityData.oldCoords.x, entityData.oldCoords.y, entityData.oldCoords.z) or coords + local dist = #(coords - entityData.oldCoords) + if entityData.oldCoords and dist > 0.5 then + if entityData.spawned then + SetEntityCoords(entityData.spawned, coords.x, coords.y, coords.z, false, false, false, true) + end + -- ClientEntity.UpdateCoords(entityData.id, entityData.coords) + if entityData.OnMove then + pcall(function (...) + return entityData.OnMove(entityData) + end) + end + Behaviors.Trigger("OnMove", entityData, entityData.oldCoords, entityData.coords) + end if not entityData.oldRotation or entityData.oldRotation ~= entityData.rotation then if entityData.spawned and DoesEntityExist(entityData.spawned) then diff --git a/lib/utility/shared/math.lua b/lib/utility/shared/math.lua index 499d8c4..3691d66 100644 --- a/lib/utility/shared/math.lua +++ b/lib/utility/shared/math.lua @@ -3,7 +3,7 @@ Math = Math or {} ---@class Math -------@field public name string @add name field to class Car, you'll see it in code completion +---@field public name string @add name field to class Car, you'll see it in code completion Math = {} @@ -44,6 +44,13 @@ function Math.Smooth(value, min, max) return value * value * (3 - 2 * value) end +--- Gets the nearest whole number. +--- @param value number The value to round. +--- @return number The rounded value. +function Math.Nearest(value) + return math.floor(value + 0.5) +end + --- Maps a value from one range to another. ---@param value number The value to map. ---@param inMin number The minimum value for the provided value.