Skip to content

Commit

Permalink
refactor: areKeysJobShared
Browse files Browse the repository at this point in the history
  • Loading branch information
artur-michalak committed May 19, 2024
1 parent 37b2a82 commit a47a2a2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
21 changes: 21 additions & 0 deletions client/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,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 a47a2a2

Please sign in to comment.