diff --git a/resources/[inventory]/nordi_pfand/config.lua b/resources/[inventory]/nordi_pfand/config.lua index e0efc955c..8518efd54 100644 --- a/resources/[inventory]/nordi_pfand/config.lua +++ b/resources/[inventory]/nordi_pfand/config.lua @@ -3,10 +3,12 @@ Config = {} -- Pfandautomat Props Config.PfandautomatProps = { 'as_rv_machine_prop', - } --- Konsumierbare Items die Pfand generieren +-- Delay in ms before adding the deposit item after consumption detection +Config.PfandDelay = 1500 + +-- Consumable items that generate deposits Config.ConsumableItems = { ['beer'] = { pfandItem = 'empty_glasbootle', @@ -26,10 +28,10 @@ Config.ConsumableItems = { }, } --- Pfand Items und ihre Werte (die leeren Behälter) +-- Deposit items and their values (the empty containers) Config.PfandItems = { ['empty_bottle'] = { - pfandwert = 25, -- Pfandwert in Cent/Credits + pfandwert = 25, -- Deposit value in cents/credits label = 'leere Flasche' }, ['empty_can'] = { @@ -40,15 +42,14 @@ Config.PfandItems = { pfandwert = 25, label = 'leere Glasflasche' } - } --- Allgemeine Einstellungen -Config.Currency = 'cash' -- 'cash' oder 'bank' +-- General settings +Config.Currency = 'cash' -- 'cash' or 'bank' Config.Debug = true -Config.ShowPfandNotification = true -- Zeige Benachrichtigung wenn Pfand erhalten wird +Config.ShowPfandNotification = true -- Show notification when deposit is received --- Sprache +-- Language Config.Locale = { ['pfand_menu_title'] = 'Pfandautomat', ['pfand_menu_description'] = 'Pfandgut einlösen', @@ -58,5 +59,4 @@ Config.Locale = { ['processing'] = 'Verarbeite Pfand...', ['select_items'] = 'Wähle die Artikel aus, die du einlösen möchtest:', ['pfand_received'] = 'Du hast %s erhalten!', - } diff --git a/resources/[inventory]/nordi_pfand/server/main.lua b/resources/[inventory]/nordi_pfand/server/main.lua index 099b00106..ef1cd3819 100644 --- a/resources/[inventory]/nordi_pfand/server/main.lua +++ b/resources/[inventory]/nordi_pfand/server/main.lua @@ -1,21 +1,24 @@ local QBCore = exports['qb-core']:GetCoreObject() --- Event wenn ein Item konsumiert wird +-- Store previous inventory states for all players +local playerInventories = {} + +-- Event when an item is consumed RegisterNetEvent('qb-pfandsystem:server:itemConsumed', function(itemName) local src = source local Player = QBCore.Functions.GetPlayer(src) if not Player then return end - -- Prüfe ob das konsumierte Item Pfand generiert + -- Check if the consumed item generates a deposit if Config.ConsumableItems[itemName] then local consumableConfig = Config.ConsumableItems[itemName] local pfandItem = consumableConfig.pfandItem - -- Warte kurz damit das originale Item erst konsumiert wird + -- Wait briefly so the original item is consumed first Wait(500) - -- Verwende Player.Functions.AddItem + -- Use Player.Functions.AddItem local success = Player.Functions.AddItem(pfandItem, 1) if success then @@ -45,7 +48,81 @@ RegisterNetEvent('qb-pfandsystem:server:itemConsumed', function(itemName) end end) --- Pfand einlösen +-- Function to monitor inventory changes and handle deposit items +RegisterNetEvent('inventory:server:SetInventory', function(playerId, inventory) + local Player = QBCore.Functions.GetPlayer(playerId) + if not Player then return end + + -- Initialize player's inventory tracking if it doesn't exist + if not playerInventories[playerId] then + playerInventories[playerId] = {} + for _, item in pairs(Player.PlayerData.items) do + if item then + playerInventories[playerId][item.name] = (playerInventories[playerId][item.name] or 0) + item.amount + end + end + return + end + + -- Check for removed consumable items + for itemName, prevAmount in pairs(playerInventories[playerId]) do + if Config.ConsumableItems[itemName] then + local currentItem = Player.Functions.GetItemByName(itemName) + local currentAmount = currentItem and currentItem.amount or 0 + + -- If the amount decreased, it might have been consumed + if currentAmount < prevAmount then + local removedAmount = prevAmount - currentAmount + + -- Schedule adding the deposit item with delay + SetTimeout(Config.PfandDelay or 1000, function() + local pfandItem = Config.ConsumableItems[itemName].pfandItem + + if Config.Debug then + print(string.format('[Pfandsystem] Detected %d %s removed, adding %s after delay', + removedAmount, itemName, pfandItem)) + end + + -- Add the deposit item + local success = Player.Functions.AddItem(pfandItem, removedAmount) + + if success then + TriggerClientEvent('inventory:client:ItemBox', playerId, QBCore.Shared.Items[pfandItem], "add", removedAmount) + + if Config.ShowPfandNotification then + local pfandLabel = Config.PfandItems[pfandItem] and Config.PfandItems[pfandItem].label or pfandItem + + TriggerClientEvent('ox_lib:notify', playerId, { + title = 'Pfandsystem', + description = string.format(Config.Locale['pfand_received'], pfandLabel), + type = 'success', + duration = 3000 + }) + end + end + end) + end + end + end + + -- Update the previous inventory state + playerInventories[playerId] = {} + for _, item in pairs(Player.PlayerData.items) do + if item then + playerInventories[playerId][item.name] = (playerInventories[playerId][item.name] or 0) + item.amount + end + end +end) + +-- Clean up player inventory tracking when they disconnect +AddEventHandler('playerDropped', function() + local src = source + if playerInventories[src] then + playerInventories[src] = nil + end +end) + +-- Redeem deposit RegisterNetEvent('qb-pfandsystem:server:redeemPfand', function(selectedItems) local src = source local Player = QBCore.Functions.GetPlayer(src) @@ -56,7 +133,7 @@ RegisterNetEvent('qb-pfandsystem:server:redeemPfand', function(selectedItems) local totalItems = 0 local canRedeem = true - -- Prüfe ob alle Items verfügbar sind + -- Check if all items are available for itemName, quantity in pairs(selectedItems) do if Config.PfandItems[itemName] then local item = Player.Functions.GetItemByName(itemName) @@ -73,7 +150,7 @@ RegisterNetEvent('qb-pfandsystem:server:redeemPfand', function(selectedItems) end if canRedeem then - -- Entferne Items und berechne Pfand + -- Remove items and calculate deposit for itemName, quantity in pairs(selectedItems) do if Config.PfandItems[itemName] then local removed = Player.Functions.RemoveItem(itemName, quantity) @@ -88,14 +165,14 @@ RegisterNetEvent('qb-pfandsystem:server:redeemPfand', function(selectedItems) end if totalPfand > 0 then - -- Gebe Geld + -- Give money if Config.Currency == 'cash' then Player.Functions.AddMoney('cash', totalPfand) else Player.Functions.AddMoney('bank', totalPfand) end - -- Formatiere Geld für Anzeige + -- Format money for display local moneyString = string.format("€%.2f", totalPfand / 100) TriggerClientEvent('ox_lib:notify', src, { @@ -112,7 +189,7 @@ RegisterNetEvent('qb-pfandsystem:server:redeemPfand', function(selectedItems) end end) --- Hole verfügbare Pfand Items des Spielers +-- Get available deposit items for the player QBCore.Functions.CreateCallback('qb-pfandsystem:server:getPfandItems', function(source, cb) local src = source local pfandItems = {} @@ -139,7 +216,7 @@ QBCore.Functions.CreateCallback('qb-pfandsystem:server:getPfandItems', function( cb(pfandItems) end) --- Zusätzliche Event-Handler für verschiedene Inventarsysteme +-- Additional event handlers for various inventory systems RegisterNetEvent('inventory:server:UseItem') AddEventHandler('inventory:server:UseItem', function(source, item) local itemName = type(item) == "table" and item.name or item @@ -151,7 +228,7 @@ AddEventHandler('inventory:server:UseItem', function(source, item) end end) --- Für tgiann-inventory +-- For tgiann-inventory RegisterNetEvent('tgiann-inventory:itemUsed') AddEventHandler('tgiann-inventory:itemUsed', function(source, itemName) if Config.ConsumableItems[itemName] then @@ -161,7 +238,7 @@ AddEventHandler('tgiann-inventory:itemUsed', function(source, itemName) end end) --- Debug-Befehl zum manuellen Auslösen +-- Debug command for manual triggering RegisterCommand('pfanditem', function(source, args) if Config.Debug then if args[1] and source > 0 then @@ -170,7 +247,7 @@ RegisterCommand('pfanditem', function(source, args) end end) --- Export für andere Scripts +-- Export for other scripts exports('ConsumePfandItem', function(source, itemName) TriggerEvent('qb-pfandsystem:server:itemConsumed', itemName) end)