Skip to content

Commit

Permalink
refactor(client): split searchkeys into its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
Manason committed Sep 28, 2024
1 parent 252430f commit 4d57e1d
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 90 deletions.
95 changes: 5 additions & 90 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,66 +47,6 @@ end

exports('SetVehicleDoorLock', setVehicleDoorLock)

local function findKeys(vehicleModel, vehicleClass, vehicle)
local hotwireTime = math.random(config.minKeysSearchTime, config.maxKeysSearchTime)

local anim = config.anims.lockpick.model[vehicleModel]
or config.anims.lockpick.model[vehicleClass]
or config.anims.lockpick.default

local searchingForKeys = true
CreateThread(function()
while searchingForKeys do
if not IsEntityPlayingAnim(cache.ped, anim.dict, anim.clip, 49) then
lib.playAnim(cache.ped, anim.dict, anim.clip, 3.0, 1.0, -1, 49)
end
Wait(100)
end
end)
if lib.progressCircle({
duration = hotwireTime,
label = locale('progress.searching_keys'),
position = 'bottom',
useWhileDead = false,
canCancel = true,
anim = anim,
disable = {
move = true,
car = true,
combat = true,
}
}) then
searchingForKeys = false
local success = lib.callback.await('qbx_vehiclekeys:server:findKeys', false, VehToNet(vehicle))
if not success then
TriggerServerEvent('hud:server:GainStress', math.random(1, 4))
exports.qbx_core:Notify(locale("notify.failed_keys"), 'error')
end
return success
end
searchingForKeys = false
end

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
isSearchAllowed = false
return
end
local isOpen, text = lib.isTextUIOpen()
local newText = locale('info.search_keys_dispatch')
local isValidMessage = text and text == newText
if isAllowed and not isValidMessage and cache.seat == -1 then
lib.showTextUI(newText)
elseif (not isAllowed or cache.seat ~= -1) and isOpen and isValidMessage then
lib.hideTextUI()
end

isSearchAllowed = isAllowed and cache.seat == -1
end

---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 Down Expand Up @@ -136,11 +76,11 @@ local function onEnteringDriverSeat()
if lib.progressActive() then
lib.cancelProgress()
end
setSearchLabelState(false)
DisableKeySearch()
end)

if sharedFunctions.getVehicleConfig(vehicle).findKeysChance ~= 0.0 then
setSearchLabelState(true)
EnableKeySearch()
end
end

Expand Down Expand Up @@ -189,31 +129,6 @@ engineBind = lib.addKeybind({
end
})

lib.addKeybind({
name = 'searchkeys',
description = locale('info.search_keys'),
defaultKey = 'H',
secondaryMapper = 'PAD_DIGITALBUTTONANY',
secondaryKey = 'LRIGHT_INDEX',
onPressed = function()
if isSearchAllowed and cache.vehicle then
isSearchLocked = true
setSearchLabelState(false)
local vehicle = cache.vehicle
local isFound
if not GetIsVehicleAccessible(vehicle) then
isFound = findKeys(GetEntityModel(vehicle), GetVehicleClass(vehicle), vehicle)
SetTimeout(10000, function()
SendPoliceAlertAttempt('steal')
end)
end
Wait(config.timeBetweenHotwires)
isSearchLocked = false
setSearchLabelState(not isFound)
end
end
})

-----------------------
---- Client Events ----
-----------------------
Expand Down Expand Up @@ -247,10 +162,10 @@ end)
RegisterNetEvent('lockpicks:UseLockpick', function(isAdvanced)
local vehicle = cache.vehicle
if vehicle then
if isSearchAllowed then
setSearchLabelState(false)
if GetKeySearchEnabled() then
DisableKeySearch()
Hotwire(vehicle, isAdvanced)
setSearchLabelState(true)
EnableKeySearch()
end
else
LockpickDoor(isAdvanced)
Expand Down
103 changes: 103 additions & 0 deletions client/searchkeys.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
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
isSearchAllowed = false
return
end
local isOpen, text = lib.isTextUIOpen()
local newText = locale('info.search_keys_dispatch')
local isValidMessage = text and text == newText
if isAllowed and not isValidMessage and cache.seat == -1 then
lib.showTextUI(newText)
elseif (not isAllowed or cache.seat ~= -1) and isOpen and isValidMessage then
lib.hideTextUI()
end

isSearchAllowed = isAllowed and cache.seat == -1
end

local function findKeys(vehicleModel, vehicleClass, vehicle)
local hotwireTime = math.random(config.minKeysSearchTime, config.maxKeysSearchTime)

local anim = config.anims.lockpick.model[vehicleModel]
or config.anims.lockpick.model[vehicleClass]
or config.anims.lockpick.default

local searchingForKeys = true
CreateThread(function()
while searchingForKeys do
if not IsEntityPlayingAnim(cache.ped, anim.dict, anim.clip, 49) then
lib.playAnim(cache.ped, anim.dict, anim.clip, 3.0, 1.0, -1, 49)
end
Wait(100)
end
end)
if lib.progressCircle({
duration = hotwireTime,
label = locale('progress.searching_keys'),
position = 'bottom',
useWhileDead = false,
canCancel = true,
anim = anim,
disable = {
move = true,
car = true,
combat = true,
}
}) then
searchingForKeys = false
local success = lib.callback.await('qbx_vehiclekeys:server:findKeys', false, VehToNet(vehicle))
if not success then
TriggerServerEvent('hud:server:GainStress', math.random(1, 4))
exports.qbx_core:Notify(locale("notify.failed_keys"), 'error')
end
return success
end
searchingForKeys = false
end

local searchKeysKeybind = lib.addKeybind({
name = 'searchkeys',
description = locale('info.search_keys'),
defaultKey = 'H',
secondaryMapper = 'PAD_DIGITALBUTTONANY',
secondaryKey = 'LRIGHT_INDEX',
disabled = true,
onPressed = function()
if isSearchAllowed and cache.vehicle then
isSearchLocked = true
setSearchLabelState(false)
local vehicle = cache.vehicle
local isFound
if not GetIsVehicleAccessible(vehicle) then
isFound = findKeys(GetEntityModel(vehicle), GetVehicleClass(vehicle), vehicle)
SetTimeout(10000, function()
SendPoliceAlertAttempt('steal')
end)
end
Wait(config.timeBetweenHotwires)
isSearchLocked = false
setSearchLabelState(not isFound)
end
end
})

function GetKeySearchEnabled()
return isSearchAllowed
end

function EnableKeySearch()
setSearchLabelState(true)
searchKeysKeybind:disable(false)
end

function DisableKeySearch()
setSearchLabelState(false)
searchKeysKeybind:disable(true)
end
1 change: 1 addition & 0 deletions fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ shared_scripts {
client_scripts {
'@qbx_core/modules/playerdata.lua',
'client/functions.lua',
'client/searchkeys.lua',
'client/main.lua',
'client/carjack.lua',
'bridge/qb/client.lua',
Expand Down

0 comments on commit 4d57e1d

Please sign in to comment.