81 lines
		
	
	
	
		
			3 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
			
		
		
	
	
			81 lines
		
	
	
	
		
			3 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
local QBCore = exports['qb-core']:GetCoreObject()
 | 
						|
 | 
						|
-- Debug Print Funktion
 | 
						|
local function Debug(msg)
 | 
						|
    print("^2[Coffee Debug] ^7" .. msg)
 | 
						|
end
 | 
						|
 | 
						|
-- Function to check if player has an item
 | 
						|
local function HasItem(source, itemName, amount)
 | 
						|
    local items = exports["tgiann-inventory"]:GetPlayerItems(source)
 | 
						|
    if not items then return false end
 | 
						|
    
 | 
						|
    local count = 0
 | 
						|
    for _, item in pairs(items) do
 | 
						|
        if item.name == itemName then
 | 
						|
            count = count + item.count
 | 
						|
            if count >= amount then
 | 
						|
                return true
 | 
						|
            end
 | 
						|
        end
 | 
						|
    end
 | 
						|
    
 | 
						|
    return false
 | 
						|
end
 | 
						|
 | 
						|
-- Event for client to check if player has an item
 | 
						|
RegisterNetEvent('coffee-script:checkItem', function(itemName, amount, callbackEvent)
 | 
						|
    local src = source
 | 
						|
    local hasItem = HasItem(src, itemName, amount)
 | 
						|
    TriggerClientEvent(callbackEvent, src, hasItem)
 | 
						|
end)
 | 
						|
 | 
						|
RegisterNetEvent('coffee-script:giveCoffee')
 | 
						|
AddEventHandler('coffee-script:giveCoffee', function(itemName, requirements)
 | 
						|
    Debug("Give coffee event triggered")
 | 
						|
    local src = source
 | 
						|
    local Player = QBCore.Functions.GetPlayer(src)
 | 
						|
 | 
						|
    if Player then
 | 
						|
        -- Überprüfe ob das Item in den erlaubten Kaffee-Optionen ist
 | 
						|
        local isValidItem = false
 | 
						|
        for _, coffee in ipairs(Config.CoffeeOptions) do
 | 
						|
            if coffee.item == itemName then
 | 
						|
                isValidItem = true
 | 
						|
                break
 | 
						|
            end
 | 
						|
        end
 | 
						|
 | 
						|
        if isValidItem then
 | 
						|
            Debug("Valid coffee item requested")
 | 
						|
            -- Überprüfe nochmal die Zutaten
 | 
						|
            local hasAllItems = true
 | 
						|
            for _, requirement in ipairs(requirements) do
 | 
						|
                if not HasItem(src, requirement.item, requirement.amount) then
 | 
						|
                    hasAllItems = false
 | 
						|
                    break
 | 
						|
                end
 | 
						|
            end
 | 
						|
 | 
						|
            if hasAllItems then
 | 
						|
                Debug("Player has all required items")
 | 
						|
                -- Entferne die benötigten Items
 | 
						|
                for _, requirement in ipairs(requirements) do
 | 
						|
                    exports["tgiann-inventory"]:RemoveItem(src, requirement.item, requirement.amount)
 | 
						|
                    TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[requirement.item], "remove")
 | 
						|
                end
 | 
						|
 | 
						|
                -- Gebe das fertige Getränk
 | 
						|
                exports["tgiann-inventory"]:AddItem(src, itemName, 1)
 | 
						|
                TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[itemName], "add")
 | 
						|
                TriggerClientEvent('QBCore:Notify', src, "Du hast einen " .. QBCore.Shared.Items[itemName].label .. " zubereitet!", "success")
 | 
						|
            else
 | 
						|
                Debug("Player missing required items")
 | 
						|
                TriggerClientEvent('QBCore:Notify', src, "Du hast nicht alle benötigten Zutaten!", "error")
 | 
						|
            end
 | 
						|
        else
 | 
						|
            Debug("Invalid coffee item requested: " .. itemName)
 | 
						|
            TriggerClientEvent('QBCore:Notify', src, "Fehler bei der Kaffeezubereitung!", "error")
 | 
						|
        end
 | 
						|
    end
 | 
						|
end)
 |