diff --git a/client/events.lua b/client/events.lua index 3c07aeb7a..532d9863f 100644 --- a/client/events.lua +++ b/client/events.lua @@ -129,20 +129,6 @@ RegisterNetEvent('QBCore:Command:GoToMarker', function() end) -- Vehicle Commands -lib.callback.register('qbx_core:client:vehicleSpawned', function(netId, props) - local veh = NetworkGetEntityFromNetworkId(netId) - - for i = -1, 0 do - local ped = GetPedInVehicleSeat(veh, i) - if ped ~= cache.ped and ped > 0 and NetworkGetEntityOwner(ped) == cache.playerId then - DeleteEntity(ped) - end - end - - if props then - lib.setVehicleProperties(veh, props) - end -end) lib.callback.register('qbx_core:client:getVehiclesInRadius', function(radius) local vehicles = lib.getNearbyVehicles(GetEntityCoords(cache.ped), radius or 5, true) @@ -208,3 +194,57 @@ RegisterNetEvent('QBCore:Client:OnSharedUpdateMultiple', function(tableName, val end TriggerEvent('QBCore:Client:UpdateObject') end) + +-- Set vehicle props +---@param vehicle number +---@param props table +qbx.entityStateHandler('setVehicleProperties', function(vehicle, _, props) + if not props then return end + + SetTimeout(0, function() + local state = Entity(vehicle).state + + local timeOut = GetGameTimer() + 10000 + + while state.setVehicleProperties do + if NetworkGetEntityOwner(vehicle) == cache.playerId then + if lib.setVehicleProperties(vehicle, props) then + state:set('setVehicleProperties', nil, true) + end + end + if GetGameTimer() > timeOut then + break + end + + Wait(50) + end + end) +end) + +-- Clear vehicle peds +---@param vehicle number +---@param init boolean +qbx.entityStateHandler('initVehicle', function(vehicle, _, init) + if not init then return end + + for i = -1, 0 do + local ped = GetPedInVehicleSeat(vehicle, i) + if ped ~= cache.ped and ped > 0 and NetworkGetEntityOwner(ped) == cache.playerId then + DeleteEntity(ped) + end + end + + lib.waitFor(function() + return not IsEntityWaitingForWorldCollision(vehicle) + end) + + if NetworkGetEntityOwner(vehicle) ~= cache.playerId then return end + + local state = Entity(vehicle).state + + SetVehicleOnGroundProperly(vehicle); + + SetTimeout(0, function() + state:set('initVehicle', nil, true) + end) +end) \ No newline at end of file diff --git a/modules/lib.lua b/modules/lib.lua index 0cd1175ce..279c22de5 100644 --- a/modules/lib.lua +++ b/modules/lib.lua @@ -227,6 +227,7 @@ if isServer then ]] ---@param params LibSpawnVehicleParams ---@return integer netId + ---@return number veh function qbx.spawnVehicle(params) local model = params.model local source = params.spawnSource @@ -260,14 +261,11 @@ if isServer then SetPedIntoVehicle(ped, veh, -1) end - local owner = lib.waitFor(function() - local owner = NetworkGetEntityOwner(veh) - if owner ~= -1 then return owner end - end, 5000) - + Entity(veh).state:set('initVehicle', true, true) + Entity(veh).state:set('setVehicleProperties', props, true) local netId = NetworkGetNetworkIdFromEntity(veh) - lib.callback.await('qbx_core:client:vehicleSpawned', owner, netId, props) - return netId + + return netId, veh end else ---@class LibDrawTextParams