1
0
Fork 0
forked from Simnation/Main

Update server.lua

This commit is contained in:
Nordi98 2025-07-29 08:30:01 +02:00
parent f0ffad0cb4
commit 59fc858611

View file

@ -90,7 +90,7 @@ RegisterNetEvent('vending:server:openManagement', function(coords)
TriggerClientEvent('vending:client:openManagement', src, machine) TriggerClientEvent('vending:client:openManagement', src, machine)
end) end)
-- Open stash (korrekt für tgiann-inventory) -- Open stash (korrigiert für tgiann-inventory)
RegisterNetEvent('vending:server:openStash', function(coords) RegisterNetEvent('vending:server:openStash', function(coords)
local src = source local src = source
local Player = QBCore.Functions.GetPlayer(src) local Player = QBCore.Functions.GetPlayer(src)
@ -105,12 +105,11 @@ RegisterNetEvent('vending:server:openStash', function(coords)
return return
end end
-- Korrekte tgiann-inventory Methode -- Verwende TriggerEvent statt TriggerClientEvent für tgiann-inventory
TriggerClientEvent('tgiann-inventory:client:openStash', src, { TriggerEvent('tgiann-inventory:server:openStash', src, machine.stash, {
stashId = machine.stash,
stashLabel = 'Vending Machine #' .. machine.id,
maxweight = Config.MaxWeight, maxweight = Config.MaxWeight,
slots = Config.MaxSlots slots = Config.MaxSlots,
label = 'Vending Machine #' .. machine.id
}) })
end) end)
@ -165,7 +164,7 @@ RegisterNetEvent('vending:server:withdrawMoney', function(coords, amount)
TriggerClientEvent('QBCore:Notify', src, 'Du hast $' .. amount .. ' abgehoben!', 'success') TriggerClientEvent('QBCore:Notify', src, 'Du hast $' .. amount .. ' abgehoben!', 'success')
end) end)
-- Buy item from vending machine -- Buy item from vending machine (korrigiert)
RegisterNetEvent('vending:server:buyItem', function(coords, itemName) RegisterNetEvent('vending:server:buyItem', function(coords, itemName)
local src = source local src = source
local Player = QBCore.Functions.GetPlayer(src) local Player = QBCore.Functions.GetPlayer(src)
@ -183,36 +182,37 @@ RegisterNetEvent('vending:server:buyItem', function(coords, itemName)
return return
end end
-- Get stash items using tgiann-inventory -- Use callback to get stash items
local stashItems = exports['tgiann-inventory']:getStashItems(machine.stash) QBCore.Functions.TriggerCallback('tgiann-inventory:server:getStashItems', function(stashItems)
local hasItem = false local hasItem = false
if stashItems then if stashItems then
for slot, item in pairs(stashItems) do for slot, item in pairs(stashItems) do
if item.name == itemName and item.amount > 0 then if item.name == itemName and item.amount > 0 then
hasItem = true hasItem = true
break break
end
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') return
return end
end
-- Remove money from player
-- Remove money from player Player.Functions.RemoveMoney('cash', price)
Player.Functions.RemoveMoney('cash', price)
-- Add money to machine
-- Add money to machine 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 add to player using events
-- Remove item from stash and add to player TriggerEvent('tgiann-inventory:server:removeItemFromStash', machine.stash, itemName, 1)
exports['tgiann-inventory']:removeItemFromStash(machine.stash, itemName, 1) TriggerEvent('tgiann-inventory:server:addItem', src, itemName, 1)
exports['tgiann-inventory']: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
@ -226,39 +226,40 @@ 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 using callback
local hasItem = exports['tgiann-inventory']:getItemByName(src, Config.RobberyItem) QBCore.Functions.TriggerCallback('tgiann-inventory:server:getItemByName', function(item)
if not hasItem or hasItem.amount < 1 then if not item or item.amount < 1 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
-- 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
end
-- Check if already being robbed
TriggerClientEvent('vending:client:startRobbery', src, coords) 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) end)
-- Complete robbery -- Complete robbery
@ -286,7 +287,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
@ -315,7 +316,7 @@ QBCore.Functions.CreateCallback('vending:server:getMachineByCoords', function(so
end end
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) QBCore.Functions.CreateCallback('vending:server:getStashItems', function(source, cb, coords)
local machineId = getMachineIdByCoords(coords) local machineId = getMachineIdByCoords(coords)
if not machineId then if not machineId then
@ -324,19 +325,22 @@ QBCore.Functions.CreateCallback('vending:server:getStashItems', function(source,
end end
local machine = vendingMachines[machineId] local machine = vendingMachines[machineId]
local stashItems = exports['tgiann-inventory']:getStashItems(machine.stash)
local items = {}
if stashItems then -- Use tgiann-inventory callback to get stash items
for slot, item in pairs(stashItems) do QBCore.Functions.TriggerCallback('tgiann-inventory:server:getStashItems', function(stashItems)
if item.amount > 0 then local items = {}
item.price = machine.prices[item.name] or Config.DefaultPrice
table.insert(items, item) 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 end
end
cb(items)
cb(items) end, source, machine.stash)
end) end)
-- Check if player owns machine -- Check if player owns machine
@ -362,3 +366,13 @@ QBCore.Functions.CreateCallback('vending:server:machineExists', function(source,
local machineId = getMachineIdByCoords(coords) local machineId = getMachineIdByCoords(coords)
cb(machineId ~= nil) cb(machineId ~= nil)
end) 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)