From 8346130b004a1e275b088c6452f5f53ac5acddd8 Mon Sep 17 00:00:00 2001 From: "Mr.Crowley" Date: Wed, 3 Sep 2025 00:28:17 -0500 Subject: [PATCH] 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