Skip to content

Commit

Permalink
refactor: single responsibility of getPlayersVehiclePlate chore: anno…
Browse files Browse the repository at this point in the history
…tations unification
  • Loading branch information
artur-michalak committed May 11, 2024
1 parent fd82c7e commit b9cff55
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 41 deletions.
48 changes: 24 additions & 24 deletions client/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ end
exports('HasKeys', public.hasKeys)

--- Checking weapon on the blacklist.
--- @return boolean? `true` if the vehicle is blacklisted, `nil` otherwise.
---@return boolean? `true` if the vehicle is blacklisted, `nil` otherwise.
function public.isBlacklistedWeapon()
local weapon = GetSelectedPedWeapon(cache.ped)

Expand All @@ -58,8 +58,8 @@ function public.isBlacklistedWeapon()
end

--- Checking vehicle on the blacklist.
--- @param vehicle number The entity number of the vehicle.
--- @return boolean? `true` if the vehicle is blacklisted, `nil` otherwise.
---@param vehicle number The entity number of the vehicle.
---@return boolean? `true` if the vehicle is blacklisted, `nil` otherwise.
function public.isBlacklistedVehicle(vehicle)
if Entity(vehicle).state.ignoreLocks or GetVehicleClass(vehicle) == 13 then return true end

Expand Down Expand Up @@ -89,9 +89,9 @@ function public.attemptPoliceAlert(type)
end

--- Gets bone coords
--- @param entity number The entity index.
--- @param boneName string The entity bone name.
--- @return vector3 `Bone coords` if exists, `entity coords` otherwise.
---@param entity number The entity index.
---@param boneName string The entity bone name.
---@return vector3 `Bone coords` if exists, `entity coords` otherwise.
local function getBoneCoords(entity, boneName)
local boneIndex = GetEntityBoneIndexByName(entity, boneName)

Expand All @@ -103,11 +103,11 @@ local function getBoneCoords(entity, boneName)
end

--- checks if any of the bones are close enough to the coords
--- @param coords vector3
--- @param entity number
--- @param bones table
--- @param maxDistance number
--- @return boolean? `true` if bone exists, `nil` otherwise.
---@param coords vector3
---@param entity number
---@param bones table
---@param maxDistance number
---@return boolean? `true` if bone exists, `nil` otherwise.
local function isCloseToAnyBone(coords, entity, bones, maxDistance)
for _, boneName in ipairs(bones) do
local boneCoords = getBoneCoords(entity, boneName)
Expand All @@ -120,9 +120,9 @@ end
local doorBones = {'door_dside_f', 'door_dside_r', 'door_pside_f', 'door_pside_r'}

--- Checking whether the character is close enough to the vehicle driver door.
--- @param vehicle number The entity number of the vehicle.
--- @param maxDistance number The max distance to check.
--- @return boolean? `true` if the player ped is next to an open vehicle, `nil` otherwise.
---@param vehicle number The entity number of the vehicle.
---@param maxDistance number The max distance to check.
---@return boolean? `true` if the player ped is next to an open vehicle, `nil` otherwise.
local function isVehicleInRange(vehicle, maxDistance)
local vehicles = GetGamePool('CVehicle')
local pedCoords = GetEntityCoords(cache.ped)
Expand All @@ -137,8 +137,8 @@ local function isVehicleInRange(vehicle, maxDistance)
end

--- Will be execuded when the opening of the lock succeeds.
--- @param vehicle number The entity number of the vehicle.
--- @param plate string The plate number of the vehicle.
---@param vehicle number The entity number of the vehicle.
---@param plate string The plate number of the vehicle.
local function lockpickSuccessCallback(vehicle, plate)
TriggerServerEvent('hud:server:GainStress', math.random(1, 4))

Expand All @@ -152,11 +152,11 @@ local function lockpickSuccessCallback(vehicle, plate)
end

--- Operations done after the LockpickDoor quickevent done.
--- @param vehicle number The entity number of the vehicle.
--- @param plate string The plate number of the vehicle.
--- @param isAdvancedLockedpick boolean Determines whether an advanced lockpick was used.
--- @param maxDistance number The max distance to check.
--- @param isSuccess boolean? Determines whether the lock has been successfully opened.
---@param vehicle number The entity number of the vehicle.
---@param plate string The plate number of the vehicle.
---@param isAdvancedLockedpick boolean Determines whether an advanced lockpick was used.
---@param maxDistance number The max distance to check.
---@param isSuccess boolean? Determines whether the lock has been successfully opened.
local function lockpickCallback(vehicle, plate, isAdvancedLockedpick, maxDistance, isSuccess)
if not isVehicleInRange(vehicle, maxDistance) then return end -- the action will be aborted if the opened vehicle is too far.
if isSuccess then
Expand All @@ -183,9 +183,9 @@ end

local islockpickingProcessLocked = false -- lock flag
--- Lockpicking quickevent.
--- @param isAdvancedLockedpick boolean Determines whether an advanced lockpick was used
--- @param maxDistance number? The max distance to check.
--- @param customChallenge boolean? lockpick challenge
---@param isAdvancedLockedpick boolean Determines whether an advanced lockpick was used
---@param maxDistance number? The max distance to check.
---@param customChallenge boolean? lockpick challenge
function public.lockpickDoor(isAdvancedLockedpick, maxDistance, customChallenge)
maxDistance = maxDistance or 2
local pedCoords = GetEntityCoords(cache.ped)
Expand Down
23 changes: 14 additions & 9 deletions server/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@ lib.addCommand('givekeys', {
TriggerClientEvent('qb-vehiclekeys:client:GiveKeys', source, args.id)
end)

local function getVehicleKeysParams(source, args)
local playerId = args.target or source
local plate = args.plate

--- Gets the plate of the vehicle in which the executor is, if args.plate is nil
---@param source number ID of the player
---@param plate string? The plate number of the vehicle.
---@return string?
local function getPlayersVehiclePlate(source, plate)
if not plate then
local ped = GetPlayerPed(source)
local vehicle = GetVehiclePedIsIn(ped, false)

if vehicle == 0 then return end
plate = GetVehicleNumberPlateText(vehicle)
end

return playerId, plate
return plate
end

lib.addCommand('addkeys', {
Expand All @@ -44,9 +47,10 @@ lib.addCommand('addkeys', {
},
restricted = 'group.admin',
}, function (source, args)
local playerId, plate = getVehicleKeysParams(source, args)
local playerId = args.target or source
local plate = getPlayersVehiclePlate(source, args.plate)

if not playerId or plate == 0 then
if not playerId or not plate then
return exports.qbx_core:Notify(source, locale('notify.fpid'), 'error')
end

Expand All @@ -71,9 +75,10 @@ lib.addCommand('removekeys', {
},
restricted = 'group.admin',
}, function (source, args)
local playerId, plate = getVehicleKeysParams(source, args)
local playerId = args.target or source
local plate = getPlayersVehiclePlate(source, args.plate)

if not playerId or plate == 0 then
if not playerId or not plate then
return exports.qbx_core:Notify(source, locale('cnotify.fpid'), 'error')
end

Expand Down
8 changes: 4 additions & 4 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ end)
-----------------------

--- Gives the user the keys to the vehicle
--- @param source number ID of the player
--- @param plate string The plate number of the vehicle.
---@param source number ID of the player
---@param plate string The plate number of the vehicle.
function GiveKeys(source, plate)
local citizenid = getCitizenId(source)

Expand Down Expand Up @@ -91,8 +91,8 @@ end)
exports('GiveKeys', GiveKeys)

--- Removing the vehicle keys from the user
--- @param source number ID of the player
--- @param plate string The plate number of the vehicle.
---@param source number ID of the player
---@param plate string The plate number of the vehicle.
function RemoveKeys(source, plate)
local citizenid = getCitizenId(source)

Expand Down
8 changes: 4 additions & 4 deletions shared/functions.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
local public = {}

--- Checks if the given two coordinates are close to each other based on distance.
--- @param coord1 vector3[] The first set of coordinates.
--- @param coord2 vector3[] The second set of coordinates.
--- @param distance number The maximum allowed distance for them to be considered close.
--- @return boolean true if the distance between two entities is less than the distance parameter.
---@param coord1 vector3[] The first set of coordinates.
---@param coord2 vector3[] The second set of coordinates.
---@param distance number The maximum allowed distance for them to be considered close.
---@return boolean true if the distance between two entities is less than the distance parameter.
function public.isCloseToCoords(coord1, coord2, distance)
return #(coord1 - coord2) < distance
end
Expand Down

0 comments on commit b9cff55

Please sign in to comment.