forked from Simnation/Main
ed
This commit is contained in:
parent
208ecc8c0c
commit
38926664fb
2 changed files with 132 additions and 35 deletions
|
@ -5,6 +5,44 @@ local function Debug(msg)
|
||||||
print("^2[Shisha Debug] ^7" .. msg)
|
print("^2[Shisha 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 = 'shisha-script: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('shisha-script: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
|
||||||
|
|
||||||
-- Check if qb-target is available
|
-- Check if qb-target is available
|
||||||
CreateThread(function()
|
CreateThread(function()
|
||||||
Wait(1000)
|
Wait(1000)
|
||||||
|
@ -68,22 +106,34 @@ AddEventHandler('nordi_shisha:client:OpenMenu', function()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
-- Check if player has required ingredients
|
-- Check if player has required ingredients
|
||||||
function CheckIngredients(requirements)
|
function CheckIngredients(requirements, callback)
|
||||||
local hasItems = true
|
local hasItems = true
|
||||||
local missingItems = {}
|
local missingItems = {}
|
||||||
|
local checkedItems = 0
|
||||||
|
local totalItems = #requirements
|
||||||
|
|
||||||
for _, requirement in ipairs(requirements) do
|
if totalItems == 0 then
|
||||||
local hasItem = QBCore.Functions.HasItem(requirement.item, requirement.amount)
|
callback(true, {})
|
||||||
if not hasItem then
|
return
|
||||||
hasItems = false
|
|
||||||
table.insert(missingItems, {
|
|
||||||
item = requirement.item,
|
|
||||||
required = requirement.amount
|
|
||||||
})
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return hasItems, missingItems
|
for _, requirement in ipairs(requirements) do
|
||||||
|
HasItem(requirement.item, requirement.amount, function(hasItem)
|
||||||
|
checkedItems = checkedItems + 1
|
||||||
|
|
||||||
|
if not hasItem then
|
||||||
|
hasItems = false
|
||||||
|
table.insert(missingItems, {
|
||||||
|
item = requirement.item,
|
||||||
|
required = requirement.amount
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
if checkedItems == totalItems then
|
||||||
|
callback(hasItems, missingItems)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Show warning for missing ingredients
|
-- Show warning for missing ingredients
|
||||||
|
@ -101,33 +151,55 @@ end
|
||||||
function OpenShishaMenu()
|
function OpenShishaMenu()
|
||||||
Debug("Erstelle Menüoptionen...")
|
Debug("Erstelle Menüoptionen...")
|
||||||
local options = {}
|
local options = {}
|
||||||
|
local shishaChecked = 0
|
||||||
|
local totalShishas = #Config.ShishaOptions
|
||||||
|
|
||||||
for _, shisha in ipairs(Config.ShishaOptions) do
|
for _, shisha in ipairs(Config.ShishaOptions) do
|
||||||
local hasIngredients, missing = CheckIngredients(shisha.requires)
|
CheckIngredients(shisha.requires, function(hasIngredients, missingItems)
|
||||||
local description = shisha.description .. "\n\nBenötigt:"
|
shishaChecked = shishaChecked + 1
|
||||||
|
|
||||||
for _, req in ipairs(shisha.requires) do
|
local description = shisha.description .. "\n\nBenötigt:"
|
||||||
local itemLabel = QBCore.Shared.Items[req.item].label
|
|
||||||
local hasItem = QBCore.Functions.HasItem(req.item, req.amount)
|
for _, req in ipairs(shisha.requires) do
|
||||||
local status = hasItem and "~g~✓" or "~r~✗"
|
local itemLabel = QBCore.Shared.Items[req.item].label
|
||||||
description = description .. "\n- " .. req.amount .. "x " .. itemLabel .. " " .. status
|
local hasItem = not table.find(missingItems, function(item) return item.item == req.item end)
|
||||||
end
|
local status = hasItem and "~g~✓" or "~r~✗"
|
||||||
|
description = description .. "\n- " .. req.amount .. "x " .. itemLabel .. " " .. status
|
||||||
table.insert(options, {
|
|
||||||
title = shisha.label,
|
|
||||||
description = description,
|
|
||||||
icon = shisha.icon,
|
|
||||||
onSelect = function()
|
|
||||||
local canMake, missingItems = CheckIngredients(shisha.requires)
|
|
||||||
if canMake then
|
|
||||||
PrepareShisha(shisha)
|
|
||||||
else
|
|
||||||
ShowMissingIngredientsWarning(missingItems)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
})
|
|
||||||
|
table.insert(options, {
|
||||||
|
title = shisha.label,
|
||||||
|
description = description,
|
||||||
|
icon = shisha.icon,
|
||||||
|
onSelect = function()
|
||||||
|
CheckIngredients(shisha.requires, function(canMake, missingItems)
|
||||||
|
if canMake then
|
||||||
|
PrepareShisha(shisha)
|
||||||
|
else
|
||||||
|
ShowMissingIngredientsWarning(missingItems)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
})
|
||||||
|
|
||||||
|
if shishaChecked == totalShishas then
|
||||||
|
ShowShishaMenu(options)
|
||||||
|
end
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Helper function to find in table
|
||||||
|
function table.find(t, cb)
|
||||||
|
for _, v in ipairs(t) do
|
||||||
|
if cb(v) then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
function ShowShishaMenu(options)
|
||||||
Debug("Zeige Menü...")
|
Debug("Zeige Menü...")
|
||||||
lib.registerContext({
|
lib.registerContext({
|
||||||
id = 'shisha_menu',
|
id = 'shisha_menu',
|
||||||
|
|
|
@ -5,6 +5,31 @@ local function Debug(msg)
|
||||||
print("^2[Shisha Debug] ^7" .. msg)
|
print("^2[Shisha 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('shisha-script:checkItem', function(itemName, amount, callbackEvent)
|
||||||
|
local src = source
|
||||||
|
local hasItem = HasItem(src, itemName, amount)
|
||||||
|
TriggerClientEvent(callbackEvent, src, hasItem)
|
||||||
|
end)
|
||||||
|
|
||||||
RegisterNetEvent('shisha-script:consumeTobacco')
|
RegisterNetEvent('shisha-script:consumeTobacco')
|
||||||
AddEventHandler('shisha-script:consumeTobacco', function(requirements)
|
AddEventHandler('shisha-script:consumeTobacco', function(requirements)
|
||||||
Debug("Tabak-Verbrauch-Event ausgelöst")
|
Debug("Tabak-Verbrauch-Event ausgelöst")
|
||||||
|
@ -15,7 +40,7 @@ AddEventHandler('shisha-script:consumeTobacco', function(requirements)
|
||||||
-- Überprüfe nochmal die Zutaten
|
-- Überprüfe nochmal die Zutaten
|
||||||
local hasAllItems = true
|
local hasAllItems = true
|
||||||
for _, requirement in ipairs(requirements) do
|
for _, requirement in ipairs(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
|
||||||
|
@ -25,7 +50,7 @@ AddEventHandler('shisha-script:consumeTobacco', function(requirements)
|
||||||
Debug("Spieler hat alle benötigten Items")
|
Debug("Spieler hat alle benötigten Items")
|
||||||
-- Entferne die benötigten Items
|
-- Entferne die benötigten Items
|
||||||
for _, requirement in ipairs(requirements) do
|
for _, requirement in ipairs(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
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue