Skip to content

Commit

Permalink
refactor!: cleanup (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
Manason authored Oct 26, 2024
1 parent 7dea3f4 commit c6f5096
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 443 deletions.
16 changes: 9 additions & 7 deletions client/carjack.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
local config = require 'config.client'
local sharedFunctions = require 'shared.functions'

local getIsCloseToCoords = sharedFunctions.getIsCloseToCoords
local getIsBlacklistedWeapon = sharedFunctions.getIsBlacklistedWeapon
local getIsVehicleCarjackingImmune = sharedFunctions.getIsVehicleCarjackingImmune

local function getNPCPedsInVehicle(vehicle)
local otherPeds = {}
Expand Down Expand Up @@ -110,6 +105,13 @@ local function carjackVehicle(driver, vehicle)
isCarjacking = false
end

---Checks if the weapon cannot be used to steal keys from drivers.
---@param weaponHash number The current weapon hash.
---@return boolean `true` if the weapon cannot be used to carjacking, `false` otherwise.
local function getIsBlacklistedWeapon(weaponHash)
return qbx.array.contains(config.noCarjackWeapons, weaponHash)
end

local isWatchCarjackingAttemptRunning = false
local function watchCarjackingAttempts()
if isWatchCarjackingAttemptRunning then return end
Expand All @@ -126,8 +128,8 @@ local function watchCarjackingAttempts()
local targetveh = GetVehiclePedIsIn(target, false)

if GetPedInVehicleSeat(targetveh, -1) == target
and not getIsVehicleCarjackingImmune(targetveh)
and getIsCloseToCoords(GetEntityCoords(cache.ped), GetEntityCoords(target), 5.0)
and not GetVehicleConfig(targetveh).carjackingImmune
and #(GetEntityCoords(cache.ped) - GetEntityCoords(target)) < 5.0
then
carjackVehicle(target, targetveh)
end
Expand Down
9 changes: 4 additions & 5 deletions client/functions.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local config = require 'config.client'
local functions = require 'shared.functions'

---Grants keys for job shared vehicles
---@param vehicle number The entity number of the vehicle.
Expand All @@ -8,7 +7,7 @@ function 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
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))
return jobInfo.vehicles and jobInfo.vehicles[GetEntityModel(vehicle)] or jobInfo.classes and jobInfo.classes[GetVehicleClass(vehicle)]
Expand Down Expand Up @@ -86,7 +85,7 @@ end
local function getIsCloseToAnyBone(coords, entity, bones, maxDistance)
for i = 1, #bones do
local boneCoords = getBoneCoords(entity, bones[i])
if functions.getIsCloseToCoords(coords, boneCoords, maxDistance) then
if #(coords - boneCoords) < maxDistance then
return true
end
end
Expand Down Expand Up @@ -116,7 +115,7 @@ end
---@param vehicle number
local function breakLockpick(isAdvancedLockedpick, vehicle)
local chance = math.random()
local vehicleConfig = functions.getVehicleConfig(vehicle)
local vehicleConfig = GetVehicleConfig(vehicle)
if isAdvancedLockedpick then -- there is no benefit to using an advanced tool in the default configuration.
if chance <= vehicleConfig.removeAdvancedLockpickChance then
TriggerServerEvent("qb-vehiclekeys:server:breakLockpick", "advancedlockpick")
Expand Down Expand Up @@ -173,7 +172,7 @@ function LockpickDoor(isAdvancedLockedpick, maxDistance, customChallenge)
--- player may attempt to open the lock if:
if not isDriverSeatFree -- no one in the driver's seat
or not getIsCloseToAnyBone(pedCoords, vehicle, doorBones, maxDistance) -- the player's ped is close enough to the driver's door
or functions.getIsVehicleLockpickImmune(vehicle)
or GetVehicleConfig(vehicle).lockpickImmune
then return end

local skillCheckConfig = config.skillCheck[isAdvancedLockedpick and 'advancedLockpick' or 'lockpick']
Expand Down
68 changes: 33 additions & 35 deletions client/main.lua
Original file line number Diff line number Diff line change
@@ -1,40 +1,22 @@
-----------------------
---- Imports ----
-----------------------

local config = require 'config.client'
local sharedFunctions = require 'shared.functions'

local getIsVehicleInitiallyLocked = sharedFunctions.getIsVehicleInitiallyLocked
local getIsVehicleShared = sharedFunctions.getIsVehicleShared
local getIsVehicleAlwaysUnlocked = sharedFunctions.getIsVehicleAlwaysUnlocked
local getIsVehicleCarjackingImmune = sharedFunctions.getIsVehicleCarjackingImmune

-----------------------
---- Functions ----
-----------------------

---manages the opening of locks
---@param vehicle number? The entity number of the vehicle.
---@param state boolean? State of the vehicle lock.
---@param anim any Animation
local function setVehicleDoorLock(vehicle, state, anim)
if not vehicle or getIsVehicleAlwaysUnlocked(vehicle) or getIsVehicleShared(vehicle) then return end
---client uses key fob to toggle the door locks
---@param vehicle number The entity number of the vehicle.
local function toggleLock(vehicle)
if not vehicle then return end
local vehicleConfig = GetVehicleConfig(vehicle)
if vehicleConfig.noLock or vehicleConfig.shared then return end
if GetIsVehicleAccessible(vehicle) then

if anim then
lib.playAnim(cache.ped, 'anim@mp_player_intmenu@key_fob@', 'fob_click', 3.0, 3.0, -1, 49)
end
lib.playAnim(cache.ped, 'anim@mp_player_intmenu@key_fob@', 'fob_click', 3.0, 3.0, -1, 49)

--- if the statebag is out of sync, rely on it as the source of truth and sync the client to the statebag's value
local stateBagValue = Entity(vehicle).state.doorslockstate
if GetVehicleDoorLockStatus(vehicle) ~= stateBagValue then
SetVehicleDoorsLocked(vehicle, stateBagValue)
end

local lockstate = state ~= nil
and (state and 2 or 1)
or (GetVehicleDoorLockStatus(vehicle) % 2) + 1 -- use ternary
local lockstate = (GetVehicleDoorLockStatus(vehicle) % 2) + 1

TriggerServerEvent('qb-vehiclekeys:server:setVehLockState', NetworkGetNetworkIdFromEntity(vehicle), lockstate)
exports.qbx_core:Notify(locale(lockstate == 2 and 'notify.vehicle_locked' or 'notify.vehicle_unlocked'))
Expand All @@ -52,8 +34,6 @@ local function setVehicleDoorLock(vehicle, state, anim)
end
end

exports('SetVehicleDoorLock', setVehicleDoorLock)

---if the player does not have ignition access to the vehicle:
---check whether to give keys if engine is on
---disable the engine and listen for search keys if applicable to the vehicle
Expand All @@ -68,7 +48,8 @@ local function onEnteringDriverSeat()
Wait(0)
end
end)
if getIsVehicleShared(vehicle) then return end
local vehicleConfig = GetVehicleConfig(vehicle)
if vehicleConfig.shared then return end

local isVehicleAccessible = GetIsVehicleAccessible(vehicle)
if isVehicleAccessible then return end
Expand All @@ -95,7 +76,7 @@ local function onEnteringDriverSeat()
DisableKeySearch()
end)

if sharedFunctions.getVehicleConfig(vehicle).findKeysChance ~= 0.0 then
if vehicleConfig.findKeysChance ~= 0.0 then
EnableKeySearch()
end
end
Expand All @@ -118,7 +99,7 @@ togglelocksBind = lib.addKeybind({
onPressed = function()
togglelocksBind:disable(true)
local vehicle = lib.getClosestVehicle(GetEntityCoords(cache.ped), config.vehicleMaximumLockingDistance, true)
setVehicleDoorLock(vehicle, nil, true)
toggleLock(vehicle)
Wait(1000)
togglelocksBind:disable(false)
end
Expand Down Expand Up @@ -176,12 +157,12 @@ EngineBind = lib.addKeybind({
local isTakingKeys = false
RegisterNetEvent('QBCore:Client:VehicleInfo', function(data)
if not LocalPlayer.state.isLoggedIn or data.event ~= 'Entering' then return end
if getIsVehicleAlwaysUnlocked(data.vehicle) or isTakingKeys then return end
local vehicleConfig = GetVehicleConfig(data.vehicle)
if vehicleConfig.noLock or isTakingKeys then return end
isTakingKeys = true
local isVehicleImmune = getIsVehicleCarjackingImmune(data.vehicle)
local driver = GetPedInVehicleSeat(data.vehicle, -1)

if driver ~= 0 and IsEntityDead(driver) and not (isVehicleImmune or IsPedAPlayer(driver)) then
if driver ~= 0 and IsEntityDead(driver) and not (vehicleConfig.carjackingImmune or IsPedAPlayer(driver)) then
TriggerServerEvent('qb-vehiclekeys:server:setVehLockState', data.netId, 1)
if lib.progressCircle({
duration = 2500,
Expand Down Expand Up @@ -249,13 +230,30 @@ AddEventHandler('onResourceStart', function(resourceName)
end
end)

---Checks the vehicle is always locked at spawn.
---@param vehicle number The entity number of the vehicle.
---@param isDriven boolean
---@return boolean `true` if the vehicle is locked, `false` otherwise.
local function getIsVehicleInitiallyLocked(vehicle, isDriven)
local vehicleConfig = GetVehicleConfig(vehicle)
local vehicleLockedChance = isDriven
and vehicleConfig.spawnLockedIfDriven
or vehicleConfig.spawnLockedIfParked

if type(vehicleLockedChance) == 'number' then
return math.random() < vehicleLockedChance
else
return vehicleLockedChance == true
end
end

local function onVehicleAttemptToEnter(vehicle)
if Entity(vehicle).state.doorslockstate then return end

local ped = GetPedInVehicleSeat(vehicle, -1)
if IsPedAPlayer(ped) then return end

local isLocked = not getIsVehicleAlwaysUnlocked(vehicle) and getIsVehicleInitiallyLocked(vehicle, ped and ped ~= 0)
local isLocked = not GetVehicleConfig(vehicle).noLock and getIsVehicleInitiallyLocked(vehicle, ped and ped ~= 0)
local lockState = isLocked and 2 or 1
SetVehicleDoorsLocked(vehicle, lockState)
TriggerServerEvent('qb-vehiclekeys:server:setVehLockState', NetworkGetNetworkIdFromEntity(vehicle), lockState)
Expand Down
4 changes: 1 addition & 3 deletions client/searchkeys.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
local config = require 'config.client'
local sharedFunctions = require 'shared.functions'

local isSearchLocked = false
local isSearchAllowed = false

local function setSearchLabelState(isAllowed)
if isSearchLocked and isAllowed then return end
if isAllowed and cache.vehicle and sharedFunctions.getVehicleConfig(cache.vehicle).findKeysChance == 0.0 then
if isAllowed and cache.vehicle and GetVehicleConfig(cache.vehicle).findKeysChance == 0.0 then
isSearchAllowed = false
return
end
Expand Down
Loading

0 comments on commit c6f5096

Please sign in to comment.