Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): full vehicle persistence #621

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
42 changes: 40 additions & 2 deletions server/vehicle-persistence.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
local persistence = GetConvarInt('qbx:enableVehiclePersistence', 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change. Either keep the convar as a string an overload the meaning (right now values are 'true' or 'false. We could add 'full' as well. OR add a new true/false convar which controls whether vehicles are persisted between restarts.

Copy link
Member

@D4isDAVID D4isDAVID Dec 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, values "true" and "false" are interpreted as 1 and 0 respectively by GetConvarInt. But at this point this can actually be replaced by the new GetConvarBool I believe, which is available since artifact 10543 — we require 10731.

print('Vehicle persistence mode ' .. persistence)

Comment on lines +2 to +3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete

---A persisted vehicle will respawn when deleted. Only works for player owned vehicles.
---Vehicles spawned using lib are automatically persisted
---@param vehicle number
Expand All @@ -15,7 +18,7 @@ end

exports('DisablePersistence', DisablePersistence)

if GetConvar('qbx:enableVehiclePersistence', 'false') == 'false' then return end
if persistence == 0 then return end

assert(lib.checkDependency('qbx_vehicles', '1.4.1', true))

Expand All @@ -29,6 +32,7 @@ RegisterNetEvent('qbx_core:server:vehiclePropsChanged', function(netId, diff)
local vehicleId = getVehicleId(vehicle)
if not vehicleId then return end

local coords = nil
local props = exports.qbx_vehicles:GetPlayerVehicle(vehicleId)?.props
if not props then return end

Expand Down Expand Up @@ -75,8 +79,15 @@ RegisterNetEvent('qbx_core:server:vehiclePropsChanged', function(netId, diff)
props.tyres = damage
end

if persistence == 2 then
local entityCoords = GetEntityCoords(vehicle)
local entityHeading = GetEntityHeading(vehicle)
coords = vec4(entityCoords.x, entityCoords.y, entityCoords.z, entityHeading)
end

exports.qbx_vehicles:SaveVehicle(vehicle, {
props = props,
coords = coords
})
end)

Expand Down Expand Up @@ -128,4 +139,31 @@ AddEventHandler('entityRemoved', function(entity)
local passenger = passengers[i]
SetPedIntoVehicle(passenger.ped, veh, passenger.seat)
end
end)
end)

if persistence == 1 then return end

local spawned = false

local function spawnVehicles()
spawned = true
local vehicles = exports.qbx_vehicles:GetPlayerVehicles({ states = 0 })
for _, vehicle in ipairs(vehicles) do
if not vehicle.coords then return end

local coords = vector3(vehicle.coords.x, vehicle.coords.y, vehicle.coords.z)
local playerVehicle = exports.qbx_vehicles:GetPlayerVehicle(vehicle.id)
if not playerVehicle then return end

local _, veh = qbx.spawnVehicle({ spawnSource = coords, model = playerVehicle.props.model, props = playerVehicle.props})
exports.qbx_core:EnablePersistence(veh)
Entity(veh).state:set('vehicleid', vehicle.id, false)
SetVehicleDoorsLocked(veh, 2)
SetEntityHeading(veh, vehicle.coords.w)
end
end

RegisterNetEvent('QBCore:Server:OnPlayerLoaded', function()
local players = exports.qbx_core:GetQBPlayers()
if not spawned and #players == 1 then spawnVehicles() end
end)
Manason marked this conversation as resolved.
Show resolved Hide resolved