From 271588ccb0e39b911046d6be243ee00743d0daec Mon Sep 17 00:00:00 2001 From: Nordi98 Date: Wed, 18 Jun 2025 05:39:08 +0200 Subject: [PATCH] fixes --- resources/[inventory]/nordi_bbq/client.lua | 140 ++++++++++++++------- resources/[inventory]/nordi_bbq/server.lua | 82 ++++++------ 2 files changed, 143 insertions(+), 79 deletions(-) diff --git a/resources/[inventory]/nordi_bbq/client.lua b/resources/[inventory]/nordi_bbq/client.lua index 6775f78fd..2d99961ab 100644 --- a/resources/[inventory]/nordi_bbq/client.lua +++ b/resources/[inventory]/nordi_bbq/client.lua @@ -1,21 +1,36 @@ local QBCore = exports['qb-core']:GetCoreObject() +-- 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 + -- Debug Print Function local function Debug(msg) - print("^2[Grill Debug] ^7" .. msg) -- Changed from Coffee to Grill + print("^2[Grill Debug] ^7" .. msg) end +-- Wait for QBCore to be fully initialized CreateThread(function() + 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 + Debug("Script starting...") - for _, prop in pairs(Config.GrillProps) do -- Changed from CoffeeProps to GrillProps + for _, prop in pairs(Config.GrillProps) do exports['qb-target']:AddTargetModel(prop, { options = { { num = 1, type = "client", - event = "nordi_grill:client:OpenMenu", -- Changed event name - icon = 'fas fa-fire', -- Changed icon - label = 'Grillen', -- Changed label + event = "nordi_grill:client:OpenMenu", + icon = 'fas fa-fire', + label = 'Grillen', } }, distance = 2.0 @@ -25,10 +40,10 @@ CreateThread(function() end) -- Event Handler for opening the menu -RegisterNetEvent('nordi_grill:client:OpenMenu') -- Changed event name -AddEventHandler('nordi_grill:client:OpenMenu', function() -- Changed event name +RegisterNetEvent('nordi_grill:client:OpenMenu') +AddEventHandler('nordi_grill:client:OpenMenu', function() Debug("Opening menu...") - OpenGrillMenu() -- Changed function name + OpenGrillMenu() end) function CheckIngredients(requirements) @@ -49,50 +64,72 @@ function CheckIngredients(requirements) return hasItems, missingItems end - function ShowMissingIngredientsWarning(missingItems) local warningText = "Fehlende Zutaten:\n" for _, item in ipairs(missingItems) do - -- Add a check to make sure the item exists in QBCore.Shared.Items - if QBCore.Shared.Items[item.item] then - local itemLabel = QBCore.Shared.Items[item.item].label - warningText = warningText .. "- " .. itemLabel .. " (benötigt: " .. item.required .. ")\n" - else - -- Use the item name if the item doesn't exist in QBCore.Shared.Items - warningText = warningText .. "- " .. item.item .. " (benötigt: " .. item.required .. ")\n" + 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 end + + warningText = warningText .. "- " .. itemLabel .. " (benötigt: " .. item.required .. ")\n" end QBCore.Functions.Notify(warningText, "error", 5000) end - function OpenGrillMenu() Debug("Building menu options...") + + -- 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 + local options = {} for _, food in ipairs(Config.GrillOptions) do + -- 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 + local hasIngredients, missing = CheckIngredients(food.requires) - local description = food.description .. "\n\nBenötigt:" + local description = (food.description or "No description") .. "\n\nBenötigt:" for _, req in ipairs(food.requires) do - -- Add a check to make sure the item exists in QBCore.Shared.Items - if QBCore.Shared.Items[req.item] then - 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 - else - -- Handle the case where the item doesn't exist - Debug("Warning: Item " .. req.item .. " not found in QBCore.Shared.Items") - description = description .. "\n- " .. req.amount .. "x " .. req.item .. " (Item not found)" + -- 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) + else + Debug("Warning: Item " .. req.item .. " not found in QBCore.Shared.Items") + end + + local status = hasItem and "~g~✓" or "~r~✗" + description = description .. "\n- " .. req.amount .. "x " .. itemLabel .. " " .. status + + ::continue_req:: end table.insert(options, { - title = food.label, + title = food.label or "Unknown Food", description = description, - icon = food.icon, + icon = food.icon or "fas fa-question", onSelect = function() local canMake, missingItems = CheckIngredients(food.requires) if canMake then @@ -102,30 +139,44 @@ function OpenGrillMenu() end end }) + + ::continue:: end - Debug("Showing menu...") - lib.registerContext({ - id = 'grill_menu', - title = 'Grill', - options = options - }) - - lib.showContext('grill_menu') + 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 end -function PrepareFood(selectedFood) -- Changed function name +function PrepareFood(selectedFood) Debug("Starting food preparation...") local player = PlayerPedId() - local animDict = "amb@prop_human_bbq@male@base" -- Changed animation - local anim = "base" -- Changed animation + local animDict = "amb@prop_human_bbq@male@base" + local anim = "base" RequestAnimDict(animDict) while not HasAnimDictLoaded(animDict) do Wait(0) end - QBCore.Functions.Progressbar("grill_food", selectedFood.label.." wird gegrillt...", Config.ProgressTime or 5000, false, true, { -- Changed text and function name + QBCore.Functions.Progressbar("grill_food", selectedFood.label.." wird gegrillt...", Config.ProgressTime or 5000, false, true, { disableMovement = true, disableCarMovement = true, disableMouse = false, @@ -136,7 +187,7 @@ function PrepareFood(selectedFood) -- Changed function name flags = 49, }, {}, {}, function() -- Success Debug("Food preparation successful, triggering server event...") - TriggerServerEvent('grill-script:giveFood', selectedFood.item, selectedFood.requires) -- Changed event name + TriggerServerEvent('grill-script:giveFood', selectedFood.item, selectedFood.requires) end, function() -- Cancelled Debug("Food preparation cancelled") QBCore.Functions.Notify("Zubereitung abgebrochen", "error") @@ -144,7 +195,8 @@ function PrepareFood(selectedFood) -- Changed function name end -- Debug Event -RegisterNetEvent('grill-script:debug') -- Changed event name +RegisterNetEvent('grill-script:debug') AddEventHandler('grill-script:debug', function(msg) Debug(msg) end) + diff --git a/resources/[inventory]/nordi_bbq/server.lua b/resources/[inventory]/nordi_bbq/server.lua index 7709e0ee6..b976b82bc 100644 --- a/resources/[inventory]/nordi_bbq/server.lua +++ b/resources/[inventory]/nordi_bbq/server.lua @@ -2,55 +2,67 @@ local QBCore = exports['qb-core']:GetCoreObject() -- Debug Print Function local function Debug(msg) - print("^2[Grill Debug] ^7" .. msg) -- Changed from Coffee to Grill + print("^2[Grill Debug] ^7" .. msg) end -RegisterNetEvent('grill-script:giveFood') -- Changed event name -AddEventHandler('grill-script:giveFood', function(itemName, requirements) -- Changed event name - Debug("Give food event triggered") -- Changed text +RegisterNetEvent('grill-script:giveFood') +AddEventHandler('grill-script:giveFood', function(itemName, requirements) + Debug("Give food event triggered") local src = source local Player = QBCore.Functions.GetPlayer(src) - if Player then - -- Check if the item is in the allowed grill options - local isValidItem = false - for _, food in ipairs(Config.GrillOptions) do -- Changed from CoffeeOptions to GrillOptions - if food.item == itemName then - isValidItem = true + if not Player then + Debug("ERROR: Player not found") + return + end + + -- Check if the item is in the allowed grill options + local isValidItem = false + for _, food in ipairs(Config.GrillOptions) do + if food.item == itemName then + isValidItem = true + break + end + end + + if isValidItem then + Debug("Valid food item requested: " .. itemName) + -- Double-check ingredients + local hasAllItems = true + for _, requirement in ipairs(requirements) do + if not Player.Functions.HasItem(requirement.item, requirement.amount) then + hasAllItems = false + Debug("Player missing item: " .. requirement.item) break end end - if isValidItem then - Debug("Valid food item requested") -- Changed text - -- Double-check ingredients - local hasAllItems = true + if hasAllItems then + Debug("Player has all required items") + -- Remove required items for _, requirement in ipairs(requirements) do - if not Player.Functions.HasItem(requirement.item, requirement.amount) then - hasAllItems = false - break - end + Player.Functions.RemoveItem(requirement.item, requirement.amount) + TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[requirement.item], "remove") end - if hasAllItems then - Debug("Player has all required items") - -- Remove required items - for _, requirement in ipairs(requirements) do - Player.Functions.RemoveItem(requirement.item, requirement.amount) - TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[requirement.item], "remove") - end - - -- Give the finished food - Player.Functions.AddItem(itemName, 1) - TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[itemName], "add") - TriggerClientEvent('QBCore:Notify', src, "Du hast " .. QBCore.Shared.Items[itemName].label .. " gegrillt!", "success") -- Changed text - else - Debug("Player missing required items") - TriggerClientEvent('QBCore:Notify', src, "Du hast nicht alle benötigten Zutaten!", "error") + -- Give the finished food + Player.Functions.AddItem(itemName, 1) + + -- Safely get the item label + local itemLabel = itemName + if QBCore.Shared.Items[itemName] and QBCore.Shared.Items[itemName].label then + itemLabel = QBCore.Shared.Items[itemName].label end + + TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[itemName], "add") + TriggerClientEvent('QBCore:Notify', src, "Du hast " .. itemLabel .. " gegrillt!", "success") else - Debug("Invalid food item requested: " .. itemName) -- Changed text - TriggerClientEvent('QBCore:Notify', src, "Fehler beim Grillen!", "error") -- Changed text + Debug("Player missing required items") + TriggerClientEvent('QBCore:Notify', src, "Du hast nicht alle benötigten Zutaten!", "error") end + else + Debug("Invalid food item requested: " .. itemName) + TriggerClientEvent('QBCore:Notify', src, "Fehler beim Grillen!", "error") end end) +