Update server.lua

This commit is contained in:
Nordi98 2025-07-29 08:09:09 +02:00
parent 90d92129ee
commit 5190cf1220

View file

@ -18,15 +18,6 @@ CreateThread(function()
prices = json.decode(data.prices) or {}, prices = json.decode(data.prices) or {},
stash = 'vending_' .. data.id 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 end
end) end)
@ -77,15 +68,6 @@ RegisterNetEvent('vending:server:registerMachine', function(coords, prop)
stash = 'vending_' .. machineId 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('QBCore:Notify', src, 'Verkaufsautomat erfolgreich gekauft für $' .. Config.VendingMachinePrice .. '!', 'success')
TriggerClientEvent('vending:client:refreshTargets', -1) TriggerClientEvent('vending:client:refreshTargets', -1)
end) end)
@ -123,7 +105,12 @@ RegisterNetEvent('vending:server:openStash', function(coords)
return return
end 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) end)
-- Set item price -- Set item price
@ -195,16 +182,18 @@ RegisterNetEvent('vending:server:buyItem', function(coords, itemName)
return return
end end
-- Check if item is available in stash -- Get stash items using tgiann-inventory callback
local stashItems = exports['tgiann-inventory']:GetStashItems(machine.stash) QBCore.Functions.TriggerCallback('tgiann-inventory:server:getStashItems', function(stashItems)
local hasItem = false local hasItem = false
for i = 1, #stashItems do if stashItems then
if stashItems[i].name == itemName and stashItems[i].amount > 0 then for slot, item in pairs(stashItems) do
if item.name == itemName and item.amount > 0 then
hasItem = true hasItem = true
break break
end end
end end
end
if not hasItem then if not hasItem then
TriggerClientEvent('QBCore:Notify', src, 'Artikel nicht verfügbar!', 'error') TriggerClientEvent('QBCore:Notify', src, 'Artikel nicht verfügbar!', 'error')
@ -218,11 +207,14 @@ RegisterNetEvent('vending:server:buyItem', function(coords, itemName)
machine.money = machine.money + price machine.money = machine.money + price
MySQL.update('UPDATE vending_machines SET money = ? WHERE id = ?', {machine.money, machineId}) MySQL.update('UPDATE vending_machines SET money = ? WHERE id = ?', {machine.money, machineId})
-- Remove item from stash and give to player -- Remove item from stash
exports['tgiann-inventory']:RemoveItemFromStash(machine.stash, itemName, 1) TriggerEvent('tgiann-inventory:server:removeItemFromStash', machine.stash, itemName, 1)
exports['tgiann-inventory']:AddItem(src, itemName, 1)
-- Add item to player
TriggerEvent('tgiann-inventory:server:addItem', src, itemName, 1)
TriggerClientEvent('QBCore:Notify', src, 'Artikel gekauft für $' .. price .. '!', 'success') TriggerClientEvent('QBCore:Notify', src, 'Artikel gekauft für $' .. price .. '!', 'success')
end, src, machine.stash)
end) end)
-- Start robbery -- Start robbery
@ -237,8 +229,15 @@ RegisterNetEvent('vending:server:startRobbery', function(coords)
local machine = vendingMachines[machineId] local machine = vendingMachines[machineId]
-- Check if player has required item -- Check if player has required item
local hasItem = exports['tgiann-inventory']:GetItemByName(src, Config.RobberyItem) local hasItem = false
if not hasItem or hasItem.amount < 1 then 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') TriggerClientEvent('QBCore:Notify', src, 'Du benötigst einen ' .. Config.RobberyItem, 'error')
return return
end end
@ -296,7 +295,7 @@ RegisterNetEvent('vending:server:completeRobbery', function(coords, success)
-- Remove robbery item with chance -- Remove robbery item with chance
if math.random(1, 100) <= Config.RobberyItemBreakChance then 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') TriggerClientEvent('QBCore:Notify', src, 'Dein ' .. Config.RobberyItem .. ' ist kaputt gegangen!', 'error')
end end
else else
@ -334,14 +333,22 @@ QBCore.Functions.CreateCallback('vending:server:getStashItems', function(source,
end end
local machine = vendingMachines[machineId] local machine = vendingMachines[machineId]
local items = exports['tgiann-inventory']:GetStashItems(machine.stash)
-- Add prices to items -- Get stash items using tgiann-inventory callback
for i = 1, #items do QBCore.Functions.TriggerCallback('tgiann-inventory:server:getStashItems', function(stashItems)
items[i].price = machine.prices[items[i].name] or Config.DefaultPrice 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) end)
-- Check if player owns machine -- Check if player owns machine