From 660c766f7977f60d36b1f9a3c4bd57d1193b45e0 Mon Sep 17 00:00:00 2001 From: Nordi98 Date: Tue, 29 Jul 2025 08:42:57 +0200 Subject: [PATCH] Update server.lua --- .../[inventory]/nordi_vending/server.lua | 190 +++++------------- 1 file changed, 52 insertions(+), 138 deletions(-) diff --git a/resources/[inventory]/nordi_vending/server.lua b/resources/[inventory]/nordi_vending/server.lua index 2301646c5..7b0f703ed 100644 --- a/resources/[inventory]/nordi_vending/server.lua +++ b/resources/[inventory]/nordi_vending/server.lua @@ -19,6 +19,7 @@ CreateThread(function() stash = 'vending_' .. data.id } end + print("^2[VENDING]^7 Loaded " .. #result .. " vending machines") end end) @@ -68,6 +69,7 @@ RegisterNetEvent('vending:server:registerMachine', function(coords, prop) stash = 'vending_' .. machineId } + print("^2[VENDING]^7 New vending machine registered: " .. machineId) TriggerClientEvent('QBCore:Notify', src, 'Verkaufsautomat erfolgreich gekauft für $' .. Config.VendingMachinePrice .. '!', 'success') TriggerClientEvent('vending:client:refreshTargets', -1) end) @@ -90,7 +92,7 @@ RegisterNetEvent('vending:server:openManagement', function(coords) TriggerClientEvent('vending:client:openManagement', src, machine) end) --- Open stash (mit mehreren Methoden versuchen) +-- Open stash (korrekt für tgiann-inventory) RegisterNetEvent('vending:server:openStash', function(coords) local src = source local Player = QBCore.Functions.GetPlayer(src) @@ -105,45 +107,12 @@ RegisterNetEvent('vending:server:openStash', function(coords) return end - -- Versuche verschiedene Methoden für tgiann-inventory - local success = false - - -- Methode 1: TriggerClientEvent - if not success then - local clientSuccess = pcall(function() - TriggerClientEvent('tgiann-inventory:client:openStash', src, { - stashId = machine.stash, - stashLabel = 'Vending Machine #' .. machine.id, - maxweight = Config.MaxWeight, - slots = Config.MaxSlots - }) - end) - if clientSuccess then - success = true - print('[Vending] Opened stash via client event') - end - end - - -- Methode 2: Server Event - if not success then - local eventSuccess = pcall(function() - TriggerEvent('tgiann-inventory:server:openStash', src, machine.stash, { - maxweight = Config.MaxWeight, - slots = Config.MaxSlots, - label = 'Vending Machine #' .. machine.id - }) - end) - if eventSuccess then - success = true - print('[Vending] Opened stash via server event') - end - end - - -- Methode 3: Fallback - eigenes Stash-System - if not success then - TriggerClientEvent('vending:client:openCustomStash', src, machine) - print('[Vending] Opened custom stash fallback') - end + -- Öffne das Inventar mit tgiann-inventory + exports["tgiann-inventory"]:OpenInventory(src, "stash", machine.stash, { + maxweight = Config.MaxWeight, + slots = Config.MaxSlots, + label = 'Vending Machine #' .. machine.id + }) end) -- Set item price @@ -197,7 +166,7 @@ RegisterNetEvent('vending:server:withdrawMoney', function(coords, amount) TriggerClientEvent('QBCore:Notify', src, 'Du hast $' .. amount .. ' abgehoben!', 'success') end) --- Buy item from vending machine (mit Fallback-System) +-- Buy item from vending machine RegisterNetEvent('vending:server:buyItem', function(coords, itemName) local src = source local Player = QBCore.Functions.GetPlayer(src) @@ -215,8 +184,20 @@ RegisterNetEvent('vending:server:buyItem', function(coords, itemName) return end - -- Check if item is available in our database - if not machine.items[itemName] or machine.items[itemName] <= 0 then + -- Get stash items + local stashItems = exports["tgiann-inventory"]:GetSecondaryInventoryItems("stash", machine.stash) + local hasItem = false + + if stashItems then + for slot, item in pairs(stashItems) do + if item.name == itemName and item.amount > 0 then + hasItem = true + break + end + end + end + + if not hasItem then TriggerClientEvent('QBCore:Notify', src, 'Artikel nicht verfügbar!', 'error') return end @@ -226,96 +207,16 @@ RegisterNetEvent('vending:server:buyItem', function(coords, itemName) -- Add money to machine machine.money = machine.money + price + MySQL.update('UPDATE vending_machines SET money = ? WHERE id = ?', {machine.money, machineId}) - -- Remove item from machine - machine.items[itemName] = machine.items[itemName] - 1 - - -- Update database - MySQL.update('UPDATE vending_machines SET money = ?, items = ? WHERE id = ?', { - machine.money, - json.encode(machine.items), - machineId - }) - - -- Add item to player using QBCore + -- Remove item from stash and add to player + exports["tgiann-inventory"]:RemoveItemFromSecondaryInventory("stash", machine.stash, itemName, 1) Player.Functions.AddItem(itemName, 1) TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[itemName], 'add') TriggerClientEvent('QBCore:Notify', src, 'Artikel gekauft für $' .. price .. '!', 'success') end) --- Add item to vending machine (for stocking) -RegisterNetEvent('vending:server:addItem', function(coords, itemName, amount) - local src = source - local Player = QBCore.Functions.GetPlayer(src) - if not Player then return end - - local machineId = getMachineIdByCoords(coords) - if not machineId then return end - - local machine = vendingMachines[machineId] - if machine.owner ~= Player.PlayerData.citizenid then - TriggerClientEvent('QBCore:Notify', src, 'Das ist nicht dein Verkaufsautomat!', 'error') - return - end - - -- Check if player has the item - local item = Player.Functions.GetItemByName(itemName) - if not item or item.amount < amount then - TriggerClientEvent('QBCore:Notify', src, 'Du hast nicht genug von diesem Item!', 'error') - return - end - - -- Remove item from player - Player.Functions.RemoveItem(itemName, amount) - TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[itemName], 'remove') - - -- Add item to machine - if not machine.items[itemName] then - machine.items[itemName] = 0 - end - machine.items[itemName] = machine.items[itemName] + amount - - -- Update database - MySQL.update('UPDATE vending_machines SET items = ? WHERE id = ?', {json.encode(machine.items), machineId}) - - TriggerClientEvent('QBCore:Notify', src, amount .. 'x ' .. (QBCore.Shared.Items[itemName] and QBCore.Shared.Items[itemName].label or itemName) .. ' zum Automaten hinzugefügt!', 'success') -end) - --- Remove item from vending machine -RegisterNetEvent('vending:server:removeItem', function(coords, itemName, amount) - local src = source - local Player = QBCore.Functions.GetPlayer(src) - if not Player then return end - - local machineId = getMachineIdByCoords(coords) - if not machineId then return end - - local machine = vendingMachines[machineId] - if machine.owner ~= Player.PlayerData.citizenid then - TriggerClientEvent('QBCore:Notify', src, 'Das ist nicht dein Verkaufsautomat!', 'error') - return - end - - -- Check if machine has the item - if not machine.items[itemName] or machine.items[itemName] < amount then - TriggerClientEvent('QBCore:Notify', src, 'Nicht genug Items im Automaten!', 'error') - return - end - - -- Remove item from machine - machine.items[itemName] = machine.items[itemName] - amount - - -- Add item to player - Player.Functions.AddItem(itemName, amount) - TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[itemName], 'add') - - -- Update database - MySQL.update('UPDATE vending_machines SET items = ? WHERE id = ?', {json.encode(machine.items), machineId}) - - TriggerClientEvent('QBCore:Notify', src, amount .. 'x ' .. (QBCore.Shared.Items[itemName] and QBCore.Shared.Items[itemName].label or itemName) .. ' aus dem Automaten entfernt!', 'success') -end) - -- Start robbery RegisterNetEvent('vending:server:startRobbery', function(coords) local src = source @@ -417,7 +318,7 @@ QBCore.Functions.CreateCallback('vending:server:getMachineByCoords', function(so end end) --- Get stash items for vending machine menu (using our database) +-- Get stash items for vending machine menu QBCore.Functions.CreateCallback('vending:server:getStashItems', function(source, cb, coords) local machineId = getMachineIdByCoords(coords) if not machineId then @@ -426,19 +327,16 @@ QBCore.Functions.CreateCallback('vending:server:getStashItems', function(source, end local machine = vendingMachines[machineId] + + -- Get stash items using correct export + local stashItems = exports["tgiann-inventory"]:GetSecondaryInventoryItems("stash", machine.stash) local items = {} - for itemName, amount in pairs(machine.items) do - if amount > 0 then - local itemData = QBCore.Shared.Items[itemName] - if itemData then - table.insert(items, { - name = itemName, - label = itemData.label, - amount = amount, - price = machine.prices[itemName] or Config.DefaultPrice, - image = itemData.image - }) + if stashItems then + for slot, item in pairs(stashItems) do + if item.amount > 0 then + item.price = machine.prices[item.name] or Config.DefaultPrice + table.insert(items, item) end end end @@ -469,3 +367,19 @@ QBCore.Functions.CreateCallback('vending:server:machineExists', function(source, local machineId = getMachineIdByCoords(coords) cb(machineId ~= nil) end) + +-- Debug command +QBCore.Commands.Add('vendingdebug', 'Debug vending machines (Admin Only)', {}, false, function(source, args) + local Player = QBCore.Functions.GetPlayer(source) + if Player.PlayerData.permission == "admin" or Player.PlayerData.permission == "god" then + local count = 0 + for id, machine in pairs(vendingMachines) do + count = count + 1 + print("^2[VENDING]^7 Machine #" .. id .. " | Owner: " .. machine.owner .. " | Money: $" .. machine.money) + end + + TriggerClientEvent('QBCore:Notify', source, count .. ' Verkaufsautomaten geladen', 'success') + else + TriggerClientEvent('QBCore:Notify', source, 'Keine Berechtigung!', 'error') + end +end, 'admin')