From 5190cf1220757efd02f7abda7135375686279b99 Mon Sep 17 00:00:00 2001 From: Nordi98 Date: Tue, 29 Jul 2025 08:09:09 +0200 Subject: [PATCH] Update server.lua --- .../[inventory]/nordi_vending/server.lua | 119 +++++++++--------- 1 file changed, 63 insertions(+), 56 deletions(-) diff --git a/resources/[inventory]/nordi_vending/server.lua b/resources/[inventory]/nordi_vending/server.lua index ad39eba7f..0c6f3e61c 100644 --- a/resources/[inventory]/nordi_vending/server.lua +++ b/resources/[inventory]/nordi_vending/server.lua @@ -18,15 +18,6 @@ CreateThread(function() prices = json.decode(data.prices) or {}, stash = 'vending_' .. data.id } - - -- Create stash for this vending machine - exports['tgiann-inventory']:CreateStash({ - stashId = 'vending_' .. data.id, - stashName = 'Vending Machine #' .. data.id, - maxWeight = Config.MaxWeight, - maxSlots = Config.MaxSlots, - stashCoords = vendingMachines[data.id].coords - }) end end end) @@ -77,15 +68,6 @@ RegisterNetEvent('vending:server:registerMachine', function(coords, prop) stash = 'vending_' .. machineId } - -- Create stash - exports['tgiann-inventory']:CreateStash({ - stashId = 'vending_' .. machineId, - stashName = 'Vending Machine #' .. machineId, - maxWeight = Config.MaxWeight, - maxSlots = Config.MaxSlots, - stashCoords = coords - }) - TriggerClientEvent('QBCore:Notify', src, 'Verkaufsautomat erfolgreich gekauft für $' .. Config.VendingMachinePrice .. '!', 'success') TriggerClientEvent('vending:client:refreshTargets', -1) end) @@ -123,7 +105,12 @@ RegisterNetEvent('vending:server:openStash', function(coords) return end - exports['tgiann-inventory']:OpenStash(src, machine.stash) + -- Open stash using tgiann-inventory + TriggerEvent('tgiann-inventory:server:openStash', src, machine.stash, { + maxweight = Config.MaxWeight, + slots = Config.MaxSlots, + label = 'Vending Machine #' .. machine.id + }) end) -- Set item price @@ -195,34 +182,39 @@ RegisterNetEvent('vending:server:buyItem', function(coords, itemName) return end - -- Check if item is available in stash - local stashItems = exports['tgiann-inventory']:GetStashItems(machine.stash) - local hasItem = false - - for i = 1, #stashItems do - if stashItems[i].name == itemName and stashItems[i].amount > 0 then - hasItem = true - break + -- Get stash items using tgiann-inventory callback + 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 give 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 + TriggerEvent('tgiann-inventory:server:removeItemFromStash', machine.stash, itemName, 1) + + -- Add item to player + 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 @@ -237,8 +229,15 @@ 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 + local hasItem = false + for k, v in pairs(Player.PlayerData.items) do + if v.name == Config.RobberyItem and v.amount > 0 then + hasItem = true + break + end + end + + if not hasItem then TriggerClientEvent('QBCore:Notify', src, 'Du benötigst einen ' .. Config.RobberyItem, 'error') return end @@ -296,7 +295,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 @@ -334,14 +333,22 @@ QBCore.Functions.CreateCallback('vending:server:getStashItems', function(source, end local machine = vendingMachines[machineId] - local items = exports['tgiann-inventory']:GetStashItems(machine.stash) - -- Add prices to items - for i = 1, #items do - items[i].price = machine.prices[items[i].name] or Config.DefaultPrice - end - - cb(items) + -- Get stash items using tgiann-inventory callback + 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 + + cb(items) + end, source, machine.stash) end) -- Check if player owns machine