forked from Simnation/Main
Update server.lua
This commit is contained in:
parent
392dc52572
commit
660c766f79
1 changed files with 52 additions and 138 deletions
|
@ -19,6 +19,7 @@ CreateThread(function()
|
||||||
stash = 'vending_' .. data.id
|
stash = 'vending_' .. data.id
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
print("^2[VENDING]^7 Loaded " .. #result .. " vending machines")
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
@ -68,6 +69,7 @@ RegisterNetEvent('vending:server:registerMachine', function(coords, prop)
|
||||||
stash = 'vending_' .. machineId
|
stash = 'vending_' .. machineId
|
||||||
}
|
}
|
||||||
|
|
||||||
|
print("^2[VENDING]^7 New vending machine registered: " .. machineId)
|
||||||
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)
|
||||||
|
@ -90,7 +92,7 @@ RegisterNetEvent('vending:server:openManagement', function(coords)
|
||||||
TriggerClientEvent('vending:client:openManagement', src, machine)
|
TriggerClientEvent('vending:client:openManagement', src, machine)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
-- Open stash (mit mehreren Methoden versuchen)
|
-- Open stash (korrekt 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,45 +107,12 @@ RegisterNetEvent('vending:server:openStash', function(coords)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Versuche verschiedene Methoden für tgiann-inventory
|
-- Öffne das Inventar mit tgiann-inventory
|
||||||
local success = false
|
exports["tgiann-inventory"]:OpenInventory(src, "stash", machine.stash, {
|
||||||
|
maxweight = Config.MaxWeight,
|
||||||
-- Methode 1: TriggerClientEvent
|
slots = Config.MaxSlots,
|
||||||
if not success then
|
label = 'Vending Machine #' .. machine.id
|
||||||
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
|
||||||
|
@ -197,7 +166,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 Fallback-System)
|
-- Buy item from vending machine
|
||||||
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)
|
||||||
|
@ -215,8 +184,20 @@ RegisterNetEvent('vending:server:buyItem', function(coords, itemName)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Check if item is available in our database
|
-- Get stash items
|
||||||
if not machine.items[itemName] or machine.items[itemName] <= 0 then
|
local stashItems = exports["tgiann-inventory"]:GetSecondaryInventoryItems("stash", machine.stash)
|
||||||
|
local hasItem = false
|
||||||
|
|
||||||
|
if stashItems then
|
||||||
|
for slot, item in pairs(stashItems) do
|
||||||
|
if item.name == itemName and item.amount > 0 then
|
||||||
|
hasItem = true
|
||||||
|
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
|
||||||
|
@ -226,96 +207,16 @@ 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 machine
|
-- Remove item from stash and add to player
|
||||||
machine.items[itemName] = machine.items[itemName] - 1
|
exports["tgiann-inventory"]:RemoveItemFromSecondaryInventory("stash", machine.stash, 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)
|
Player.Functions.AddItem(itemName, 1)
|
||||||
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[itemName], 'add')
|
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
|
||||||
|
@ -417,7 +318,7 @@ QBCore.Functions.CreateCallback('vending:server:getMachineByCoords', function(so
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
-- Get stash items for vending machine menu (using our database)
|
-- Get stash items for vending machine menu
|
||||||
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
|
||||||
|
@ -426,19 +327,16 @@ QBCore.Functions.CreateCallback('vending:server:getStashItems', function(source,
|
||||||
end
|
end
|
||||||
|
|
||||||
local machine = vendingMachines[machineId]
|
local machine = vendingMachines[machineId]
|
||||||
|
|
||||||
|
-- Get stash items using correct export
|
||||||
|
local stashItems = exports["tgiann-inventory"]:GetSecondaryInventoryItems("stash", machine.stash)
|
||||||
local items = {}
|
local items = {}
|
||||||
|
|
||||||
for itemName, amount in pairs(machine.items) do
|
if stashItems then
|
||||||
if amount > 0 then
|
for slot, item in pairs(stashItems) do
|
||||||
local itemData = QBCore.Shared.Items[itemName]
|
if item.amount > 0 then
|
||||||
if itemData then
|
item.price = machine.prices[item.name] or Config.DefaultPrice
|
||||||
table.insert(items, {
|
table.insert(items, item)
|
||||||
name = itemName,
|
|
||||||
label = itemData.label,
|
|
||||||
amount = amount,
|
|
||||||
price = machine.prices[itemName] or Config.DefaultPrice,
|
|
||||||
image = itemData.image
|
|
||||||
})
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -469,3 +367,19 @@ 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
|
||||||
|
QBCore.Commands.Add('vendingdebug', 'Debug vending machines (Admin Only)', {}, false, function(source, args)
|
||||||
|
local Player = QBCore.Functions.GetPlayer(source)
|
||||||
|
if Player.PlayerData.permission == "admin" or Player.PlayerData.permission == "god" then
|
||||||
|
local count = 0
|
||||||
|
for id, machine in pairs(vendingMachines) do
|
||||||
|
count = count + 1
|
||||||
|
print("^2[VENDING]^7 Machine #" .. id .. " | Owner: " .. machine.owner .. " | Money: $" .. machine.money)
|
||||||
|
end
|
||||||
|
|
||||||
|
TriggerClientEvent('QBCore:Notify', source, count .. ' Verkaufsautomaten geladen', 'success')
|
||||||
|
else
|
||||||
|
TriggerClientEvent('QBCore:Notify', source, 'Keine Berechtigung!', 'error')
|
||||||
|
end
|
||||||
|
end, 'admin')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue