diff --git a/client/main.lua b/client/main.lua index 21c299b..d057ab8 100644 --- a/client/main.lua +++ b/client/main.lua @@ -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 @@ -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) \ No newline at end of file diff --git a/server/main.lua b/server/main.lua index fe7014c..1fb1267 100644 --- a/server/main.lua +++ b/server/main.lua @@ -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 @@ -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)