Skip to content

Commit

Permalink
🔑 Optionally save acquired keys into DB
Browse files Browse the repository at this point in the history
  • Loading branch information
Cocodrulo committed Apr 18, 2024
1 parent 487fd51 commit c4f4193
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,12 @@ RegisterNetEvent('qb-vehiclekeys:client:AddKeys', function(plate)
SetVehicleEngineOn(vehicle, false, false, false)
end
end
if Config.SaveInDB then TriggerServerEvent("qb-vehiclekeys:server:synckeys", KeysList) end
end)

RegisterNetEvent('qb-vehiclekeys:client:RemoveKeys', function(plate)
KeysList[plate] = nil
if Config.SaveInDB then TriggerServerEvent("qb-vehiclekeys:server:synckeys", KeysList) end
end)

RegisterNetEvent('qb-vehiclekeys:client:ToggleEngine', function()
Expand Down
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.SaveInDB = true -- Whether the own keys should save into database

-- Vehicle lock settings
Config.LockToggleAnimation = {
AnimDict = 'anim@mp_player_intmenu@key_fob@',
Expand Down
13 changes: 12 additions & 1 deletion 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["VehKeys"] and Config.SaveInDB then
for plate, value in Player.PlayerData.metadata["VehKeys"] do
keysList[plate] = true
end
end
cb(keysList)
end)

Expand Down Expand Up @@ -133,4 +144,4 @@ QBCore.Commands.Add("removekeys", Lang:t("addcom.rkeys"), {{name = Lang:t("addco
return
end
RemoveKeys(tonumber(args[1]), args[2])
end, 'admin')
end, 'admin')

0 comments on commit c4f4193

Please sign in to comment.