207 lines
		
	
	
	
		
			6.7 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
			
		
		
	
	
			207 lines
		
	
	
	
		
			6.7 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
local QBCore = exports['qb-core']:GetCoreObject()
 | 
						|
 | 
						|
-- Debug Print Funktion
 | 
						|
local function Debug(msg)
 | 
						|
    print("^2[Coffee Debug] ^7" .. msg)
 | 
						|
end
 | 
						|
 | 
						|
-- Cache system for item checks
 | 
						|
local itemCheckCache = {}
 | 
						|
local lastCheckTime = {}
 | 
						|
local checkCooldown = 1000 -- 1 second cooldown between checks for the same item
 | 
						|
 | 
						|
-- Function to check if player has an item
 | 
						|
local function HasItem(itemName, amount, callback)
 | 
						|
    -- Check cache first (to avoid spamming the server)
 | 
						|
    local cacheKey = itemName .. "_" .. amount
 | 
						|
    local currentTime = GetGameTimer()
 | 
						|
    if itemCheckCache[cacheKey] ~= nil and lastCheckTime[cacheKey] and 
 | 
						|
       (currentTime - lastCheckTime[cacheKey]) < checkCooldown then
 | 
						|
        callback(itemCheckCache[cacheKey])
 | 
						|
        return
 | 
						|
    end
 | 
						|
    
 | 
						|
    -- Create a unique event name for this check
 | 
						|
    local uniqueEventName = 'coffee-script:itemCheckResult:' .. math.random(100000, 999999)
 | 
						|
    
 | 
						|
    -- Register the event handler
 | 
						|
    local eventHandler = RegisterNetEvent(uniqueEventName)
 | 
						|
    AddEventHandler(uniqueEventName, function(hasItem)
 | 
						|
        itemCheckCache[cacheKey] = hasItem
 | 
						|
        lastCheckTime[cacheKey] = GetGameTimer()
 | 
						|
        callback(hasItem)
 | 
						|
    end)
 | 
						|
    
 | 
						|
    -- Request the check from server with our unique event name
 | 
						|
    TriggerServerEvent('coffee-script:checkItem', itemName, amount, uniqueEventName)
 | 
						|
    
 | 
						|
    -- Set a timeout to prevent hanging if something goes wrong
 | 
						|
    SetTimeout(1000, function()
 | 
						|
        if not lastCheckTime[cacheKey] or (currentTime - lastCheckTime[cacheKey]) >= checkCooldown then
 | 
						|
            callback(false)
 | 
						|
        end
 | 
						|
    end)
 | 
						|
end
 | 
						|
 | 
						|
CreateThread(function()
 | 
						|
    Debug("Script starting...")
 | 
						|
    for _, prop in pairs(Config.CoffeeProps) do
 | 
						|
        exports['qb-target']:AddTargetModel(prop, {
 | 
						|
            options = {
 | 
						|
                {
 | 
						|
                    num = 1,
 | 
						|
                    type = "client",
 | 
						|
                    event = "nordi_coffeemachine:client:OpenMenu",
 | 
						|
                    icon = 'fas fa-coffee',
 | 
						|
                    label = 'Kaffee zubereiten',
 | 
						|
                }
 | 
						|
            },
 | 
						|
            distance = 2.0
 | 
						|
        })
 | 
						|
    end
 | 
						|
    Debug("Target options registered")
 | 
						|
end)
 | 
						|
 | 
						|
-- Event Handler für das Öffnen des Menüs
 | 
						|
RegisterNetEvent('nordi_coffeemachine:client:OpenMenu')
 | 
						|
AddEventHandler('nordi_coffeemachine:client:OpenMenu', function()
 | 
						|
    Debug("Opening menu...")
 | 
						|
    OpenCoffeeMenu()
 | 
						|
end)
 | 
						|
 | 
						|
function CheckIngredients(requirements, callback)
 | 
						|
    local hasItems = true
 | 
						|
    local missingItems = {}
 | 
						|
    local checkedItems = 0
 | 
						|
    local totalItems = #requirements
 | 
						|
    
 | 
						|
    if totalItems == 0 then
 | 
						|
        callback(true, {})
 | 
						|
        return
 | 
						|
    end
 | 
						|
    
 | 
						|
    for _, requirement in ipairs(requirements) do
 | 
						|
        HasItem(requirement.item, requirement.amount, function(hasItem)
 | 
						|
            checkedItems = checkedItems + 1
 | 
						|
            
 | 
						|
            if not hasItem then
 | 
						|
                hasItems = false
 | 
						|
                table.insert(missingItems, {
 | 
						|
                    item = requirement.item,
 | 
						|
                    required = requirement.amount
 | 
						|
                })
 | 
						|
            end
 | 
						|
            
 | 
						|
            if checkedItems == totalItems then
 | 
						|
                callback(hasItems, missingItems)
 | 
						|
            end
 | 
						|
        end)
 | 
						|
    end
 | 
						|
end
 | 
						|
 | 
						|
function ShowMissingIngredientsWarning(missingItems)
 | 
						|
    local warningText = "Fehlende Zutaten:\n"
 | 
						|
    for _, item in ipairs(missingItems) do
 | 
						|
        local itemLabel = QBCore.Shared.Items[item.item].label
 | 
						|
        warningText = warningText .. "- " .. itemLabel .. " (benötigt: " .. item.required .. ")\n"
 | 
						|
    end
 | 
						|
    
 | 
						|
    QBCore.Functions.Notify(warningText, "error", 5000)
 | 
						|
end
 | 
						|
 | 
						|
function OpenCoffeeMenu()
 | 
						|
    Debug("Building menu options...")
 | 
						|
    local options = {}
 | 
						|
    local coffeeChecked = 0
 | 
						|
    local totalCoffees = #Config.CoffeeOptions
 | 
						|
    
 | 
						|
    for _, coffee in ipairs(Config.CoffeeOptions) do
 | 
						|
        CheckIngredients(coffee.requires, function(hasIngredients, missingItems)
 | 
						|
            coffeeChecked = coffeeChecked + 1
 | 
						|
            
 | 
						|
            local description = coffee.description .. "\n\nBenötigt:"
 | 
						|
            
 | 
						|
            for _, req in ipairs(coffee.requires) do
 | 
						|
                local itemLabel = QBCore.Shared.Items[req.item].label
 | 
						|
                local hasItem = not table.find(missingItems, function(item) return item.item == req.item end)
 | 
						|
                local status = hasItem and "~g~✓" or "~r~✗"
 | 
						|
                description = description .. "\n- " .. req.amount .. "x " .. itemLabel .. " " .. status
 | 
						|
            end
 | 
						|
            
 | 
						|
            table.insert(options, {
 | 
						|
                title = coffee.label,
 | 
						|
                description = description,
 | 
						|
                icon = coffee.icon,
 | 
						|
                onSelect = function()
 | 
						|
                    CheckIngredients(coffee.requires, function(canMake, missingItems)
 | 
						|
                        if canMake then
 | 
						|
                            PrepareCoffee(coffee)
 | 
						|
                        else
 | 
						|
                            ShowMissingIngredientsWarning(missingItems)
 | 
						|
                        end
 | 
						|
                    end)
 | 
						|
                end
 | 
						|
            })
 | 
						|
            
 | 
						|
            if coffeeChecked == totalCoffees then
 | 
						|
                ShowCoffeeMenu(options)
 | 
						|
            end
 | 
						|
        end)
 | 
						|
    end
 | 
						|
end
 | 
						|
 | 
						|
-- Helper function to find in table
 | 
						|
function table.find(t, cb)
 | 
						|
    for _, v in ipairs(t) do
 | 
						|
        if cb(v) then
 | 
						|
            return true
 | 
						|
        end
 | 
						|
    end
 | 
						|
    return false
 | 
						|
end
 | 
						|
 | 
						|
function ShowCoffeeMenu(options)
 | 
						|
    Debug("Showing menu...")
 | 
						|
    lib.registerContext({
 | 
						|
        id = 'coffee_menu',
 | 
						|
        title = 'Kaffeeautomat',
 | 
						|
        options = options
 | 
						|
    })
 | 
						|
 | 
						|
    lib.showContext('coffee_menu')
 | 
						|
end
 | 
						|
 | 
						|
function PrepareCoffee(selectedCoffee)
 | 
						|
    Debug("Starting coffee preparation...")
 | 
						|
    local player = PlayerPedId()
 | 
						|
    local animDict = "mini@drinking"
 | 
						|
    local anim = "shots_barman_b"
 | 
						|
    
 | 
						|
    RequestAnimDict(animDict)
 | 
						|
    while not HasAnimDictLoaded(animDict) do
 | 
						|
        Wait(0)
 | 
						|
    end
 | 
						|
 | 
						|
    QBCore.Functions.Progressbar("get_coffee", selectedCoffee.label.." wird zubereitet...", Config.ProgressTime or 5000, false, true, {
 | 
						|
        disableMovement = true,
 | 
						|
        disableCarMovement = true,
 | 
						|
        disableMouse = false,
 | 
						|
        disableCombat = true,
 | 
						|
    }, {
 | 
						|
        animDict = animDict,
 | 
						|
        anim = anim,
 | 
						|
        flags = 49,
 | 
						|
    }, {}, {}, function() -- Erfolg
 | 
						|
        Debug("Coffee preparation successful, triggering server event...")
 | 
						|
        TriggerServerEvent('coffee-script:giveCoffee', selectedCoffee.item, selectedCoffee.requires)
 | 
						|
    end, function() -- Abgebrochen
 | 
						|
        Debug("Coffee preparation cancelled")
 | 
						|
        QBCore.Functions.Notify("Zubereitung abgebrochen", "error")
 | 
						|
    end)
 | 
						|
end
 | 
						|
 | 
						|
-- Debug Event
 | 
						|
RegisterNetEvent('coffee-script:debug')
 | 
						|
AddEventHandler('coffee-script:debug', function(msg)
 | 
						|
    Debug(msg)
 | 
						|
end)
 |