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 25 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
9d87f03
feat: full vehicle persistence
CodexisPhantom Nov 13, 2024
f00aa1c
fix: vehicles spawning multiple times if 1 player
CodexisPhantom Nov 13, 2024
f6e2391
refactor: use onResourceStart event
CodexisPhantom Nov 15, 2024
50b3523
fix: players never refreshed
CodexisPhantom Nov 20, 2024
52605fd
Merge branch 'Qbox-project:main' into main
CodexisPhantom Nov 29, 2024
a5189ae
Merge branch 'Qbox-project:main' into main
CodexisPhantom Dec 1, 2024
72f0be4
Merge branch 'Qbox-project:main' into main
CodexisPhantom Dec 3, 2024
0cdc565
Merge branch 'Qbox-project:main' into main
CodexisPhantom Dec 5, 2024
4dc64af
Merge branch 'Qbox-project:main' into main
CodexisPhantom Dec 23, 2024
8454ba5
refactor: better persistence handling
CodexisPhantom Dec 23, 2024
c820fe8
fix: vehicles duplication
CodexisPhantom Dec 28, 2024
2b1868c
Merge branch 'Qbox-project:main' into main
CodexisPhantom Dec 29, 2024
d7f08cf
refactor: better persistence handling
CodexisPhantom Jan 3, 2025
7318687
Merge branch 'main' into main
CodexisPhantom Jan 3, 2025
943513e
fix: use correct export from qbx_vehiclekeys
CodexisPhantom Jan 6, 2025
3bfedb8
Merge branch 'Qbox-project:main' into main
CodexisPhantom Jan 10, 2025
7f5925c
Merge branch 'Qbox-project:main' into main
CodexisPhantom Jan 23, 2025
6982f28
fix: position not saved correctly
CodexisPhantom Jan 23, 2025
9366980
fix: save a vector4
CodexisPhantom Jan 23, 2025
2996d23
refactor: removed useless check
CodexisPhantom Jan 23, 2025
c302631
refactor: cleaned code
CodexisPhantom Jan 24, 2025
7905660
feat: save position on server restart
CodexisPhantom Jan 24, 2025
5618df4
fix: check entity exist
CodexisPhantom Jan 24, 2025
4fc475f
fix: vehicle position for plane and heli
CodexisPhantom Jan 26, 2025
d2c326b
refactor: cleaned code
CodexisPhantom Jan 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Contributor

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)

Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

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

vehicles is an array, use a numeric for loop instead of this.

Copy link
Author

Choose a reason for hiding this comment

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

All done

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