From 59fc8586116a206e18ea0f330f5fd01a235774cf Mon Sep 17 00:00:00 2001 From: Nordi98 Date: Tue, 29 Jul 2025 08:30:01 +0200 Subject: [PATCH] Update server.lua --- .../[inventory]/nordi_vending/server.lua | 172 ++++++++++-------- 1 file changed, 93 insertions(+), 79 deletions(-) diff --git a/resources/[inventory]/nordi_vending/server.lua b/resources/[inventory]/nordi_vending/server.lua index 352463ace..24e45baed 100644 --- a/resources/[inventory]/nordi_vending/server.lua +++ b/resources/[inventory]/nordi_vending/server.lua @@ -90,7 +90,7 @@ RegisterNetEvent('vending:server:openManagement', function(coords) TriggerClientEvent('vending:client:openManagement', src, machine) end) --- Open stash (korrekt für tgiann-inventory) +-- Open stash (korrigiert für tgiann-inventory) RegisterNetEvent('vending:server:openStash', function(coords) local src = source local Player = QBCore.Functions.GetPlayer(src) @@ -105,12 +105,11 @@ RegisterNetEvent('vending:server:openStash', function(coords) return end - -- Korrekte tgiann-inventory Methode - TriggerClientEvent('tgiann-inventory:client:openStash', src, { - stashId = machine.stash, - stashLabel = 'Vending Machine #' .. machine.id, + -- Verwende TriggerEvent statt TriggerClientEvent für tgiann-inventory + TriggerEvent('tgiann-inventory:server:openStash', src, machine.stash, { maxweight = Config.MaxWeight, - slots = Config.MaxSlots + slots = Config.MaxSlots, + label = 'Vending Machine #' .. machine.id }) end) @@ -165,7 +164,7 @@ RegisterNetEvent('vending:server:withdrawMoney', function(coords, amount) TriggerClientEvent('QBCore:Notify', src, 'Du hast $' .. amount .. ' abgehoben!', 'success') end) --- Buy item from vending machine +-- Buy item from vending machine (korrigiert) RegisterNetEvent('vending:server:buyItem', function(coords, itemName) local src = source local Player = QBCore.Functions.GetPlayer(src) @@ -183,36 +182,37 @@ RegisterNetEvent('vending:server:buyItem', function(coords, itemName) return end - -- Get stash items using tgiann-inventory - local stashItems = exports['tgiann-inventory']:getStashItems(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 + -- Use callback to get stash items + QBCore.Functions.TriggerCallback('tgiann-inventory:server:getStashItems', function(stashItems) + 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 - end - - if not hasItem then - TriggerClientEvent('QBCore:Notify', src, 'Artikel nicht verfügbar!', 'error') - return - end - - -- Remove money from player - Player.Functions.RemoveMoney('cash', price) - - -- Add money to machine - machine.money = machine.money + price - MySQL.update('UPDATE vending_machines SET money = ? WHERE id = ?', {machine.money, machineId}) - - -- Remove item from stash and add to player - exports['tgiann-inventory']:removeItemFromStash(machine.stash, itemName, 1) - exports['tgiann-inventory']:addItem(src, itemName, 1) - - TriggerClientEvent('QBCore:Notify', src, 'Artikel gekauft für $' .. price .. '!', 'success') + + if not hasItem then + TriggerClientEvent('QBCore:Notify', src, 'Artikel nicht verfügbar!', 'error') + return + end + + -- Remove money from player + Player.Functions.RemoveMoney('cash', price) + + -- Add money to machine + machine.money = machine.money + price + MySQL.update('UPDATE vending_machines SET money = ? WHERE id = ?', {machine.money, machineId}) + + -- Remove item from stash and add to player using events + TriggerEvent('tgiann-inventory:server:removeItemFromStash', machine.stash, itemName, 1) + TriggerEvent('tgiann-inventory:server:addItem', src, itemName, 1) + + TriggerClientEvent('QBCore:Notify', src, 'Artikel gekauft für $' .. price .. '!', 'success') + end, src, machine.stash) end) -- Start robbery @@ -226,39 +226,40 @@ RegisterNetEvent('vending:server:startRobbery', function(coords) local machine = vendingMachines[machineId] - -- Check if player has required item - local hasItem = exports['tgiann-inventory']:getItemByName(src, Config.RobberyItem) - if not hasItem or hasItem.amount < 1 then - TriggerClientEvent('QBCore:Notify', src, 'Du benötigst einen ' .. Config.RobberyItem, 'error') - return - end - - -- Check if already being robbed - if robberyInProgress[machineId] then - TriggerClientEvent('QBCore:Notify', src, 'Dieser Automat wird bereits aufgebrochen!', 'error') - return - end - - -- Check if machine has money - if machine.money < Config.MinRobberyAmount then - TriggerClientEvent('QBCore:Notify', src, 'Nicht genug Geld im Automaten!', 'error') - return - end - - robberyInProgress[machineId] = true - - -- Alert police - local streetHash = GetStreetNameAtCoord(coords.x, coords.y, coords.z) - local streetName = GetStreetNameFromHashKey(streetHash) - - local players = QBCore.Functions.GetQBPlayers() - for k, v in pairs(players) do - if v.PlayerData.job.name == 'police' and v.PlayerData.job.onduty then - TriggerClientEvent('vending:client:policeAlert', v.PlayerData.source, coords, streetName) + -- Check if player has required item using callback + QBCore.Functions.TriggerCallback('tgiann-inventory:server:getItemByName', function(item) + if not item or item.amount < 1 then + TriggerClientEvent('QBCore:Notify', src, 'Du benötigst einen ' .. Config.RobberyItem, 'error') + return end - end - - TriggerClientEvent('vending:client:startRobbery', src, coords) + + -- Check if already being robbed + if robberyInProgress[machineId] then + TriggerClientEvent('QBCore:Notify', src, 'Dieser Automat wird bereits aufgebrochen!', 'error') + return + end + + -- Check if machine has money + if machine.money < Config.MinRobberyAmount then + TriggerClientEvent('QBCore:Notify', src, 'Nicht genug Geld im Automaten!', 'error') + return + end + + robberyInProgress[machineId] = true + + -- Alert police + local streetHash = GetStreetNameAtCoord(coords.x, coords.y, coords.z) + local streetName = GetStreetNameFromHashKey(streetHash) + + local players = QBCore.Functions.GetQBPlayers() + for k, v in pairs(players) do + if v.PlayerData.job.name == 'police' and v.PlayerData.job.onduty then + TriggerClientEvent('vending:client:policeAlert', v.PlayerData.source, coords, streetName) + end + end + + TriggerClientEvent('vending:client:startRobbery', src, coords) + end, src, Config.RobberyItem) end) -- Complete robbery @@ -286,7 +287,7 @@ RegisterNetEvent('vending:server:completeRobbery', function(coords, success) -- Remove robbery item with chance if math.random(1, 100) <= Config.RobberyItemBreakChance then - exports['tgiann-inventory']:removeItem(src, Config.RobberyItem, 1) + TriggerEvent('tgiann-inventory:server:removeItem', src, Config.RobberyItem, 1) TriggerClientEvent('QBCore:Notify', src, 'Dein ' .. Config.RobberyItem .. ' ist kaputt gegangen!', 'error') end else @@ -315,7 +316,7 @@ QBCore.Functions.CreateCallback('vending:server:getMachineByCoords', function(so end end) --- Get stash items for vending machine menu +-- Get stash items for vending machine menu (korrigiert) QBCore.Functions.CreateCallback('vending:server:getStashItems', function(source, cb, coords) local machineId = getMachineIdByCoords(coords) if not machineId then @@ -324,19 +325,22 @@ QBCore.Functions.CreateCallback('vending:server:getStashItems', function(source, end local machine = vendingMachines[machineId] - local stashItems = exports['tgiann-inventory']:getStashItems(machine.stash) - local items = {} - 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) + -- Use tgiann-inventory callback to get stash items + QBCore.Functions.TriggerCallback('tgiann-inventory:server:getStashItems', function(stashItems) + local items = {} + + 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 - end - - cb(items) + + cb(items) + end, source, machine.stash) end) -- Check if player owns machine @@ -362,3 +366,13 @@ QBCore.Functions.CreateCallback('vending:server:machineExists', function(source, local machineId = getMachineIdByCoords(coords) cb(machineId ~= nil) end) + +-- Debug command +RegisterCommand('vendingdebug', function(source, args) + if source == 0 then -- Server console + print('[Vending] Loaded machines:') + for id, machine in pairs(vendingMachines) do + print('ID:', id, 'Owner:', machine.owner, 'Money:', machine.money) + end + end +end, true)