1
0
Fork 0
forked from Simnation/Main
Main/resources/[inventory]/nordi_bbq/client.lua

227 lines
7.1 KiB
Lua
Raw Normal View History

2025-06-18 02:30:02 +02:00
local QBCore = exports['qb-core']:GetCoreObject()
2025-06-18 05:39:08 +02:00
-- Check if lib exists (ox_lib)
if not lib then
print("^1ERROR: lib is not defined. Make sure ox_lib is properly installed and loaded.^7")
end
2025-06-18 02:30:02 +02:00
-- Debug Print Function
local function Debug(msg)
2025-06-18 05:39:08 +02:00
print("^2[Grill Debug] ^7" .. msg)
2025-06-18 02:30:02 +02:00
end
2025-06-18 05:39:08 +02:00
-- Wait for QBCore to be fully initialized
2025-06-18 02:30:02 +02:00
CreateThread(function()
2025-06-18 05:39:08 +02:00
while not QBCore do
Wait(100)
end
while not QBCore.Shared or not QBCore.Shared.Items do
Debug("Waiting for QBCore.Shared.Items to be initialized...")
Wait(1000)
end
2025-06-18 02:30:02 +02:00
Debug("Script starting...")
2025-06-18 05:39:08 +02:00
for _, prop in pairs(Config.GrillProps) do
2025-06-18 02:30:02 +02:00
exports['qb-target']:AddTargetModel(prop, {
options = {
{
num = 1,
type = "client",
2025-06-18 06:00:28 +02:00
event = "nordi_bbq:client:OpenMenu",
2025-06-18 05:39:08 +02:00
icon = 'fas fa-fire',
label = 'Grillen',
2025-06-18 02:30:02 +02:00
}
},
distance = 2.0
})
end
Debug("Target options registered")
end)
-- Event Handler for opening the menu
2025-06-18 06:04:36 +02:00
RegisterNetEvent('nordi_bbq:client:OpenMenu')
AddEventHandler('nordi_bbq:client:OpenMenu', function()
2025-06-18 02:30:02 +02:00
Debug("Opening menu...")
2025-06-18 05:39:08 +02:00
OpenGrillMenu()
2025-06-18 02:30:02 +02:00
end)
function CheckIngredients(requirements)
local hasItems = true
local missingItems = {}
2025-06-18 05:47:41 +02:00
if not requirements then
Debug("FEHLER: requirements ist nil")
return false, {}
end
2025-06-18 02:30:02 +02:00
for _, requirement in ipairs(requirements) do
2025-06-18 05:47:41 +02:00
if not requirement or not requirement.item then
Debug("FEHLER: requirement oder requirement.item ist nil")
hasItems = false
goto continue_req
end
local amount = requirement.amount or 1
local hasItem = QBCore.Functions.HasItem(requirement.item, amount)
2025-06-18 02:30:02 +02:00
if not hasItem then
hasItems = false
table.insert(missingItems, {
item = requirement.item,
2025-06-18 05:47:41 +02:00
required = amount
2025-06-18 02:30:02 +02:00
})
end
2025-06-18 05:47:41 +02:00
::continue_req::
2025-06-18 02:30:02 +02:00
end
return hasItems, missingItems
end
function ShowMissingIngredientsWarning(missingItems)
local warningText = "Fehlende Zutaten:\n"
for _, item in ipairs(missingItems) do
2025-06-18 05:47:41 +02:00
if not item or not item.item then
Debug("FEHLER: item oder item.item ist nil")
goto continue_missing
end
2025-06-18 05:39:08 +02:00
local itemLabel = item.item -- Default to item name if label not found
-- Safely check if the item exists in QBCore.Shared.Items
if QBCore.Shared and QBCore.Shared.Items and QBCore.Shared.Items[item.item] and QBCore.Shared.Items[item.item].label then
itemLabel = QBCore.Shared.Items[item.item].label
2025-06-18 05:33:48 +02:00
end
2025-06-18 05:39:08 +02:00
warningText = warningText .. "- " .. itemLabel .. " (benötigt: " .. item.required .. ")\n"
2025-06-18 05:47:41 +02:00
::continue_missing::
2025-06-18 02:30:02 +02:00
end
QBCore.Functions.Notify(warningText, "error", 5000)
end
2025-06-18 05:33:48 +02:00
function OpenGrillMenu()
2025-06-18 02:30:02 +02:00
Debug("Building menu options...")
2025-06-18 05:39:08 +02:00
-- Make sure Config.GrillOptions exists
if not Config or not Config.GrillOptions then
Debug("ERROR: Config.GrillOptions is nil")
QBCore.Functions.Notify("Fehler beim Laden des Grillmenüs", "error")
return
end
2025-06-18 02:30:02 +02:00
local options = {}
2025-06-18 05:33:48 +02:00
for _, food in ipairs(Config.GrillOptions) do
2025-06-18 05:39:08 +02:00
-- Make sure food.requires exists
2025-06-18 05:47:41 +02:00
if not food or not food.requires then
Debug("ERROR: food oder food.requires ist nil für " .. (food.label or "unbekanntes Essen"))
2025-06-18 05:39:08 +02:00
goto continue
end
2025-06-18 02:30:02 +02:00
local hasIngredients, missing = CheckIngredients(food.requires)
2025-06-18 05:47:41 +02:00
local description = (food.description or "Keine Beschreibung") .. "\n\nBenötigt:"
2025-06-18 02:30:02 +02:00
for _, req in ipairs(food.requires) do
2025-06-18 05:39:08 +02:00
-- Make sure req.item exists
2025-06-18 05:47:41 +02:00
if not req or not req.item then
Debug("ERROR: req oder req.item ist nil")
2025-06-18 05:39:08 +02:00
goto continue_req
end
local itemLabel = req.item -- Default to item name if label not found
local hasItem = false
-- Safely check if the item exists in QBCore.Shared.Items
2025-06-18 05:47:41 +02:00
if QBCore.Shared and QBCore.Shared.Items and QBCore.Shared.Items[req.item] then
if QBCore.Shared.Items[req.item].label then
itemLabel = QBCore.Shared.Items[req.item].label
end
hasItem = QBCore.Functions.HasItem(req.item, req.amount or 1)
2025-06-18 05:33:48 +02:00
else
2025-06-18 05:47:41 +02:00
Debug("Warnung: Item " .. req.item .. " nicht in QBCore.Shared.Items gefunden")
2025-06-18 05:33:48 +02:00
end
2025-06-18 05:39:08 +02:00
local status = hasItem and "~g~✓" or "~r~✗"
2025-06-18 05:47:41 +02:00
description = description .. "\n- " .. (req.amount or 1) .. "x " .. itemLabel .. " " .. status
2025-06-18 05:39:08 +02:00
::continue_req::
2025-06-18 02:30:02 +02:00
end
table.insert(options, {
2025-06-18 05:47:41 +02:00
title = food.label or "Unbekanntes Essen",
2025-06-18 02:30:02 +02:00
description = description,
2025-06-18 05:39:08 +02:00
icon = food.icon or "fas fa-question",
2025-06-18 02:30:02 +02:00
onSelect = function()
local canMake, missingItems = CheckIngredients(food.requires)
if canMake then
2025-06-18 05:33:48 +02:00
PrepareFood(food)
2025-06-18 02:30:02 +02:00
else
ShowMissingIngredientsWarning(missingItems)
end
end
})
2025-06-18 05:39:08 +02:00
::continue::
2025-06-18 02:30:02 +02:00
end
2025-06-18 05:39:08 +02:00
Debug("Showing menu with " .. #options .. " options")
-- Safely register and show context
if lib and lib.registerContext then
lib.registerContext({
id = 'grill_menu',
title = 'Grill',
options = options
})
if lib.showContext then
lib.showContext('grill_menu')
else
Debug("ERROR: lib.showContext is not available")
QBCore.Functions.Notify("Fehler beim Anzeigen des Menüs", "error")
end
else
Debug("ERROR: lib.registerContext is not available")
QBCore.Functions.Notify("Fehler beim Erstellen des Menüs", "error")
end
2025-06-18 02:30:02 +02:00
end
2025-06-18 05:39:08 +02:00
function PrepareFood(selectedFood)
2025-06-18 02:30:02 +02:00
Debug("Starting food preparation...")
local player = PlayerPedId()
2025-06-18 05:39:08 +02:00
local animDict = "amb@prop_human_bbq@male@base"
local anim = "base"
2025-06-18 02:30:02 +02:00
RequestAnimDict(animDict)
while not HasAnimDictLoaded(animDict) do
Wait(0)
end
2025-06-18 05:39:08 +02:00
QBCore.Functions.Progressbar("grill_food", selectedFood.label.." wird gegrillt...", Config.ProgressTime or 5000, false, true, {
2025-06-18 02:30:02 +02:00
disableMovement = true,
disableCarMovement = true,
disableMouse = false,
disableCombat = true,
}, {
animDict = animDict,
anim = anim,
flags = 49,
}, {}, {}, function() -- Success
Debug("Food preparation successful, triggering server event...")
2025-06-18 05:39:08 +02:00
TriggerServerEvent('grill-script:giveFood', selectedFood.item, selectedFood.requires)
2025-06-18 02:30:02 +02:00
end, function() -- Cancelled
Debug("Food preparation cancelled")
QBCore.Functions.Notify("Zubereitung abgebrochen", "error")
end)
end
-- Debug Event
2025-06-18 05:39:08 +02:00
RegisterNetEvent('grill-script:debug')
2025-06-18 02:30:02 +02:00
AddEventHandler('grill-script:debug', function(msg)
Debug(msg)
end)
2025-06-18 05:39:08 +02:00
2025-06-18 05:47:41 +02:00