1
0
Fork 0
forked from Simnation/Main
This commit is contained in:
Nordi98 2025-07-29 10:23:04 +02:00
parent 3ddb7cd650
commit 935af62c44
2 changed files with 71 additions and 814 deletions

View file

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