Skip to content

Commit

Permalink
feat: toggleEngine and getIsVehicleAlwaysUnlocked (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
artur-michalak authored Jul 17, 2024
1 parent 74448a9 commit 7d97e89
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
30 changes: 19 additions & 11 deletions client/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local getIsBlacklistedWeapon = functions.getIsBlacklistedWeapon
local getIsVehicleAlwaysUnlocked = functions.getIsVehicleAlwaysUnlocked
local getIsVehicleLockpickImmune = functions.getIsVehicleLockpickImmune
local getIsVehicleCarjackingImmune = functions.getIsVehicleCarjackingImmune
local getIsVehicleTypeAlwaysUnlocked = functions.getIsVehicleTypeAlwaysUnlocked

local alertSend = false
local public = {}
Expand Down Expand Up @@ -44,12 +45,22 @@ end

exports('HasKeys', public.hasKeys)

function public.toggleEngine()
local vehicle = cache.vehicle
if vehicle and public.hasKeys(qbx.getVehiclePlate(vehicle)) then
local engineOn = GetIsVehicleEngineRunning(vehicle)
SetVehicleEngineOn(vehicle, not engineOn, false, true)
end
---Checks if player has vehicle keys of or access to the vehicle is provided as part of his job.
---@param vehicle number The entity number of the vehicle.
---@param plate string? The plate number of the vehicle.
---@return boolean? `true` if player has access to the vehicle, `nil` otherwise.
function public.getIsVehicleAccessible(vehicle, plate)
plate = plate or qbx.getVehiclePlate(vehicle)
return public.hasKeys(plate) or public.areKeysJobShared(vehicle)
end

exports('HasAccess', public.getIsVehicleAccessible)

function public.toggleEngine(vehicle)
local veh = vehicle or cache.vehicle
if not public.getIsVehicleAccessible(veh) then return end
local engineOn = GetIsVehicleEngineRunning(veh)
SetVehicleEngineOn(veh, not engineOn, false, true)
end

exports('ToggleEngine', public.toggleEngine)
Expand All @@ -58,18 +69,15 @@ exports('ToggleEngine', public.toggleEngine)
---@param vehicle number The entity number of the vehicle.
---@return boolean? `true` if the vehicle is blacklisted, `nil` otherwise.
function public.getIsVehicleAlwaysUnlocked(vehicle)
if Entity(vehicle).state.ignoreLocks or GetVehicleClass(vehicle) == 13 then
return true
end

return getIsVehicleAlwaysUnlocked(vehicle)
or getIsVehicleTypeAlwaysUnlocked(vehicle)
end

function public.getNPCPedsInVehicle(vehicle)
local otherPeds = {}
for seat = -1, GetVehicleModelNumberOfSeats(GetEntityModel(vehicle)) - 2 do
local pedInSeat = GetPedInVehicleSeat(vehicle, seat)
if not IsPedAPlayer(pedInSeat) and pedInSeat ~= 0 then
if pedInSeat ~= 0 and not IsPedAPlayer(pedInSeat) then
otherPeds[#otherPeds + 1] = pedInSeat
end
end
Expand Down
17 changes: 16 additions & 1 deletion client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,9 @@ local function watchCarjackingAttempts()
end

-----------------------
---- Client Events ----
------ Key Binds ------
-----------------------

local togglelocksBind
togglelocksBind = lib.addKeybind({
name = 'togglelocks',
Expand All @@ -297,6 +298,7 @@ engineBind = lib.addKeybind({
name = 'toggleengine',
description = locale('info.engine'),
defaultKey = 'G',
disabled = not cache.vehicle,
onPressed = function()
engineBind:disable(true)
toggleEngine()
Expand All @@ -305,6 +307,10 @@ engineBind = lib.addKeybind({
end
})

-----------------------
---- Client Events ----
-----------------------

local isTakingKeys = false
RegisterNetEvent('QBCore:Client:VehicleInfo', function(data)
if not LocalPlayer.state.isLoggedIn or data.event ~= 'Entering' then return end
Expand Down Expand Up @@ -339,6 +345,15 @@ RegisterNetEvent('lockpicks:UseLockpick', function(isAdvanced)
end
end)

RegisterNetEvent('qbx_vehiclekeys:client:OnLostKeys', function()
Wait(0)
showHotwiringLabel()
end)

AddEventHandler('ox_lib:cache:seat', function()
showHotwiringLabel()
end)

AddEventHandler('ox_lib:cache:vehicle', function()
showHotwiringLabel()
end)
Expand Down
2 changes: 1 addition & 1 deletion locales/da.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"info": {
"engine": "Tænd/sluk motor",
"search_keys": "~g~[H]~w~ - Søg efter nøgler",
"search_keys_dispatch": "[H] - Søg efter nøgler",
"toggle_locks": "Lås/oplås køretøj",
"vehicle_theft": "Køretøjstyveri i gang. Type: "
},
Expand Down
1 change: 1 addition & 0 deletions server/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ function public.removeKeys(source, plate)

Player(source).state:set('keysList', keys, true)

TriggerClientEvent('qbx_vehiclekeys:client:OnLostKeys', source)
exports.qbx_core:Notify(source, locale('notify.keys_removed'))

return true
Expand Down

0 comments on commit 7d97e89

Please sign in to comment.