local QBCore = exports['qb-core']:GetCoreObject() -- Add targets to all vending machine props CreateThread(function() Wait(2000) print('[Vending] Adding targets to vending machine props') exports['qb-target']:AddTargetModel(Config.VendingProps, { options = { { type = "client", event = "vending:client:buyMachine", icon = "fas fa-dollar-sign", label = "Automaten kaufen ($" .. Config.VendingMachinePrice .. ")" }, { type = "client", event = "vending:client:openBuyMenu", icon = "fas fa-shopping-cart", label = "Kaufen" }, { type = "client", event = "vending:client:openOwnerMenu", icon = "fas fa-cog", label = "Verwalten" } }, distance = 2.0 }) print('[Vending] Targets added successfully') end) -- Buy vending machine RegisterNetEvent('vending:client:buyMachine', function(data) local entity = data.entity local coords = GetEntityCoords(entity) local model = GetEntityModel(entity) local prop = nil print('[Vending] Trying to buy machine at coords:', coords.x, coords.y, coords.z) -- Find prop name for i = 1, #Config.VendingProps do if GetHashKey(Config.VendingProps[i]) == model then prop = Config.VendingProps[i] break end end if not prop then print('[Vending] Prop not found in config') return end print('[Vending] Found prop:', prop) 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() print('[Vending] Confirming purchase') 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 local itemLabel = QBCore.Shared.Items[item.name] and QBCore.Shared.Items[item.name].label or item.name table.insert(options, { title = itemLabel, 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) 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 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 -- Debug command RegisterCommand('vendingtest', function() print('[Vending] Testing vending machine system') QBCore.Functions.Notify('Vending system test', 'primary') end, false)