Skip to content

Commit 5be1faa

Browse files
feat: carjacking watcher to ox_lib:cache:weapon event handler
1 parent c42c2e5 commit 5be1faa

File tree

2 files changed

+62
-52
lines changed

2 files changed

+62
-52
lines changed

client/functions.lua

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ exports('HasKeys', public.hasKeys)
1818
---Checking weapon on the blacklist.
1919
---@return boolean? `true` if the vehicle is blacklisted, `nil` otherwise.
2020
function public.isBlacklistedWeapon()
21-
local weapon = GetSelectedPedWeapon(cache.ped)
2221
for i = 1, #config.noCarjackWeapons do
23-
if weapon == joaat(config.noCarjackWeapons[i]) then
22+
if cache.weapon == joaat(config.noCarjackWeapons[i]) then
2423
return true
2524
end
2625
end

client/main.lua

+61-50
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ local areKeysJobShared = functions.areKeysJobShared
1919
-----------------------
2020

2121
local isTakingKeys = false
22-
local isCarjacking = false
2322
local isHotwiring = false
24-
local canCarjack = true
23+
local isCarjackingAvailable = true
2524

2625
-----------------------
2726
---- Functions ----
@@ -201,9 +200,9 @@ local function showHotwiringLabel()
201200
end
202201

203202
local function carjackVehicle(target)
204-
if not config.carjackEnable then return end
205-
isCarjacking = true
206-
canCarjack = false
203+
if not isCarjackingAvailable then return end
204+
isCarjackingAvailable = false
205+
local isCarjacking = true
207206
local vehicle = GetVehiclePedIsUsing(target)
208207
local occupants = getPedsInVehicle(vehicle)
209208

@@ -244,10 +243,9 @@ local function carjackVehicle(target)
244243
car = true,
245244
},
246245
}) then
247-
local hasWeapon, weaponHash = GetCurrentPedWeapon(cache.ped, true)
248-
if hasWeapon and isCarjacking then
246+
if cache.weapon and isCarjacking then
249247
local carjackChance = 0.5
250-
local chance = config.carjackChance[GetWeapontypeGroup(weaponHash) --[[@as string]]]
248+
local chance = config.carjackChance[GetWeapontypeGroup(cache.weapon) --[[@as string]]]
251249
if chance then
252250
carjackChance = chance
253251
end
@@ -285,7 +283,7 @@ local function carjackVehicle(target)
285283
end
286284

287285
Wait(config.delayBetweenCarjackingsInMs)
288-
canCarjack = true
286+
isCarjackingAvailable = true
289287
end
290288

291289
local function toggleEngine()
@@ -296,48 +294,40 @@ local function toggleEngine()
296294
end
297295
end
298296

299-
-----------------------
300-
---- Threads ----
301-
-----------------------
302-
303-
if config.carjackEnable then
297+
local function watchCarjackingAttempts()
304298
CreateThread(function()
305-
while true do
306-
if LocalPlayer.state.isLoggedIn then
307-
if canCarjack then
308-
local aiming, target = GetEntityPlayerIsFreeAimingAt(cache.playerId)
309-
if aiming
310-
and target
311-
and target ~= 0
312-
and DoesEntityExist(target)
313-
and IsPedInAnyVehicle(target, false)
314-
and not IsEntityDead(target)
315-
and not IsPedAPlayer(target)
316-
then
317-
local targetveh = GetVehiclePedIsIn(target, false)
318-
local carIsImmune = false
319-
for i = 1, #config.immuneVehicles do
320-
if GetEntityModel(targetveh) == joaat(config.immuneVehicles[i]) then
321-
carIsImmune = true
322-
end
299+
while cache.weapon do
300+
if isCarjackingAvailable then
301+
local aiming, target = GetEntityPlayerIsFreeAimingAt(cache.playerId)
302+
if aiming
303+
and target
304+
and target ~= 0
305+
and DoesEntityExist(target)
306+
and IsPedInAnyVehicle(target, false)
307+
and not IsEntityDead(target)
308+
and not IsPedAPlayer(target)
309+
then
310+
local targetveh = GetVehiclePedIsIn(target, false)
311+
local isVehicleImmune = false
312+
for i = 1, #config.immuneVehicles do
313+
if GetEntityModel(targetveh) == joaat(config.immuneVehicles[i]) then
314+
isVehicleImmune = true
323315
end
316+
end
324317

325-
if not carIsImmune
326-
and GetPedInVehicleSeat(targetveh, -1) == target
327-
and not isBlacklistedWeapon()
328-
then
329-
local pos = GetEntityCoords(cache.ped)
330-
local targetpos = GetEntityCoords(target)
331-
if #(pos - targetpos) < 5.0 and not carIsImmune then
332-
carjackVehicle(target)
333-
end
318+
if not isVehicleImmune
319+
and GetPedInVehicleSeat(targetveh, -1) == target
320+
and not isBlacklistedWeapon()
321+
then
322+
local pos = GetEntityCoords(cache.ped)
323+
local targetpos = GetEntityCoords(target)
324+
if #(pos - targetpos) < 5.0 and not isVehicleImmune then
325+
carjackVehicle(target)
334326
end
335327
end
336328
end
337-
Wait(100)
338-
else
339-
Wait(1000)
340329
end
330+
Wait(100)
341331
end
342332
end)
343333
end
@@ -371,11 +361,6 @@ engineBind = lib.addKeybind({
371361
end
372362
})
373363

374-
AddEventHandler('ox_lib:cache:vehicle', function()
375-
showHotwiringLabel()
376-
end)
377-
showHotwiringLabel()
378-
379364
RegisterNetEvent('QBCore:Client:VehicleInfo', function(data)
380365
if not LocalPlayer.state.isLoggedIn and data.event ~= 'Entering' then return end
381366
if isBlacklistedVehicle(data.vehicle) then return end
@@ -425,7 +410,12 @@ RegisterNetEvent('QBCore:Client:VehicleInfo', function(data)
425410
end
426411
end
427412
-- Parked car logic
428-
elseif driver == 0 and not Entity(data.vehicle).state.isOpen and not hasKeys(plate) and not isTakingKeys and not Entity(data.vehicle).state.vehicleid then
413+
elseif driver == 0 and
414+
not (isTakingKeys
415+
or Entity(data.vehicle).state.isOpen
416+
or Entity(data.vehicle).state.vehicleid
417+
or hasKeys(plate))
418+
then
429419
TriggerServerEvent('qb-vehiclekeys:server:setVehLockState', data.netId, config.lockNPCParkedCars and 2 or 1)
430420
end
431421
end)
@@ -466,6 +456,27 @@ RegisterNetEvent('lockpicks:UseLockpick', function(isAdvanced)
466456
end
467457
end)
468458

459+
AddEventHandler('ox_lib:cache:vehicle', function()
460+
showHotwiringLabel()
461+
end)
462+
463+
464+
if config.carjackEnable then
465+
AddEventHandler('ox_lib:cache:weapon', function()
466+
watchCarjackingAttempts()
467+
end)
468+
end
469+
470+
AddEventHandler('onResourceStart', function (resourceName)
471+
if (GetCurrentResourceName() ~= resourceName) then return end
472+
473+
showHotwiringLabel()
474+
475+
if config.carjackEnable then
476+
watchCarjackingAttempts()
477+
end
478+
end)
479+
469480
--#region Backwards Compatibility ONLY -- Remove at some point --
470481
RegisterNetEvent('qb-vehiclekeys:client:AddKeys', function(plate)
471482
TriggerServerEvent('qb-vehiclekeys:server:AcquireVehicleKeys', plate)

0 commit comments

Comments
 (0)