Skip to content

Commit

Permalink
fix (vehiclekeys): update metadata key handling and improve key manag…
Browse files Browse the repository at this point in the history
…ement logic
  • Loading branch information
Cocodrulo committed Nov 14, 2024
1 parent 346b673 commit 5636725
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ QBCore.Functions.CreateCallback('qb-vehiclekeys:server:GetVehicleKeys', function
keysList[plate] = true
end
end
if Player.PlayerData.metadata["VehKeys"] and Config.SaveInDB then
for plate, value in Player.PlayerData.metadata["VehKeys"] do
if Player.PlayerData.metadata["vehicleKeys"] and Config.PersistentKeys then
for plate, value in Player.PlayerData.metadata["vehicleKeys"] do
keysList[plate] = true
end
end
Expand All @@ -89,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
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
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 Down

0 comments on commit 5636725

Please sign in to comment.