Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
3 changes: 3 additions & 0 deletions config.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Config = {}

-- Key System Settings
Config.PersistentKeys = true -- Whether the own keys should save into database or not

-- Vehicle lock settings
Config.LockToggleAnimation = {
AnimDict = 'anim@mp_player_intmenu@key_fob@',
Expand Down
40 changes: 37 additions & 3 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ RegisterNetEvent('qb-vehiclekeys:server:setVehLockState', function(vehNetId, sta
SetVehicleDoorsLocked(NetworkGetEntityFromNetworkId(vehNetId), state)
end)

RegisterServerEvent("qb-vehiclekeys:server:synckeys", function(KeyList)
if not Config.SaveInDB then return end
local Player = QBCore.Functions.GetPlayer(source)
Player.Functions.SetMetaData("VehKeys", KeyList)
end)

QBCore.Functions.CreateCallback('qb-vehiclekeys:server:GetVehicleKeys', function(source, cb)
local Player = QBCore.Functions.GetPlayer(source)
if not Player then return end
Expand All @@ -59,6 +65,11 @@ QBCore.Functions.CreateCallback('qb-vehiclekeys:server:GetVehicleKeys', function
keysList[plate] = true
end
end
if Player.PlayerData.metadata["vehicleKeys"] and Config.PersistentKeys then
for plate in pairs(Player.PlayerData.metadata["vehicleKeys"]) do
keysList[plate] = true
end
end
cb(keysList)
end)

Expand All @@ -78,38 +89,61 @@ function GiveKeys(id, plate)
local Player = QBCore.Functions.GetPlayer(id)
if not Player then return end
local citizenid = Player.PlayerData.citizenid

if not plate then
if GetVehiclePedIsIn(GetPlayerPed(id), false) ~= 0 then
plate = QBCore.Shared.Trim(GetVehicleNumberPlateText(GetVehiclePedIsIn(GetPlayerPed(id), false)))
else
return
end
end

if not VehicleList[plate] then VehicleList[plate] = {} end
VehicleList[plate][citizenid] = true

local oldKeys = Player.PlayerData.metadata["vehicleKeys"] or {}
oldKeys[plate] = true
Player.Functions.SetMetaData("vehicleKeys", oldKeys)

TriggerClientEvent('QBCore:Notify', id, Lang:t('notify.vgetkeys'))
TriggerClientEvent('qb-vehiclekeys:client:AddKeys', id, plate)
end

exports('GiveKeys', GiveKeys)

function RemoveKeys(id, plate)
local citizenid = QBCore.Functions.GetPlayer(id).PlayerData.citizenid
local Player = QBCore.Functions.GetPlayer(id)
if not Player then return end
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if not Player then return end
if not Player then
return
end

better readability

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think is better to keep it one line, it's cleaner from my view

local citizenid = Player.PlayerData.citizenid

if VehicleList[plate] and VehicleList[plate][citizenid] then
VehicleList[plate][citizenid] = nil
end

local oldKeys = Player.PlayerData.metadata["vehicleKeys"] or {}
oldKeys[plate] = nil
Player.Functions.SetMetaData("vehicleKeys", oldKeys)

TriggerClientEvent('qb-vehiclekeys:client:RemoveKeys', id, plate)
end

exports('RemoveKeys', RemoveKeys)

function HasKeys(id, plate)
local citizenid = QBCore.Functions.GetPlayer(id).PlayerData.citizenid
local Player = QBCore.Functions.GetPlayer(id)
if not Player then return false end
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if not Player then return false end
if not Player then
return false
end

better readability

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think is better to keep it one line, it's cleaner from my view

local citizenid = Player.PlayerData.citizenid

if VehicleList[plate] and VehicleList[plate][citizenid] then
return true
end

if Player.PlayerData.metadata["vehicleKeys"] and Config.PersistentKeys then
if Player.PlayerData.metadata["vehicleKeys"][plate] then
return true
end
end

return false
end

Expand All @@ -136,4 +170,4 @@ QBCore.Commands.Add('removekeys', Lang:t('addcom.rkeys'), { { name = Lang:t('add
return
end
RemoveKeys(tonumber(args[1]), args[2])
end, 'admin')
end, 'admin')
Loading