diff --git a/client/carjack.lua b/client/carjack.lua index ed07d7d..80ccfa9 100644 --- a/client/carjack.lua +++ b/client/carjack.lua @@ -52,15 +52,11 @@ local function onCarjackSuccess(occupants, vehicle) makePedFlee(ped) end) end - TriggerServerEvent('hud:server:GainStress', math.random(1, 4)) - TriggerServerEvent('qb-vehiclekeys:server:setVehLockState', NetworkGetNetworkIdFromEntity(vehicle), 1) - TriggerServerEvent('qb-vehiclekeys:server:AcquireVehicleKeys', VehToNet(vehicle)) end local function onCarjackFail(driver) exports.qbx_core:Notify(locale('notify.carjack_failed'), 'error') makePedFlee(driver) - TriggerServerEvent('hud:server:GainStress', math.random(1, 4)) end local function carjackVehicle(driver, vehicle) @@ -98,14 +94,14 @@ local function carjackVehicle(driver, vehicle) }, }) then if cache.weapon and isCarjacking then - local carjackChance = config.carjackChance[GetWeapontypeGroup(cache.weapon) --[[@as string]]] or 0.5 isCarjacking = false -- make this false to stop TaskVehicleTempAction from preventing ped to leave the car - - if math.random() <= carjackChance then + local success = lib.callback.await('qbx_vehiclekeys:server:carjack', false, VehToNet(vehicle), GetWeapontypeGroup(cache.weapon)) + if success then onCarjackSuccess(occupants, vehicle) else onCarjackFail(driver) end + TriggerServerEvent('hud:server:GainStress', math.random(1, 4)) Wait(2000) sendPoliceAlertAttempt('carjack') end diff --git a/client/functions.lua b/client/functions.lua index 5400e08..64b8c9e 100644 --- a/client/functions.lua +++ b/client/functions.lua @@ -227,7 +227,7 @@ end ---Will be executed when the lock opening is successful. ---@param vehicle number The entity number of the vehicle. local function hotwireSuccessCallback(vehicle) - TriggerServerEvent('qb-vehiclekeys:server:AcquireVehicleKeys', VehToNet(vehicle)) + TriggerServerEvent('qbx_vehiclekeys:server:hotwiredVehicle', VehToNet(vehicle)) end ---Operations done after the LockpickDoor quickevent done. diff --git a/client/main.lua b/client/main.lua index 9774bbe..27ee3cf 100644 --- a/client/main.lua +++ b/client/main.lua @@ -56,7 +56,6 @@ end exports('SetVehicleDoorLock', setVehicleDoorLock) local function findKeys(vehicleModel, vehicleClass, vehicle) - local vehicleConfig = sharedFunctions.getVehicleConfig(vehicle) local hotwireTime = math.random(config.minKeysSearchTime, config.maxKeysSearchTime) local anim = config.anims.lockpick.model[vehicleModel] @@ -75,13 +74,12 @@ local function findKeys(vehicleModel, vehicleClass, vehicle) combat = true, } }) then - if math.random() <= vehicleConfig.findKeysChance then - TriggerServerEvent('qb-vehiclekeys:server:AcquireVehicleKeys', VehToNet(vehicle)) - return true - else + local success = lib.callback.await('qbx_vehiclekeys:server:findKeys', false, VehToNet(vehicle)) + if not success then TriggerServerEvent('hud:server:GainStress', math.random(1, 4)) exports.qbx_core:Notify(locale("notify.failed_keys"), 'error') end + return success end end @@ -118,7 +116,7 @@ local function onEnteringDriverSeat() local isVehicleRunning = GetIsVehicleEngineRunning(vehicle) if config.getKeysWhenEngineIsRunning and isVehicleRunning then lib.print.debug("giving keys because engine is running") - TriggerServerEvent('qb-vehiclekeys:server:AcquireVehicleKeys', VehToNet(vehicle)) + TriggerServerEvent('qbx_vehiclekeys:server:playerEnteredVehicleWithEngineOn', VehToNet(vehicle)) return end @@ -230,7 +228,7 @@ RegisterNetEvent('QBCore:Client:VehicleInfo', function(data) car = true, }, }) then - TriggerServerEvent('qb-vehiclekeys:server:AcquireVehicleKeys', VehToNet(data.vehicle)) + TriggerServerEvent('qbx_vehiclekeys:server:tookKeys', VehToNet(data.vehicle)) end end isTakingKeys = false diff --git a/config/client.lua b/config/client.lua index 2250307..bfb4d5e 100644 --- a/config/client.lua +++ b/config/client.lua @@ -48,19 +48,6 @@ return { carjackEnable = true, -- Enables the ability to carjack pedestrian vehicles, stealing them by pointing a weapon at them carjackingTimeInMs = 7500, -- Time it takes to successfully carjack in miliseconds delayBetweenCarjackingsInMs = 10000, -- Time before you can attempt another carjack in miliseconds - ---@type table - carjackChance = { -- Probability of successful carjacking based on weapon used - [WeaponTypeGroup.MELEE] = 0.0, - [WeaponTypeGroup.HANDGUN] = 0.5, - [WeaponTypeGroup.SMG] = 0.75, - [WeaponTypeGroup.SHOTGUN] = 0.90, - [WeaponTypeGroup.RIFLE] = 0.90, - [WeaponTypeGroup.LMG] = 0.99, - [WeaponTypeGroup.SNIPER] = 0.99, - [WeaponTypeGroup.HEAVY] = 0.99, - [WeaponTypeGroup.THROWABLE] = 0.0, - [WeaponTypeGroup.MISC] = 0.0, - }, -- Hotwire Settings timeBetweenHotwires = 5000, -- Time in milliseconds between hotwire attempts diff --git a/config/server.lua b/config/server.lua index 9f65e71..b51d90a 100644 --- a/config/server.lua +++ b/config/server.lua @@ -1,3 +1,16 @@ return { runClearCronMinutes = 5, + ---@type table + carjackChance = { -- Probability of successful carjacking based on weapon used + [WeaponTypeGroup.MELEE] = 0.0, + [WeaponTypeGroup.HANDGUN] = 0.5, + [WeaponTypeGroup.SMG] = 0.75, + [WeaponTypeGroup.SHOTGUN] = 0.90, + [WeaponTypeGroup.RIFLE] = 0.90, + [WeaponTypeGroup.LMG] = 0.99, + [WeaponTypeGroup.SNIPER] = 0.99, + [WeaponTypeGroup.HEAVY] = 0.99, + [WeaponTypeGroup.THROWABLE] = 0.0, + [WeaponTypeGroup.MISC] = 0.0, + }, } diff --git a/server/keys.lua b/server/keys.lua index 26ec358..833b8e6 100644 --- a/server/keys.lua +++ b/server/keys.lua @@ -1,7 +1,13 @@ local config = require 'config.server' local debug = GetConvarInt(('%s-debug'):format(GetCurrentResourceName()), 0) == 1 +---@alias CitizenId string +---@alias SessionId integer +---@type table> local keysList = {} ---holds key status for some time after player logs out (Prevents frustration by crashing the client) + +---@alias LogoutTime integer +---@type table local keysLifetime = {} ---Life timestamp of the keys of a character who has logged out ---Gets Citizen Id based on source diff --git a/server/main.lua b/server/main.lua index 79c40ca..fe7014c 100644 --- a/server/main.lua +++ b/server/main.lua @@ -1,3 +1,4 @@ +local config = require 'config.server' local sharedFunctions = require 'shared.functions' local getIsVehicleAlwaysUnlocked = sharedFunctions.getIsVehicleAlwaysUnlocked @@ -12,7 +13,38 @@ local EntityType = { Object = 3 } -RegisterNetEvent('qb-vehiclekeys:server:AcquireVehicleKeys', function(netId) +lib.callback.register('qbx_vehiclekeys:server:findKeys', function(source, netId) + local vehicle = NetworkGetEntityFromNetworkId(netId) + if math.random() <= sharedFunctions.getVehicleConfig(vehicle).findKeysChance then + GiveKeys(source, vehicle) + return true + end +end) + +lib.callback.register('qbx_vehiclekeys:server:carjack', function(source, netId, weaponTypeGroup) + local chance = config.carjackChance[weaponTypeGroup] or 0.5 + if math.random() <= chance then + local vehicle = NetworkGetEntityFromNetworkId(netId) + GiveKeys(source, vehicle) + TriggerEvent('qb-vehiclekeys:server:setVehLockState', netId, 1) + return true + end +end) + +RegisterNetEvent('qbx_vehiclekeys:server:playerEnteredVehicleWithEngineOn', function(netId) + local src = source + local vehicle = NetworkGetEntityFromNetworkId(netId) + if not GetIsVehicleEngineRunning(vehicle) then return end + GiveKeys(src, vehicle) +end) + +---TODO: secure this event +RegisterNetEvent('qbx_vehiclekeys:server:tookKeys', function(netId) + GiveKeys(source, NetworkGetEntityFromNetworkId(netId)) +end) + +---TODO: secure this event +RegisterNetEvent('qbx_vehiclekeys:server:hotwiredVehicle', function(netId) GiveKeys(source, NetworkGetEntityFromNetworkId(netId)) end)