Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prevention of saving glovebox's and trunks that are unowned to the DB #607

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions server/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ local function GetFirstFreeSlot(items, maxSlots)
return nil
end


local function SetupShopItems(shopItems)
local items = {}
local slot = 1
Expand Down Expand Up @@ -48,6 +49,12 @@ local function SetupShopItems(shopItems)
return items
end

function IsVehicleOwned(plate)
local result = MySQL.scalar.await('SELECT 1 from player_vehicles WHERE plate = ?', { plate })
if result then return true end
return false
end

-- Exported Functions

function LoadInventory(source, citizenid)
Expand Down
15 changes: 14 additions & 1 deletion server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ end)
AddEventHandler('txAdmin:events:serverShuttingDown', function()
for inventory, data in pairs(Inventories) do
if data.isOpen then
MySQL.prepare('INSERT INTO inventories (identifier, items) VALUES (?, ?) ON DUPLICATE KEY UPDATE items = ?', { inventory, json.encode(data.items), json.encode(data.items) })
if (inventory:find('glovebox%-') and IsVehicleOwned(inventory:match('glovebox%-(.+)'))) or (inventory:find('trunk%-') and IsVehicleOwned(inventory:match('trunk%-(.+)'))) then
MySQL.prepare('INSERT INTO inventories (identifier, items) VALUES (?, ?) ON DUPLICATE KEY UPDATE items = ?', { inventory, json.encode(data.items), json.encode(data.items) })
else
MySQL.prepare('INSERT INTO inventories (identifier, items) VALUES (?, ?) ON DUPLICATE KEY UPDATE items = ?', { inventory, json.encode(data.items), json.encode(data.items) })
end
end
end
end)
Expand Down Expand Up @@ -174,6 +178,15 @@ RegisterNetEvent('qb-inventory:server:closeInventory', function(inventory)
end
if not Inventories[inventory] then return end
Inventories[inventory].isOpen = false

if inventory:find('glovebox%-') then
if not IsVehicleOwned(inventory:match('glovebox%-(.+)')) then return end
end

if inventory:find('trunk%-') then
if not IsVehicleOwned(inventory:match('trunk%-(.+)')) then return end
end

MySQL.prepare('INSERT INTO inventories (identifier, items) VALUES (?, ?) ON DUPLICATE KEY UPDATE items = ?', { inventory, json.encode(Inventories[inventory].items), json.encode(Inventories[inventory].items) })
end)

Expand Down
Loading