Main/resources/[inventory]/nordi_coffeemachine/client.lua
2025-06-07 08:51:21 +02:00

143 lines
4.3 KiB
Lua

local QBCore = exports['qb-core']:GetCoreObject()
-- Debug Print Funktion
local function Debug(msg)
print("^2[Coffee Debug] ^7" .. msg)
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)
local hasItems = true
local missingItems = {}
for _, requirement in ipairs(requirements) do
local hasItem = QBCore.Functions.HasItem(requirement.item, requirement.amount)
if not hasItem then
hasItems = false
table.insert(missingItems, {
item = requirement.item,
required = requirement.amount
})
end
end
return hasItems, missingItems
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 = {}
for _, coffee in ipairs(Config.CoffeeOptions) do
local hasIngredients, missing = CheckIngredients(coffee.requires)
local description = coffee.description .. "\n\nBenötigt:"
for _, req in ipairs(coffee.requires) do
local itemLabel = QBCore.Shared.Items[req.item].label
local hasItem = QBCore.Functions.HasItem(req.item, req.amount)
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()
local canMake, missingItems = CheckIngredients(coffee.requires)
if canMake then
PrepareCoffee(coffee)
else
ShowMissingIngredientsWarning(missingItems)
end
end
})
end
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)