From e263aa32550d67d81b567bdf4aff6f6f6747865c Mon Sep 17 00:00:00 2001 From: Jim Shield Date: Tue, 14 Feb 2023 14:52:57 +0000 Subject: [PATCH] Add HasItem() exploit protection --- server/server.lua | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/server/server.lua b/server/server.lua index 400cfd8..b4f8a3e 100644 --- a/server/server.lua +++ b/server/server.lua @@ -19,6 +19,41 @@ RegisterNetEvent('jim-consumables:server:toggleItem', function(give, item, amoun end end) +RegisterNetEvent("jim-consumables:server:DupeWarn", function(item, newsrc) + local src = newsrc or source + local P = QBCore.Functions.GetPlayer(src) + print("^5DupeWarn: ^1"..P.PlayerData.charinfo.firstname.." "..P.PlayerData.charinfo.lastname.."^7(^1"..tostring(src).."^7) ^2Tried to remove item ^7('^3"..item.."^7')^2 but it wasn't there^7") + if not Config.Debug then DropPlayer(src, "Kicked for attempting to duplicate items") end + print("^5DupeWarn: ^1"..P.PlayerData.charinfo.firstname.." "..P.PlayerData.charinfo.lastname.."^7(^1"..tostring(src).."^7) ^2Dropped from server for item duplicating^7") +end) + +RegisterNetEvent('jim-consumables:server:toggleItem', function(give, item, amount, newsrc) + local src = newsrc or source + local amount = amount or 1 + local remamount = amount + if not give then + if HasItem(src, item, amount) then -- check if you still have the item + if QBCore.Functions.GetPlayer(src).Functions.GetItemByName(item).unique then -- If unique item, keep removing until gone + while remamount > 0 do + QBCore.Functions.GetPlayer(src).Functions.RemoveItem(item, 1) + remamount -= 1 + end + TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[item], "remove", amount) -- Show removal item box when all are removed + return + end + if QBCore.Functions.GetPlayer(src).Functions.RemoveItem(item, amount) then + if Config.Debug then print("^5Debug^7: ^1Removing ^2from Player^7(^2"..src.."^7) '^6"..QBCore.Shared.Items[item].label.."^7(^2x^6"..(amount or "1").."^7)'") end + TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[item], "remove", amount) + end + else TriggerEvent("jim-consumables:server:DupeWarn", item, src) end -- if not boot the player + elseif give then + if QBCore.Functions.GetPlayer(src).Functions.AddItem(item, amount) then + if Config.Debug then print("^5Debug^7: ^4Giving ^2Player^7(^2"..src.."^7) '^6"..QBCore.Shared.Items[item].label.."^7(^2x^6"..(amount or "1").."^7)'") end + TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[item], "add", amount) + end + end +end) + RegisterNetEvent('jim-consumables:server:addThirst', function(amount) local Player = QBCore.Functions.GetPlayer(source) if not Player then return end @@ -33,6 +68,28 @@ RegisterNetEvent('jim-consumables:server:addHunger', function(amount) TriggerClientEvent('hud:client:UpdateNeeds', source, amount, Player.PlayerData.metadata.thirst) end) +function HasItem(source, items, amount) + local amount = amount or 1 + local Player = QBCore.Functions.GetPlayer(source) + if not Player then return false end + local count = 0 + + if Config.Debug then print("^5Debug^7: ^3HasItem^7: ^2Checking if player has required item^7 '^3"..tostring(items).."^7'") end + + for _, itemData in pairs(Player.PlayerData.items) do + if itemData and (itemData.name == items) then + if Config.Debug then print("^5Debug^7: ^3HasItem^7: ^2Item^7: '^3"..tostring(items).."^7' ^2Slot^7: ^3"..itemData.slot.." ^7x(^3"..tostring(itemData.amount).."^7)") end + count += itemData.amount + end + end + if count >= amount then + if Config.Debug then print("^5Debug^7: ^3HasItem^7: ^2Items ^5FOUND^7 x^3"..count.."^7") end + return true + end + if Config.Debug then print("^5Debug^7: ^3HasItem^7: ^2Items ^1NOT FOUND^7") end + return false +end + --Export Import System-- RegisterNetEvent("jim-consumables:getSync", function() local src = source TriggerClientEvent("jim-consumables:syncItems", source, Config.Consumables, Config.Emotes) end) local function importEmote(emoteName, eInfo)