From 16f9210cb67602fc33e4459d65fd0fdc7fdfa4fb Mon Sep 17 00:00:00 2001 From: HC DEV <36608116+ArturMichalak@users.noreply.github.com> Date: Tue, 21 May 2024 18:25:18 +0200 Subject: [PATCH] refactor: areKeysJobShared * feat: refactor of the areKeysJobShared * feat: lockpickDoor plate assert --- client/functions.lua | 26 ++++++++++++++++++++++++-- client/main.lua | 23 +---------------------- config/client.lua | 10 +++++----- 3 files changed, 30 insertions(+), 29 deletions(-) diff --git a/client/functions.lua b/client/functions.lua index 4c99900..38f8f1b 100644 --- a/client/functions.lua +++ b/client/functions.lua @@ -203,9 +203,10 @@ function public.lockpickDoor(isAdvancedLockedpick, maxDistance, customChallenge) local plate = qbx.getVehiclePlate(vehicle) local isDriverSeatFree = IsVehicleSeatFree(vehicle, -1) + assert(plate, 'Vehicle has no plate') + --- player may attempt to open the lock if: - if not plate - or not isDriverSeatFree -- no one in the driver's seat + if not isDriverSeatFree -- no one in the driver's seat or public.hasKeys(plate) -- player does not have keys to the vehicle or Entity(vehicle).state.isOpen -- the lock is locked or not isCloseToAnyBone(pedCoords, vehicle, doorBones, maxDistance) -- the player's ped is close enough to the driver's door @@ -242,4 +243,25 @@ function public.getVehicleByPlate(plate) end end +---Grants keys for job shared vehicles +---@param vehicle number The entity number of the vehicle. +---@return boolean? `true` if the vehicle is shared for a player's job, `nil` otherwise. +function public.areKeysJobShared(vehicle) + local job = QBX.PlayerData.job.name + local jobInfo = config.sharedKeys[job] + + if not jobInfo or (jobInfo.requireOnduty and not QBX.PlayerData.job.onduty) then return end + + assert(jobInfo.vehicles, string.format('Vehicles not configured for the %s job.', job)) + + if not jobInfo.vehicles[GetEntityModel(vehicle)] then return end + + local vehPlate = qbx.getVehiclePlate(vehicle) + if not public.hasKeys(vehPlate) then + TriggerServerEvent('qb-vehiclekeys:server:AcquireVehicleKeys', vehPlate) + end + + return true +end + return public diff --git a/client/main.lua b/client/main.lua index dfe87aa..fb57080 100644 --- a/client/main.lua +++ b/client/main.lua @@ -11,6 +11,7 @@ local attemptPoliceAlert = functions.attemptPoliceAlert local isBlacklistedWeapon = functions.isBlacklistedWeapon local isBlacklistedVehicle = functions.isBlacklistedVehicle local getVehicleByPlate = functions.getVehicleByPlate +local areKeysJobShared = functions.areKeysJobShared ----------------------- ---- Variables ---- @@ -65,28 +66,6 @@ local function getVehicle() return vehicle end -local function areKeysJobShared(veh) - local vehName = GetDisplayNameFromVehicleModel(GetEntityModel(veh)) - local vehPlate = qbx.getVehiclePlate(veh) - for job, v in pairs(config.sharedKeys) do - if job == QBX.PlayerData.job.name then - if config.sharedKeys[job].requireOnduty and not QBX.PlayerData.job.onduty then return false end - - for _, vehicle in ipairs(v.vehicles) do - if string.upper(vehicle) == string.upper(vehName) then - if not hasKeys(vehPlate) then - TriggerServerEvent('qb-vehiclekeys:server:AcquireVehicleKeys', vehPlate) - end - - return true - end - end - end - end - - return false -end - ---manages the opening of locks ---@param vehicle number? The entity number of the vehicle. ---@param state boolean? State of the vehicle lock. diff --git a/config/client.lua b/config/client.lua index 1118aee..9af00b7 100644 --- a/config/client.lua +++ b/config/client.lua @@ -158,17 +158,17 @@ return { -- Job Settings sharedKeys = { -- Share keys amongst employees. Employees can lock/unlock any job-listed vehicle - ['police'] = { -- Job name + police = { -- Job name requireOnduty = false, vehicles = { - 'police', -- Vehicle model - 'police2', -- Vehicle model + [`police`] = true, -- Vehicle model + [`police2`] = true, -- Vehicle model } }, - ['mechanic'] = { + mechanic = { requireOnduty = false, vehicles = { - 'towtruck', + [`towtruck`] = true, } } },