From 0156a8765fb92e9746aba1f534975a5b4ed94920 Mon Sep 17 00:00:00 2001 From: Nordi98 Date: Mon, 28 Jul 2025 22:51:59 +0200 Subject: [PATCH] ed --- resources/[inventory]/nordi_pfand/config.lua | 6 +- .../[inventory]/nordi_pfand/server/main.lua | 180 ++++++------------ 2 files changed, 60 insertions(+), 126 deletions(-) diff --git a/resources/[inventory]/nordi_pfand/config.lua b/resources/[inventory]/nordi_pfand/config.lua index 99390cebf..524c25540 100644 --- a/resources/[inventory]/nordi_pfand/config.lua +++ b/resources/[inventory]/nordi_pfand/config.lua @@ -10,22 +10,18 @@ Config.PfandautomatProps = { Config.ConsumableItems = { ['beer'] = { pfandItem = 'empty_glasbootle', - chance = 100, -- 100% Chance Pfand zu bekommen label = 'Bier' }, ['water_bottle'] = { pfandItem = 'empty_bottle', - chance = 100, label = 'Wasserflasche' }, ['ecola_dose'] = { pfandItem = 'soda_can', - chance = 100, label = 'Cola' }, ['sprunk_dose'] = { pfandItem = 'soda_can', - chance = 100, label = 'Sprite' }, } @@ -62,5 +58,5 @@ Config.Locale = { ['processing'] = 'Verarbeite Pfand...', ['select_items'] = 'Wähle die Artikel aus, die du einlösen möchtest:', ['pfand_received'] = 'Du hast %s erhalten!', - ['pfand_bottle_broken'] = 'Die Flasche ist beim Trinken zerbrochen!' + } diff --git a/resources/[inventory]/nordi_pfand/server/main.lua b/resources/[inventory]/nordi_pfand/server/main.lua index 1356e4272..056d98b10 100644 --- a/resources/[inventory]/nordi_pfand/server/main.lua +++ b/resources/[inventory]/nordi_pfand/server/main.lua @@ -11,50 +11,32 @@ RegisterNetEvent('qb-pfandsystem:server:itemConsumed', function(itemName) if Config.ConsumableItems[itemName] then local consumableConfig = Config.ConsumableItems[itemName] local pfandItem = consumableConfig.pfandItem - local chance = consumableConfig.chance or 100 - -- Würfle ob Pfand erhalten wird - local randomChance = math.random(1, 100) + -- Verwende Player.Functions.AddItem statt tgiann-inventory + local success = Player.Functions.AddItem(pfandItem, 1) - if randomChance <= chance then - -- Gebe Pfanditem mit korrektem tgiann-inventory Export - local success = exports['tgiann-inventory']:AddItem(src, pfandItem, 1, nil, nil, function(success, reason) - if success then - if Config.ShowPfandNotification then - local pfandLabel = Config.PfandItems[pfandItem] and Config.PfandItems[pfandItem].label or pfandItem - - TriggerClientEvent('ox_lib:notify', src, { - title = 'Pfandsystem', - description = string.format(Config.Locale['pfand_received'], pfandLabel), - type = 'success', - duration = 3000 - }) - end - - if Config.Debug then - print(string.format('[Pfandsystem] Spieler %s hat %s konsumiert und %s erhalten', - Player.PlayerData.name, itemName, pfandItem)) - end - else - if Config.Debug then - print(string.format('[Pfandsystem] Fehler beim Hinzufügen von %s: %s', pfandItem, reason or 'Unbekannt')) - end - end - end) - else - -- Pfand nicht erhalten (z.B. Flasche zerbrochen) - if Config.ShowPfandNotification and chance < 100 then + if success then + -- Trigger inventory update + TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[pfandItem], "add", 1) + + if Config.ShowPfandNotification then + local pfandLabel = Config.PfandItems[pfandItem] and Config.PfandItems[pfandItem].label or pfandItem + TriggerClientEvent('ox_lib:notify', src, { title = 'Pfandsystem', - description = Config.Locale['pfand_bottle_broken'], - type = 'error', + description = string.format(Config.Locale['pfand_received'], pfandLabel), + type = 'success', duration = 3000 }) end if Config.Debug then - print(string.format('[Pfandsystem] Spieler %s hat %s konsumiert aber kein Pfand erhalten (%d%% Chance)', - Player.PlayerData.name, itemName, chance)) + print(string.format('[Pfandsystem] Spieler %s hat %s konsumiert und %s erhalten', + Player.PlayerData.name, itemName, pfandItem)) + end + else + if Config.Debug then + print(string.format('[Pfandsystem] Fehler beim Hinzufügen von %s für Spieler %s', pfandItem, Player.PlayerData.name)) end end end @@ -69,55 +51,40 @@ RegisterNetEvent('qb-pfandsystem:server:redeemPfand', function(selectedItems) local totalPfand = 0 local totalItems = 0 - local itemsToRemove = {} + local canRedeem = true - -- Überprüfe ob Spieler die Items hat und berechne Gesamtpfand + -- Prüfe ob alle Items verfügbar sind for itemName, quantity in pairs(selectedItems) do if Config.PfandItems[itemName] then - -- Verwende tgiann-inventory Export um Item zu prüfen - exports['tgiann-inventory']:GetItem(src, itemName, function(item) - if item and item.count >= quantity then - local pfandWert = Config.PfandItems[itemName].pfandwert * quantity - totalPfand = totalPfand + pfandWert - totalItems = totalItems + quantity - itemsToRemove[itemName] = quantity - else - TriggerClientEvent('ox_lib:notify', src, { - title = 'Pfandsystem', - description = 'Du hast nicht genug ' .. (Config.PfandItems[itemName].label or itemName), - type = 'error' - }) - return - end - end) + local item = Player.Functions.GetItemByName(itemName) + if not item or item.amount < quantity then + canRedeem = false + TriggerClientEvent('ox_lib:notify', src, { + title = 'Pfandsystem', + description = 'Du hast nicht genug ' .. (Config.PfandItems[itemName].label or itemName), + type = 'error' + }) + return + end end end - -- Warte kurz damit alle Callbacks verarbeitet werden - SetTimeout(500, function() - if totalPfand <= 0 then - TriggerClientEvent('ox_lib:notify', src, { - title = 'Pfandsystem', - description = Config.Locale['no_pfand_items'], - type = 'error' - }) - return - end - - -- Entferne Items aus dem Inventar - local removedSuccessfully = true - for itemName, quantity in pairs(itemsToRemove) do - exports['tgiann-inventory']:RemoveItem(src, itemName, quantity, nil, function(success, reason) - if not success then - removedSuccessfully = false - if Config.Debug then - print(string.format('[Pfandsystem] Fehler beim Entfernen von %s: %s', itemName, reason or 'Unbekannt')) - end + if canRedeem then + -- Entferne Items und berechne Pfand + for itemName, quantity in pairs(selectedItems) do + if Config.PfandItems[itemName] then + local removed = Player.Functions.RemoveItem(itemName, quantity) + if removed then + TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[itemName], "remove", quantity) + + local pfandWert = Config.PfandItems[itemName].pfandwert * quantity + totalPfand = totalPfand + pfandWert + totalItems = totalItems + quantity end - end) + end end - if removedSuccessfully then + if totalPfand > 0 then -- Gebe Geld if Config.Currency == 'cash' then Player.Functions.AddMoney('cash', totalPfand) @@ -138,56 +105,38 @@ RegisterNetEvent('qb-pfandsystem:server:redeemPfand', function(selectedItems) print(string.format('[Pfandsystem] Spieler %s hat %d Items für €%.2f eingelöst', Player.PlayerData.name, totalItems, totalPfand / 100)) end - else - TriggerClientEvent('ox_lib:notify', src, { - title = 'Pfandsystem', - description = Config.Locale['pfand_error'], - type = 'error' - }) end - end) + end end) -- Hole verfügbare Pfand Items des Spielers QBCore.Functions.CreateCallback('qb-pfandsystem:server:getPfandItems', function(source, cb) local src = source local pfandItems = {} - local itemsChecked = 0 - local totalItems = 0 + local Player = QBCore.Functions.GetPlayer(src) - -- Zähle wie viele Items wir prüfen müssen - for _ in pairs(Config.PfandItems) do - totalItems = totalItems + 1 - end - - if totalItems == 0 then + if not Player then cb(pfandItems) - return + return end for itemName, itemConfig in pairs(Config.PfandItems) do - exports['tgiann-inventory']:GetItem(src, itemName, function(item) - itemsChecked = itemsChecked + 1 - - if item and item.count > 0 then - pfandItems[itemName] = { - count = item.count, - label = itemConfig.label, - pfandwert = itemConfig.pfandwert, - totalWert = itemConfig.pfandwert * item.count - } - end - - -- Wenn alle Items geprüft wurden, sende Ergebnis zurück - if itemsChecked >= totalItems then - cb(pfandItems) - end - end) + local item = Player.Functions.GetItemByName(itemName) + + if item and item.amount > 0 then + pfandItems[itemName] = { + count = item.amount, + label = itemConfig.label, + pfandwert = itemConfig.pfandwert, + totalWert = itemConfig.pfandwert * item.amount + } + end end + + cb(pfandItems) end) --- Event-Handler für tgiann-inventory item usage --- Basierend auf der Dokumentation +-- Event-Handler für Item-Nutzung RegisterNetEvent('tgiann-inventory:itemUsed') AddEventHandler('tgiann-inventory:itemUsed', function(source, itemName, itemData) if Config.ConsumableItems[itemName] then @@ -197,17 +146,6 @@ AddEventHandler('tgiann-inventory:itemUsed', function(source, itemName, itemData end end) --- Alternative Event (falls der obere nicht funktioniert) -RegisterNetEvent('inventory:server:UseItem') -AddEventHandler('inventory:server:UseItem', function(itemName, itemData) - local src = source - if Config.ConsumableItems[itemName] then - SetTimeout(100, function() - TriggerEvent('qb-pfandsystem:server:itemConsumed', itemName) - end) - end -end) - -- Export für andere Scripts exports('ConsumePfandItem', function(source, itemName) TriggerEvent('qb-pfandsystem:server:itemConsumed', itemName)