Skip to content

Commit

Permalink
feat: on demand vehicle lock init (#149)
Browse files Browse the repository at this point in the history
* feat: on demand vehicle lock init

* fix: lint

* optimize when loop is active

Co-authored-by: Solareon <[email protected]>

---------

Co-authored-by: Solareon <[email protected]>
  • Loading branch information
Manason and solareon authored Oct 25, 2024
1 parent 537d3e3 commit 394faf2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
39 changes: 39 additions & 0 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
local config = require 'config.client'
local sharedFunctions = require 'shared.functions'

local getIsVehicleInitiallyLocked = sharedFunctions.getIsVehicleInitiallyLocked
local getIsVehicleShared = sharedFunctions.getIsVehicleShared
local getIsVehicleAlwaysUnlocked = sharedFunctions.getIsVehicleAlwaysUnlocked
local getIsVehicleCarjackingImmune = sharedFunctions.getIsVehicleCarjackingImmune
Expand Down Expand Up @@ -246,4 +247,42 @@ AddEventHandler('onResourceStart', function(resourceName)
if cache.seat == -1 then
onEnteringDriverSeat()
end
end)

local function onVehicleAttemptToEnter(vehicle)
if Entity(vehicle).state.doorslockstate then return end

local ped = GetPedInVehicleSeat(vehicle, -1)
if IsPedAPlayer(ped) then return end

local isLocked = not getIsVehicleAlwaysUnlocked(vehicle) and getIsVehicleInitiallyLocked(vehicle, ped and ped ~= 0)
local lockState = isLocked and 2 or 1
SetVehicleDoorsLocked(vehicle, lockState)
TriggerServerEvent('qb-vehiclekeys:server:setVehLockState', NetworkGetNetworkIdFromEntity(vehicle), lockState)
end

local isLoggedIn = false

local function playerEnterVehLoop()
CreateThread(function()
while isLoggedIn do
local vehicle = GetVehiclePedIsTryingToEnter(cache.ped)
if vehicle ~= 0 then
onVehicleAttemptToEnter(vehicle)
end
Wait(100)
end
end)
end

CreateThread(function()
if LocalPlayer.state.isLoggedIn then
playerEnterVehLoop()
end
end)

AddStateBagChangeHandler('isLoggedIn', ('player:%s'):format(cache.serverId), function(_, _, value)
isLoggedIn = value
if not value then return end
playerEnterVehLoop()
end)
31 changes: 0 additions & 31 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,8 @@ local config = require 'config.server'
local sharedFunctions = require 'shared.functions'

local getIsVehicleAlwaysUnlocked = sharedFunctions.getIsVehicleAlwaysUnlocked
local getIsVehicleInitiallyLocked = sharedFunctions.getIsVehicleInitiallyLocked
local getIsVehicleShared = sharedFunctions.getIsVehicleShared

---@enum EntityType
local EntityType = {
NoEntity = 0,
Ped = 1,
Vehicle = 2,
Object = 3
}

lib.callback.register('qbx_vehiclekeys:server:findKeys', function(source, netId)
local vehicle = NetworkGetEntityFromNetworkId(netId)
if math.random() <= sharedFunctions.getVehicleConfig(vehicle).findKeysChance then
Expand Down Expand Up @@ -59,25 +50,3 @@ RegisterNetEvent('qb-vehiclekeys:server:setVehLockState', function(vehNetId, sta
if getIsVehicleAlwaysUnlocked(vehicleEntity) or getIsVehicleShared(vehicleEntity) then return end
Entity(vehicleEntity).state:set('doorslockstate', state, true)
end)

---Lock every spawned vehicle
---@param entity number The entity number of the vehicle.
AddEventHandler('entityCreated', function (entity)
if not entity
or type(entity) ~= 'number'
or not DoesEntityExist(entity)
or GetEntityPopulationType(entity) > 5
then return end

local type = GetEntityType(entity)
local isPed = type == EntityType.Ped
local isVehicle = type == EntityType.Vehicle
if not isPed and not isVehicle then return end
local vehicle = isPed and GetVehiclePedIsIn(entity, false) or entity

if not DoesEntityExist(vehicle) then return end -- ped can be not in vehicle, so we need to check if vehicle is a entity, otherwise it will return 0

local isLocked = not getIsVehicleAlwaysUnlocked(vehicle)
and getIsVehicleInitiallyLocked(vehicle, isPed)
SetVehicleDoorsLocked(vehicle, isLocked and 2 or 1)
end)

0 comments on commit 394faf2

Please sign in to comment.