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

203 lines
6.5 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 05:39:08 +02:00
event = "nordi_grill:client:OpenMenu",
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 05:39:08 +02:00
RegisterNetEvent('nordi_grill:client:OpenMenu')
AddEventHandler('nordi_grill: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 = {}
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
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 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
if not food.requires then
Debug("ERROR: food.requires is nil for " .. (food.label or "unknown food"))
goto continue
end
2025-06-18 02:30:02 +02:00
local hasIngredients, missing = CheckIngredients(food.requires)
2025-06-18 05:39:08 +02:00
local description = (food.description or "No description") .. "\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
if not req.item then
Debug("ERROR: req.item is nil")
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
if QBCore.Shared and QBCore.Shared.Items and QBCore.Shared.Items[req.item] and QBCore.Shared.Items[req.item].label then
itemLabel = QBCore.Shared.Items[req.item].label
hasItem = QBCore.Functions.HasItem(req.item, req.amount)
2025-06-18 05:33:48 +02:00
else
Debug("Warning: Item " .. req.item .. " not found in QBCore.Shared.Items")
end
2025-06-18 05:39:08 +02:00
local status = hasItem and "~g~✓" or "~r~✗"
description = description .. "\n- " .. req.amount .. "x " .. itemLabel .. " " .. status
::continue_req::
2025-06-18 02:30:02 +02:00
end
table.insert(options, {
2025-06-18 05:39:08 +02:00
title = food.label or "Unknown Food",
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