Skip to content

Commit

Permalink
feat(server/main): attempt to clean up testdrives when stopping
Browse files Browse the repository at this point in the history
  • Loading branch information
D4isDAVID committed Dec 28, 2024
1 parent c0aa06a commit a9649e0
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,32 +70,45 @@ RegisterNetEvent('qbx_vehicleshop:server:customTestDrive', function(vehicle, pla
end
end)

AddStateBagChangeHandler('isInTestDrive', nil, function(bagName, _, value)
if value then return end
---@param player integer
local function endTestDrive(player)
local testDrive = testDrives[player]
if not testDrive or not testDrive.netId then return end

local plySrc = GetPlayerFromStateBagName(bagName)
if not plySrc then return end
local netId = testDrives[plySrc].netId
local endBehavior = testDrives[plySrc].endBehavior
if not netId or endBehavior == 'none' then return end
local vehicle = NetworkGetEntityFromNetworkId(testDrive.netId)

local vehicle = NetworkGetEntityFromNetworkId(netId)

if endBehavior == 'return' then
local coords = testDrives[plySrc].returnLocation
local plyPed = GetPlayerPed(plySrc)
if testDrive.endBehavior == 'return' then
local coords = testDrive.returnLocation
local plyPed = GetPlayerPed(player)
if #(GetEntityCoords(plyPed) - coords) > 10 then -- don't teleport if they are standing near the spot
SetEntityCoords(plyPed, coords.x, coords.y, coords.z, false, false, false, false)
end
if DoesEntityExist(vehicle) then
DeleteEntity(vehicle)
end
elseif endBehavior == 'destroy' then
elseif testDrive.endBehavior == 'destroy' then
if DoesEntityExist(vehicle) then
DeleteEntity(vehicle)
end
end
testDrives[plySrc] = nil

testDrives[player] = nil
end

AddStateBagChangeHandler('isInTestDrive', nil, function(bagName, _, value)
if value then return end

local player = GetPlayerFromStateBagName(bagName)
if not player then return end
endTestDrive(player)
end)

AddEventHandler('onResourceStop', function (resourceName)
if cache.resource ~= resourceName then return end

for player, _ in pairs(testDrives) do
Player(player).state:set('isInTestDrive', nil, true)
end
end)

---@param vehicleData {buyVehicle: string}
Expand Down

0 comments on commit a9649e0

Please sign in to comment.