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

refactor: outside vehicles tracking vehicle id #45

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
27 changes: 7 additions & 20 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,13 @@ local function isOfType(category, vehicle)
end

---@param vehicle number
local function checkPlayers(vehicle)
local function kickOutPeds(vehicle)
for i = -1, 5, 1 do
local seat = GetPedInVehicleSeat(vehicle, i)
if seat then
TaskLeaveVehicle(seat, vehicle, 0)
end
end

SetVehicleDoorsLocked(vehicle, 2)
Wait(1500)
DeleteVehicle(vehicle)
end

local function takeOutDepot(data)
Expand Down Expand Up @@ -205,14 +201,8 @@ RegisterNetEvent('qb-garages:client:takeOutGarage', function(data)
return
end

local spawn = lib.callback.await('qb-garage:server:IsSpawnOk', false, data.vehicle.plate, data.garageInfo.type)

if not spawn then
exports.qbx_core:Notify(Lang:t('error.not_impound'), 'error', 5000)
return
end

local netId = lib.callback.await('qb-garage:server:spawnvehicle', false, data.vehicle, data.garageInfo.spawn)
local netId = lib.callback.await('qb-garage:server:spawnvehicle', false, data.vehicle, data.garageInfo.spawn, data.garageInfo.type)
if not netId then return end

local veh = lib.waitFor(function()
if NetworkDoesEntityExistWithNetworkId(netId) then
Expand Down Expand Up @@ -243,13 +233,10 @@ local function parkVehicle(vehicle, garageName, garageInfo)
return
end

lib.callback('qbx_garages:server:saveVehicle', false, nil, lib.getVehicleProperties(vehicle), garageName, garageInfo.type, QBX.PlayerData.gang.name)
checkPlayers(vehicle)

if plate then
TriggerServerEvent('qb-garages:server:UpdateOutsideVehicle', plate, nil)
end

kickOutPeds(vehicle)
SetVehicleDoorsLocked(vehicle, 2)
Wait(1500)
lib.callback('qbx_garages:server:parkVehicle', false, nil, NetworkGetNetworkIdFromEntity(vehicle), lib.getVehicleProperties(vehicle), garageName, garageInfo.type, QBX.PlayerData.gang.name)
exports.qbx_core:Notify(Lang:t('success.vehicle_parked'), 'primary', 4500)
else
exports.qbx_core:Notify(Lang:t('error.vehicle_occupied'), 'error', 3500)
Expand Down
39 changes: 18 additions & 21 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ lib.callback.register('qb-garage:server:GetGarageVehicles', function(source, gar
local toSend = {}
if not result[1] then return false end
for _, vehicle in pairs(result) do -- Check vehicle type against depot type
if not outsideVehicles[vehicle.plate] or not DoesEntityExist(outsideVehicles[vehicle.plate].entity) then
if not outsideVehicles[vehicle.id] or not DoesEntityExist(outsideVehicles[vehicle.id].entity) then
if (category == VehicleType.AIR and (VEHICLES[vehicle.vehicle].category == 'helicopters' or VEHICLES[vehicle.vehicle].category == 'planes')) or
(category == VehicleType.SEA and VEHICLES[vehicle.vehicle].category == 'boats') or
(category == VehicleType.CAR and VEHICLES[vehicle.vehicle].category ~= 'helicopters' and VEHICLES[vehicle.vehicle].category ~= 'planes' and VEHICLES[vehicle.vehicle].category ~= 'boats') then
Expand Down Expand Up @@ -68,11 +68,16 @@ end

lib.callback.register('qb-garage:server:checkOwnership', checkOwnership)

lib.callback.register('qb-garage:server:spawnvehicle', function (source, vehInfo, coords)
lib.callback.register('qb-garage:server:spawnvehicle', function (source, vehInfo, coords, garageType)
local props = {}

local result = MySQL.query.await('SELECT mods FROM player_vehicles WHERE plate = ?', {vehInfo.plate})
local result = MySQL.query.await('SELECT id, mods FROM player_vehicles WHERE plate = ?', {vehInfo.plate})

if result[1] then
if garageType == GarageType.DEPOT and (not outsideVehicles[result[1].id] or not DoesEntityExist(outsideVehicles[result[1].id].entity)) then -- If depot, check if vehicle is not already spawned on the map
exports.qbx_core:Notify(source, Lang:t('error.not_impound'), 'error', 5000)
return
end
props = json.decode(result[1].mods)
end

Expand All @@ -86,23 +91,12 @@ lib.callback.register('qb-garage:server:spawnvehicle', function (source, vehInfo

TriggerClientEvent('vehiclekeys:client:SetOwner', source, vehInfo.plate)

outsideVehicles[vehInfo.plate] = {netID = netId, entity = veh}
Entity(veh).state:set('vehicleid', result[1].id, false)
outsideVehicles[result[1].id] = {netID = netId, entity = veh}
return netId
end)

lib.callback.register('qb-garage:server:GetVehicleProperties', function(_, plate)
local result = MySQL.query.await('SELECT mods FROM player_vehicles WHERE plate = ?', {plate})
return result[1] and json.decode(result[1].mods) or {}
end)

lib.callback.register('qb-garage:server:IsSpawnOk', function(_, plate, type)
if type == GarageType.DEPOT then -- If depot, check if vehicle is not already spawned on the map
return not outsideVehicles[plate] or not DoesEntityExist(outsideVehicles[plate].entity)
end
return true
end)

lib.callback.register('qbx_garages:server:saveVehicle', function(source, props, garage, type, gang)
lib.callback.register('qbx_garages:server:parkVehicle', function(source, netId, props, garage, type, gang)
local owned = checkOwnership(source, props.plate, type, garage, gang) --Check ownership
if not owned then
exports.qbx_core:Notify(source, Lang:t('error.not_owned'), 'error')
Expand All @@ -112,6 +106,13 @@ lib.callback.register('qbx_garages:server:saveVehicle', function(source, props,
if type ~= 'house' and not sharedConfig.garages[garage] then return end

MySQL.update('UPDATE player_vehicles SET state = ?, garage = ?, fuel = ?, engine = ?, body = ?, mods = ? WHERE plate = ?', {VehicleState.GARAGED, garage, props.fuelLevel, props.engineHealth, props.bodyHealth, json.encode(props), props.plate})

local vehicle = NetworkGetEntityFromNetworkId(netId)
local vehicleId = Entity(vehicle).state.vehicleid
if vehicleId then
outsideVehicles[vehicleId] = nil
end
DeleteEntity(vehicle)
end)

RegisterNetEvent('qb-garage:server:updateVehicleState', function(state, plate, garage)
Expand Down Expand Up @@ -146,10 +147,6 @@ RegisterNetEvent('qb-garage:server:updateVehicleState', function(state, plate, g
end
end)

RegisterNetEvent('qb-garages:server:UpdateOutsideVehicle', function(plate, vehicle)
outsideVehicles[plate] = {netID = vehicle, entity = NetworkGetEntityFromNetworkId(vehicle)}
end)

AddEventHandler('onResourceStart', function(resource)
if resource ~= cache.resource then return end
Wait(100)
Expand Down