Skip to content

Commit

Permalink
feat(server): init locks statuses (#68)
Browse files Browse the repository at this point in the history
* feat(server): init locks statuses

* feat(server): lock only random population cars

* feat(server): on entityCreated always enabled

* refactor: getIsVehicleBlacklisted to getIsVehicleAlwaysUnlocked

* refactor: getIsVehicleImmune to getIsVehicleCarjackingImmune
  • Loading branch information
ArturMichalak committed Jun 23, 2024
1 parent ea515de commit 756deca
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 41 deletions.
10 changes: 5 additions & 5 deletions client/functions.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
local config = require 'config.client'
local functions = require 'shared.functions'
local getIsCloseToCoords = functions.getIsCloseToCoords
local getIsVehicleBlacklisted = functions.getIsVehicleBlacklisted
local getIsVehicleImmune = functions.getIsVehicleImmune
local getIsVehicleAlwaysUnlocked = functions.getIsVehicleAlwaysUnlocked
local getIsVehicleCarjackingImmune = functions.getIsVehicleCarjackingImmune

local alertSend = false
local public = {}

public.getIsVehicleImmune = getIsVehicleImmune -- to prevent circular-dependency error
public.getIsVehicleCarjackingImmune = getIsVehicleCarjackingImmune -- to prevent circular-dependency error

---Checks if player has vehicle keys
---@param plate string The plate number of the vehicle.
Expand All @@ -32,12 +32,12 @@ end
---Checking vehicle on the blacklist.
---@param vehicle number The entity number of the vehicle.
---@return boolean? `true` if the vehicle is blacklisted, `nil` otherwise.
function public.getIsVehicleBlacklisted(vehicle)
function public.getIsVehicleAlwaysUnlocked(vehicle)
if Entity(vehicle).state.ignoreLocks or GetVehicleClass(vehicle) == 13 then
return true
end

return getIsVehicleBlacklisted(vehicle)
return getIsVehicleAlwaysUnlocked(vehicle)
end

function public.attemptPoliceAlert(type)
Expand Down
39 changes: 9 additions & 30 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ local hotwire = functions.hotwire
local lockpickDoor = functions.lockpickDoor
local attemptPoliceAlert = functions.attemptPoliceAlert
local isBlacklistedWeapon = functions.isBlacklistedWeapon
local getIsVehicleBlacklisted = functions.getIsVehicleBlacklisted
local getIsVehicleAlwaysUnlocked = functions.getIsVehicleAlwaysUnlocked
local getVehicleByPlate = functions.getVehicleByPlate
local areKeysJobShared = functions.areKeysJobShared
local getIsVehicleImmune = functions.getIsVehicleImmune
local getIsVehicleCarjackingImmune = functions.getIsVehicleCarjackingImmune

-----------------------
---- Variables ----
Expand Down Expand Up @@ -74,7 +74,7 @@ end
---@param anim any Aniation
local function setVehicleDoorLock(vehicle, state, anim)
if not vehicle then return end
if not getIsVehicleBlacklisted(vehicle) then
if not getIsVehicleAlwaysUnlocked(vehicle) then
if hasKeys(qbx.getVehiclePlate(vehicle)) or areKeysJobShared(vehicle) then

if anim then
Expand Down Expand Up @@ -182,7 +182,7 @@ local function showHotwiringLabel()
local plate = qbx.getVehiclePlate(cache.vehicle)
if cache.seat == -1
and not hasKeys(plate)
and not getIsVehicleBlacklisted(cache.vehicle)
and not getIsVehicleAlwaysUnlocked(cache.vehicle)
and not areKeysJobShared(cache.vehicle)
then
local vehiclePos = GetOffsetFromEntityInWorldCoords(cache.vehicle, 0.0, 1.0, 0.5)
Expand Down Expand Up @@ -267,6 +267,7 @@ local function carjackVehicle(target)
end)
end
TriggerServerEvent('hud:server:GainStress', math.random(1, 4))
TriggerServerEvent('qb-vehiclekeys:server:setVehLockState', NetworkGetNetworkIdFromEntity(vehicle), 1)
TriggerServerEvent('qb-vehiclekeys:server:AcquireVehicleKeys', plate)
else
exports.qbx_core:Notify(locale('notify.carjack_failed'), 'error')
Expand Down Expand Up @@ -313,7 +314,7 @@ local function watchCarjackingAttempts()
and not IsPedAPlayer(target)
then
local targetveh = GetVehiclePedIsIn(target, false)
local isVehicleImmune = getIsVehicleImmune(targetveh)
local isVehicleImmune = getIsVehicleCarjackingImmune(targetveh)

if not isVehicleImmune
and GetPedInVehicleSeat(targetveh, -1) == target
Expand Down Expand Up @@ -364,8 +365,8 @@ engineBind = lib.addKeybind({

RegisterNetEvent('QBCore:Client:VehicleInfo', function(data)
if not LocalPlayer.state.isLoggedIn and data.event ~= 'Entering' then return end
if getIsVehicleBlacklisted(data.vehicle) then return end
local isVehicleImmune = getIsVehicleImmune(data.vehicle)
if getIsVehicleAlwaysUnlocked(data.vehicle) then return end
local isVehicleImmune = getIsVehicleCarjackingImmune(data.vehicle)
local driver = GetPedInVehicleSeat(data.vehicle, -1)
local plate = qbx.getVehiclePlate(data.vehicle)

Expand All @@ -389,29 +390,7 @@ RegisterNetEvent('QBCore:Client:VehicleInfo', function(data)
end
isTakingKeys = false
end
elseif config.lockNPCDrivenCars then
TriggerServerEvent('qb-vehiclekeys:server:setVehLockState', data.netId, 2)
else
TriggerServerEvent('qb-vehiclekeys:server:setVehLockState', data.netId, 1)
TriggerServerEvent('qb-vehiclekeys:server:AcquireVehicleKeys', plate)

--Make passengers flee
local pedsInVehicle = getPedsInVehicle(data.vehicle)
for i = 1, #pedsInVehicle do
local pedInVehicle = pedsInVehicle[i]
if pedInVehicle ~= GetPedInVehicleSeat(data.vehicle, -1) then
makePedFlee(pedInVehicle)
end
end
end
-- Parked car logic
elseif driver == 0 and
not (isTakingKeys
or Entity(data.vehicle).state.isOpen
or Entity(data.vehicle).state.vehicleid
or hasKeys(plate))
then
TriggerServerEvent('qb-vehiclekeys:server:setVehLockState', data.netId, config.lockNPCParkedCars and 2 or 1)
end
end)

Expand Down Expand Up @@ -462,7 +441,7 @@ if config.carjackEnable then
end)
end

AddEventHandler('onResourceStart', function (resourceName)
AddEventHandler('onResourceStart', function(resourceName)
if (GetCurrentResourceName() ~= resourceName) then return end

showHotwiringLabel()
Expand Down
4 changes: 0 additions & 4 deletions config/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ return {

vehicleMaximumLockingDistance = 5.0, -- Minimum distance for vehicle locking

-- NPC Vehicle Lock States
lockNPCDrivenCars = true, -- Lock state for NPC cars being driven by NPCs [true = locked, false = unlocked]
lockNPCParkedCars = true, -- Lock state for NPC parked cars [true = locked, false = unlocked]

-- Lockpick Settings
removeNormalLockpickChance = { -- Chance to remove lockpick on fail by vehicle class
[VehicleClasses.COMPACTS] = 0.5,
Expand Down
4 changes: 4 additions & 0 deletions config/server.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
return {
runClearCronMinutes = 5,

-- NPC Vehicle Lock States
lockNPCDrivenCars = true, -- Lock state for NPC cars being driven by NPCs [true = locked, false = unlocked]
lockNPCParkedCars = true, -- Lock state for NPC parked cars [true = locked, false = unlocked]
}
16 changes: 16 additions & 0 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
---- Imports ----
-----------------------

local config = require 'config.server'
local functions = require 'server.functions'
local sharedFunctions = require 'shared.functions'

local giveKeys = functions.giveKeys
local addPlayer = functions.addPlayer
local removePlayer = functions.removePlayer
local getIsVehicleAlwaysUnlocked = sharedFunctions.getIsVehicleAlwaysUnlocked

-----------------------
---- Events ----
Expand Down Expand Up @@ -49,3 +52,16 @@ end)
AddEventHandler('playerDropped', function()
removePlayer(source --[[@as integer]])
end)

---Lock every spawned vehicle
---@param vehicle number The entity number of the vehicle.
AddEventHandler('entityCreated', function (vehicle)
if not vehicle
or GetEntityType(vehicle) ~= 2
or GetEntityPopulationType(vehicle) > 5
then return end
local isDriver = GetPedInVehicleSeat(vehicle, -1) ~= 0
local isLocked = (config.lockNPCDrivenCars and isDriver) or (config.lockNPCParkedCars and not isDriver)
and GetVehicleType(vehicle) ~= 'bike' and not getIsVehicleAlwaysUnlocked(vehicle)
SetVehicleDoorsLocked(vehicle, isLocked and 2 or 1)
end)
4 changes: 2 additions & 2 deletions shared/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ end
---Checking vehicle on the blacklist.
---@param vehicle number The entity number of the vehicle.
---@return boolean? `true` if the vehicle is blacklisted, `nil` otherwise.
function public.getIsVehicleBlacklisted(vehicle)
function public.getIsVehicleAlwaysUnlocked(vehicle)
return getIsVehicleOnList(vehicle, config.noLockVehicles)
end

---Checking vehicle on the immunes list.
---@param vehicle any
---@return boolean? `true` if the vehicle is immune, `nil` otherwise.
function public.getIsVehicleImmune(vehicle)
function public.getIsVehicleCarjackingImmune(vehicle)
return getIsVehicleOnList(vehicle, config.immuneVehicles)
end

Expand Down

0 comments on commit 756deca

Please sign in to comment.