Skip to content

Commit

Permalink
feat(server/main): new SetLockState export (#162)
Browse files Browse the repository at this point in the history
* feat: new export

* refactor: setLockState function parameters
  • Loading branch information
CodexisPhantom authored Jan 9, 2025
1 parent 02666e9 commit e703167
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions server/main.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
local config = require 'config.server'

---@param veh number
---@param state string
local function setLockState(veh, state)
if type(state) ~= 'string' or not DoesEntityExist(veh) then return end
local vehicleConfig = GetVehicleConfig(veh)
if vehicleConfig.noLock or vehicleConfig.shared then return end
Entity(veh).state:set('doorslockstate', state == 'lock' and 2 or 1, true)
end
exports('SetLockState', setLockState)

lib.callback.register('qbx_vehiclekeys:server:findKeys', function(source, netId)
local vehicle = NetworkGetEntityFromNetworkId(netId)
if math.random() <= GetVehicleConfig(vehicle).findKeysChance then
Expand All @@ -13,7 +23,7 @@ lib.callback.register('qbx_vehiclekeys:server:carjack', function(source, netId,
if math.random() <= chance then
local vehicle = NetworkGetEntityFromNetworkId(netId)
GiveKeys(source, vehicle)
TriggerEvent('qb-vehiclekeys:server:setVehLockState', netId, 1)
setLockState(vehicle, 'unlock')
return true
end
end)
Expand All @@ -40,10 +50,9 @@ RegisterNetEvent('qb-vehiclekeys:server:breakLockpick', function(itemName)
exports.ox_inventory:RemoveItem(source, itemName, 1)
end)

RegisterNetEvent('qb-vehiclekeys:server:setVehLockState', function(vehNetId, state)
local vehicleEntity = NetworkGetEntityFromNetworkId(vehNetId)
if type(state) ~= 'number' or not DoesEntityExist(vehicleEntity) then return end
local vehicleConfig = GetVehicleConfig(vehicleEntity)
if vehicleConfig.noLock or vehicleConfig.shared then return end
Entity(vehicleEntity).state:set('doorslockstate', state, true)
RegisterNetEvent('qb-vehiclekeys:server:setVehLockState', function(netId, state)
local vehicle = NetworkGetEntityFromNetworkId(netId)
if type(state) ~= 'number' or not DoesEntityExist(vehicle) then return end
if state == 2 then state = 'lock' else state = 'unlock' end
setLockState(vehicle, state)
end)

0 comments on commit e703167

Please sign in to comment.