2025-07-29 07:30:32 +02:00
|
|
|
local QBCore = exports['qb-core']:GetCoreObject()
|
|
|
|
|
|
|
|
-- Add targets to all vending machine props
|
|
|
|
CreateThread(function()
|
2025-07-29 08:18:21 +02:00
|
|
|
Wait(2000)
|
|
|
|
print('[Vending] Adding targets to vending machine props')
|
2025-07-29 07:30:32 +02:00
|
|
|
|
|
|
|
exports['qb-target']:AddTargetModel(Config.VendingProps, {
|
|
|
|
options = {
|
|
|
|
{
|
|
|
|
type = "client",
|
|
|
|
event = "vending:client:buyMachine",
|
|
|
|
icon = "fas fa-dollar-sign",
|
2025-07-29 08:18:21 +02:00
|
|
|
label = "Automaten kaufen ($" .. Config.VendingMachinePrice .. ")"
|
2025-07-29 07:30:32 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "client",
|
|
|
|
event = "vending:client:openBuyMenu",
|
|
|
|
icon = "fas fa-shopping-cart",
|
2025-07-29 08:18:21 +02:00
|
|
|
label = "Kaufen"
|
2025-07-29 07:30:32 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "client",
|
|
|
|
event = "vending:client:openOwnerMenu",
|
|
|
|
icon = "fas fa-cog",
|
2025-07-29 08:18:21 +02:00
|
|
|
label = "Verwalten"
|
2025-07-29 07:30:32 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
distance = 2.0
|
|
|
|
})
|
2025-07-29 07:51:42 +02:00
|
|
|
|
2025-07-29 08:18:21 +02:00
|
|
|
print('[Vending] Targets added successfully')
|
|
|
|
end)
|
2025-07-29 07:30:32 +02:00
|
|
|
|
|
|
|
-- Buy vending machine
|
|
|
|
RegisterNetEvent('vending:client:buyMachine', function(data)
|
|
|
|
local entity = data.entity
|
|
|
|
local coords = GetEntityCoords(entity)
|
|
|
|
local model = GetEntityModel(entity)
|
|
|
|
local prop = nil
|
|
|
|
|
2025-07-29 08:18:21 +02:00
|
|
|
print('[Vending] Trying to buy machine at coords:', coords.x, coords.y, coords.z)
|
|
|
|
|
2025-07-29 07:30:32 +02:00
|
|
|
-- Find prop name
|
|
|
|
for i = 1, #Config.VendingProps do
|
|
|
|
if GetHashKey(Config.VendingProps[i]) == model then
|
|
|
|
prop = Config.VendingProps[i]
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2025-07-29 08:18:21 +02:00
|
|
|
if not prop then
|
|
|
|
print('[Vending] Prop not found in config')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
print('[Vending] Found prop:', prop)
|
2025-07-29 07:30:32 +02:00
|
|
|
|
|
|
|
lib.registerContext({
|
|
|
|
id = 'vending_buy_confirm',
|
|
|
|
title = 'Verkaufsautomat kaufen',
|
|
|
|
options = {
|
|
|
|
{
|
|
|
|
title = 'Bestätigen',
|
|
|
|
description = 'Automaten für $' .. Config.VendingMachinePrice .. ' kaufen',
|
|
|
|
icon = 'fas fa-check',
|
|
|
|
onSelect = function()
|
2025-07-29 08:18:21 +02:00
|
|
|
print('[Vending] Confirming purchase')
|
2025-07-29 07:30:32 +02:00
|
|
|
TriggerServerEvent('vending:server:registerMachine', coords, prop)
|
|
|
|
end
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title = 'Abbrechen',
|
|
|
|
description = 'Kauf abbrechen',
|
|
|
|
icon = 'fas fa-times'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
lib.showContext('vending_buy_confirm')
|
|
|
|
end)
|
|
|
|
|
|
|
|
-- Open buy menu
|
|
|
|
RegisterNetEvent('vending:client:openBuyMenu', function(data)
|
|
|
|
local entity = data.entity
|
|
|
|
local coords = GetEntityCoords(entity)
|
|
|
|
|
|
|
|
QBCore.Functions.TriggerCallback('vending:server:getStashItems', function(items)
|
|
|
|
if #items == 0 then
|
|
|
|
QBCore.Functions.Notify('Dieser Automat ist leer!', 'error')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local options = {}
|
|
|
|
|
|
|
|
for i = 1, #items do
|
|
|
|
local item = items[i]
|
|
|
|
if item.amount > 0 then
|
2025-07-29 07:51:42 +02:00
|
|
|
local itemLabel = QBCore.Shared.Items[item.name] and QBCore.Shared.Items[item.name].label or item.name
|
2025-07-29 07:30:32 +02:00
|
|
|
table.insert(options, {
|
2025-07-29 07:51:42 +02:00
|
|
|
title = itemLabel,
|
2025-07-29 07:30:32 +02:00
|
|
|
description = 'Preis: $' .. item.price .. ' | Verfügbar: ' .. item.amount,
|
|
|
|
onSelect = function()
|
|
|
|
TriggerServerEvent('vending:server:buyItem', coords, item.name)
|
|
|
|
end
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
lib.registerContext({
|
|
|
|
id = 'vending_buy_menu',
|
|
|
|
title = 'Verkaufsautomat',
|
|
|
|
options = options
|
|
|
|
})
|
|
|
|
|
|
|
|
lib.showContext('vending_buy_menu')
|
|
|
|
end, coords)
|
|
|
|
end)
|
|
|
|
|
|
|
|
-- Open owner menu
|
|
|
|
RegisterNetEvent('vending:client:openOwnerMenu', function(data)
|
|
|
|
local entity = data.entity
|
|
|
|
local coords = GetEntityCoords(entity)
|
|
|
|
|
|
|
|
lib.registerContext({
|
|
|
|
id = 'vending_owner_menu',
|
|
|
|
title = 'Verkaufsautomat Verwaltung',
|
|
|
|
options = {
|
|
|
|
{
|
|
|
|
title = 'Inventar verwalten',
|
|
|
|
description = 'Items hinzufügen/entfernen',
|
|
|
|
icon = 'fas fa-box',
|
|
|
|
onSelect = function()
|
|
|
|
TriggerServerEvent('vending:server:openStash', coords)
|
|
|
|
end
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title = 'Geld abheben',
|
|
|
|
description = 'Einnahmen auszahlen lassen',
|
|
|
|
icon = 'fas fa-money-bill',
|
|
|
|
onSelect = function()
|
|
|
|
openWithdrawMenu(coords)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
lib.showContext('vending_owner_menu')
|
|
|
|
end)
|
|
|
|
|
|
|
|
-- Open withdraw menu
|
|
|
|
function openWithdrawMenu(coords)
|
|
|
|
QBCore.Functions.TriggerCallback('vending:server:getMachineByCoords', function(machine)
|
2025-07-29 07:51:42 +02:00
|
|
|
if not machine then
|
|
|
|
QBCore.Functions.Notify('Automat nicht gefunden!', 'error')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if machine.money <= 0 then
|
|
|
|
QBCore.Functions.Notify('Kein Geld im Automaten!', 'error')
|
|
|
|
return
|
|
|
|
end
|
2025-07-29 07:30:32 +02:00
|
|
|
|
|
|
|
local input = lib.inputDialog('Geld abheben', {
|
|
|
|
{
|
|
|
|
type = 'number',
|
|
|
|
label = 'Betrag (Verfügbar: $' .. machine.money .. ')',
|
|
|
|
description = 'Wie viel möchtest du abheben?',
|
|
|
|
required = true,
|
|
|
|
min = 1,
|
|
|
|
max = machine.money
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if input and input[1] then
|
|
|
|
TriggerServerEvent('vending:server:withdrawMoney', coords, tonumber(input[1]))
|
|
|
|
end
|
|
|
|
end, coords)
|
|
|
|
end
|
|
|
|
|
2025-07-29 08:18:21 +02:00
|
|
|
-- Debug command
|
|
|
|
RegisterCommand('vendingtest', function()
|
|
|
|
print('[Vending] Testing vending machine system')
|
|
|
|
QBCore.Functions.Notify('Vending system test', 'primary')
|
2025-07-29 07:51:42 +02:00
|
|
|
end, false)
|