Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Doesn't allow owners to exit a locked vehicle #51

Closed
wants to merge 5 commits into from
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 36 additions & 22 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

local config = require 'config.client'
local functions = require 'client.functions'
local childLockEnabled = GetConvar('qbx:vehiclekeys:enablechildlock', 'false') == 'true'

local hasKeys = functions.hasKeys
local lockpickDoor = functions.lockpickDoor
Expand Down Expand Up @@ -66,33 +67,35 @@ local function getVehicle()
return vehicle
end

---manages the opening of locks
---@param vehicle number? The entity number of the vehicle.
---@param state boolean? State of the vehicle lock.
---@param anim any Aniation
--- Manages the opening of locks
--- @param vehicle number? The entity number of the vehicle.
artur-michalak marked this conversation as resolved.
Show resolved Hide resolved
--- @param state boolean? State of the vehicle lock.
--- @param anim any Animation
local function setVehicleDoorLock(vehicle, state, anim)
if not vehicle then return end
if not isBlacklistedVehicle(vehicle) then
if hasKeys(qbx.getVehiclePlate(vehicle)) or areKeysJobShared(vehicle) then

if anim then
lib.requestAnimDict('anim@mp_player_intmenu@key_fob@')
TaskPlayAnim(cache.ped, 'anim@mp_player_intmenu@key_fob@', 'fob_click', 3.0, 3.0, -1, 49, 0, false, false, false)
end

TriggerServerEvent('InteractSound_SV:PlayWithinDistance', 5, 'lock', 0.3)
NetworkRequestControlOfEntity(vehicle)

local lockstate
if state ~= nil then
lockstate = state and 2 or 1
else
lockstate = (GetVehicleDoorLockStatus(vehicle) % 2) + 1 -- (1 % 2) + 1 -> 2 (2 % 2) + 1 -> 1
local currentLockState = GetVehicleDoorLockStatus(vehicle)
local isPedInVehicle = cache.vehicle == vehicle
if childLockEnabled and isPedInVehicle and currentLockState == 4 then
-- Check if the player is in the vehicle and the lock state is 4, then switch it to 1
lockstate = 1
else
lockstate = isPedInVehicle and 4 or (currentLockState % 2) + 1 -- Use state 4 if the player is inside, otherwise 1 or 2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will someone be able to get in the car while someone is sitting in?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it locks on position 4 the car and closes for players who want to exit/enter the vehicle, I send you a video, I wait for a friend and I shoot the video.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the locks are open and someone tries to sit as a passenger?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, when the driver is already in the car.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmmh thank you for making me think that I will adjust the code, I will adjust the code

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to make some changes to qbx_smallresources

end
end

TriggerServerEvent('qb-vehiclekeys:server:setVehLockState', NetworkGetNetworkIdFromEntity(vehicle), lockstate)
exports.qbx_core:Notify(locale(lockstate == 2 and 'notify.vehicle_locked' or 'notify.vehicle_unlocked'))

SetVehicleLights(vehicle, 2)
Wait(250)
SetVehicleLights(vehicle, 1)
Expand All @@ -106,9 +109,7 @@ local function setVehicleDoorLock(vehicle, state, anim)
else
TriggerServerEvent('qb-vehiclekeys:server:setVehLockState', NetworkGetNetworkIdFromEntity(vehicle), 1)
end
end

exports('SetVehicleDoorLock', setVehicleDoorLock)
end exports('SetVehicleDoorLock', setVehicleDoorLock)
artur-michalak marked this conversation as resolved.
Show resolved Hide resolved

local function getOtherPlayersInVehicle(vehicle)
local otherPeds = {}
Expand Down Expand Up @@ -367,15 +368,28 @@ end)
---- Client Events ----
-----------------------

RegisterKeyMapping('togglelocks', locale('info.toggle_locks'), 'keyboard', 'L')
RegisterCommand('togglelocks', function()
setVehicleDoorLock(getVehicle(), nil, true)
end, false)

RegisterKeyMapping('engine', locale('info.engine'), 'keyboard', 'G')
RegisterCommand('engine', function()
TriggerEvent('qb-vehiclekeys:client:ToggleEngine')
end, false)
local toggleLocksKeybind = lib.addKeybind({
name = 'togglelocks',
description = locale('info.toggle_locks'),
defaultKey = 'L',
onPressed = function(self)
local vehicle = getVehicle()
setVehicleDoorLock(vehicle, nil, true)
print(('pressed %s (%s)'):format(self.currentKey, self.name))
artur-michalak marked this conversation as resolved.
Show resolved Hide resolved
end,
onReleased = function(self)
print(('released %s (%s)'):format(self.currentKey, self.name))
end
})

local engineKeybind = lib.addKeybind({
name = 'engine',
description = locale('info.engine'),
defaultKey = 'G',
onPressed = function(self)
TriggerEvent('qb-vehiclekeys:client:ToggleEngine')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using a function instead of an event.

end
})

RegisterNetEvent('qb-vehiclekeys:client:ToggleEngine', function()
local vehicle = cache.vehicle
Expand Down
Loading