forked from Simnation/Main
Update server.lua
This commit is contained in:
parent
ab8f61b35b
commit
392dc52572
1 changed files with 144 additions and 58 deletions
|
@ -90,7 +90,7 @@ RegisterNetEvent('vending:server:openManagement', function(coords)
|
|||
TriggerClientEvent('vending:client:openManagement', src, machine)
|
||||
end)
|
||||
|
||||
-- Open stash (mit korrektem tgiann-inventory Export)
|
||||
-- Open stash (mit mehreren Methoden versuchen)
|
||||
RegisterNetEvent('vending:server:openStash', function(coords)
|
||||
local src = source
|
||||
local Player = QBCore.Functions.GetPlayer(src)
|
||||
|
@ -105,8 +105,45 @@ RegisterNetEvent('vending:server:openStash', function(coords)
|
|||
return
|
||||
end
|
||||
|
||||
-- Korrekte tgiann-inventory Stash öffnen
|
||||
exports['tgiann-inventory']:OpenStash(src, machine.stash, 'Vending Machine #' .. machine.id, Config.MaxWeight, Config.MaxSlots)
|
||||
-- Versuche verschiedene Methoden für tgiann-inventory
|
||||
local success = false
|
||||
|
||||
-- Methode 1: TriggerClientEvent
|
||||
if not success then
|
||||
local clientSuccess = pcall(function()
|
||||
TriggerClientEvent('tgiann-inventory:client:openStash', src, {
|
||||
stashId = machine.stash,
|
||||
stashLabel = 'Vending Machine #' .. machine.id,
|
||||
maxweight = Config.MaxWeight,
|
||||
slots = Config.MaxSlots
|
||||
})
|
||||
end)
|
||||
if clientSuccess then
|
||||
success = true
|
||||
print('[Vending] Opened stash via client event')
|
||||
end
|
||||
end
|
||||
|
||||
-- Methode 2: Server Event
|
||||
if not success then
|
||||
local eventSuccess = pcall(function()
|
||||
TriggerEvent('tgiann-inventory:server:openStash', src, machine.stash, {
|
||||
maxweight = Config.MaxWeight,
|
||||
slots = Config.MaxSlots,
|
||||
label = 'Vending Machine #' .. machine.id
|
||||
})
|
||||
end)
|
||||
if eventSuccess then
|
||||
success = true
|
||||
print('[Vending] Opened stash via server event')
|
||||
end
|
||||
end
|
||||
|
||||
-- Methode 3: Fallback - eigenes Stash-System
|
||||
if not success then
|
||||
TriggerClientEvent('vending:client:openCustomStash', src, machine)
|
||||
print('[Vending] Opened custom stash fallback')
|
||||
end
|
||||
end)
|
||||
|
||||
-- Set item price
|
||||
|
@ -160,7 +197,7 @@ RegisterNetEvent('vending:server:withdrawMoney', function(coords, amount)
|
|||
TriggerClientEvent('QBCore:Notify', src, 'Du hast $' .. amount .. ' abgehoben!', 'success')
|
||||
end)
|
||||
|
||||
-- Buy item from vending machine (mit korrekten Exports)
|
||||
-- Buy item from vending machine (mit Fallback-System)
|
||||
RegisterNetEvent('vending:server:buyItem', function(coords, itemName)
|
||||
local src = source
|
||||
local Player = QBCore.Functions.GetPlayer(src)
|
||||
|
@ -178,22 +215,8 @@ RegisterNetEvent('vending:server:buyItem', function(coords, itemName)
|
|||
return
|
||||
end
|
||||
|
||||
-- Get stash items using correct export
|
||||
local stashItems = exports['tgiann-inventory']:GetStashItems(machine.stash)
|
||||
local hasItem = false
|
||||
local itemAmount = 0
|
||||
|
||||
if stashItems then
|
||||
for slot, item in pairs(stashItems) do
|
||||
if item.name == itemName and item.amount > 0 then
|
||||
hasItem = true
|
||||
itemAmount = item.amount
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if not hasItem then
|
||||
-- Check if item is available in our database
|
||||
if not machine.items[itemName] or machine.items[itemName] <= 0 then
|
||||
TriggerClientEvent('QBCore:Notify', src, 'Artikel nicht verfügbar!', 'error')
|
||||
return
|
||||
end
|
||||
|
@ -203,15 +226,96 @@ RegisterNetEvent('vending:server:buyItem', function(coords, itemName)
|
|||
|
||||
-- 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)
|
||||
-- Remove item from machine
|
||||
machine.items[itemName] = machine.items[itemName] - 1
|
||||
|
||||
-- Update database
|
||||
MySQL.update('UPDATE vending_machines SET money = ?, items = ? WHERE id = ?', {
|
||||
machine.money,
|
||||
json.encode(machine.items),
|
||||
machineId
|
||||
})
|
||||
|
||||
-- Add item to player using QBCore
|
||||
Player.Functions.AddItem(itemName, 1)
|
||||
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[itemName], 'add')
|
||||
|
||||
TriggerClientEvent('QBCore:Notify', src, 'Artikel gekauft für $' .. price .. '!', 'success')
|
||||
end)
|
||||
|
||||
-- Add item to vending machine (for stocking)
|
||||
RegisterNetEvent('vending:server:addItem', function(coords, itemName, amount)
|
||||
local src = source
|
||||
local Player = QBCore.Functions.GetPlayer(src)
|
||||
if not Player then return end
|
||||
|
||||
local machineId = getMachineIdByCoords(coords)
|
||||
if not machineId then return end
|
||||
|
||||
local machine = vendingMachines[machineId]
|
||||
if machine.owner ~= Player.PlayerData.citizenid then
|
||||
TriggerClientEvent('QBCore:Notify', src, 'Das ist nicht dein Verkaufsautomat!', 'error')
|
||||
return
|
||||
end
|
||||
|
||||
-- Check if player has the item
|
||||
local item = Player.Functions.GetItemByName(itemName)
|
||||
if not item or item.amount < amount then
|
||||
TriggerClientEvent('QBCore:Notify', src, 'Du hast nicht genug von diesem Item!', 'error')
|
||||
return
|
||||
end
|
||||
|
||||
-- Remove item from player
|
||||
Player.Functions.RemoveItem(itemName, amount)
|
||||
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[itemName], 'remove')
|
||||
|
||||
-- Add item to machine
|
||||
if not machine.items[itemName] then
|
||||
machine.items[itemName] = 0
|
||||
end
|
||||
machine.items[itemName] = machine.items[itemName] + amount
|
||||
|
||||
-- Update database
|
||||
MySQL.update('UPDATE vending_machines SET items = ? WHERE id = ?', {json.encode(machine.items), machineId})
|
||||
|
||||
TriggerClientEvent('QBCore:Notify', src, amount .. 'x ' .. (QBCore.Shared.Items[itemName] and QBCore.Shared.Items[itemName].label or itemName) .. ' zum Automaten hinzugefügt!', 'success')
|
||||
end)
|
||||
|
||||
-- Remove item from vending machine
|
||||
RegisterNetEvent('vending:server:removeItem', function(coords, itemName, amount)
|
||||
local src = source
|
||||
local Player = QBCore.Functions.GetPlayer(src)
|
||||
if not Player then return end
|
||||
|
||||
local machineId = getMachineIdByCoords(coords)
|
||||
if not machineId then return end
|
||||
|
||||
local machine = vendingMachines[machineId]
|
||||
if machine.owner ~= Player.PlayerData.citizenid then
|
||||
TriggerClientEvent('QBCore:Notify', src, 'Das ist nicht dein Verkaufsautomat!', 'error')
|
||||
return
|
||||
end
|
||||
|
||||
-- Check if machine has the item
|
||||
if not machine.items[itemName] or machine.items[itemName] < amount then
|
||||
TriggerClientEvent('QBCore:Notify', src, 'Nicht genug Items im Automaten!', 'error')
|
||||
return
|
||||
end
|
||||
|
||||
-- Remove item from machine
|
||||
machine.items[itemName] = machine.items[itemName] - amount
|
||||
|
||||
-- Add item to player
|
||||
Player.Functions.AddItem(itemName, amount)
|
||||
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[itemName], 'add')
|
||||
|
||||
-- Update database
|
||||
MySQL.update('UPDATE vending_machines SET items = ? WHERE id = ?', {json.encode(machine.items), machineId})
|
||||
|
||||
TriggerClientEvent('QBCore:Notify', src, amount .. 'x ' .. (QBCore.Shared.Items[itemName] and QBCore.Shared.Items[itemName].label or itemName) .. ' aus dem Automaten entfernt!', 'success')
|
||||
end)
|
||||
|
||||
-- Start robbery
|
||||
RegisterNetEvent('vending:server:startRobbery', function(coords)
|
||||
local src = source
|
||||
|
@ -223,8 +327,8 @@ RegisterNetEvent('vending:server:startRobbery', function(coords)
|
|||
|
||||
local machine = vendingMachines[machineId]
|
||||
|
||||
-- Check if player has required item using correct export
|
||||
local hasItem = exports['tgiann-inventory']:GetItemByName(src, Config.RobberyItem)
|
||||
-- Check if player has required item
|
||||
local hasItem = Player.Functions.GetItemByName(Config.RobberyItem)
|
||||
if not hasItem or hasItem.amount < 1 then
|
||||
TriggerClientEvent('QBCore:Notify', src, 'Du benötigst einen ' .. Config.RobberyItem, 'error')
|
||||
return
|
||||
|
@ -283,7 +387,8 @@ 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)
|
||||
Player.Functions.RemoveItem(Config.RobberyItem, 1)
|
||||
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[Config.RobberyItem], 'remove')
|
||||
TriggerClientEvent('QBCore:Notify', src, 'Dein ' .. Config.RobberyItem .. ' ist kaputt gegangen!', 'error')
|
||||
end
|
||||
else
|
||||
|
@ -312,7 +417,7 @@ QBCore.Functions.CreateCallback('vending:server:getMachineByCoords', function(so
|
|||
end
|
||||
end)
|
||||
|
||||
-- Get stash items for vending machine menu (mit korrektem Export)
|
||||
-- Get stash items for vending machine menu (using our database)
|
||||
QBCore.Functions.CreateCallback('vending:server:getStashItems', function(source, cb, coords)
|
||||
local machineId = getMachineIdByCoords(coords)
|
||||
if not machineId then
|
||||
|
@ -321,16 +426,19 @@ QBCore.Functions.CreateCallback('vending:server:getStashItems', function(source,
|
|||
end
|
||||
|
||||
local machine = vendingMachines[machineId]
|
||||
|
||||
-- Use correct tgiann-inventory export
|
||||
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)
|
||||
for itemName, amount in pairs(machine.items) do
|
||||
if amount > 0 then
|
||||
local itemData = QBCore.Shared.Items[itemName]
|
||||
if itemData then
|
||||
table.insert(items, {
|
||||
name = itemName,
|
||||
label = itemData.label,
|
||||
amount = amount,
|
||||
price = machine.prices[itemName] or Config.DefaultPrice,
|
||||
image = itemData.image
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -361,25 +469,3 @@ 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
|
||||
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, false)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue