1
0
Fork 0
forked from Simnation/Main

Update server.lua

This commit is contained in:
Nordi98 2025-07-29 08:35:03 +02:00
parent 75740691bc
commit ab8f61b35b

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 (korrigiert für tgiann-inventory) -- Open stash (mit korrektem tgiann-inventory Export)
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,8 @@ RegisterNetEvent('vending:server:openStash', function(coords)
return return
end end
-- Verwende TriggerEvent statt TriggerClientEvent für tgiann-inventory -- Korrekte tgiann-inventory Stash öffnen
TriggerEvent('tgiann-inventory:server:openStash', src, machine.stash, { exports['tgiann-inventory']:OpenStash(src, machine.stash, 'Vending Machine #' .. machine.id, Config.MaxWeight, Config.MaxSlots)
maxweight = Config.MaxWeight,
slots = Config.MaxSlots,
label = 'Vending Machine #' .. machine.id
})
end) end)
-- Set item price -- Set item price
@ -164,7 +160,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 (korrigiert) -- Buy item from vending machine (mit korrekten Exports)
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)
@ -182,37 +178,38 @@ RegisterNetEvent('vending:server:buyItem', function(coords, itemName)
return return
end end
-- Use callback to get stash items -- Get stash items using correct export
QBCore.Functions.TriggerCallback('tgiann-inventory:server:getStashItems', function(stashItems) local stashItems = exports['tgiann-inventory']:GetStashItems(machine.stash)
local hasItem = false local hasItem = false
local itemAmount = 0
if stashItems then
for slot, item in pairs(stashItems) do if stashItems then
if item.name == itemName and item.amount > 0 then for slot, item in pairs(stashItems) do
hasItem = true if item.name == itemName and item.amount > 0 then
break hasItem = true
end itemAmount = item.amount
break
end end
end end
end
if not hasItem then
TriggerClientEvent('QBCore:Notify', src, 'Artikel nicht verfügbar!', 'error') if not hasItem then
return TriggerClientEvent('QBCore:Notify', src, 'Artikel nicht verfügbar!', 'error')
end return
end
-- Remove money from player
Player.Functions.RemoveMoney('cash', price) -- Remove money from player
Player.Functions.RemoveMoney('cash', price)
-- Add money to machine
machine.money = machine.money + price -- Add money to machine
MySQL.update('UPDATE vending_machines SET money = ? WHERE id = ?', {machine.money, machineId}) 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) -- Remove item from stash and add to player
TriggerEvent('tgiann-inventory:server:addItem', src, itemName, 1) 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')
end, src, machine.stash) TriggerClientEvent('QBCore:Notify', src, 'Artikel gekauft für $' .. price .. '!', 'success')
end) end)
-- Start robbery -- Start robbery
@ -226,40 +223,39 @@ RegisterNetEvent('vending:server:startRobbery', function(coords)
local machine = vendingMachines[machineId] local machine = vendingMachines[machineId]
-- Check if player has required item using callback -- Check if player has required item using correct export
QBCore.Functions.TriggerCallback('tgiann-inventory:server:getItemByName', function(item) local hasItem = exports['tgiann-inventory']:GetItemByName(src, Config.RobberyItem)
if not item or item.amount < 1 then if not hasItem or hasItem.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
if robberyInProgress[machineId] then TriggerClientEvent('vending:client:startRobbery', src, coords)
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
@ -287,7 +283,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
TriggerEvent('tgiann-inventory:server:removeItem', src, Config.RobberyItem, 1) exports['tgiann-inventory']: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
@ -316,7 +312,7 @@ QBCore.Functions.CreateCallback('vending:server:getMachineByCoords', function(so
end end
end) end)
-- Get stash items for vending machine menu (korrigiert) -- Get stash items for vending machine menu (mit korrektem Export)
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
@ -326,21 +322,20 @@ QBCore.Functions.CreateCallback('vending:server:getStashItems', function(source,
local machine = vendingMachines[machineId] local machine = vendingMachines[machineId]
-- Use tgiann-inventory callback to get stash items -- Use correct tgiann-inventory export
QBCore.Functions.TriggerCallback('tgiann-inventory:server:getStashItems', function(stashItems) local stashItems = exports['tgiann-inventory']:GetStashItems(machine.stash)
local items = {} local items = {}
if stashItems then if stashItems then
for slot, item in pairs(stashItems) do for slot, item in pairs(stashItems) do
if item.amount > 0 then if item.amount > 0 then
item.price = machine.prices[item.name] or Config.DefaultPrice item.price = machine.prices[item.name] or Config.DefaultPrice
table.insert(items, item) table.insert(items, item)
end
end end
end end
end
cb(items)
end, source, machine.stash) cb(items)
end) end)
-- Check if player owns machine -- Check if player owns machine
@ -374,5 +369,17 @@ RegisterCommand('vendingdebug', function(source, args)
for id, machine in pairs(vendingMachines) do for id, machine in pairs(vendingMachines) do
print('ID:', id, 'Owner:', machine.owner, 'Money:', machine.money) print('ID:', id, 'Owner:', machine.owner, 'Money:', machine.money)
end end
else
local Player = QBCore.Functions.GetPlayer(source)
if Player then
local coords = GetEntityCoords(GetPlayerPed(source))
local machineId = getMachineIdByCoords(coords)
if machineId then
local machine = vendingMachines[machineId]
TriggerClientEvent('QBCore:Notify', source, 'Machine ID: ' .. machineId .. ' | Owner: ' .. machine.owner .. ' | Money: $' .. machine.money, 'primary')
else
TriggerClientEvent('QBCore:Notify', source, 'Keine Vending Machine in der Nähe!', 'error')
end
end
end end
end, true) end, false)