Skip to content

Commit 6a1d156

Browse files
committed
fix: small issues
- Added getVehicleByPlate function - Adjusted main client loop to check for player ownership when parked - Added plate to givekey - Fixed commands not grabbing name from locale - Made vehicleList be populated by the database on start
1 parent d3172d4 commit 6a1d156

13 files changed

+140
-87
lines changed

client/functions.lua

+45-31
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local getHash, isCloseToCoords in functions
55
local alertSend = false
66
local public = {}
77

8-
--- Checks if the current player has a key for the specified vehicle.
8+
---Checks if the current player has a key for the specified vehicle.
99
---@param vehicle number The entity number of the vehicle to check for a key.
1010
---@return boolean? `true` if the player has a key for the vehicle, nil otherwise.
1111
function HasKey(vehicle)
@@ -15,31 +15,31 @@ function HasKey(vehicle)
1515
return ent.state.keys[QBX.PlayerData.citizenid]
1616
end
1717

18-
--- Attempt to Give a key to a target player for the specified vehicle.
18+
---Attempt to Give a key to a target player for the specified vehicle.
1919
---@param targetPlayerId number The ID of the target player who will receive the key.
2020
---@param vehicle number The entity number of the vehicle for which the key is being given.
2121
function GiveKey(targetPlayerId, vehicle)
2222
-- This function is not yet implemented
2323
-- Will call the corresponding callback
2424
end
2525

26-
--- Attempt to Remove a key from a target player for the specified vehicle.
26+
---Attempt to Remove a key from a target player for the specified vehicle.
2727
---@param targetPlayerId number The ID of the target player from whom the key is being removed.
2828
---@param vehicle number The entity number of the vehicle from which the key is being removed.
2929
function RemoveKey(targetPlayerId, vehicle)
3030
-- This function is not yet implemented
3131
-- Will call the corresponding callback
3232
end
3333

34-
--- Toggles the state of a vehicle's doors. If a door is open, it will be closed, and if it's closed, it will be opened.
34+
---Toggles the state of a vehicle's doors. If a door is open, it will be closed, and if it's closed, it will be opened.
3535
---@param vehicle number The entity number of the vehicle for which the door state is being toggled.
3636
function ToggleVehicleDoor(vehicle)
3737
-- This function is not yet implemented
3838
-- Will call the corresponding callback
3939
end
4040

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

@@ -48,9 +48,9 @@ function public.isBlacklistedWeapon()
4848
end
4949
end
5050

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

@@ -76,10 +76,10 @@ function public.attemptPoliceAlert(type)
7676
end
7777
end
7878

79-
--- Gets bone coords
80-
--- @param vehicle number The entity number of the vehicle.
81-
--- @param boneName string The entity bone name.
82-
--- @return vector3 Bone coords if exists, entity coords otherwise.
79+
---Gets bone coords
80+
---@param vehicle number The entity number of the vehicle.
81+
---@param boneName string The entity bone name.
82+
---@return vector3 Bone coords if exists, entity coords otherwise.
8383
local function getBoneCoords(vehicle, boneName)
8484
local boneIndex = GetEntityBoneIndexByName(vehicle, boneName)
8585

@@ -90,10 +90,10 @@ local function getBoneCoords(vehicle, boneName)
9090
end
9191
end
9292

93-
--- Checking whether the character is close enough to the vehicle driver door.
94-
--- @param vehicle number The entity number of the vehicle.
95-
--- @param maxDistance number The max distance to check.
96-
--- @return boolean? `true` if the ped is out of a vehicle and in the range of the opened vehicle, nil otherwise.
93+
---Checking whether the character is close enough to the vehicle driver door.
94+
---@param vehicle number The entity number of the vehicle.
95+
---@param maxDistance number The max distance to check.
96+
---@return boolean? `true` if the ped is out of a vehicle and in the range of the opened vehicle, nil otherwise.
9797
local function isVehicleInRange(vehicle, maxDistance)
9898
local vehicles = GetGamePool('CVehicle')
9999
local pedCoords = GetEntityCoords(cache.ped)
@@ -108,9 +108,9 @@ local function isVehicleInRange(vehicle, maxDistance)
108108
end
109109
end
110110

111-
--- The function will be execuded when the opening of the lock succeeds.
112-
--- @param vehicle number The entity number of the vehicle.
113-
--- @param plate string The plate number of the vehicle.
111+
---The function will be execuded when the opening of the lock succeeds.
112+
---@param vehicle number The entity number of the vehicle.
113+
---@param plate string The plate number of the vehicle.
114114
local function lockpickSuccessCallback(vehicle, plate)
115115
TriggerServerEvent('hud:server:GainStress', math.random(1, 4))
116116

@@ -123,12 +123,12 @@ local function lockpickSuccessCallback(vehicle, plate)
123123
end
124124
end
125125

126-
--- Operations done after the LockpickDoor quickevent done.
127-
--- @param vehicle number The entity number of the vehicle.
128-
--- @param plate string The plate number of the vehicle.
129-
--- @param isAdvancedLockedpick boolean Determines whether an advanced lockpick was used.
130-
--- @param maxDistance number The max distance to check.
131-
--- @param isSuccess boolean? Determines whether the lock has been successfully opened.
126+
---Operations done after the LockpickDoor quickevent done.
127+
---@param vehicle number The entity number of the vehicle.
128+
---@param plate string The plate number of the vehicle.
129+
---@param isAdvancedLockedpick boolean Determines whether an advanced lockpick was used.
130+
---@param maxDistance number The max distance to check.
131+
---@param isSuccess boolean? Determines whether the lock has been successfully opened.
132132
local function lockpickCallback(vehicle, plate, isAdvancedLockedpick, maxDistance, isSuccess)
133133
if not isVehicleInRange(vehicle, maxDistance) then return end -- the action will be aborted if the opened vehicle is too far.
134134
if isSuccess then
@@ -152,8 +152,9 @@ local function lockpickCallback(vehicle, plate, isAdvancedLockedpick, maxDistanc
152152
end
153153

154154
local lockpickingSemaphore = 0
155-
--- Lockpicking quickevent.
156-
--- @param isAdvancedLockedpick boolean Determines whether an advanced lockpick was used
155+
156+
---Lockpicking quickevent.
157+
---@param isAdvancedLockedpick boolean Determines whether an advanced lockpick was used
157158
function public.lockpickDoor(isAdvancedLockedpick)
158159
local maxDistance = 2
159160
local pedCoords = GetEntityCoords(cache.ped)
@@ -165,7 +166,7 @@ function public.lockpickDoor(isAdvancedLockedpick)
165166
local isDriverSeatFree = IsVehicleSeatFree(vehicle, -1)
166167
local doorCoords = getBoneCoords(vehicle, 'door_dside_f')
167168

168-
--- player may attempt to open the lock if:
169+
-- player may attempt to open the lock if:
169170
if not vehicle
170171
or not plate
171172
or not isDriverSeatFree -- no one in the driver's seat
@@ -182,7 +183,7 @@ function public.lockpickDoor(isAdvancedLockedpick)
182183
Wait(0)
183184

184185
CreateThread(function()
185-
--- lock opening animation
186+
-- lock opening animation
186187
lib.requestAnimDict('veh@break_in@0h@p_m_one@')
187188
TaskPlayAnim(cache.ped, 'veh@break_in@0h@p_m_one@', "low_force_entry_ds", 3.0, 3.0, -1, 16, 0, false, false, false)
188189

@@ -195,4 +196,17 @@ function public.lockpickDoor(isAdvancedLockedpick)
195196
lockpickingSemaphore = 0
196197
end
197198

198-
return public
199+
---Get a vehicle in the players scope by the plate
200+
---@param plate string
201+
---@return integer?
202+
function public.getVehicleByPlate(plate)
203+
local vehicles = GetGamePool('CVehicle')
204+
for i = 1, #vehicles do
205+
local vehicle = vehicles[i]
206+
if qbx.getVehiclePlate(vehicle) == plate then
207+
return vehicle
208+
end
209+
end
210+
end
211+
212+
return public

client/main.lua

+27-28
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
local config = require 'config.client'
66
local functions = require 'client.functions'
7-
local lockpickDoor, attemptPoliceAlert, isBlacklistedWeapon, isBlacklistedVehicle in functions
7+
local lockpickDoor, attemptPoliceAlert, isBlacklistedWeapon, isBlacklistedVehicle, getVehicleByPlate in functions
88

99
-----------------------
1010
---- Variables ----
@@ -38,14 +38,13 @@ end
3838
local function hasKeys(plate)
3939
return KeysList[plate]
4040
end
41+
4142
exports('HasKeys', hasKeys)
4243

4344
local function getVehicleInDirection(coordFromOffset, coordToOffset)
44-
local coordFrom = GetOffsetFromEntityInWorldCoords(cache.ped, coordFromOffset.x, coordFromOffset.y, coordFromOffset
45-
.z)
45+
local coordFrom = GetOffsetFromEntityInWorldCoords(cache.ped, coordFromOffset.x, coordFromOffset.y, coordFromOffset.z)
4646
local coordTo = GetOffsetFromEntityInWorldCoords(cache.ped, coordToOffset.x, coordToOffset.y, coordToOffset.z)
47-
local rayHandle = CastRayPointToPoint(coordFrom.x, coordFrom.y, coordFrom.z, coordTo.x, coordTo.y, coordTo.z, 10,
48-
cache.ped, 0)
47+
local rayHandle = CastRayPointToPoint(coordFrom.x, coordFrom.y, coordFrom.z, coordTo.x, coordTo.y, coordTo.z, 10, cache.ped, 0)
4948
local _, _, _, _, vehicle = GetShapeTestResult(rayHandle)
5049
return vehicle
5150
end
@@ -63,7 +62,7 @@ local function getVehicle()
6362

6463
local count = 0
6564
while not vehicle and count < #RaycastOffsetTable do
66-
count = count + 1
65+
count += 1
6766
vehicle = getVehicleInDirection(RaycastOffsetTable[count]['fromOffset'], RaycastOffsetTable[count]['toOffset'])
6867
end
6968

@@ -102,10 +101,9 @@ local function setVehicleDoorLock(veh, state, anim)
102101
end
103102

104103
TriggerServerEvent("InteractSound_SV:PlayWithinDistance", 5, "lock", 0.3)
105-
106104
NetworkRequestControlOfEntity(veh)
107105
if state then
108-
state = state == true and 2 or 1
106+
state = not state and 1 or 2
109107
TriggerServerEvent('qb-vehiclekeys:server:setVehLockState', NetworkGetNetworkIdFromEntity(veh), state)
110108
exports.qbx_core:Notify(state == 2 and locale("notify.vehicle_locked") or locale("notify.vehicle_unlocked"), 'inform')
111109
else
@@ -117,6 +115,7 @@ local function setVehicleDoorLock(veh, state, anim)
117115
exports.qbx_core:Notify(locale("notify.vehicle_unlocked"), 'inform')
118116
end
119117
end
118+
120119
SetVehicleLights(veh, 2)
121120
Wait(250)
122121
SetVehicleLights(veh, 1)
@@ -132,7 +131,8 @@ local function setVehicleDoorLock(veh, state, anim)
132131
end
133132
end
134133
end
135-
exports("SetVehicleDoorLock", setVehicleDoorLock)
134+
135+
exports('SetVehicleDoorLock', setVehicleDoorLock)
136136

137137
local function getOtherPlayersInVehicle(vehicle)
138138
local otherPeds = {}
@@ -164,17 +164,16 @@ end
164164
local function hotwire(vehicle, plate)
165165
local hotwireTime = math.random(config.minHotwireTime, config.maxHotwireTime)
166166
isHotwiring = true
167+
167168
SetVehicleAlarm(vehicle, true)
168169
SetVehicleAlarmTimeLeft(vehicle, 2)
169170

170171
SetTimeout(hotwireTime * 2, function()
171-
if not DoesEntityExist(vehicle) then return end
172-
if not IsEntityAVehicle(vehicle) then return end
173-
if isHotwiring then return end
174-
if IsVehicleAlarmActivated(vehicle) then
175-
SetVehicleAlarm(vehicle, false)
176-
end
172+
if not DoesEntityExist(vehicle) or not IsEntityAVehicle(vehicle) or isHotwiring or not IsVehicleAlarmActivated(vehicle) then return end
173+
174+
SetVehicleAlarm(vehicle, false)
177175
end)
176+
178177
if lib.progressCircle({
179178
duration = hotwireTime,
180179
label = locale("progress.searching_keys"),
@@ -297,20 +296,18 @@ CreateThread(function()
297296
local sleep = 1000
298297
if LocalPlayer.state.isLoggedIn then
299298
sleep = 100
300-
301-
302299
local entering = GetVehiclePedIsTryingToEnter(cache.ped)
303300
local carIsImmune = false
304301
if entering ~= 0 and not isBlacklistedVehicle(entering) then
305302
sleep = 2000
306303
local plate = qbx.getVehiclePlate(entering)
307-
308304
local driver = GetPedInVehicleSeat(entering, -1)
309-
for _, veh in ipairs(config.immuneVehicles) do
310-
if GetEntityModel(entering) == joaat(veh) then
305+
for i = 1, #config.immuneVehicles do
306+
if GetEntityModel(entering) == joaat(config.immuneVehicles[i]) then
311307
carIsImmune = true
312308
end
313309
end
310+
314311
-- Driven vehicle logic
315312
if driver ~= 0 and not IsPedAPlayer(driver) and not hasKeys(plate) and not carIsImmune then
316313
if IsEntityDead(driver) then
@@ -342,14 +339,15 @@ CreateThread(function()
342339

343340
--Make passengers flee
344341
local pedsInVehicle = getPedsInVehicle(entering)
345-
for _, pedInVehicle in pairs(pedsInVehicle) do
342+
for i = 1, #pedsInVehicle do
343+
local pedInVehicle = pedsInVehicle[i]
346344
if pedInVehicle ~= GetPedInVehicleSeat(entering, -1) then
347345
makePedFlee(pedInVehicle)
348346
end
349347
end
350348
end
351349
-- Parked car logic
352-
elseif driver == 0 and not Entity(entering).state.isOpen and not hasKeys(plate) and not isTakingKeys then
350+
elseif driver == 0 and Entity(entering).state.isOpen == false and not hasKeys(plate) and not isTakingKeys and not lib.callback.await('vehiclekeys:server:IsPlayerOwned', false, plate) then
353351
if config.lockNPCParkedCars then
354352
TriggerServerEvent('qb-vehiclekeys:server:setVehLockState', NetworkGetNetworkIdFromEntity(entering), 2)
355353
else
@@ -381,11 +379,12 @@ CreateThread(function()
381379
if aiming and (target ~= nil and target ~= 0) then
382380
if DoesEntityExist(target) and IsPedInAnyVehicle(target, false) and not IsEntityDead(target) and not IsPedAPlayer(target) then
383381
local targetveh = GetVehiclePedIsIn(target, false)
384-
for _, veh in ipairs(config.immuneVehicles) do
385-
if GetEntityModel(targetveh) == joaat(veh) then
382+
for i = 1, #config.immuneVehicles do
383+
if GetEntityModel(targetveh) == joaat(config.immuneVehicles[i]) then
386384
carIsImmune = true
387385
end
388386
end
387+
389388
if GetPedInVehicleSeat(targetveh, -1) == target and not isBlacklistedWeapon() then
390389
local pos = GetEntityCoords(cache.ped, true)
391390
local targetpos = GetEntityCoords(target, true)
@@ -397,6 +396,7 @@ CreateThread(function()
397396
end
398397
end
399398
end
399+
400400
Wait(sleep)
401401
end
402402
end)
@@ -436,7 +436,6 @@ RegisterNetEvent('qb-vehiclekeys:client:AddKeys', function(plate)
436436

437437
if cache.vehicle then
438438
local vehicleplate = qbx.getVehiclePlate(cache.vehicle)
439-
440439
if plate == vehicleplate then
441440
SetVehicleEngineOn(cache.vehicle, false, false, false)
442441
end
@@ -459,8 +458,8 @@ RegisterNetEvent('qb-vehiclekeys:client:ToggleEngine', function()
459458
end
460459
end)
461460

462-
RegisterNetEvent('qb-vehiclekeys:client:GiveKeys', function(id)
463-
local targetVehicle = getVehicle()
461+
RegisterNetEvent('qb-vehiclekeys:client:GiveKeys', function(id, plate)
462+
local targetVehicle = plate and getVehicleByPlate(plate) or cache.vehicle or getVehicle()
464463

465464
if targetVehicle then
466465
local targetPlate = qbx.getVehiclePlate(targetVehicle)
@@ -492,4 +491,4 @@ end)
492491
RegisterNetEvent('vehiclekeys:client:SetOwner', function(plate)
493492
TriggerServerEvent('qb-vehiclekeys:server:AcquireVehicleKeys', plate)
494493
end)
495-
-- Backwards Compatibility ONLY -- Remove at some point --
494+
-- Backwards Compatibility ONLY -- Remove at some point --

locales/cs.json

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
"givekeys": "Předejte někomu klíče. Pokud nemáte průkaz totožnosti, dejte je nejbližší osobě nebo všem ve vozidle.",
99
"givekeys_id": "ID",
1010
"givekeys_id_help": "ID hráče",
11+
"givekeys_plate": "SPZ",
12+
"givekeys_plate_help": "SPZ",
1113
"remove_keys": "Odebrat někomu klíče od vozidla.",
1214
"remove_keys_id": "id",
1315
"remove_keys_id_help": "ID hráče",

locales/de.json

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
"givekeys": "Übergebe die Schlüssel an jemanden. Wenn keine Bürger-ID angegeben, geht er an die nächstgelegene Person oder an alle Personen im Fahrzeug.",
99
"givekeys_id": "id",
1010
"givekeys_id_help": "Bürger-ID",
11+
"givekeys_plate": "kennzeichen",
12+
"givekeys_plate_help": "Kennzeichen",
1113
"remove_keys": "Jemandem die Schlüssel eines Fahrzeugs abnehmen.",
1214
"remove_keys_id": "id",
1315
"remove_keys_id_help": "Bürger-ID",

locales/en.json

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
"givekeys": "Hand over the keys to someone. If no ID, gives to closest person or everyone in the vehicle.",
99
"givekeys_id": "id",
1010
"givekeys_id_help": "Player ID",
11+
"givekeys_plate": "plate",
12+
"givekeys_plate_help": "Plate",
1113
"remove_keys": "Remove keys to a vehicle for someone.",
1214
"remove_keys_id": "id",
1315
"remove_keys_id_help": "Player ID",

locales/es.json

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
"givekeys": "Entregar llaves a alguien. Si no hay ID, entregar a la persona más cercana o a todos en el vehículo.",
99
"givekeys_id": "id",
1010
"givekeys_id_help": "ID de jugador",
11+
"givekeys_plate": "placa",
12+
"givekeys_plate_help": "Placa",
1113
"remove_keys": "Quitar llaves de un vehículo a alguien.",
1214
"remove_keys_id": "id",
1315
"remove_keys_id_help": "ID de jugador",

locales/et.json

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
"givekeys": "Andke võtmed kellelegi üle. Kui isikut tõendav dokument puudub, annab see lähimale inimesele või kõigile sõidukis viibijatele.",
99
"givekeys_id": "id",
1010
"givekeys_id_help": "Mängija ID",
11+
"givekeys_plate": "Numbrimärk",
12+
"givekeys_plate_help": "Numbrimärk",
1113
"remove_keys": "Eemaldage kellegi jaoks sõiduki võtmed.",
1214
"remove_keys_id": "id",
1315
"remove_keys_id_help": "Mängija ID",

0 commit comments

Comments
 (0)