From c23df31199032b75586d07a7ed0895c821adff9b Mon Sep 17 00:00:00 2001 From: gononono64 <96976330+gononono64@users.noreply.github.com> Date: Mon, 28 Apr 2025 22:51:54 -0500 Subject: [PATCH] Server and client entity update syncing non networked entities to all players in range allow for firing functions when entering or exiting the range of the entity --- lib/entities/client/client_entity.lua | 26 +- lib/entities/client/client_entity_actions.lua | 200 +++++++------ .../client/client_entity_actions_ext.lua | 271 ++++++++++++++++++ lib/entities/server/server_entity.lua | 6 +- lib/points/client/points.lua | 10 + 5 files changed, 417 insertions(+), 96 deletions(-) create mode 100644 lib/entities/client/client_entity_actions_ext.lua diff --git a/lib/entities/client/client_entity.lua b/lib/entities/client/client_entity.lua index ec2f54a..751f6e0 100644 --- a/lib/entities/client/client_entity.lua +++ b/lib/entities/client/client_entity.lua @@ -1,7 +1,7 @@ Utility = Utility or Require("lib/utility/client/utility.lua") Ids = Ids or Require("lib/utility/shared/ids.lua") Point = Point or Require("lib/points/client/points.lua") -ClientEntityActions = ClientEntityActions or Require("lib/entities/client/client_entity_actions.lua") -- Added +ClientEntityActions = ClientEntityActions or Require("lib/entities/client/client_entity_actions_ext.lua") -- Added local Entities = {} -- Stores entity data received from server ClientEntity = {} -- Renamed from BaseEntity @@ -52,7 +52,7 @@ local function RemoveEntity(entityData) entityData = entityData and entityData.args if not entityData then return end -- Added safety check - ClientEntityActions.StopAction(entityData.id) -- Stop any ongoing action before despawning + ClientEntityActions.StopAction(entityData.id) -- Stop any ongoing action AND clear queue before despawning if entityData.spawned and DoesEntityExist(entityData.spawned) then local entityHandle = entityData.spawned @@ -75,6 +75,7 @@ function ClientEntity.Register(entityData) -- print(string.format("[ClientEntity] Registering %s entity %s", entityData.entityType, entityData.id)) -- Use Point system for proximity checks + print(string.format("[ClientEntity] Registering entity %s at %s", entityData.id, json.encode(entityData))) Point.Register(entityData.id, entityData.coords, entityData.spawnDistance or 50.0, entityData, SpawnEntity, RemoveEntity, function() end) end @@ -171,18 +172,21 @@ end) -- New handler for entity actions RegisterNetEvent("community_bridge:client:TriggerEntityAction", function(entityId, actionName, ...) local entityData = Entities[entityId] - -- Check if entity exists locally AND is currently spawned - if entityData and entityData.spawned and DoesEntityExist(entityData.spawned) then - local actionFunc = ClientEntityActions[actionName] - if actionFunc and type(actionFunc) == 'function' then - -- print(string.format("[ClientEntity] Executing action '%s' for entity %s", actionName, entityId)) - actionFunc(entityData, ...) -- Pass entityData and any additional arguments + print("here", json.encode(entityData)) + -- Check if entity exists locally (it doesn't need to be spawned to queue actions) + if entityData then + if actionName == "Stop" then + ClientEntityActions.StopAction(entityId) + elseif actionName == "Skip" then + ClientEntityActions.SkipAction(entityId) else - print(string.format("[ClientEntity] Received unknown action '%s' for entity %s", actionName, entityId)) + print(string.format("[ClientEntity] Triggering action '%s' for entity %s", actionName, entityId)) + local currentAction = ClientEntityActions.ActionQueue[entityId] and ClientEntityActions.ActionQueue[entityId][1] + ClientEntityActions.QueueAction(entityData, actionName, ...) end -- else - -- Optional: Log if action received but entity not spawned locally - -- print(string.format("[ClientEntity] Received action '%s' for entity %s, but entity not spawned locally.", actionName, entityId)) + -- Optional: Log if action received but entity doesn't exist locally at all + -- print(string.format("[ClientEntity] Received action '%s' for non-existent entity %s.", actionName, entityId)) end end) diff --git a/lib/entities/client/client_entity_actions.lua b/lib/entities/client/client_entity_actions.lua index e25d2dd..9cf9f1e 100644 --- a/lib/entities/client/client_entity_actions.lua +++ b/lib/entities/client/client_entity_actions.lua @@ -1,101 +1,135 @@ -local LA = LA or Require("lib/utility/shared/la.lua") -local ActionThreads = {} -- Store running action threads { [entityId] = thread } + ClientEntityActions = {} +ClientEntityActions.ActionThreads = {} -- Store running action threads { [entityId] = thread } +ClientEntityActions.ActionQueue = {} -- Stores pending actions { [entityId] = {{name="ActionName", args={...}}, ...} } +ClientEntityActions.IsActionRunning = {} -- Tracks if an action is currently running { [entityId] = boolean } +ClientEntityActions.RegisteredActions = {} -- New: Registry for action implementations { [actionName] = function(entityData, ...) } +-- Forward declaration ---- Makes a ped entity walk to specified coordinates. --- @param entityData table The entity data containing the spawned ped handle. --- @param coords vector3 The target coordinates. --- @param speed number Movement speed. --- @param timeout number Timeout in ms. -function ClientEntityActions.WalkTo(entityData, coords, speed, timeout) - local entity = entityData.spawned - if not entity or not DoesEntityExist(entity) or not IsEntityAPed(entity) then return end - -- Clear any existing task for this entity - if ActionThreads[entityData.id] then - -- Potentially stop the old thread if needed, depending on desired behavior - ClearPedTasks(entity) +--- Processes the next action in the queue for a given entity. +-- @param entityId string|number The ID of the entity. +function ClientEntityActions.ProcessNextAction(entityId) + if ClientEntityActions.IsActionRunning [entityId] then return end -- Already running something + print(string.format("[ClientEntityActions] Processing next action for entity %s", entityId)) + local queue = ClientEntityActions.ActionQueue[entityId] + if not queue or #queue == 0 then return end -- Queue is empty + + local nextAction = table.remove(queue, 1) -- Dequeue (FIFO) + local entityData = ClientEntity.Get(entityId) -- Assumes ClientEntity is accessible + -- Check if entity is still valid and spawned before starting next action + if not entityData or not entityData.spawned or not DoesEntityExist(entityData.spawned) then + -- Entity despawned while idle, clear queue and do nothing + ClientEntityActions.ActionQueue[entityId] = nil + return end - local thread = CreateThread(function() - TaskGoToCoordAnyMeans(entity, coords.x, coords.y, coords.z, speed or 1.0, 0, false, 786603, timeout or -1) - -- Wait until task is completed or entity is despawned - while GetScriptTaskStatus(entity, 288358301) ~= 7 and entityData.spawned == entity and DoesEntityExist(entity) do - Wait(500) - end - ActionThreads[entityData.id] = nil -- Clear thread reference when done/stopped - end) - ActionThreads[entityData.id] = thread + -- Look up the action in the registry + local actionFunc = ClientEntityActions.RegisteredActions [nextAction.name] + print(string.format("[ClientEntityActions] Action '%s' dequeued for entity %s", nextAction.name, entityId)) + if actionFunc then + -- print(string.format("[ClientEntityActions] Starting action '%s' for entity %s", nextAction.name, entityId)) + ClientEntityActions.IsActionRunning [entityId] = true + -- Call the registered function + actionFunc(entityData, table.unpack(nextAction.args)) + else + print(string.format("[ClientEntityActions] Unknown action '%s' dequeued for entity %s", nextAction.name, entityId)) + -- Skip unknown action and try the next one immediately + ClientEntityActions.ProcessNextAction(entityId) + end +end +--- Registers a custom action implementation. +-- The action function should handle its own logic, including threading if needed, +-- and MUST call ClientEntityActions.ProcessNextAction(entityData.id) when it completes or fails, +-- after setting ClientEntityActions.IsActionRunning [entityData.id] = false. +-- @param actionName string The name used to trigger this action. +-- @param actionFunc function The function to execute. Signature: function(entityData, ...) +function ClientEntityActions.RegisterAction(actionName, actionFunc) + if ClientEntityActions.RegisteredActions [actionName] then + print(string.format("[ClientEntityActions] WARNING: Overwriting registered action '%s'", actionName)) + end + assert(type(actionName) == "string", "actionName must be a string") + assert(type(actionFunc) == "function", "actionFunc must be a function") + print(string.format("[ClientEntityActions] Registered action: %s", actionName)) + ClientEntityActions.RegisteredActions [actionName] = actionFunc + -- print(string.format("[ClientEntityActions] Registered action: %s", actionName)) end ---- Lerps an entity's position over time. --- @param entityData table The entity data containing the spawned entity handle. --- @param targetCoords vector3 The target coordinates. --- @param duration number Duration of the lerp in milliseconds. --- @param easingType string (Optional) Easing function name from LA library (e.g., "linear", "sine"). --- @param easingDirection string (Optional) "in", "out", or "inout". -function ClientEntityActions.LerpTo(entityData, targetCoords, duration, easingType, easingDirection) - local entity = entityData.spawned - if not entity or not DoesEntityExist(entity) then return end - - local startCoords = GetEntityCoords(entity) - local startTime = GetGameTimer() - easingType = easingType or "linear" - easingDirection = easingDirection or "inout" - - -- Clear any existing lerp for this entity - if ActionThreads[entityData.id] then - -- Stop the old thread if needed +--- Queues an action for an entity. Starts processing if idle. +-- @param entityData table The entity data. +-- @param actionName string The name of the action. +-- @param ... any Arguments for the action. +function ClientEntityActions.QueueAction(entityData, actionName, ...) + local entityId = entityData.id + if not ClientEntityActions.ActionQueue[entityId] then + ClientEntityActions.ActionQueue[entityId] = {} end - local thread = CreateThread(function() - while GetGameTimer() < startTime + duration do - -- Check if entity still exists and is the same one we started with - if not entityData.spawned or entityData.spawned ~= entity or not DoesEntityExist(entity) then - break -- Stop if entity despawned or changed - end + local actionArgs = {...} + table.insert(ClientEntityActions.ActionQueue[entityId], { name = actionName, args = actionArgs }) + -- print(string.format("[ClientEntityActions] Queued action '%s' for entity %s. Queue size: %d", actionName, entityId, #ClientEntityActions.ActionQueue[entityId])) - local elapsed = GetGameTimer() - startTime - local t = LA.Clamp(elapsed / duration, 0.0, 1.0) - local easedT = LA.EaseInOut(t, easingType) -- Default to EaseInOut if direction not specified or invalid - - if easingDirection == "in" then - easedT = LA.EaseIn(t, easingType) - elseif easingDirection == "out" then - easedT = LA.EaseOut(t, easingType) - end - - local currentPos = LA.LerpVector(startCoords, targetCoords, easedT) - SetEntityCoordsNoOffset(entity, currentPos.x, currentPos.y, currentPos.z, false, false, false) - Wait(0) - end - - -- Ensure final position if completed fully - if entityData.spawned == entity and DoesEntityExist(entity) then - SetEntityCoordsNoOffset(entity, targetCoords.x, targetCoords.y, targetCoords.z, false, false, false) - end - ActionThreads[entityData.id] = nil -- Clear thread reference - end) - ActionThreads[entityData.id] = thread + -- If the entity isn't currently doing anything, start processing immediately + if not ClientEntityActions.IsActionRunning [entityId] then + ClientEntityActions.ProcessNextAction(entityId) + end end - ---- Stops any ongoing action for a specific entity. +--- Stops the current action and clears the queue for a specific entity. -- @param entityId string|number The ID of the entity. function ClientEntityActions.StopAction(entityId) - if ActionThreads[entityId] then - -- Stopping Lua threads directly isn't safe/possible. - -- Instead, rely on checks within the thread loops (e.g., entityData.spawned check) - -- For peds, we can clear tasks. - local entityData = ClientEntity.Get(entityId) -- Assumes ClientEntity is accessible or passed in - if entityData and entityData.spawned and DoesEntityExist(entityData.spawned) then - if IsEntityAPed(entityData.spawned) then - ClearPedTasks(entityData.spawned) - end - -- Other entity types might need different stop logic if applicable - end - ActionThreads[entityId] = nil -- Clear reference to let the thread terminate naturally + -- print(string.format("[ClientEntityActions] Stopping all actions for entity %s", entityId)) + ClientEntityActions.ActionQueue[entityId] = nil -- Clear the queue + ClientEntityActions.IsActionRunning [entityId] = false -- Mark as not running (this will stop loops in threads) + + -- Stop current task/thread if applicable + if ClientEntityActions.ActionThreads[entityId] then + -- Lua threads stop themselves based on ClientEntityActions.IsActionRunning flag + ClientEntityActions.ActionThreads[entityId] = nil end + + -- Specific task clearing for peds + local entityData = ClientEntity.Get(entityId) + if entityData and entityData.spawned and DoesEntityExist(entityData.spawned) then + if IsEntityAPed(entityData.spawned) then + ClearPedTasksImmediately(entityData.spawned) -- Use Immediately for forceful stop + end + -- Other entity types might need different stop logic + end +end +--- Skips the current action and starts the next one in the queue, if any. +-- @param entityId string|number The ID of the entity. +function ClientEntityActions.SkipAction(entityId) + if not ClientEntityActions.IsActionRunning [entityId] then + -- print(string.format("[ClientEntityActions] SkipAction called for %s, but no action running.", entityId)) + return -- Nothing to skip + end + -- print(string.format("[ClientEntityActions] Skipping current action for entity %s", entityId)) + + ClientEntityActions.IsActionRunning [entityId] = false -- Mark as not running (this will stop loops in threads) + + -- Stop current task/thread if applicable + if ClientEntityActions.ActionThreads[entityId] then + ClientEntityActions.ActionThreads[entityId] = nil + end + + local entityData = ClientEntity.Get(entityId) + if entityData and entityData.spawned and DoesEntityExist(entityData.spawned) then + if IsEntityAPed(entityData.spawned) then + ClearPedTasksImmediately(entityData.spawned) + end + end + + -- Immediately try to process the next action + ClientEntityActions.ProcessNextAction(entityId) +end +-- Add server-callable functions for Stop and Skip +function ClientEntityActions.Stop(entityData) + ClientEntityActions.StopAction(entityData.id) +end + +function ClientEntityActions.Skip(entityData) + ClientEntityActions.SkipAction(entityData.id) end return ClientEntityActions diff --git a/lib/entities/client/client_entity_actions_ext.lua b/lib/entities/client/client_entity_actions_ext.lua new file mode 100644 index 0000000..99622f2 --- /dev/null +++ b/lib/entities/client/client_entity_actions_ext.lua @@ -0,0 +1,271 @@ +DefaultActions = {} +ClientEntityActions = ClientEntityActions or Require("lib/entities/client/client_entity_actions.lua") -- Added +--- Internal implementation for walking. Registered via RegisterAction. +function DefaultActions.WalkTo(entityData, coords, speed, timeout) + print("[ClientEntityActions] WalkTo action called", coords, speed, timeout) + local entity = entityData.spawned + local entityId = entityData.id -- Store ID locally for safety in thread + + if not entity or not DoesEntityExist(entity) or not IsEntityAPed(entity) then + ClientEntityActions.IsActionRunning[entityId] = false + ClientEntityActions.ProcessNextAction(entityId) -- Try next action if this one failed immediately + return + end + + -- Clear previous tasks just in case + ClearPedTasks(entity) + + local thread = CreateThread(function() + TaskGoToCoordAnyMeans(entity, coords.x, coords.y, coords.z, speed or 1.0, 0, false, 786603, timeout or -1) + -- Wait until task is completed/interrupted or entity is despawned/changed + local entityCoords = GetEntityCoords(entity) + while ClientEntityActions.IsActionRunning[entityId] and entityData.spawned == entity and DoesEntityExist(entity) and #(entityCoords - coords) > 2.0 do + entityCoords = GetEntityCoords(entity) + Wait(0) -- Yield to avoid freezing the game + end + ClientEntityActions.ActionThreads[entityId] = nil -- Clear thread reference + -- Only process next action if this thread was the one running the action + if ClientEntityActions.IsActionRunning[entityId] then + ClientEntityActions.IsActionRunning[entityId] = false + ClientEntityActions.ProcessNextAction(entityId) + end + end) + ClientEntityActions.ActionThreads[entityId] = thread +end +--- Internal implementation for playing an animation. Registered via RegisterAction. +-- @param entityData table +-- @param animDict string +-- @param animName string +-- @param blendIn number (Optional, default 8.0) +-- @param blendOut number (Optional, default -8.0) +-- @param duration number (Optional, default -1 for loop/until stopped) +-- @param flag number (Optional, default 0) +-- @param playbackRate number (Optional, default 0.0) +function DefaultActions.PlayAnim(entityData, animDict, animName, blendIn, blendOut, duration, flag, playbackRate) + local entity = entityData.spawned + local entityId = entityData.id + + if not entity or not DoesEntityExist(entity) or not IsEntityAPed(entity) then + ClientEntityActions.IsActionRunning[entityId] = false + ClientEntityActions.ProcessNextAction(entityId) + return + end + + blendIn = blendIn or 8.0 + blendOut = blendOut or -8.0 + duration = duration or -1 + flag = flag or 0 + playbackRate = playbackRate or 0.0 + + local thread = CreateThread(function() + if not HasAnimDictLoaded(animDict) then + RequestAnimDict(animDict) + local timeout = 100 + while not HasAnimDictLoaded(animDict) and timeout > 0 do + Wait(10) + timeout = timeout - 1 + end + end + + if HasAnimDictLoaded(animDict) then + TaskPlayAnim(entity, animDict, animName, blendIn, blendOut, duration, flag, playbackRate, false, false, false) + + -- Wait for completion or interruption + local startTime = GetGameTimer() + local animTime = duration > 0 and (startTime + duration) or -1 + + while ClientEntityActions.IsActionRunning[entityId] and entityData.spawned == entity and DoesEntityExist(entity) do + local isPlaying = IsEntityPlayingAnim(entity, animDict, animName, 3) + + -- Break conditions: + -- 1. Action was stopped/skipped externally + -- 2. Entity changed/despawned + -- 3. Animation finished naturally (if not looping based on flags/duration) + -- 4. Duration expired (if duration > 0) + if not ClientEntityActions.IsActionRunning[entityId] or entityData.spawned ~= entity or not DoesEntityExist(entity) then break end + if duration == -1 and not isPlaying and GetEntityAnimCurrentTime(entity, animDict, animName) > 0.1 then break end -- Check if non-looping anim finished + if animTime ~= -1 and GetGameTimer() >= animTime then break end + + Wait(100) -- Check periodically + end + -- Don't remove dict here if other actions might use it immediately after + -- Consider a separate cleanup mechanism if needed + else + print(string.format("[ClientEntityActions] Failed to load anim dict '%s' for entity %s", animDict, entityId)) + end + + -- Crucial: Mark as finished and process next + if ClientEntityActions.IsActionRunning[entityId] then + ClientEntityActions.IsActionRunning[entityId] = false + ClientEntityActions.ProcessNextAction(entityId) + end + end) + ClientEntityActions.ActionThreads[entityId] = thread +end +--- Internal implementation for lerping. Registered via RegisterAction. +function DefaultActions.LerpTo(entityData, targetCoords, duration, easingType, easingDirection) + local entity = entityData.spawned + local entityId = entityData.id -- Store ID locally + + if not entity or not DoesEntityExist(entity) then + ClientEntityActions.IsActionRunning[entityId] = false + ClientEntityActions.ProcessNextAction(entityId) -- Try next action if this one failed immediately + return + end + + local startCoords = GetEntityCoords(entity) + local startTime = GetGameTimer() + easingType = easingType or "linear" + easingDirection = easingDirection or "inout" + + local thread = CreateThread(function() + while GetGameTimer() < startTime + duration do + -- Check if action should continue + if not ClientEntityActions.IsActionRunning[entityId] or not entityData.spawned or entityData.spawned ~= entity or not DoesEntityExist(entity) then + break -- Stop if entity despawned, changed, or action was stopped/skipped + end + + local elapsed = GetGameTimer() - startTime + local t = LA.Clamp(elapsed / duration, 0.0, 1.0) + local easedT = LA.EaseInOut(t, easingType) -- Default + + if easingDirection == "in" then + easedT = LA.EaseIn(t, easingType) + elseif easingDirection == "out" then + easedT = LA.EaseOut(t, easingType) + end + + local currentPos = LA.LerpVector(startCoords, targetCoords, easedT) + SetEntityCoordsNoOffset(entity, currentPos.x, currentPos.y, currentPos.z, false, false, false) + Wait(0) + end + + -- Ensure final position if completed fully and action wasn't stopped/skipped + if ClientEntityActions.IsActionRunning[entityId] and entityData.spawned == entity and DoesEntityExist(entity) then + SetEntityCoordsNoOffset(entity, targetCoords.x, targetCoords.y, targetCoords.z, false, false, false) + end + + ClientEntityActions.ActionThreads[entityId] = nil -- Clear thread reference + -- Only process next action if this thread was the one running the action + if ClientEntityActions.IsActionRunning[entityId] then + ClientEntityActions.IsActionRunning[entityId] = false + ClientEntityActions.ProcessNextAction(entityId) + end + end) + ClientEntityActions.ActionThreads[entityId] = thread +end +--- Internal implementation for attaching a prop. Registered via RegisterAction. +-- This action completes immediately after attaching. Use DetachProp to remove. +-- @param entityData table +-- @param propModel string|number +-- @param boneIndex number (Optional, default -1 for root) +-- @param offsetPos vector3 (Optional, default vector3(0,0,0)) +-- @param offsetRot vector3 (Optional, default vector3(0,0,0)) +-- @param useSoftPinning boolean (Optional, default false) +-- @param collision boolean (Optional, default false) +-- @param isPed boolean (Optional, default false) - Seems unused in native? +-- @param vertexIndex number (Optional, default 2) - Seems unused in native? +-- @param fixedRot boolean (Optional, default true) +function DefaultActions.AttachProp(entityData, propModel, boneName, offsetPos, offsetRot, useSoftPinning, collision, isPed, vertexIndex, fixedRot) + local entity = entityData.spawned + local entityId = entityData.id + + if not entity or not DoesEntityExist(entity) then + ClientEntityActions.IsActionRunning[entityId] = false + ClientEntityActions.ProcessNextAction(entityId) + return + end + + local modelHash = Utility.GetEntityHashFromModel(propModel) + print(modelHash, propModel) + if not Utility.LoadModel(modelHash) then + print(string.format("[ClientEntityActions] Failed to load prop model '%s' for entity %s", propModel, entityId)) + ClientEntityActions.IsActionRunning[entityId] = false + ClientEntityActions.ProcessNextAction(entityId) + return + end + + local boneIndex = GetEntityBoneIndexByName(entity, boneName) + local coords = GetEntityCoords(entity) + local prop = CreateObject(modelHash, coords.x, coords.y, coords.z, false, false, false) + SetModelAsNoLongerNeeded(modelHash) + + boneIndex = boneIndex or GetPedBoneIndex(entity, 60309) -- SKEL_R_Hand if not specified and is ped + if boneIndex == -1 then boneIndex = 0 end -- Default to root if bone not found or not ped + offsetPos = offsetPos or vector3(0.0, 0.0, 0.0) + offsetRot = offsetRot or vector3(0.0, 0.0, 0.0) + print(string.format("[ClientEntityActions] Attaching prop '%s' to entity %s at bone %d", propModel, entityId, boneIndex)) + AttachEntityToEntity(prop, entity, boneIndex, offsetPos.x, offsetPos.y, offsetPos.z, offsetRot.x, offsetRot.y, offsetRot.z, false, useSoftPinning or false, collision or false, isPed or false, vertexIndex or 2, fixedRot == nil and true or fixedRot) + + -- Store the attached prop handle for later removal + if not entityData.attachedProps then entityData.attachedProps = {} end + entityData.attachedProps[propModel] = prop -- Store by model name/hash for easy lookup + + -- This action finishes immediately + ClientEntityActions.IsActionRunning[entityId] = false + ClientEntityActions.ProcessNextAction(entityId) +end +--- Internal implementation for detaching a prop. Registered via RegisterAction. +-- @param entityData table +-- @param propModel string|number The model name/hash of the prop to detach. +function DefaultActions.DetachProp(entityData, propModel) + local entityId = entityData.id + + if entityData.attachedProps and propModel then + local propHandle = entityData.attachedProps[propModel] + if propHandle and DoesEntityExist(propHandle) then + DetachEntity(propHandle, true, true) -- Detach + DeleteEntity(propHandle) -- Delete + entityData.attachedProps[propModel] = nil -- Remove from tracking + -- print(string.format("[ClientEntityActions] Detached prop '%s' from entity %s", propModel, entityId)) + else + -- print(string.format("[ClientEntityActions] Prop '%s' not found attached to entity %s for detachment.", propModel, entityId)) + end + else + -- print(string.format("[ClientEntityActions] No props tracked or propModel not specified for detachment on entity %s.", entityId)) + end + + -- This action finishes immediately + ClientEntityActions.IsActionRunning[entityId] = false + ClientEntityActions.ProcessNextAction(entityId) +end + +function DefaultActions.GetInCar(entityData, vehicleData, seatIndex, timeout) + local entity = entityData.spawned + local vehicle = vehicleData.spawned + local entityId = entityData.id + + if not entity or not DoesEntityExist(entity) or not IsEntityAPed(entity) then + ClientEntityActions.IsActionRunning[entityId] = false + ClientEntityActions.ProcessNextAction(entityId) -- Try next action if this one failed immediately + return + end + + if not vehicle or not DoesEntityExist(vehicle) or not IsEntityAVehicle(vehicle) then + ClientEntityActions.IsActionRunning[entityId] = false + ClientEntityActions.ProcessNextAction(entityId) -- Try next action if this one failed immediately + return + end + + -- Clear previous tasks just in case + ClearPedTasks(entity) + + local thread = CreateThread(function() + TaskEnterVehicle(entity, vehicle, timeout or 1000, seatIndex or -1, 1.0, 1, 0) -- Enter vehicle + Wait(timeout or 1000) -- Wait for a bit to ensure the task is completed + + ClientEntityActions.ActionThreads[entityId] = nil -- Clear thread reference + -- Only process next action if this thread was the one running the action + if ClientEntityActions.IsActionRunning[entityId] then + ClientEntityActions.IsActionRunning[entityId] = false + ClientEntityActions.ProcessNextAction(entityId) + end + end) + ClientEntityActions.ActionThreads[entityId] = thread +end + +for name, func in pairs(DefaultActions) do + ClientEntityActions.RegisterAction(name, func) +end + +return ClientEntityActions diff --git a/lib/entities/server/server_entity.lua b/lib/entities/server/server_entity.lua index 663c556..b040332 100644 --- a/lib/entities/server/server_entity.lua +++ b/lib/entities/server/server_entity.lua @@ -58,13 +58,15 @@ end -- @param entityId string|number The ID of the entity. -- @param actionName string The name of the action to trigger (must match a function in ClientEntityActions). -- @param ... any Additional arguments for the action function. -function ServerEntity.TriggerAction(entityId, actionName, ...) +function ServerEntity.TriggerAction(entityId, actionName, endPosition, ...) + print("Triggering action: ", entityId, actionName, ...) local entity = Entities[entityId] if not entity then print(string.format("[ServerEntity] Attempted to trigger action '%s' on non-existent entity %s", actionName, entityId)) return end - TriggerClientEvent("community_bridge:client:TriggerEntityAction", -1, entityId, actionName, ...) + print(string.format("[ServerEntity] Triggering action '%s' for entity %s", actionName, entityId)) + TriggerClientEvent("community_bridge:client:TriggerEntityAction", -1, entityId, actionName, endPosition, ...) end function ServerEntity.GetAll() diff --git a/lib/points/client/points.lua b/lib/points/client/points.lua index 9acf9d5..06f9769 100644 --- a/lib/points/client/points.lua +++ b/lib/points/client/points.lua @@ -236,6 +236,16 @@ function Point.Get(id) return ActivePoints[id] end +function Point.UpdateCoords(id, coords) + local point = ActivePoints[id] + if point then + point.coords = coords + local oldCellKey = point.cellKey + point.cellKey = Point.GetCellKey(coords) + Point.UpdateInGrid(point, oldCellKey) + end +end + ---Returns all points ---@return table function Point.GetAll()