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)
|
TriggerClientEvent('vending:client:openManagement', src, machine)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
-- Open stash (mit korrektem tgiann-inventory Export)
|
-- Open stash (mit mehreren Methoden versuchen)
|
||||||
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,8 +105,45 @@ RegisterNetEvent('vending:server:openStash', function(coords)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Korrekte tgiann-inventory Stash öffnen
|
-- Versuche verschiedene Methoden für tgiann-inventory
|
||||||
exports['tgiann-inventory']:OpenStash(src, machine.stash, 'Vending Machine #' .. machine.id, Config.MaxWeight, Config.MaxSlots)
|
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)
|
end)
|
||||||
|
|
||||||
-- Set item price
|
-- Set item price
|
||||||
|
@ -160,7 +197,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 (mit korrekten Exports)
|
-- Buy item from vending machine (mit Fallback-System)
|
||||||
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)
|
||||||
|
@ -178,22 +215,8 @@ RegisterNetEvent('vending:server:buyItem', function(coords, itemName)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Get stash items using correct export
|
-- Check if item is available in our database
|
||||||
local stashItems = exports['tgiann-inventory']:GetStashItems(machine.stash)
|
if not machine.items[itemName] or machine.items[itemName] <= 0 then
|
||||||
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
|
|
||||||
TriggerClientEvent('QBCore:Notify', src, 'Artikel nicht verfügbar!', 'error')
|
TriggerClientEvent('QBCore:Notify', src, 'Artikel nicht verfügbar!', 'error')
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
@ -203,15 +226,96 @@ RegisterNetEvent('vending:server:buyItem', function(coords, itemName)
|
||||||
|
|
||||||
-- 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})
|
|
||||||
|
|
||||||
-- Remove item from stash and add to player
|
-- Remove item from machine
|
||||||
exports['tgiann-inventory']:RemoveItemFromStash(machine.stash, itemName, 1)
|
machine.items[itemName] = machine.items[itemName] - 1
|
||||||
exports['tgiann-inventory']:AddItem(src, 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')
|
TriggerClientEvent('QBCore:Notify', src, 'Artikel gekauft für $' .. price .. '!', 'success')
|
||||||
end)
|
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
|
-- Start robbery
|
||||||
RegisterNetEvent('vending:server:startRobbery', function(coords)
|
RegisterNetEvent('vending:server:startRobbery', function(coords)
|
||||||
local src = source
|
local src = source
|
||||||
|
@ -223,8 +327,8 @@ RegisterNetEvent('vending:server:startRobbery', function(coords)
|
||||||
|
|
||||||
local machine = vendingMachines[machineId]
|
local machine = vendingMachines[machineId]
|
||||||
|
|
||||||
-- Check if player has required item using correct export
|
-- Check if player has required item
|
||||||
local hasItem = exports['tgiann-inventory']:GetItemByName(src, Config.RobberyItem)
|
local hasItem = Player.Functions.GetItemByName(Config.RobberyItem)
|
||||||
if not hasItem or hasItem.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
|
||||||
|
@ -283,7 +387,8 @@ 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)
|
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')
|
TriggerClientEvent('QBCore:Notify', src, 'Dein ' .. Config.RobberyItem .. ' ist kaputt gegangen!', 'error')
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
@ -312,7 +417,7 @@ QBCore.Functions.CreateCallback('vending:server:getMachineByCoords', function(so
|
||||||
end
|
end
|
||||||
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)
|
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
|
||||||
|
@ -321,16 +426,19 @@ QBCore.Functions.CreateCallback('vending:server:getStashItems', function(source,
|
||||||
end
|
end
|
||||||
|
|
||||||
local machine = vendingMachines[machineId]
|
local machine = vendingMachines[machineId]
|
||||||
|
|
||||||
-- Use correct tgiann-inventory export
|
|
||||||
local stashItems = exports['tgiann-inventory']:GetStashItems(machine.stash)
|
|
||||||
local items = {}
|
local items = {}
|
||||||
|
|
||||||
if stashItems then
|
for itemName, amount in pairs(machine.items) do
|
||||||
for slot, item in pairs(stashItems) do
|
if amount > 0 then
|
||||||
if item.amount > 0 then
|
local itemData = QBCore.Shared.Items[itemName]
|
||||||
item.price = machine.prices[item.name] or Config.DefaultPrice
|
if itemData then
|
||||||
table.insert(items, item)
|
table.insert(items, {
|
||||||
|
name = itemName,
|
||||||
|
label = itemData.label,
|
||||||
|
amount = amount,
|
||||||
|
price = machine.prices[itemName] or Config.DefaultPrice,
|
||||||
|
image = itemData.image
|
||||||
|
})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -361,25 +469,3 @@ 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
|
|
||||||
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