From 7c5b02d68953a42a9f901775cdef5ecfaa2ed030 Mon Sep 17 00:00:00 2001 From: Matthew <22198949+mafewtm@users.noreply.github.com> Date: Wed, 8 Jan 2025 23:29:08 -0500 Subject: [PATCH] refactor: vehicles --- client/vehicles.lua | 24 ++++--------- locales/en.json | 4 +-- server/main.lua | 84 ++++++++++++++++++++++----------------------- 3 files changed, 50 insertions(+), 62 deletions(-) diff --git a/client/vehicles.lua b/client/vehicles.lua index 046ceaf..615dd8f 100644 --- a/client/vehicles.lua +++ b/client/vehicles.lua @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/locales/en.json b/locales/en.json index ee8c9ad..9dd8a7d 100644 --- a/locales/en.json +++ b/locales/en.json @@ -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": { diff --git a/server/main.lua b/server/main.lua index 5f46b5d..5f0bfe3 100644 --- a/server/main.lua +++ b/server/main.lua @@ -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) @@ -57,8 +54,6 @@ 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) @@ -66,61 +61,66 @@ lib.callback.register('qbx_police:server:canImpound', function(_, netId) 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) \ No newline at end of file