From 5eeb76a5de318091150a01b949f0ae1f36f8b97c Mon Sep 17 00:00:00 2001 From: ArturMichalak Date: Sat, 18 May 2024 11:47:54 +0200 Subject: [PATCH] feat: vehicleId statebag on vehicle spawn, safety of plate generation refactor: rename generatePlate to generateUniquePlate chore: MySQL to lint --- .vscode/settings.json | 3 ++- client/main.lua | 4 ++-- server/main.lua | 35 ++++++++++++++++++----------------- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 1f091d0..4e9be72 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -6,6 +6,7 @@ "cache", "QBX", "locale", - "qbx" + "qbx", + "MySQL" ] } diff --git a/client/main.lua b/client/main.lua index 5e16b85..0d6f6a9 100644 --- a/client/main.lua +++ b/client/main.lua @@ -632,9 +632,9 @@ end) --- Buys the selected vehicle ---@param vehicle number ---@param plate string -RegisterNetEvent('qbx_vehicleshop:client:buyShowroomVehicle', function(vehicle, plate) +RegisterNetEvent('qbx_vehicleshop:client:buyShowroomVehicle', function(vehicle, plate, vehicleId) local tempShop = insideShop -- temp hacky way of setting the shop because it changes after the callback has returned since you are outside the zone - local netId = lib.callback.await('qbx_vehicleshop:server:spawnVehicle', false, vehicle, config.shops[tempShop].vehicleSpawn, plate) + local netId = lib.callback.await('qbx_vehicleshop:server:spawnVehicle', false, vehicle, config.shops[tempShop].vehicleSpawn, plate, vehicleId) local veh = NetToVeh(netId) local props = lib.getVehicleProperties(veh) props.plate = plate diff --git a/server/main.lua b/server/main.lua index c3b5c9f..bcbd163 100644 --- a/server/main.lua +++ b/server/main.lua @@ -79,12 +79,12 @@ local function calculateNewFinance(paymentAmount, vehData) return qbx.math.round(newBalance), qbx.math.round(newPayment), newPaymentsLeft end -local function generatePlate() - local plate - repeat - plate = qbx.generateRandomPlate('11AAA111') - until not DoesVehicleEntityExist(plate) - return plate:upper() +local function generateUniquePlate() + while true do + local plate = qbx.generateRandomPlate('11AAA111') + if not DoesVehicleEntityExist(plate) then return plate end + Wait(0) + end end -- Callbacks @@ -106,11 +106,13 @@ lib.callback.register('qbx_vehicleshop:server:GetVehiclesByName', function(sourc end end) -lib.callback.register('qbx_vehicleshop:server:spawnVehicle', function(source, model, coords, plate) +lib.callback.register('qbx_vehicleshop:server:spawnVehicle', function(source, model, coords, plate, vehicleId) local netId, veh = qbx.spawnVehicle({model = model, spawnSource = coords, warp = GetPlayerPed(source)}) if not netId or netId == 0 then return end if not veh or veh == 0 then return end + if vehicleId then Entity(veh).state:set('vehicleid', vehicleId, false) end + SetVehicleNumberPlateText(veh, plate) TriggerClientEvent('vehiclekeys:client:SetOwner', source, plate) return netId @@ -239,18 +241,17 @@ RegisterNetEvent('qbx_vehicleshop:server:buyShowroomVehicle', function(vehicle) local vehiclePrice = coreVehicles[vehicle].price local currencyType = findChargeableCurrencyType(vehiclePrice, player.PlayerData.money.cash, player.PlayerData.money.bank) if not currencyType then - exports.qbx_core:Notify(src, locale('error.notenoughmoney'), 'error') - return + return exports.qbx_core:Notify(src, locale('error.notenoughmoney'), 'error') end - local plate = generatePlate() - exports.qbx_vehicles:CreateVehicleEntity({ + local plate = generateUniquePlate() + local vehicleId = exports.qbx_vehicles:CreateVehicleEntity({ citizenId = player.PlayerData.citizenid, model = vehicle, plate = plate, }) exports.qbx_core:Notify(src, locale('success.purchased'), 'success') - TriggerClientEvent('qbx_vehicleshop:client:buyShowroomVehicle', src, vehicle, plate) + TriggerClientEvent('qbx_vehicleshop:client:buyShowroomVehicle', src, vehicle, plate, vehicleId) player.Functions.RemoveMoney(currencyType, vehiclePrice, 'vehicle-bought-in-showroom') end) @@ -279,7 +280,7 @@ RegisterNetEvent('qbx_vehicleshop:server:financeVehicle', function(downPayment, return exports.qbx_core:Notify(src, locale('error.notenoughmoney'), 'error') end - local plate = generatePlate() + local plate = generateUniquePlate() local balance, vehPaymentAmount = calculateFinance(vehiclePrice, downPayment, paymentAmount) local cid = player.PlayerData.citizenid local timer = (config.finance.paymentInterval * 60) @@ -342,17 +343,17 @@ RegisterNetEvent('qbx_vehicleshop:server:sellShowroomVehicle', function(data, pl local vehicle = data local vehiclePrice = coreVehicles[vehicle].price local cid = target.PlayerData.citizenid - local plate = generatePlate() + local plate = generateUniquePlate() if not sellShowroomVehicleTransact(src, target, vehiclePrice, vehiclePrice) then return end - exports.qbx_vehicles:CreateVehicleEntity({ + local vehicleId = exports.qbx_vehicles:CreateVehicleEntity({ citizenId = cid, model = vehicle, plate = plate }) - TriggerClientEvent('qbx_vehicleshop:client:buyShowroomVehicle', target.PlayerData.source, vehicle, plate) + TriggerClientEvent('qbx_vehicleshop:client:buyShowroomVehicle', target.PlayerData.source, vehicle, plate, vehicleId) end) -- Finance vehicle to customer @@ -385,7 +386,7 @@ RegisterNetEvent('qbx_vehicleshop:server:sellfinanceVehicle', function(downPayme local cid = target.PlayerData.citizenid local timer = (config.finance.paymentInterval * 60) - local plate = generatePlate() + local plate = generateUniquePlate() local balance, vehPaymentAmount = calculateFinance(vehiclePrice, downPayment, paymentAmount) if not sellShowroomVehicleTransact(src, target, vehiclePrice, downPayment) then return end