From dcffe7376349e88a80a2e6e8f04974d0a4ba82ae Mon Sep 17 00:00:00 2001 From: Manason Date: Sat, 14 Sep 2024 12:31:41 -0700 Subject: [PATCH 01/15] feat: persistent player vehicles --- client/vehicle-persistence.lua | 73 ++++++++++++++++++++++++++ fxmanifest.lua | 3 ++ modules/lib.lua | 2 +- server/vehicle-persistence.lua | 96 ++++++++++++++++++++++++++++++++++ 4 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 client/vehicle-persistence.lua create mode 100644 server/vehicle-persistence.lua diff --git a/client/vehicle-persistence.lua b/client/vehicle-persistence.lua new file mode 100644 index 000000000..0ee572d9c --- /dev/null +++ b/client/vehicle-persistence.lua @@ -0,0 +1,73 @@ +if GetConvar('qbx:enable_vehicle_persistence', 'true') == 'false' then return end + +local cachedProps +local netId +local vehicle +local seat + +local watchedKeys = { + 'bodyHealth', + 'engineHealth', + 'tankHealth', + 'fuelLevel', + 'oilLevel', + 'dirtLevel', + 'windows', + 'doors', + 'tyres', +} + +---Calculates the difference in values of two tables for the watched keys. +---If the second table does not have a value that the first table has, it will be marked 'deleted'. +---@param tbl1 table +---@param tbl2 table +---@return table diff +---@return boolean hasChanged if diff table is not empty +local function calculateDiff(tbl1, tbl2) + local diff = {} + local hasChanged = false + + for i = 1, #watchedKeys do + local key = watchedKeys[i] + local val1 = tbl1[key] + local val2 = tbl2[key] + + if val1 ~= val2 then + diff[key] = val2 == nil and 'deleted' or val2 + hasChanged = true + end + end + + return diff, hasChanged +end + +local function sendPropsDiff() + local newProps = lib.getVehicleProperties(vehicle) + if not cachedProps then + cachedProps = newProps + return + end + local diff, hasChanged = calculateDiff(cachedProps, newProps) + cachedProps = newProps + if not hasChanged then return end + TriggerServerEvent('qbx_core:server:vehiclePropsChanged', netId, diff) +end + +lib.onCache('seat', function(newSeat) + if newSeat == -1 then + seat = -1 + vehicle = cache.vehicle + netId = NetworkGetNetworkIdFromEntity(vehicle) + CreateThread(function() + while seat == -1 do + sendPropsDiff() + Wait(10000) + end + end) + else + seat = nil + sendPropsDiff() + vehicle = nil + netId = nil + end +end) \ No newline at end of file diff --git a/fxmanifest.lua b/fxmanifest.lua index 7cb8ea57a..00095ca83 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -23,6 +23,7 @@ client_scripts { 'client/events.lua', 'client/character.lua', 'client/discord.lua', + 'client/vehicle-persistence.lua', 'bridge/qb/client/main.lua', } @@ -36,6 +37,7 @@ server_scripts { 'server/commands.lua', 'server/loops.lua', 'server/character.lua', + 'server/vehicle-persistence.lua', 'bridge/qb/server/main.lua', } @@ -63,6 +65,7 @@ dependencies { '/onesync', 'ox_lib', 'oxmysql', + 'qbx_vehicles', } provide 'qb-core' diff --git a/modules/lib.lua b/modules/lib.lua index 9351dbbb2..8bb21bd87 100644 --- a/modules/lib.lua +++ b/modules/lib.lua @@ -316,7 +316,7 @@ if isServer then end local netId = NetworkGetNetworkIdFromEntity(veh) - + TriggerServerEvent('qbx_core:server:vehicleSpawned', veh) return netId, veh end else diff --git a/server/vehicle-persistence.lua b/server/vehicle-persistence.lua new file mode 100644 index 000000000..4507900a0 --- /dev/null +++ b/server/vehicle-persistence.lua @@ -0,0 +1,96 @@ +if GetConvar('qbx:enable_vehicle_persistence', 'true') == 'false' then return end + +assert(lib.checkDependency('qbx_vehicles', '1.4.1', true)) + +local function getVehicleId(vehicle) + return Entity(vehicle).state.vehicleid or exports.qbx_vehicles:GetVehicleByIdPlate(GetVehicleNumberPlateText(vehicle)) +end + +RegisterNetEvent('qbx_core:server:vehiclePropsChanged', function(netId, diff) + local vehicle = NetworkGetEntityFromNetworkId(netId) + + local vehicleId = getVehicleId(vehicle) + if not vehicleId then return end + + local props = exports.qbx_vehicles:GetPlayerVehicle(vehicleId)?.props + if not props then return end + + if diff.bodyHealth then + props.bodyHealth = GetVehicleBodyHealth(vehicle) + end + + if diff.engineHealth then + props.engineHealth = GetVehicleEngineHealth(vehicle) + end + + if diff.tankHealth then + props.tankHealth = GetVehiclePetrolTankHealth(vehicle) + end + + if diff.fuelLevel then + props.fuelLevel = diff.fuelLevel ~= 'deleted' and diff.fuelLevel or nil + end + + if diff.oilLevel then + props.oilLevel = diff.oilLevel ~= 'deleted' and diff.oilLevel or nil + end + + if diff.dirtLevel then + props.dirtLevel = GetVehicleDirtLevel(vehicle) + end + + if diff.windows then + props.windows = diff.windows ~= 'deleted' and diff.windows or nil + end + + if diff.doors then + local damage = {} + local numDoors = 0 + + for i = 0, 5 do + if IsVehicleDoorDamaged(vehicle, i) then + numDoors += 1 + damage[numDoors] = i + end + end + + props.doors = damage + end + + if diff.tyres then + local damage = {} + for i = 0, 7 do + if IsVehicleTyreBurst(vehicle, i, false) then + damage[i] = IsVehicleTyreBurst(vehicle, i, true) and 2 or 1 + end + end + + props.tyres = damage + end + + exports.qbx_vehicles:SaveVehicle(vehicle, { + props = props, + }) +end) + +AddEventHandler('entityRemoved', function(entity) + if not IsEntityAVehicle(entity) then return end + local coords = GetEntityCoords(entity) + local heading = GetEntityHeading(entity) + local bucket = GetEntityRoutingBucket(entity) + + local vehicleId = getVehicleId(entity) + if not vehicleId then return end + local playerVehicle = exports.qbx_vehicles:GetPlayerVehicle(vehicleId) + + if DoesEntityExist(entity) then + DeleteVehicle(entity) + end + + qbx.spawnVehicle({ + model = playerVehicle.props.model, + spawnSource = vec4(coords.x, coords.y, coords.z, heading), + bucket = bucket, + props = playerVehicle.props + }) +end) \ No newline at end of file From 68c5825e877498f4f2d6f7ba466bfaeeba529149 Mon Sep 17 00:00:00 2001 From: Manason Date: Sat, 14 Sep 2024 12:34:27 -0700 Subject: [PATCH 02/15] fix: linting --- server/vehicle-persistence.lua | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/server/vehicle-persistence.lua b/server/vehicle-persistence.lua index 4507900a0..67c272337 100644 --- a/server/vehicle-persistence.lua +++ b/server/vehicle-persistence.lua @@ -44,17 +44,7 @@ RegisterNetEvent('qbx_core:server:vehiclePropsChanged', function(netId, diff) end if diff.doors then - local damage = {} - local numDoors = 0 - - for i = 0, 5 do - if IsVehicleDoorDamaged(vehicle, i) then - numDoors += 1 - damage[numDoors] = i - end - end - - props.doors = damage + props.doors = diff.doors ~= 'deleted' and diff.doors or nil end if diff.tyres then @@ -74,7 +64,6 @@ RegisterNetEvent('qbx_core:server:vehiclePropsChanged', function(netId, diff) end) AddEventHandler('entityRemoved', function(entity) - if not IsEntityAVehicle(entity) then return end local coords = GetEntityCoords(entity) local heading = GetEntityHeading(entity) local bucket = GetEntityRoutingBucket(entity) From de78ed7c12512a59bf6a1d6fd33649d490a1671e Mon Sep 17 00:00:00 2001 From: Manason Date: Tue, 17 Sep 2024 07:15:01 -0700 Subject: [PATCH 03/15] removed from manifest --- fxmanifest.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/fxmanifest.lua b/fxmanifest.lua index 00095ca83..9c5581591 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -65,7 +65,6 @@ dependencies { '/onesync', 'ox_lib', 'oxmysql', - 'qbx_vehicles', } provide 'qb-core' From 55e083bf47987db91eabbd71159a3a17397b4de5 Mon Sep 17 00:00:00 2001 From: Manason Date: Tue, 17 Sep 2024 07:18:12 -0700 Subject: [PATCH 04/15] fix: driver switching seats or exiting breaks loop only --- client/vehicle-persistence.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/vehicle-persistence.lua b/client/vehicle-persistence.lua index 0ee572d9c..cd7078076 100644 --- a/client/vehicle-persistence.lua +++ b/client/vehicle-persistence.lua @@ -64,7 +64,7 @@ lib.onCache('seat', function(newSeat) Wait(10000) end end) - else + elseif seat == -1 then seat = nil sendPropsDiff() vehicle = nil From eaf61b9145118506de7f6734d99fe2bb7dc0a00b Mon Sep 17 00:00:00 2001 From: Manason Date: Tue, 17 Sep 2024 08:10:36 -0700 Subject: [PATCH 05/15] feat: persistence tracking --- modules/lib.lua | 2 +- server/functions.lua | 15 ++++++++++++++- server/vehicle-persistence.lua | 27 +++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/modules/lib.lua b/modules/lib.lua index 8bb21bd87..b0e80706b 100644 --- a/modules/lib.lua +++ b/modules/lib.lua @@ -316,7 +316,7 @@ if isServer then end local netId = NetworkGetNetworkIdFromEntity(veh) - TriggerServerEvent('qbx_core:server:vehicleSpawned', veh) + TriggerEvent('qbx_core:server:vehicleSpawned', veh) return netId, veh end else diff --git a/server/functions.lua b/server/functions.lua index 297beb671..1ce37e14c 100644 --- a/server/functions.lua +++ b/server/functions.lua @@ -483,4 +483,17 @@ local function getGroupMembers(group, type) return storage.fetchGroupMembers(group, type) end -exports('GetGroupMembers', getGroupMembers) \ No newline at end of file +exports('GetGroupMembers', getGroupMembers) + +---Disables persistence before deleting a vehicle, then deletes it. +---@param vehicle number +local function deleteVehicle(vehicle) + if DisablePersistence then + DisablePersistence(vehicle) + end + if DoesEntityExist(vehicle) then + DeleteEntity(vehicle) + end +end + +exports('DeleteVehicle', deleteVehicle) \ No newline at end of file diff --git a/server/vehicle-persistence.lua b/server/vehicle-persistence.lua index 67c272337..c754b55e0 100644 --- a/server/vehicle-persistence.lua +++ b/server/vehicle-persistence.lua @@ -2,6 +2,27 @@ if GetConvar('qbx:enable_vehicle_persistence', 'true') == 'false' then return en assert(lib.checkDependency('qbx_vehicles', '1.4.1', true)) +---@alias Entity number +---@type table +local persistedVehicles = {} + +---A persisted vehicle will respawn when deleted. Only works for player owned vehicles. +---Vehicles spawned using lib are automatically persisted +---@param vehicle number +local function enablePersistence(vehicle) + persistedVehicles[vehicle] = true +end + +exports('EnablePersistence', enablePersistence) + +---A vehicle without persistence will not respawn when deleted. +---@param vehicle number +function DisablePersistence(vehicle) + persistedVehicles[vehicle] = nil +end + +exports('DisablePersistence', DisablePersistence) + local function getVehicleId(vehicle) return Entity(vehicle).state.vehicleid or exports.qbx_vehicles:GetVehicleByIdPlate(GetVehicleNumberPlateText(vehicle)) end @@ -63,7 +84,12 @@ RegisterNetEvent('qbx_core:server:vehiclePropsChanged', function(netId, diff) }) end) +AddEventHandler('qbx_core:server:vehicleSpawned', function(entity) + persistedVehicles[entity] = true +end) + AddEventHandler('entityRemoved', function(entity) + if not persistedVehicles(entity) then return end local coords = GetEntityCoords(entity) local heading = GetEntityHeading(entity) local bucket = GetEntityRoutingBucket(entity) @@ -71,6 +97,7 @@ AddEventHandler('entityRemoved', function(entity) local vehicleId = getVehicleId(entity) if not vehicleId then return end local playerVehicle = exports.qbx_vehicles:GetPlayerVehicle(vehicleId) + persistedVehicles[entity] = nil if DoesEntityExist(entity) then DeleteVehicle(entity) From 2f20fd2f0deed70b941c8aeb7c650bb05e382ccd Mon Sep 17 00:00:00 2001 From: Manason Date: Tue, 17 Sep 2024 12:05:19 -0700 Subject: [PATCH 06/15] use statebag instead of server table --- client/vehicle-persistence.lua | 1 + server/vehicle-persistence.lua | 14 +++++--------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/client/vehicle-persistence.lua b/client/vehicle-persistence.lua index cd7078076..194287b4a 100644 --- a/client/vehicle-persistence.lua +++ b/client/vehicle-persistence.lua @@ -42,6 +42,7 @@ local function calculateDiff(tbl1, tbl2) end local function sendPropsDiff() + if not Entity(vehicle).state.persisted then return end local newProps = lib.getVehicleProperties(vehicle) if not cachedProps then cachedProps = newProps diff --git a/server/vehicle-persistence.lua b/server/vehicle-persistence.lua index c754b55e0..88a9add09 100644 --- a/server/vehicle-persistence.lua +++ b/server/vehicle-persistence.lua @@ -2,15 +2,11 @@ if GetConvar('qbx:enable_vehicle_persistence', 'true') == 'false' then return en assert(lib.checkDependency('qbx_vehicles', '1.4.1', true)) ----@alias Entity number ----@type table -local persistedVehicles = {} - ---A persisted vehicle will respawn when deleted. Only works for player owned vehicles. ---Vehicles spawned using lib are automatically persisted ---@param vehicle number local function enablePersistence(vehicle) - persistedVehicles[vehicle] = true + Entity(vehicle).state:set('persisted', true, true) end exports('EnablePersistence', enablePersistence) @@ -18,7 +14,7 @@ exports('EnablePersistence', enablePersistence) ---A vehicle without persistence will not respawn when deleted. ---@param vehicle number function DisablePersistence(vehicle) - persistedVehicles[vehicle] = nil + Entity(vehicle).state:set('persisted', nil, true) end exports('DisablePersistence', DisablePersistence) @@ -85,11 +81,11 @@ RegisterNetEvent('qbx_core:server:vehiclePropsChanged', function(netId, diff) end) AddEventHandler('qbx_core:server:vehicleSpawned', function(entity) - persistedVehicles[entity] = true + Entity(entity).state:set('persisted', true, true) end) AddEventHandler('entityRemoved', function(entity) - if not persistedVehicles(entity) then return end + if not Entity(entity).state.persisted then return end local coords = GetEntityCoords(entity) local heading = GetEntityHeading(entity) local bucket = GetEntityRoutingBucket(entity) @@ -97,7 +93,7 @@ AddEventHandler('entityRemoved', function(entity) local vehicleId = getVehicleId(entity) if not vehicleId then return end local playerVehicle = exports.qbx_vehicles:GetPlayerVehicle(vehicleId) - persistedVehicles[entity] = nil + Entity(entity).state:set('persisted', nil, true) if DoesEntityExist(entity) then DeleteVehicle(entity) From be038f7e2838fc3beab87695655dbf5ce3cf98fe Mon Sep 17 00:00:00 2001 From: David Malchin Date: Wed, 18 Sep 2024 04:54:48 +0300 Subject: [PATCH 07/15] fix(server/vehicle-persistence): typo --- server/vehicle-persistence.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/vehicle-persistence.lua b/server/vehicle-persistence.lua index 88a9add09..1911a2e68 100644 --- a/server/vehicle-persistence.lua +++ b/server/vehicle-persistence.lua @@ -20,7 +20,7 @@ end exports('DisablePersistence', DisablePersistence) local function getVehicleId(vehicle) - return Entity(vehicle).state.vehicleid or exports.qbx_vehicles:GetVehicleByIdPlate(GetVehicleNumberPlateText(vehicle)) + return Entity(vehicle).state.vehicleid or exports.qbx_vehicles:GetVehicleIdByPlate(GetVehicleNumberPlateText(vehicle)) end RegisterNetEvent('qbx_core:server:vehiclePropsChanged', function(netId, diff) From f320f90a61d6784bc5472f77ccae132e2ee03733 Mon Sep 17 00:00:00 2001 From: David Malchin Date: Wed, 18 Sep 2024 05:16:41 +0300 Subject: [PATCH 08/15] fix(server/vehicle-persistence): remove state only when entity exists --- server/vehicle-persistence.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/vehicle-persistence.lua b/server/vehicle-persistence.lua index 1911a2e68..04c21b0d6 100644 --- a/server/vehicle-persistence.lua +++ b/server/vehicle-persistence.lua @@ -93,9 +93,9 @@ AddEventHandler('entityRemoved', function(entity) local vehicleId = getVehicleId(entity) if not vehicleId then return end local playerVehicle = exports.qbx_vehicles:GetPlayerVehicle(vehicleId) - Entity(entity).state:set('persisted', nil, true) if DoesEntityExist(entity) then + Entity(entity).state:set('persisted', nil, true) DeleteVehicle(entity) end From 44015746ab5d222b73ceefcbfcd56f38f982d043 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 17 Sep 2024 22:01:06 -0700 Subject: [PATCH 09/15] disabling by default --- server/vehicle-persistence.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/vehicle-persistence.lua b/server/vehicle-persistence.lua index 04c21b0d6..6521a920c 100644 --- a/server/vehicle-persistence.lua +++ b/server/vehicle-persistence.lua @@ -1,4 +1,4 @@ -if GetConvar('qbx:enable_vehicle_persistence', 'true') == 'false' then return end +if GetConvar('qbx:enable_vehicle_persistence', 'false') == 'false' then return end assert(lib.checkDependency('qbx_vehicles', '1.4.1', true)) From 738a08b55ad19eb663ce3bc5026533f59bf54e99 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 17 Sep 2024 22:24:57 -0700 Subject: [PATCH 10/15] delete vehicles using internal function --- server/commands.lua | 4 ++-- server/functions.lua | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/server/commands.lua b/server/commands.lua index 259676bfc..34fa656ee 100644 --- a/server/commands.lua +++ b/server/commands.lua @@ -146,7 +146,7 @@ lib.addCommand('car', { local keepCurrentVehicle = args[locale('command.car.params.keepCurrentVehicle.name')] local currentVehicle = not keepCurrentVehicle and GetVehiclePedIsIn(ped, false) if currentVehicle and currentVehicle ~= 0 then - DeleteEntity(currentVehicle) + DeleteVehicle(currentVehicle) end local _, vehicle = qbx.spawnVehicle({ @@ -180,7 +180,7 @@ lib.addCommand('dv', { for i = 1, #pedCars do local pedCar = NetworkGetEntityFromNetworkId(pedCars[i]) if pedCar and DoesEntityExist(pedCar) then - DeleteEntity(pedCar) + DeleteVehicle(pedCar) end end end diff --git a/server/functions.lua b/server/functions.lua index 1ce37e14c..d0cdb6908 100644 --- a/server/functions.lua +++ b/server/functions.lua @@ -487,7 +487,7 @@ exports('GetGroupMembers', getGroupMembers) ---Disables persistence before deleting a vehicle, then deletes it. ---@param vehicle number -local function deleteVehicle(vehicle) +function DeleteVehicle(vehicle) if DisablePersistence then DisablePersistence(vehicle) end @@ -496,4 +496,4 @@ local function deleteVehicle(vehicle) end end -exports('DeleteVehicle', deleteVehicle) \ No newline at end of file +exports('DeleteVehicle', DeleteVehicle) \ No newline at end of file From f1678d99a505f39c5b0dbccd9dc469c0cf2ddf91 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 17 Sep 2024 22:47:58 -0700 Subject: [PATCH 11/15] deprecate qbx.deleteVehicle --- modules/lib.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/lib.lua b/modules/lib.lua index b0e80706b..cc5519b91 100644 --- a/modules/lib.lua +++ b/modules/lib.lua @@ -419,6 +419,7 @@ else end ---Deletes the specified vehicle and returns whether it was successful. + ---@deprecated use exports.qbx_core:DeleteVehicle(vehicle) instead ---@param vehicle integer ---@return boolean deleted function qbx.deleteVehicle(vehicle) From 62d0a33063fe8b0385bdefbbdffbafdc0bbe0bff Mon Sep 17 00:00:00 2001 From: Manason Date: Wed, 18 Sep 2024 07:17:32 -0700 Subject: [PATCH 12/15] revert: deprecating qbx.deleteVehicle --- modules/lib.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/lib.lua b/modules/lib.lua index cc5519b91..b0e80706b 100644 --- a/modules/lib.lua +++ b/modules/lib.lua @@ -419,7 +419,6 @@ else end ---Deletes the specified vehicle and returns whether it was successful. - ---@deprecated use exports.qbx_core:DeleteVehicle(vehicle) instead ---@param vehicle integer ---@return boolean deleted function qbx.deleteVehicle(vehicle) From f0be08a642e0054b4421c77f2bb5d6f7e1db81e8 Mon Sep 17 00:00:00 2001 From: Manason Date: Wed, 18 Sep 2024 07:36:02 -0700 Subject: [PATCH 13/15] feat: warp passengers back into a vehicle on removal --- server/vehicle-persistence.lua | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/server/vehicle-persistence.lua b/server/vehicle-persistence.lua index 6521a920c..08bcfef1f 100644 --- a/server/vehicle-persistence.lua +++ b/server/vehicle-persistence.lua @@ -84,11 +84,28 @@ AddEventHandler('qbx_core:server:vehicleSpawned', function(entity) Entity(entity).state:set('persisted', true, true) end) +local function getPedsInVehicleSeats(vehicle) + local occupants = {} + local occupantsI = 1 + for i = -1, 7 do + local ped = GetPedInVehicleSeat(vehicle, i) + if ped ~= 0 then + occupants[occupantsI] = { + ped = ped, + seat = i, + } + occupantsI += 1 + end + end + return occupants +end + AddEventHandler('entityRemoved', function(entity) if not Entity(entity).state.persisted then return end local coords = GetEntityCoords(entity) local heading = GetEntityHeading(entity) local bucket = GetEntityRoutingBucket(entity) + local passengers = getPedsInVehicleSeats(entity) local vehicleId = getVehicleId(entity) if not vehicleId then return end @@ -99,10 +116,15 @@ AddEventHandler('entityRemoved', function(entity) DeleteVehicle(entity) end - qbx.spawnVehicle({ + local _, veh = qbx.spawnVehicle({ model = playerVehicle.props.model, spawnSource = vec4(coords.x, coords.y, coords.z, heading), bucket = bucket, props = playerVehicle.props }) + + for i = 1, #passengers do + local passenger = passengers[i] + SetPedIntoVehicle(passenger.ped, veh, passenger.seat) + end end) \ No newline at end of file From a9fe0b2bef18aab337897606452921bba4ac7b27 Mon Sep 17 00:00:00 2001 From: Manason Date: Wed, 18 Sep 2024 11:08:27 -0700 Subject: [PATCH 14/15] use persistence exports instead of event and make them always available --- modules/lib.lua | 2 +- server/functions.lua | 4 +--- server/vehicle-persistence.lua | 12 ++++-------- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/modules/lib.lua b/modules/lib.lua index b0e80706b..a61e9df94 100644 --- a/modules/lib.lua +++ b/modules/lib.lua @@ -316,7 +316,7 @@ if isServer then end local netId = NetworkGetNetworkIdFromEntity(veh) - TriggerEvent('qbx_core:server:vehicleSpawned', veh) + exports.qbx_core:EnablePersistence(veh) return netId, veh end else diff --git a/server/functions.lua b/server/functions.lua index d0cdb6908..e5a57d845 100644 --- a/server/functions.lua +++ b/server/functions.lua @@ -488,9 +488,7 @@ exports('GetGroupMembers', getGroupMembers) ---Disables persistence before deleting a vehicle, then deletes it. ---@param vehicle number function DeleteVehicle(vehicle) - if DisablePersistence then - DisablePersistence(vehicle) - end + DisablePersistence(vehicle) if DoesEntityExist(vehicle) then DeleteEntity(vehicle) end diff --git a/server/vehicle-persistence.lua b/server/vehicle-persistence.lua index 08bcfef1f..7329c1f11 100644 --- a/server/vehicle-persistence.lua +++ b/server/vehicle-persistence.lua @@ -1,7 +1,3 @@ -if GetConvar('qbx:enable_vehicle_persistence', 'false') == 'false' then return end - -assert(lib.checkDependency('qbx_vehicles', '1.4.1', true)) - ---A persisted vehicle will respawn when deleted. Only works for player owned vehicles. ---Vehicles spawned using lib are automatically persisted ---@param vehicle number @@ -19,6 +15,10 @@ end exports('DisablePersistence', DisablePersistence) +if GetConvar('qbx:enable_vehicle_persistence', 'false') == 'false' then return end + +assert(lib.checkDependency('qbx_vehicles', '1.4.1', true)) + local function getVehicleId(vehicle) return Entity(vehicle).state.vehicleid or exports.qbx_vehicles:GetVehicleIdByPlate(GetVehicleNumberPlateText(vehicle)) end @@ -80,10 +80,6 @@ RegisterNetEvent('qbx_core:server:vehiclePropsChanged', function(netId, diff) }) end) -AddEventHandler('qbx_core:server:vehicleSpawned', function(entity) - Entity(entity).state:set('persisted', true, true) -end) - local function getPedsInVehicleSeats(vehicle) local occupants = {} local occupantsI = 1 From 156e7d36514e93ec5b0d808c625a1bdd94e7dfb1 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 18 Sep 2024 19:09:12 -0700 Subject: [PATCH 15/15] camelCasing convar and defaulting to false --- client/vehicle-persistence.lua | 2 +- server/vehicle-persistence.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/client/vehicle-persistence.lua b/client/vehicle-persistence.lua index 194287b4a..790826a08 100644 --- a/client/vehicle-persistence.lua +++ b/client/vehicle-persistence.lua @@ -1,4 +1,4 @@ -if GetConvar('qbx:enable_vehicle_persistence', 'true') == 'false' then return end +if GetConvar('qbx:enableVehiclePersistence', 'false') == 'false' then return end local cachedProps local netId diff --git a/server/vehicle-persistence.lua b/server/vehicle-persistence.lua index 7329c1f11..8369427b5 100644 --- a/server/vehicle-persistence.lua +++ b/server/vehicle-persistence.lua @@ -15,7 +15,7 @@ end exports('DisablePersistence', DisablePersistence) -if GetConvar('qbx:enable_vehicle_persistence', 'false') == 'false' then return end +if GetConvar('qbx:enableVehiclePersistence', 'false') == 'false' then return end assert(lib.checkDependency('qbx_vehicles', '1.4.1', true))