diff --git a/resources/[inventory]/nordi_pfand/server/main.lua b/resources/[inventory]/nordi_pfand/server/main.lua index fbd04ef42..f6853cf91 100644 --- a/resources/[inventory]/nordi_pfand/server/main.lua +++ b/resources/[inventory]/nordi_pfand/server/main.lua @@ -17,10 +17,10 @@ RegisterNetEvent('qb-pfandsystem:server:itemConsumed', function(itemName) local randomChance = math.random(1, 100) if randomChance <= chance then - -- Gebe Pfanditem - local success = exports['tgiann-inventory']:AddItem(src, pfandItem, 1) + -- Gebe Pfanditem mit tgiann-inventory + TriggerEvent('tgiann-inventory:server:addItem', src, pfandItem, 1) - if success and Config.ShowPfandNotification then + if Config.ShowPfandNotification then local pfandLabel = Config.PfandItems[pfandItem] and Config.PfandItems[pfandItem].label or pfandItem TriggerClientEvent('ox_lib:notify', src, { @@ -68,99 +68,113 @@ RegisterNetEvent('qb-pfandsystem:server:redeemPfand', function(selectedItems) -- Überprüfe ob Spieler die Items hat und berechne Gesamtpfand for itemName, quantity in pairs(selectedItems) do if Config.PfandItems[itemName] then - local playerItem = exports['tgiann-inventory']:GetItem(src, itemName) - - if playerItem and playerItem.count >= quantity then - local pfandWert = Config.PfandItems[itemName].pfandwert * quantity - totalPfand = totalPfand + pfandWert - totalItems = totalItems + quantity - itemsToRemove[itemName] = quantity + -- Verwende tgiann-inventory callback um Item zu prüfen + QBCore.Functions.TriggerCallback('tgiann-inventory:server:getItem', function(playerItem) + if playerItem and playerItem.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, src, itemName) + end + end + + -- Warte kurz damit alle Callbacks verarbeitet werden + SetTimeout(100, 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 success = true + for itemName, quantity in pairs(itemsToRemove) do + TriggerEvent('tgiann-inventory:server:removeItem', src, itemName, quantity) + end + + if success then + -- Gebe Geld + if Config.Currency == 'cash' then + Player.Functions.AddMoney('cash', totalPfand) else - TriggerClientEvent('ox_lib:notify', src, { - title = 'Pfandsystem', - description = 'Du hast nicht genug ' .. (Config.PfandItems[itemName].label or itemName), - type = 'error' - }) - return + Player.Functions.AddMoney('bank', totalPfand) + end + + -- Formatiere Geld für Anzeige + local moneyString = string.format("€%.2f", totalPfand / 100) + + TriggerClientEvent('ox_lib:notify', src, { + title = 'Pfandsystem', + description = string.format(Config.Locale['pfand_success'], moneyString, totalItems), + type = 'success' + }) + + if Config.Debug then + print(string.format('[Pfandsystem] Spieler %s hat %d Items für €%.2f eingelöst', + Player.PlayerData.name, totalItems, totalPfand / 100)) end - end - end - - 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 success = true - for itemName, quantity in pairs(itemsToRemove) do - local removed = exports['tgiann-inventory']:RemoveItem(src, itemName, quantity) - if not removed then - success = false - break - end - end - - if success then - -- Gebe Geld - if Config.Currency == 'cash' then - Player.Functions.AddMoney('cash', totalPfand) else - Player.Functions.AddMoney('bank', totalPfand) + TriggerClientEvent('ox_lib:notify', src, { + title = 'Pfandsystem', + description = Config.Locale['pfand_error'], + type = 'error' + }) end - - -- Formatiere Geld für Anzeige - local moneyString = string.format("€%.2f", totalPfand / 100) - - TriggerClientEvent('ox_lib:notify', src, { - title = 'Pfandsystem', - description = string.format(Config.Locale['pfand_success'], moneyString, totalItems), - type = 'success' - }) - - if Config.Debug then - 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) -- 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 - for itemName, itemConfig in pairs(Config.PfandItems) do - local playerItem = exports['tgiann-inventory']:GetItem(src, itemName) - - if playerItem and playerItem.count > 0 then - pfandItems[itemName] = { - count = playerItem.count, - label = itemConfig.label, - pfandwert = itemConfig.pfandwert, - totalWert = itemConfig.pfandwert * playerItem.count - } - end + -- Zähle wie viele Items wir prüfen müssen + for _ in pairs(Config.PfandItems) do + totalItems = totalItems + 1 end - cb(pfandItems) + if totalItems == 0 then + cb(pfandItems) + return + end + + for itemName, itemConfig in pairs(Config.PfandItems) do + QBCore.Functions.TriggerCallback('tgiann-inventory:server:getItem', function(playerItem) + itemsChecked = itemsChecked + 1 + + if playerItem and playerItem.count > 0 then + pfandItems[itemName] = { + count = playerItem.count, + label = itemConfig.label, + pfandwert = itemConfig.pfandwert, + totalWert = itemConfig.pfandwert * playerItem.count + } + end + + -- Wenn alle Items geprüft wurden, sende Ergebnis zurück + if itemsChecked >= totalItems then + cb(pfandItems) + end + end, src, itemName) + end end) --- Hook für tgiann-inventory item usage --- Dieser Hook wird ausgelöst wenn ein Item verwendet wird -RegisterNetEvent('tgiann-inventory:server:UseItem', function(itemName, itemData) - local src = source - +-- Korrekte Event-Handler für tgiann-inventory +RegisterNetEvent('tgiann-inventory:server:itemUsed', function(source, itemName, itemData) -- Prüfe ob das verwendete Item in unserer Konsumierbare Items Liste ist if Config.ConsumableItems[itemName] then -- Warte kurz damit das originale Item erst konsumiert wird @@ -170,8 +184,16 @@ RegisterNetEvent('tgiann-inventory:server:UseItem', function(itemName, itemData) end end) --- Alternative: Falls der obige Hook nicht funktioniert, kann man auch einen Export verwenden --- Dieser Export kann von anderen Scripten aufgerufen werden +-- Alternative: Nutze den korrekten Event Namen basierend auf der Dokumentation +RegisterNetEvent('tgiann-inventory:itemUsed', function(source, itemName, itemData) + 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) end)