Skip to content

Commit

Permalink
refactor: vehicles
Browse files Browse the repository at this point in the history
  • Loading branch information
mafewtm authored Jan 9, 2025
1 parent 7d9429c commit 7c5b02d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 62 deletions.
24 changes: 6 additions & 18 deletions client/vehicles.lua
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ local function impound()
},
}) then
local netId = NetworkGetNetworkIdFromEntity(cache.vehicle)
local canBeImpounded = lib.callback.await('qbx_police:server:canImpound', false, netId)
local canImpound = lib.callback.await('qbx_police:server:canImpound', false, netId)

if not canBeImpounded then
if not canImpound then
store(cache.vehicle)
return
end
Expand All @@ -151,13 +151,7 @@ local function impound()
return
end

local impounded = lib.callback.await('qbx_police:server:impoundVehicle', false, netId)

if impounded then
exports.qbx_core:Notify(locale('notify.impounded'), 'success')
else
exports.qbx_core:Notify(locale('notify.failed_impound'), 'error')
end
TriggerServerEvent('qbx_police:server:impoundVehicle', false, netId)
else
exports.qbx_core:Notify(locale('notify.canceled'), 'error')
end
Expand All @@ -170,9 +164,9 @@ local function confiscate()
end

local netId = NetworkGetNetworkIdFromEntity(cache.vehicle)
local canBeConfiscated = lib.callback.await('qbx_police:server:canImpound', false, netId)
local canConfiscate = lib.callback.await('qbx_police:server:canImpound', false, netId)

if not canBeConfiscated then
if not canConfiscate then
exports.qbx_core:Notify(locale('notify.cannot_confiscate'), 'error')
return
end
Expand All @@ -199,13 +193,7 @@ local function confiscate()
return
end

local confiscated = lib.callback.await('qbx_police:server:confiscateVehicle', false, netId)

if confiscated then
exports.qbx_core:Notify(locale('notify.confiscated'), 'success')
else
exports.qbx_core:Notify(locale('notify.failed_confiscate'), 'error')
end
TriggerServerEvent('qbx_police:server:confiscateVehicle', netId)
else
exports.qbx_core:Notify(locale('notify.canceled'), 'error')
end
Expand Down
4 changes: 2 additions & 2 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
"still_occupied": "You cannot do this while there are individuals inside the vehicle...",
"cannot_confiscate": "This vehicle cannot be confiscated...",
"failed_to_retrieve": "Failed to retrieve vehicle...",
"failed_impound": "Failed to impound vehicle...",
"failed_save": "Failed to save vehicle...",
"failed_delete": "Failed to delete vehicle...",
"impounded": "Vehicle impounded!",
"failed_confiscate": "Failed to confiscate vehicle...",
"confiscated": "Vehicle confiscated!"
},
"vehicles": {
Expand Down
84 changes: 42 additions & 42 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ local function registerImpound(impound)
exports.qbx_garages:RegisterGarage(impound.name, impound.lot)
end

---@param source number
---@param vehicle table
---@param spawn vector4
lib.callback.register('qbx_police:server:spawnVehicle', function(source, vehicle, spawn)
local ped = GetPlayerPed(source)

Expand All @@ -57,70 +54,73 @@ lib.callback.register('qbx_police:server:spawnVehicle', function(source, vehicle
return netId
end)

---@param netId number
---@return integer
lib.callback.register('qbx_police:server:canImpound', function(_, netId)
local entity = NetworkGetEntityFromNetworkId(netId)
local plate = GetVehicleNumberPlateText(entity)

return Entity(entity).state.vehicleid or exports.qbx_vehicles:DoesPlayerVehiclePlateExist(plate)
end)

---@param netId integer
---@return boolean
lib.callback.register('qbx_police:server:impoundVehicle', function(_, netId)
local player = exports.qbx_core:GetPlayer(source)

if not player or player.PlayerData.job.type ~= 'police' then return false end

local entity = NetworkGetEntityFromNetworkId(netId)
RegisterNetEvent('QBCore:Server:OnPlayerLoaded', function()
local source = source
local isHandcuffed = exports.qbx_core:GetMetadata(source, 'ishandcuffed')

exports.qbx_vehicles:SaveVehicle(entity, {
garage = 'impoundlot',
state = 2, -- Impounded
})
if isHandcuffed then
Player(source).state:set('handcuffed', true, true)
end
end)

exports.qbx_core:DeleteVehicle(entity)
AddEventHandler('onServerResourceStart', function(resource)
if resource ~= cache.resource then return end

return true
for job, data in pairs(sharedConfig.departments) do
registerArmory(data.armory)
registerPersonalStash(job, data.personalStash)
registerImpound(data.impound)
end
end)

---@param source number
---@param netId integer
---@return boolean
lib.callback.register('qbx_police:server:confiscateVehicle', function(source, netId)
RegisterNetEvent('qbx_police:server:impoundVehicle', function(netId)
local source = source
local player = exports.qbx_core:GetPlayer(source)

if not player or player.PlayerData.job.type ~= 'police' then return false end
if not player or player.PlayerData.job.type ~= 'leo' then return end

local entity = NetworkGetEntityFromNetworkId(netId)
local impound = sharedConfig.departments[player.PlayerData.job.name].impound
local saved, _ = exports.qbx_vehicles:SaveVehicle(entity, { garage = 'impoundlot', state = 2 })

exports.qbx_vehicles:SaveVehicle(entity, {
garage = impound.name,
state = 1, -- Garaged
})
if not saved then
exports.qbx_core:Notify(source, locale('notify.failed_save'), 'error')
return
end

exports.qbx_core:DeleteVehicle(entity)
Wait(500)

return true
if DoesEntityExist(entity) then
exports.qbx_core:Notify(source, locale('notify.failed_delete'), 'error')
end
end)

RegisterNetEvent('QBCore:Server:OnPlayerLoaded', function()
local src = source
local isHandcuffed = exports.qbx_core:GetMetadata(source, 'ishandcuffed')
RegisterNetEvent('qbx_police:server:confiscateVehicle', function(netId)
local source = source
local player = exports.qbx_core:GetPlayer(source)

if isHandcuffed then
Player(src).state:set('handcuffed', true, true)
if not player or player.PlayerData.job.type ~= 'leo' then return end

local entity = NetworkGetEntityFromNetworkId(netId)
local impound = sharedConfig.departments[player.PlayerData.job.name].impound
local saved, _ = exports.qbx_vehicles:SaveVehicle(entity, { garage = impound.name, state = 1 })

if not saved then
exports.qbx_core:Notify(source, locale('notify.failed_save'), 'error')
return
end
end)

AddEventHandler('onServerResourceStart', function(resource)
if resource ~= cache.resource then return end
exports.qbx_core:DeleteVehicle(entity)
Wait(500)

for job, data in pairs(sharedConfig.departments) do
registerArmory(data.armory)
registerPersonalStash(job, data.personalStash)
registerImpound(data.impound)
if DoesEntityExist(entity) then
exports.qbx_core:Notify(source, locale('notify.failed_delete'), 'error')
end
end)

0 comments on commit 7c5b02d

Please sign in to comment.