Skip to content

Commit b752c84

Browse files
feat(shared): move getIsVehicleBlacklisted and getIsVehicleImmune to shared
1 parent b5aa774 commit b752c84

File tree

6 files changed

+55
-43
lines changed

6 files changed

+55
-43
lines changed

client/functions.lua

+13-14
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
local config = require 'config.client'
22
local functions = require 'shared.functions'
3-
local isCloseToCoords = functions.isCloseToCoords
3+
local getIsCloseToCoords = functions.getIsCloseToCoords
4+
local getIsVehicleBlacklisted = functions.getIsVehicleBlacklisted
5+
local getIsVehicleImmune = functions.getIsVehicleImmune
46

57
local alertSend = false
68
local public = {}
79

10+
public.getIsVehicleImmune = getIsVehicleImmune -- to prevent circular-dependency error
11+
812
---Checks if player has vehicle keys
913
---@param plate string The plate number of the vehicle.
1014
---@return boolean? `true` if player has vehicle keys, `nil` otherwise.
@@ -28,17 +32,12 @@ end
2832
---Checking vehicle on the blacklist.
2933
---@param vehicle number The entity number of the vehicle.
3034
---@return boolean? `true` if the vehicle is blacklisted, `nil` otherwise.
31-
function public.isBlacklistedVehicle(vehicle)
35+
function public.getIsVehicleBlacklisted(vehicle)
3236
if Entity(vehicle).state.ignoreLocks or GetVehicleClass(vehicle) == 13 then
3337
return true
3438
end
3539

36-
local vehicleHash = GetEntityModel(vehicle)
37-
for i = 1, #config.noLockVehicles do
38-
if vehicleHash == joaat(config.noLockVehicles[i]) then
39-
return true
40-
end
41-
end
40+
return getIsVehicleBlacklisted(vehicle)
4241
end
4342

4443
function public.attemptPoliceAlert(type)
@@ -80,10 +79,10 @@ end
8079
---@param bones table
8180
---@param maxDistance number
8281
---@return boolean? `true` if bone exists, `nil` otherwise.
83-
local function isCloseToAnyBone(coords, entity, bones, maxDistance)
82+
local function getIsCloseToAnyBone(coords, entity, bones, maxDistance)
8483
for i = 1, #bones do
8584
local boneCoords = getBoneCoords(entity, bones[i])
86-
if isCloseToCoords(coords, boneCoords, maxDistance) then
85+
if getIsCloseToCoords(coords, boneCoords, maxDistance) then
8786
return true
8887
end
8988
end
@@ -95,13 +94,13 @@ local doorBones = {'door_dside_f', 'door_dside_r', 'door_pside_f', 'door_pside_r
9594
---@param vehicle number The entity number of the vehicle.
9695
---@param maxDistance number The max distance to check.
9796
---@return boolean? `true` if the player ped is next to an open vehicle, `nil` otherwise.
98-
local function isVehicleInRange(vehicle, maxDistance)
97+
local function getIsVehicleInRange(vehicle, maxDistance)
9998
local vehicles = GetGamePool('CVehicle')
10099
local pedCoords = GetEntityCoords(cache.ped)
101100
for i = 1, #vehicles do
102101
local v = vehicles[i]
103102
if not cache.vehicle or v ~= cache.vehicle then
104-
if vehicle == v and isCloseToAnyBone(pedCoords, vehicle, doorBones, maxDistance) then
103+
if vehicle == v and getIsCloseToAnyBone(pedCoords, vehicle, doorBones, maxDistance) then
105104
return true
106105
end
107106
end
@@ -173,7 +172,7 @@ function public.lockpickDoor(isAdvancedLockedpick, maxDistance, customChallenge)
173172
if not isDriverSeatFree -- no one in the driver's seat
174173
or public.hasKeys(plate) -- player does not have keys to the vehicle
175174
or Entity(vehicle).state.isOpen -- the lock is locked
176-
or not isCloseToAnyBone(pedCoords, vehicle, doorBones, maxDistance) -- the player's ped is close enough to the driver's door
175+
or not getIsCloseToAnyBone(pedCoords, vehicle, doorBones, maxDistance) -- the player's ped is close enough to the driver's door
177176
or GetVehicleDoorLockStatus(vehicle) < 2 -- the vehicle is locked
178177
or (not isAdvancedLockedpick and config.advancedLockpickVehicleClasses[class])
179178
then return end
@@ -185,7 +184,7 @@ function public.lockpickDoor(isAdvancedLockedpick, maxDistance, customChallenge)
185184
lib.playAnim(cache.ped, 'veh@break_in@0h@p_m_one@', "low_force_entry_ds", 3.0, 3.0, -1, 16, 0, false, false, false) -- lock opening animation
186185
local isSuccess = customChallenge or lib.skillCheck({ 'easy', 'easy', { areaSize = 60, speedMultiplier = 1 }, 'medium' }, { '1', '2', '3', '4' })
187186

188-
if isVehicleInRange(vehicle, maxDistance) then -- the action will be aborted if the opened vehicle is too far.
187+
if getIsVehicleInRange(vehicle, maxDistance) then -- the action will be aborted if the opened vehicle is too far.
189188
lockpickCallback(vehicle, isAdvancedLockedpick, isSuccess)
190189
end
191190

client/main.lua

+7-17
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ local hotwire = functions.hotwire
1010
local lockpickDoor = functions.lockpickDoor
1111
local attemptPoliceAlert = functions.attemptPoliceAlert
1212
local isBlacklistedWeapon = functions.isBlacklistedWeapon
13-
local isBlacklistedVehicle = functions.isBlacklistedVehicle
13+
local getIsVehicleBlacklisted = functions.getIsVehicleBlacklisted
1414
local getVehicleByPlate = functions.getVehicleByPlate
1515
local areKeysJobShared = functions.areKeysJobShared
16+
local getIsVehicleImmune = functions.getIsVehicleImmune
1617

1718
-----------------------
1819
---- Variables ----
@@ -72,7 +73,7 @@ end
7273
---@param anim any Aniation
7374
local function setVehicleDoorLock(vehicle, state, anim)
7475
if not vehicle then return end
75-
if not isBlacklistedVehicle(vehicle) then
76+
if not getIsVehicleBlacklisted(vehicle) then
7677
if hasKeys(qbx.getVehiclePlate(vehicle)) or areKeysJobShared(vehicle) then
7778

7879
if anim then
@@ -180,7 +181,7 @@ local function showHotwiringLabel()
180181
local plate = qbx.getVehiclePlate(cache.vehicle)
181182
if cache.seat == -1
182183
and not hasKeys(plate)
183-
and not isBlacklistedVehicle(cache.vehicle)
184+
and not getIsVehicleBlacklisted(cache.vehicle)
184185
and not areKeysJobShared(cache.vehicle)
185186
then
186187
local vehiclePos = GetOffsetFromEntityInWorldCoords(cache.vehicle, 0.0, 1.0, 0.5)
@@ -311,12 +312,7 @@ local function watchCarjackingAttempts()
311312
and not IsPedAPlayer(target)
312313
then
313314
local targetveh = GetVehiclePedIsIn(target, false)
314-
local isVehicleImmune = false
315-
for i = 1, #config.immuneVehicles do
316-
if GetEntityModel(targetveh) == joaat(config.immuneVehicles[i]) then
317-
isVehicleImmune = true
318-
end
319-
end
315+
local isVehicleImmune = getIsVehicleImmune(targetveh)
320316

321317
if not isVehicleImmune
322318
and GetPedInVehicleSeat(targetveh, -1) == target
@@ -367,14 +363,8 @@ engineBind = lib.addKeybind({
367363

368364
RegisterNetEvent('QBCore:Client:VehicleInfo', function(data)
369365
if not LocalPlayer.state.isLoggedIn and data.event ~= 'Entering' then return end
370-
if isBlacklistedVehicle(data.vehicle) then return end
371-
local isVehicleImmune
372-
for i = 1, #config.immuneVehicles do
373-
if GetEntityModel(data.vehicle) == joaat(config.immuneVehicles[i]) then
374-
isVehicleImmune = true
375-
end
376-
end
377-
366+
if getIsVehicleBlacklisted(data.vehicle) then return end
367+
local isVehicleImmune = getIsVehicleImmune(data.vehicle)
378368
local driver = GetPedInVehicleSeat(data.vehicle, -1)
379369
local plate = qbx.getVehiclePlate(data.vehicle)
380370

config/client.lua

-10
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,6 @@ return {
178178
}
179179
},
180180

181-
-- Vehicles that cannot be jacked
182-
immuneVehicles = {
183-
'stockade'
184-
},
185-
186-
-- Vehicles that will never lock example:
187-
noLockVehicles = {
188-
-- 'stockade' -- example
189-
},
190-
191181
-- Weapons that cannot be used for carjacking
192182
noCarjackWeapons = {
193183
"WEAPON_UNARMED",

config/shared.lua

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
return {
2+
-- Vehicles that will never lock example:
3+
noLockVehicles = {
4+
-- 'stockade' -- example
5+
},
6+
7+
-- Vehicles that cannot be jacked
8+
immuneVehicles = {
9+
'stockade'
10+
},
11+
}

fxmanifest.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ server_scripts {
2525

2626
files {
2727
'locales/*.json',
28-
'config/client.lua'
28+
'config/client.lua',
29+
'config/shared.lua'
2930
}
3031

3132
dependencies {

shared/functions.lua

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
11
local public = {}
2+
local config = require 'config.shared'
23

34
--- Checks if the given two coordinates are close to each other based on distance.
45
---@param coord1 vector3[] The first set of coordinates.
56
---@param coord2 vector3[] The second set of coordinates.
67
---@param distance number The maximum allowed distance for them to be considered close.
78
---@return boolean true if the distance between two entities is less than the distance parameter.
8-
function public.isCloseToCoords(coord1, coord2, distance)
9+
function public.getIsCloseToCoords(coord1, coord2, distance)
910
return #(coord1 - coord2) < distance
1011
end
1112

13+
---Checking vehicle on the blacklist.
14+
---@param vehicle number The entity number of the vehicle.
15+
---@return boolean? `true` if the vehicle is blacklisted, `nil` otherwise.
16+
function public.getIsVehicleBlacklisted(vehicle)
17+
local vehicleHash = GetEntityModel(vehicle)
18+
for i = 1, #config.noLockVehicles do
19+
if vehicleHash == joaat(config.noLockVehicles[i]) then
20+
return true
21+
end
22+
end
23+
end
24+
25+
function public.getIsVehicleImmune(vehicle)
26+
for i = 1, #config.immuneVehicles do
27+
if GetEntityModel(vehicle) == joaat(config.immuneVehicles[i]) then
28+
return true
29+
end
30+
end
31+
end
32+
1233
return public

0 commit comments

Comments
 (0)