Skip to content

Commit

Permalink
refactor: areKeysJobShared
Browse files Browse the repository at this point in the history
* feat: refactor of the areKeysJobShared

* feat: lockpickDoor plate assert
  • Loading branch information
artur-michalak authored May 21, 2024
1 parent 37b2a82 commit 16f9210
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
26 changes: 24 additions & 2 deletions client/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
23 changes: 1 addition & 22 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 ----
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 5 additions & 5 deletions config/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
},
Expand Down

0 comments on commit 16f9210

Please sign in to comment.