forked from Simnation/Main
ed
This commit is contained in:
parent
961260f3f5
commit
5f4adf5818
3 changed files with 177 additions and 72 deletions
|
@ -1348,12 +1348,6 @@ CodeStudio.Products = {
|
||||||
itemPrice = 0,
|
itemPrice = 0,
|
||||||
itemInfo = "Extendable baton for self-defense",
|
itemInfo = "Extendable baton for self-defense",
|
||||||
},
|
},
|
||||||
['weapon_flashlight'] = {
|
|
||||||
itemName = "Taschenlampe",
|
|
||||||
itemStock = 50,
|
|
||||||
itemPrice = 0,
|
|
||||||
itemInfo = "Handheld flashlight",
|
|
||||||
},
|
|
||||||
['weapon_colbaton'] = {
|
['weapon_colbaton'] = {
|
||||||
itemName = "Teleskopschlagstock",
|
itemName = "Teleskopschlagstock",
|
||||||
itemStock = 50,
|
itemStock = 50,
|
||||||
|
|
|
@ -5,6 +5,44 @@ local function Debug(msg)
|
||||||
print("^2[Grill Debug] ^7" .. msg)
|
print("^2[Grill Debug] ^7" .. msg)
|
||||||
end
|
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 = 'nordi_bbq: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('nordi_bbq: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
|
||||||
|
|
||||||
-- Warte auf vollständige Initialisierung von QBCore
|
-- Warte auf vollständige Initialisierung von QBCore
|
||||||
CreateThread(function()
|
CreateThread(function()
|
||||||
if not QBCore then
|
if not QBCore then
|
||||||
|
@ -43,26 +81,44 @@ RegisterNetEvent('nordi_bbq:OpenGrillMenu', function()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
-- Funktion zum Überprüfen der Zutaten
|
-- Funktion zum Überprüfen der Zutaten
|
||||||
function CheckIngredients(recipe)
|
function CheckIngredients(recipe, callback)
|
||||||
local hasItems = true
|
local hasItems = true
|
||||||
local missingItems = {}
|
local missingItems = {}
|
||||||
|
local checkedItems = 0
|
||||||
|
local totalItems = 0
|
||||||
|
|
||||||
if not recipe or not recipe.requires then
|
if not recipe or not recipe.requires then
|
||||||
Debug("Rezept oder Anforderungen fehlen")
|
Debug("Rezept oder Anforderungen fehlen")
|
||||||
return false, {}
|
callback(false, {})
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
for _, _ in pairs(recipe.requires) do
|
||||||
|
totalItems = totalItems + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
if totalItems == 0 then
|
||||||
|
callback(true, {})
|
||||||
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
for _, item in pairs(recipe.requires) do
|
for _, item in pairs(recipe.requires) do
|
||||||
if not QBCore.Functions.HasItem(item.item, item.amount) then
|
HasItem(item.item, item.amount, function(hasItem)
|
||||||
|
checkedItems = checkedItems + 1
|
||||||
|
|
||||||
|
if not hasItem then
|
||||||
hasItems = false
|
hasItems = false
|
||||||
table.insert(missingItems, {
|
table.insert(missingItems, {
|
||||||
item = item.item,
|
item = item.item,
|
||||||
amount = item.amount
|
amount = item.amount
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
return hasItems, missingItems
|
if checkedItems == totalItems then
|
||||||
|
callback(hasItems, missingItems)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Funktion zum Anzeigen fehlender Zutaten
|
-- Funktion zum Anzeigen fehlender Zutaten
|
||||||
|
@ -82,16 +138,27 @@ function OpenGrillMenu()
|
||||||
Debug("Erstelle Grill-Menü")
|
Debug("Erstelle Grill-Menü")
|
||||||
|
|
||||||
local menuOptions = {}
|
local menuOptions = {}
|
||||||
|
local recipesChecked = 0
|
||||||
|
local totalRecipes = 0
|
||||||
|
|
||||||
|
-- Count total recipes
|
||||||
|
for _, _ in pairs(Config.GrillRecipes) do
|
||||||
|
totalRecipes = totalRecipes + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Check each recipe
|
||||||
for _, recipe in pairs(Config.GrillRecipes) do
|
for _, recipe in pairs(Config.GrillRecipes) do
|
||||||
local hasIngredients, _ = CheckIngredients(recipe)
|
CheckIngredients(recipe, function(hasIngredients, missingItems)
|
||||||
|
recipesChecked = recipesChecked + 1
|
||||||
|
|
||||||
|
-- Create menu option for this recipe
|
||||||
local status = hasIngredients and "~g~✓" or "~r~✗"
|
local status = hasIngredients and "~g~✓" or "~r~✗"
|
||||||
|
|
||||||
-- Erstelle Beschreibung mit Zutaten
|
-- Erstelle Beschreibung mit Zutaten
|
||||||
local description = recipe.description .. "\n\nZutaten:"
|
local description = recipe.description .. "\n\nZutaten:"
|
||||||
for _, item in pairs(recipe.requires) do
|
for _, item in pairs(recipe.requires) do
|
||||||
local itemLabel = QBCore.Shared.Items[item.item] and QBCore.Shared.Items[item.item].label or item.item
|
local itemLabel = QBCore.Shared.Items[item.item] and QBCore.Shared.Items[item.item].label or item.item
|
||||||
local hasItem = QBCore.Functions.HasItem(item.item, item.amount)
|
local hasItem = not table.find(missingItems, function(missingItem) return missingItem.item == item.item end)
|
||||||
local itemStatus = hasItem and "~g~✓" or "~r~✗"
|
local itemStatus = hasItem and "~g~✓" or "~r~✗"
|
||||||
description = description .. "\n- " .. item.amount .. "x " .. itemLabel .. " " .. itemStatus
|
description = description .. "\n- " .. item.amount .. "x " .. itemLabel .. " " .. itemStatus
|
||||||
end
|
end
|
||||||
|
@ -104,8 +171,27 @@ function OpenGrillMenu()
|
||||||
StartGrilling(recipe)
|
StartGrilling(recipe)
|
||||||
end
|
end
|
||||||
})
|
})
|
||||||
|
|
||||||
|
-- If all recipes checked, show the menu
|
||||||
|
if recipesChecked == totalRecipes then
|
||||||
|
ShowGrillMenu(menuOptions)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Helper function to find in table
|
||||||
|
function table.find(t, cb)
|
||||||
|
for _, v in pairs(t) do
|
||||||
|
if cb(v) then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Function to show the menu
|
||||||
|
function ShowGrillMenu(menuOptions)
|
||||||
-- Registriere und zeige das Menü mit ox_lib
|
-- Registriere und zeige das Menü mit ox_lib
|
||||||
if lib and lib.registerContext then
|
if lib and lib.registerContext then
|
||||||
lib.registerContext({
|
lib.registerContext({
|
||||||
|
@ -126,8 +212,7 @@ function StartGrilling(recipe)
|
||||||
Debug("Starte Grillvorgang für: " .. recipe.label)
|
Debug("Starte Grillvorgang für: " .. recipe.label)
|
||||||
|
|
||||||
-- Überprüfe Zutaten erneut
|
-- Überprüfe Zutaten erneut
|
||||||
local hasIngredients, missingItems = CheckIngredients(recipe)
|
CheckIngredients(recipe, function(hasIngredients, missingItems)
|
||||||
|
|
||||||
if not hasIngredients then
|
if not hasIngredients then
|
||||||
ShowMissingIngredients(missingItems)
|
ShowMissingIngredients(missingItems)
|
||||||
return
|
return
|
||||||
|
@ -158,4 +243,5 @@ function StartGrilling(recipe)
|
||||||
Debug("Grillvorgang abgebrochen")
|
Debug("Grillvorgang abgebrochen")
|
||||||
QBCore.Functions.Notify("Grillvorgang abgebrochen", "error")
|
QBCore.Functions.Notify("Grillvorgang abgebrochen", "error")
|
||||||
end)
|
end)
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,6 +5,31 @@ local function Debug(msg)
|
||||||
print("^2[Grill Debug] ^7" .. msg)
|
print("^2[Grill Debug] ^7" .. msg)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Function to check if player has an item
|
||||||
|
local function HasItem(source, itemName, amount)
|
||||||
|
local items = exports["tgiann-inventory"]:GetPlayerItems(source)
|
||||||
|
if not items then return false end
|
||||||
|
|
||||||
|
local count = 0
|
||||||
|
for _, item in pairs(items) do
|
||||||
|
if item.name == itemName then
|
||||||
|
count = count + item.count
|
||||||
|
if count >= amount then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Event for client to check if player has an item
|
||||||
|
RegisterNetEvent('nordi_bbq:checkItem', function(itemName, amount, callbackEvent)
|
||||||
|
local src = source
|
||||||
|
local hasItem = HasItem(src, itemName, amount)
|
||||||
|
TriggerClientEvent(callbackEvent, src, hasItem)
|
||||||
|
end)
|
||||||
|
|
||||||
-- Event zum Geben des gegrillten Essens
|
-- Event zum Geben des gegrillten Essens
|
||||||
RegisterNetEvent('nordi_bbq:server:GiveGrilledFood', function(itemName, requirements)
|
RegisterNetEvent('nordi_bbq:server:GiveGrilledFood', function(itemName, requirements)
|
||||||
local src = source
|
local src = source
|
||||||
|
@ -33,7 +58,7 @@ RegisterNetEvent('nordi_bbq:server:GiveGrilledFood', function(itemName, requirem
|
||||||
-- Überprüfe Zutaten
|
-- Überprüfe Zutaten
|
||||||
local hasAllItems = true
|
local hasAllItems = true
|
||||||
for _, requirement in pairs(requirements) do
|
for _, requirement in pairs(requirements) do
|
||||||
if not Player.Functions.HasItem(requirement.item, requirement.amount) then
|
if not HasItem(src, requirement.item, requirement.amount) then
|
||||||
hasAllItems = false
|
hasAllItems = false
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
@ -42,12 +67,12 @@ RegisterNetEvent('nordi_bbq:server:GiveGrilledFood', function(itemName, requirem
|
||||||
if hasAllItems then
|
if hasAllItems then
|
||||||
-- Entferne Zutaten
|
-- Entferne Zutaten
|
||||||
for _, requirement in pairs(requirements) do
|
for _, requirement in pairs(requirements) do
|
||||||
Player.Functions.RemoveItem(requirement.item, requirement.amount)
|
exports["tgiann-inventory"]:RemoveItem(src, requirement.item, requirement.amount)
|
||||||
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[requirement.item], "remove")
|
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[requirement.item], "remove")
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Gib das fertige Essen
|
-- Gib das fertige Essen
|
||||||
Player.Functions.AddItem(itemName, 1)
|
exports["tgiann-inventory"]:AddItem(src, itemName, 1)
|
||||||
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[itemName], "add")
|
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[itemName], "add")
|
||||||
|
|
||||||
local itemLabel = QBCore.Shared.Items[itemName] and QBCore.Shared.Items[itemName].label or itemName
|
local itemLabel = QBCore.Shared.Items[itemName] and QBCore.Shared.Items[itemName].label or itemName
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue