1
0
Fork 0
forked from Simnation/Main

Update client.lua

This commit is contained in:
Nordi98 2025-07-29 10:11:01 +02:00
parent 06a3a3297c
commit a4780f13be

View file

@ -93,12 +93,12 @@ end, false)
-- Check if machine is registered -- Check if machine is registered
function isRegisteredMachine(entity) function isRegisteredMachine(entity)
local coords = GetEntityCoords(entity) local entityId = NetworkGetNetworkIdFromEntity(entity)
local isRegistered = false local isRegistered = false
QBCore.Functions.TriggerCallback('vending:server:machineExists', function(exists) QBCore.Functions.TriggerCallback('vending:server:machineExists', function(exists)
isRegistered = exists isRegistered = exists
end, coords) end, entityId)
-- Wait for callback (not ideal but works for canInteract) -- Wait for callback (not ideal but works for canInteract)
local timeout = 0 local timeout = 0
@ -112,12 +112,12 @@ end
-- Check if player can manage machine -- Check if player can manage machine
function canManageMachine(entity) function canManageMachine(entity)
local coords = GetEntityCoords(entity) local entityId = NetworkGetNetworkIdFromEntity(entity)
local canManage = false local canManage = false
QBCore.Functions.TriggerCallback('vending:server:canManage', function(result) QBCore.Functions.TriggerCallback('vending:server:canManage', function(result)
canManage = result canManage = result
end, coords) end, entityId)
-- Wait for callback -- Wait for callback
local timeout = 0 local timeout = 0
@ -134,8 +134,11 @@ RegisterNetEvent('vending:client:buyMachine', function(data)
local entity = data.entity local entity = data.entity
local coords = GetEntityCoords(entity) local coords = GetEntityCoords(entity)
local model = GetEntityModel(entity) local model = GetEntityModel(entity)
local entityId = NetworkGetNetworkIdFromEntity(entity)
local prop = nil local prop = nil
print("[VENDING] Buying machine: Entity ID: " .. entityId)
-- Find prop name -- Find prop name
for i = 1, #Config.VendingProps do for i = 1, #Config.VendingProps do
if GetHashKey(Config.VendingProps[i]) == model then if GetHashKey(Config.VendingProps[i]) == model then
@ -155,7 +158,7 @@ RegisterNetEvent('vending:client:buyMachine', function(data)
description = 'Automaten für $' .. Config.VendingMachinePrice .. ' kaufen', description = 'Automaten für $' .. Config.VendingMachinePrice .. ' kaufen',
icon = 'fas fa-check', icon = 'fas fa-check',
onSelect = function() onSelect = function()
TriggerServerEvent('vending:server:registerMachine', coords, prop) TriggerServerEvent('vending:server:registerMachine', coords, prop, entityId)
end end
}, },
{ {
@ -172,7 +175,9 @@ end)
-- Open buy menu with quantity selection -- Open buy menu with quantity selection
RegisterNetEvent('vending:client:openBuyMenu', function(data) RegisterNetEvent('vending:client:openBuyMenu', function(data)
local entity = data.entity local entity = data.entity
local coords = GetEntityCoords(entity) local entityId = NetworkGetNetworkIdFromEntity(entity)
print("[VENDING] Opening buy menu: Entity ID: " .. entityId)
QBCore.Functions.TriggerCallback('vending:server:getStashItems', function(items) QBCore.Functions.TriggerCallback('vending:server:getStashItems', function(items)
if #items == 0 then if #items == 0 then
@ -191,7 +196,7 @@ RegisterNetEvent('vending:client:openBuyMenu', function(data)
description = 'Preis: $' .. item.price .. ' | Verfügbar: ' .. item.amount, description = 'Preis: $' .. item.price .. ' | Verfügbar: ' .. item.amount,
icon = 'fas fa-shopping-cart', icon = 'fas fa-shopping-cart',
onSelect = function() onSelect = function()
openQuantityDialog(coords, item.name, item.price, item.amount, itemLabel) openQuantityDialog(entityId, item.name, item.price, item.amount, itemLabel)
end end
}) })
end end
@ -209,11 +214,11 @@ RegisterNetEvent('vending:client:openBuyMenu', function(data)
}) })
lib.showContext('vending_buy_menu') lib.showContext('vending_buy_menu')
end, coords) end, entityId)
end) end)
-- Open quantity dialog for buying items -- Open quantity dialog for buying items
function openQuantityDialog(coords, itemName, price, maxAmount, itemLabel) function openQuantityDialog(entityId, itemName, price, maxAmount, itemLabel)
local input = lib.inputDialog('Menge auswählen', { local input = lib.inputDialog('Menge auswählen', {
{ {
type = 'number', type = 'number',
@ -229,7 +234,7 @@ function openQuantityDialog(coords, itemName, price, maxAmount, itemLabel)
if input and input[1] then if input and input[1] then
local amount = tonumber(input[1]) local amount = tonumber(input[1])
if amount > 0 and amount <= maxAmount then if amount > 0 and amount <= maxAmount then
TriggerServerEvent('vending:server:buyItem', coords, itemName, amount) TriggerServerEvent('vending:server:buyItem', itemName, amount, entityId)
else else
QBCore.Functions.Notify('Ungültige Menge!', 'error') QBCore.Functions.Notify('Ungültige Menge!', 'error')
end end
@ -239,21 +244,25 @@ end
-- Open owner menu -- Open owner menu
RegisterNetEvent('vending:client:openOwnerMenu', function(data) RegisterNetEvent('vending:client:openOwnerMenu', function(data)
local entity = data.entity local entity = data.entity
local coords = GetEntityCoords(entity) local entityId = NetworkGetNetworkIdFromEntity(entity)
QBCore.Functions.TriggerCallback('vending:server:getMachineByCoords', function(machine) print("[VENDING] Opening owner menu: Entity ID: " .. entityId)
QBCore.Functions.TriggerCallback('vending:server:getMachineByEntity', function(machine)
if not machine then if not machine then
QBCore.Functions.Notify('Automat nicht gefunden!', 'error') QBCore.Functions.Notify('Automat nicht gefunden!', 'error')
return return
end end
print("[VENDING] Machine data received: ID: " .. machine.id .. ", isOwner: " .. tostring(machine.isOwner))
local options = { local options = {
{ {
title = 'Inventar verwalten', title = 'Inventar verwalten',
description = 'Items hinzufügen/entfernen', description = 'Items hinzufügen/entfernen',
icon = 'fas fa-box', icon = 'fas fa-box',
onSelect = function() onSelect = function()
TriggerServerEvent('vending:server:openStash', coords) TriggerServerEvent('vending:server:openStash', entityId)
end end
}, },
{ {
@ -261,7 +270,7 @@ RegisterNetEvent('vending:client:openOwnerMenu', function(data)
description = 'Verkaufspreise für Items setzen', description = 'Verkaufspreise für Items setzen',
icon = 'fas fa-tags', icon = 'fas fa-tags',
onSelect = function() onSelect = function()
openPriceMenu(coords) openPriceMenu(entityId)
end end
}, },
{ {
@ -269,7 +278,7 @@ RegisterNetEvent('vending:client:openOwnerMenu', function(data)
description = 'Verfügbar: $' .. machine.money, description = 'Verfügbar: $' .. machine.money,
icon = 'fas fa-money-bill', icon = 'fas fa-money-bill',
onSelect = function() onSelect = function()
openWithdrawMenu(coords, machine.money) openWithdrawMenu(entityId, machine.money)
end end
}, },
{ {
@ -284,12 +293,13 @@ RegisterNetEvent('vending:client:openOwnerMenu', function(data)
-- Add manager options only for owner -- Add manager options only for owner
if machine.isOwner then if machine.isOwner then
print("[VENDING] Adding manager options for owner")
table.insert(options, { table.insert(options, {
title = 'Verwalter', title = 'Verwalter',
description = 'Verwalter hinzufügen/entfernen', description = 'Verwalter hinzufügen/entfernen',
icon = 'fas fa-users-cog', icon = 'fas fa-users-cog',
onSelect = function() onSelect = function()
openManagersMenu(coords) openManagersMenu(entityId)
end end
}) })
@ -299,9 +309,11 @@ RegisterNetEvent('vending:client:openOwnerMenu', function(data)
description = 'Verkaufe den Automaten für ' .. math.floor(Config.VendingMachinePrice * Config.SellBackPercentage / 100) .. '$', description = 'Verkaufe den Automaten für ' .. math.floor(Config.VendingMachinePrice * Config.SellBackPercentage / 100) .. '$',
icon = 'fas fa-dollar-sign', icon = 'fas fa-dollar-sign',
onSelect = function() onSelect = function()
sellVendingMachine(coords, machine.id) sellVendingMachine(entityId, machine.id)
end end
}) })
else
print("[VENDING] Not adding manager options - not owner")
end end
lib.registerContext({ lib.registerContext({
@ -311,11 +323,11 @@ RegisterNetEvent('vending:client:openOwnerMenu', function(data)
}) })
lib.showContext('vending_owner_menu') lib.showContext('vending_owner_menu')
end, coords) end, entityId)
end) end)
-- Funktion zum Verkaufen des Automaten -- Funktion zum Verkaufen des Automaten
function sellVendingMachine(coords, machineId) function sellVendingMachine(entityId, machineId)
local input = lib.inputDialog('Automaten verkaufen', { local input = lib.inputDialog('Automaten verkaufen', {
{ {
type = 'checkbox', type = 'checkbox',
@ -326,12 +338,12 @@ function sellVendingMachine(coords, machineId)
}) })
if input and input[1] then if input and input[1] then
TriggerServerEvent('vending:server:sellMachine', coords, machineId) TriggerServerEvent('vending:server:sellMachine', machineId, entityId)
end end
end end
-- Open price menu -- Open price menu
function openPriceMenu(coords) function openPriceMenu(entityId)
QBCore.Functions.TriggerCallback('vending:server:getStashItems', function(items) QBCore.Functions.TriggerCallback('vending:server:getStashItems', function(items)
if #items == 0 then if #items == 0 then
QBCore.Functions.Notify('Keine Items im Automaten!', 'error') QBCore.Functions.Notify('Keine Items im Automaten!', 'error')
@ -348,7 +360,7 @@ function openPriceMenu(coords)
description = 'Aktueller Preis: $' .. item.price, description = 'Aktueller Preis: $' .. item.price,
icon = 'fas fa-tag', icon = 'fas fa-tag',
onSelect = function() onSelect = function()
setPriceForItem(coords, item.name, itemLabel) setPriceForItem(entityId, item.name, itemLabel)
end end
}) })
end end
@ -361,11 +373,11 @@ function openPriceMenu(coords)
}) })
lib.showContext('vending_price_menu') lib.showContext('vending_price_menu')
end, coords) end, entityId)
end end
-- Set price for specific item -- Set price for specific item
function setPriceForItem(coords, itemName, itemLabel) function setPriceForItem(entityId, itemName, itemLabel)
local input = lib.inputDialog('Preis festlegen', { local input = lib.inputDialog('Preis festlegen', {
{ {
type = 'number', type = 'number',
@ -378,12 +390,12 @@ function setPriceForItem(coords, itemName, itemLabel)
}) })
if input and input[1] then if input and input[1] then
TriggerServerEvent('vending:server:setItemPrice', coords, itemName, tonumber(input[1])) TriggerServerEvent('vending:server:setItemPrice', itemName, tonumber(input[1]), entityId)
end end
end end
-- Open withdraw menu -- Open withdraw menu
function openWithdrawMenu(coords, availableMoney) function openWithdrawMenu(entityId, availableMoney)
if availableMoney <= 0 then if availableMoney <= 0 then
QBCore.Functions.Notify('Kein Geld im Automaten!', 'error') QBCore.Functions.Notify('Kein Geld im Automaten!', 'error')
return return
@ -401,7 +413,7 @@ function openWithdrawMenu(coords, availableMoney)
}) })
if input and input[1] then if input and input[1] then
TriggerServerEvent('vending:server:withdrawMoney', coords, tonumber(input[1])) TriggerServerEvent('vending:server:withdrawMoney', tonumber(input[1]), entityId)
end end
end end
@ -434,7 +446,7 @@ function openStatsMenu(machine)
end end
-- Open managers menu -- Open managers menu
function openManagersMenu(coords) function openManagersMenu(entityId)
-- Get current managers -- Get current managers
QBCore.Functions.TriggerCallback('vending:server:getManagers', function(managers) QBCore.Functions.TriggerCallback('vending:server:getManagers', function(managers)
local options = { local options = {
@ -443,7 +455,7 @@ function openManagersMenu(coords)
description = 'Neuen Verwalter hinzufügen', description = 'Neuen Verwalter hinzufügen',
icon = 'fas fa-user-plus', icon = 'fas fa-user-plus',
onSelect = function() onSelect = function()
openAddManagerMenu(coords) openAddManagerMenu(entityId)
end end
} }
} }
@ -467,9 +479,9 @@ function openManagersMenu(coords)
description = 'Verwalter entfernen', description = 'Verwalter entfernen',
icon = 'fas fa-user-minus', icon = 'fas fa-user-minus',
onSelect = function() onSelect = function()
TriggerServerEvent('vending:server:removeManager', coords, manager.citizenid) TriggerServerEvent('vending:server:removeManager', manager.citizenid, entityId)
Wait(500) Wait(500)
openManagersMenu(coords) -- Refresh the menu openManagersMenu(entityId) -- Refresh the menu
end end
} }
} }
@ -495,11 +507,11 @@ function openManagersMenu(coords)
}) })
lib.showContext('managers_menu') lib.showContext('managers_menu')
end, coords) end, entityId)
end end
-- Open add manager menu -- Open add manager menu
function openAddManagerMenu(coords) function openAddManagerMenu(entityId)
QBCore.Functions.TriggerCallback('vending:server:getOnlinePlayers', function(players) QBCore.Functions.TriggerCallback('vending:server:getOnlinePlayers', function(players)
if #players == 0 then if #players == 0 then
QBCore.Functions.Notify('Keine Spieler online!', 'error') QBCore.Functions.Notify('Keine Spieler online!', 'error')
@ -515,9 +527,9 @@ function openAddManagerMenu(coords)
description = 'ID: ' .. player.id, description = 'ID: ' .. player.id,
icon = 'fas fa-user', icon = 'fas fa-user',
onSelect = function() onSelect = function()
TriggerServerEvent('vending:server:addManager', coords, player.id) TriggerServerEvent('vending:server:addManager', player.id, entityId)
Wait(500) Wait(500)
openManagersMenu(coords) -- Refresh the menu openManagersMenu(entityId) -- Refresh the menu
end end
}) })
end end
@ -536,7 +548,7 @@ end
-- Robbery menu -- Robbery menu
RegisterNetEvent('vending:client:startRobbery', function(data) RegisterNetEvent('vending:client:startRobbery', function(data)
local entity = data.entity local entity = data.entity
local coords = GetEntityCoords(entity) local entityId = NetworkGetNetworkIdFromEntity(entity)
lib.registerContext({ lib.registerContext({
id = 'vending_robbery_confirm', id = 'vending_robbery_confirm',
@ -547,7 +559,7 @@ RegisterNetEvent('vending:client:startRobbery', function(data)
description = 'Versuche den Automaten aufzubrechen', description = 'Versuche den Automaten aufzubrechen',
icon = 'fas fa-mask', icon = 'fas fa-mask',
onSelect = function() onSelect = function()
TriggerServerEvent('vending:server:startRobbery', coords) TriggerServerEvent('vending:server:startRobbery', entityId)
end end
}, },
{ {
@ -562,7 +574,7 @@ RegisterNetEvent('vending:client:startRobbery', function(data)
end) end)
-- Start robbery animation and progress -- Start robbery animation and progress
RegisterNetEvent('vending:client:startRobbery', function(coords) RegisterNetEvent('vending:client:startRobbery', function(entityId)
local playerPed = PlayerPedId() local playerPed = PlayerPedId()
local robberyTime = 10000 -- 10 seconds local robberyTime = 10000 -- 10 seconds
@ -589,12 +601,12 @@ RegisterNetEvent('vending:client:startRobbery', function(coords)
}) })
ClearPedTasks(playerPed) ClearPedTasks(playerPed)
TriggerServerEvent('vending:server:completeRobbery', coords, success) TriggerServerEvent('vending:server:completeRobbery', entityId, success)
else else
-- Fallback without progress bar -- Fallback without progress bar
Wait(robberyTime) Wait(robberyTime)
ClearPedTasks(playerPed) ClearPedTasks(playerPed)
TriggerServerEvent('vending:server:completeRobbery', coords, true) TriggerServerEvent('vending:server:completeRobbery', entityId, true)
end end
end) end)
@ -640,7 +652,7 @@ RegisterNetEvent('vending:client:openManagement', function(machine)
description = 'Items hinzufügen oder entfernen', description = 'Items hinzufügen oder entfernen',
icon = 'fas fa-box', icon = 'fas fa-box',
onSelect = function() onSelect = function()
TriggerServerEvent('vending:server:openStash', machine.coords) TriggerServerEvent('vending:server:openStash', machine.entityId)
end end
}, },
{ {
@ -648,7 +660,7 @@ RegisterNetEvent('vending:client:openManagement', function(machine)
description = 'Geld abheben', description = 'Geld abheben',
icon = 'fas fa-money-bill', icon = 'fas fa-money-bill',
onSelect = function() onSelect = function()
openWithdrawMenu(machine.coords, machine.money) openWithdrawMenu(machine.entityId, machine.money)
end end
} }
} }
@ -676,7 +688,8 @@ RegisterCommand('checkvendingprops', function()
if dist < 30.0 then if dist < 30.0 then
foundProps = foundProps + 1 foundProps = foundProps + 1
print("Found " .. propName .. " at distance: " .. dist) local entityId = NetworkGetNetworkIdFromEntity(obj)
print("Found " .. propName .. " at distance: " .. dist .. " | Entity ID: " .. entityId)
-- Add a temporary blip -- Add a temporary blip
local blip = AddBlipForEntity(obj) local blip = AddBlipForEntity(obj)
@ -684,7 +697,7 @@ RegisterCommand('checkvendingprops', function()
SetBlipColour(blip, 2) SetBlipColour(blip, 2)
SetBlipScale(blip, 0.8) SetBlipScale(blip, 0.8)
BeginTextCommandSetBlipName("STRING") BeginTextCommandSetBlipName("STRING")
AddTextComponentString(propName) AddTextComponentString(propName .. " | ID: " .. entityId)
EndTextCommandSetBlipName(blip) EndTextCommandSetBlipName(blip)
-- Remove blip after 10 seconds -- Remove blip after 10 seconds
@ -703,14 +716,39 @@ end, false)
RegisterCommand('vendingdebug', function() RegisterCommand('vendingdebug', function()
local playerPed = PlayerPedId() local playerPed = PlayerPedId()
local coords = GetEntityCoords(playerPed) local coords = GetEntityCoords(playerPed)
local entity = nil
local entityId = 0
QBCore.Functions.TriggerCallback('vending:server:getMachineByCoords', function(machine) -- Try to find the closest vending machine
if machine then local minDist = 3.0
print('Machine found:', json.encode(machine)) local objects = GetGamePool('CObject')
QBCore.Functions.Notify('Machine data logged to console', 'primary')
else for _, obj in ipairs(objects) do
print('No machine found at current location') local model = GetEntityModel(obj)
QBCore.Functions.Notify('No machine found here', 'error') for _, propName in ipairs(Config.VendingProps) do
if model == GetHashKey(propName) then
local objCoords = GetEntityCoords(obj)
local dist = #(coords - objCoords)
if dist < minDist then
minDist = dist
entity = obj
entityId = NetworkGetNetworkIdFromEntity(obj)
end
end
end end
end, coords) end
if entity then
QBCore.Functions.TriggerCallback('vending:server:getMachineByEntity', function(machine)
if machine then
print('Machine found:', json.encode(machine))
QBCore.Functions.Notify('Machine #' .. machine.id .. ' | Entity ID: ' .. entityId .. ' | Owner: ' .. machine.owner, 'primary')
else
print('No machine found with entity ID:', entityId)
QBCore.Functions.Notify('No machine found with entity ID: ' .. entityId, 'error')
end
end, entityId)
else
QBCore.Functions.Notify('No vending machine found nearby', 'error')
end
end, false) end, false)