Update main.lua
This commit is contained in:
		
							parent
							
								
									8ffed48f91
								
							
						
					
					
						commit
						ad74a2d686
					
				
					 1 changed files with 142 additions and 63 deletions
				
			
		| 
						 | 
				
			
			@ -17,29 +17,50 @@ RegisterNetEvent('qb-pfandsystem:server:itemConsumed', function(itemName)
 | 
			
		|||
        local randomChance = math.random(1, 100)
 | 
			
		||||
        
 | 
			
		||||
        if randomChance <= chance then
 | 
			
		||||
            -- Gebe Pfanditem mit tgiann-inventory Event
 | 
			
		||||
            TriggerEvent('tgiann-inventory:server:addItem', src, pfandItem, 1)
 | 
			
		||||
            
 | 
			
		||||
            if Config.ShowPfandNotification then
 | 
			
		||||
                local pfandLabel = Config.PfandItems[pfandItem] and Config.PfandItems[pfandItem].label or pfandItem
 | 
			
		||||
                
 | 
			
		||||
            -- 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
 | 
			
		||||
                TriggerClientEvent('ox_lib:notify', src, {
 | 
			
		||||
                    title = 'Pfandsystem',
 | 
			
		||||
                    description = string.format(Config.Locale['pfand_received'], pfandLabel),
 | 
			
		||||
                    type = 'success',
 | 
			
		||||
                    description = Config.Locale['pfand_bottle_broken'],
 | 
			
		||||
                    type = 'error',
 | 
			
		||||
                    duration = 3000
 | 
			
		||||
                })
 | 
			
		||||
            end
 | 
			
		||||
            
 | 
			
		||||
            if Config.Debug then
 | 
			
		||||
                print(string.format('[Pfandsystem] Spieler %s hat %s konsumiert und %s erhalten', 
 | 
			
		||||
                    Player.PlayerData.name, itemName, pfandItem))
 | 
			
		||||
                print(string.format('[Pfandsystem] Spieler %s hat %s konsumiert aber kein Pfand erhalten (%d%% Chance)', 
 | 
			
		||||
                    Player.PlayerData.name, itemName, chance))
 | 
			
		||||
            end
 | 
			
		||||
        end
 | 
			
		||||
    end
 | 
			
		||||
end)
 | 
			
		||||
 | 
			
		||||
-- Vereinfachte Pfand-Einlösung
 | 
			
		||||
-- Pfand einlösen
 | 
			
		||||
RegisterNetEvent('qb-pfandsystem:server:redeemPfand', function(selectedItems)
 | 
			
		||||
    local src = source
 | 
			
		||||
    local Player = QBCore.Functions.GetPlayer(src)
 | 
			
		||||
| 
						 | 
				
			
			@ -48,80 +69,138 @@ RegisterNetEvent('qb-pfandsystem:server:redeemPfand', function(selectedItems)
 | 
			
		|||
    
 | 
			
		||||
    local totalPfand = 0
 | 
			
		||||
    local totalItems = 0
 | 
			
		||||
    local itemsToRemove = {}
 | 
			
		||||
    
 | 
			
		||||
    -- Berechne Gesamtpfand und entferne Items
 | 
			
		||||
    -- Überprüfe ob Spieler die Items hat und berechne Gesamtpfand
 | 
			
		||||
    for itemName, quantity in pairs(selectedItems) do
 | 
			
		||||
        if Config.PfandItems[itemName] then
 | 
			
		||||
            -- Entferne Item direkt
 | 
			
		||||
            TriggerEvent('tgiann-inventory:server:removeItem', src, itemName, quantity)
 | 
			
		||||
            
 | 
			
		||||
            local pfandWert = Config.PfandItems[itemName].pfandwert * quantity
 | 
			
		||||
            totalPfand = totalPfand + pfandWert
 | 
			
		||||
            totalItems = totalItems + quantity
 | 
			
		||||
            -- 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)
 | 
			
		||||
        end
 | 
			
		||||
    end
 | 
			
		||||
    
 | 
			
		||||
    if totalPfand > 0 then
 | 
			
		||||
        -- Gebe Geld
 | 
			
		||||
        if Config.Currency == 'cash' then
 | 
			
		||||
            Player.Functions.AddMoney('cash', totalPfand)
 | 
			
		||||
    -- 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
 | 
			
		||||
                end
 | 
			
		||||
            end)
 | 
			
		||||
        end
 | 
			
		||||
        
 | 
			
		||||
        if removedSuccessfully then
 | 
			
		||||
            -- Gebe Geld
 | 
			
		||||
            if Config.Currency == 'cash' then
 | 
			
		||||
                Player.Functions.AddMoney('cash', totalPfand)
 | 
			
		||||
            else
 | 
			
		||||
                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
 | 
			
		||||
        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
 | 
			
		||||
    end
 | 
			
		||||
    end)
 | 
			
		||||
end)
 | 
			
		||||
 | 
			
		||||
-- Vereinfachte Item-Abfrage
 | 
			
		||||
-- 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
 | 
			
		||||
    
 | 
			
		||||
    -- Nutze Player.PlayerData.items direkt
 | 
			
		||||
    local Player = QBCore.Functions.GetPlayer(src)
 | 
			
		||||
    if not Player then 
 | 
			
		||||
    -- Zähle wie viele Items wir prüfen müssen
 | 
			
		||||
    for _ in pairs(Config.PfandItems) do
 | 
			
		||||
        totalItems = totalItems + 1
 | 
			
		||||
    end
 | 
			
		||||
    
 | 
			
		||||
    if totalItems == 0 then
 | 
			
		||||
        cb(pfandItems)
 | 
			
		||||
        return 
 | 
			
		||||
        return
 | 
			
		||||
    end
 | 
			
		||||
    
 | 
			
		||||
    for itemName, itemConfig in pairs(Config.PfandItems) do
 | 
			
		||||
        local itemCount = 0
 | 
			
		||||
        
 | 
			
		||||
        -- Durchsuche Spieler-Inventar
 | 
			
		||||
        for slot, item in pairs(Player.PlayerData.items) do
 | 
			
		||||
            if item and item.name == itemName then
 | 
			
		||||
                itemCount = itemCount + item.amount
 | 
			
		||||
        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
 | 
			
		||||
        end
 | 
			
		||||
        
 | 
			
		||||
        if itemCount > 0 then
 | 
			
		||||
            pfandItems[itemName] = {
 | 
			
		||||
                count = itemCount,
 | 
			
		||||
                label = itemConfig.label,
 | 
			
		||||
                pfandwert = itemConfig.pfandwert,
 | 
			
		||||
                totalWert = itemConfig.pfandwert * itemCount
 | 
			
		||||
            }
 | 
			
		||||
        end
 | 
			
		||||
            
 | 
			
		||||
            -- Wenn alle Items geprüft wurden, sende Ergebnis zurück
 | 
			
		||||
            if itemsChecked >= totalItems then
 | 
			
		||||
                cb(pfandItems)
 | 
			
		||||
            end
 | 
			
		||||
        end)
 | 
			
		||||
    end
 | 
			
		||||
    
 | 
			
		||||
    cb(pfandItems)
 | 
			
		||||
end)
 | 
			
		||||
 | 
			
		||||
-- Event-Handler für Item-Nutzung
 | 
			
		||||
RegisterNetEvent('tgiann-inventory:server:itemUsed', function(source, itemName, itemData)
 | 
			
		||||
-- Event-Handler für tgiann-inventory item usage
 | 
			
		||||
-- Basierend auf der Dokumentation
 | 
			
		||||
RegisterNetEvent('tgiann-inventory:itemUsed')
 | 
			
		||||
AddEventHandler('tgiann-inventory:itemUsed', function(source, itemName, itemData)
 | 
			
		||||
    if Config.ConsumableItems[itemName] then
 | 
			
		||||
        SetTimeout(100, function()
 | 
			
		||||
            TriggerEvent('qb-pfandsystem:server:itemConsumed', itemName)
 | 
			
		||||
        end)
 | 
			
		||||
    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)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue