1
0
Fork 0
forked from Simnation/Main
This commit is contained in:
Nordi98 2025-07-29 10:53:08 +02:00
parent e73fc734a3
commit 8110c00382
2 changed files with 497 additions and 582 deletions

View file

@ -24,46 +24,16 @@ CreateThread(function()
end
end)
-- Helper function to get machine ID by precise coordinates
function getMachineIdByCoords(preciseCoords)
-- First try to find an exact match
for id, machine in pairs(vendingMachines) do
if machine.coords.x == preciseCoords.x and
machine.coords.y == preciseCoords.y and
machine.coords.z == preciseCoords.z and
machine.coords.model == preciseCoords.model then
return id
end
end
-- If no exact match, try with a small tolerance
local closestId = nil
local closestDist = 0.1 -- Very small tolerance
for id, machine in pairs(vendingMachines) do
if machine.coords.model == preciseCoords.model then
local dist = #(vector3(preciseCoords.x, preciseCoords.y, preciseCoords.z) - vector3(machine.coords.x, machine.coords.y, machine.coords.z))
if dist < closestDist then
closestDist = dist
closestId = id
end
end
end
return closestId
end
-- Register vending machine (when player buys it)
RegisterNetEvent('vending:server:registerMachine', function(preciseCoords, prop)
RegisterNetEvent('vending:server:registerMachine', function(coords, prop)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if not Player then return end
-- Check if there's already a machine at these coords
for id, machine in pairs(vendingMachines) do
if machine.coords.x == preciseCoords.x and
machine.coords.y == preciseCoords.y and
machine.coords.z == preciseCoords.z then
local dist = #(vector3(coords.x, coords.y, coords.z) - vector3(machine.coords.x, machine.coords.y, machine.coords.z))
if dist < 2.0 then
TriggerClientEvent('QBCore:Notify', src, 'Hier ist bereits ein Automat registriert!', 'error')
return
end
@ -81,7 +51,7 @@ RegisterNetEvent('vending:server:registerMachine', function(preciseCoords, prop)
-- Create machine in database
local machineId = MySQL.insert.await('INSERT INTO vending_machines (owner, coords, prop, money, items, prices, managers) VALUES (?, ?, ?, ?, ?, ?, ?)', {
Player.PlayerData.citizenid,
json.encode(preciseCoords),
json.encode(coords),
prop,
0,
json.encode({}),
@ -93,7 +63,7 @@ RegisterNetEvent('vending:server:registerMachine', function(preciseCoords, prop)
vendingMachines[machineId] = {
id = machineId,
owner = Player.PlayerData.citizenid,
coords = preciseCoords,
coords = coords,
prop = prop,
money = 0,
items = {},
@ -102,19 +72,19 @@ RegisterNetEvent('vending:server:registerMachine', function(preciseCoords, prop)
stash = 'vending_' .. machineId
}
print("^2[VENDING]^7 New vending machine registered: #" .. machineId)
print("^2[VENDING]^7 New vending machine registered: " .. machineId)
TriggerClientEvent('QBCore:Notify', src, 'Verkaufsautomat erfolgreich gekauft für $' .. Config.VendingMachinePrice .. '!', 'success')
TriggerClientEvent('vending:client:refreshTargets', -1)
end)
-- Sell vending machine
RegisterNetEvent('vending:server:sellMachine', function(preciseCoords, machineId)
RegisterNetEvent('vending:server:sellMachine', function(coords, machineId)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if not Player then return end
if not machineId then
machineId = getMachineIdByCoords(preciseCoords)
machineId = getMachineIdByCoords(coords)
end
if not machineId then
@ -194,16 +164,13 @@ function canManageMachine(playerId, machineId)
end
-- Open management menu
RegisterNetEvent('vending:server:openManagement', function(preciseCoords)
RegisterNetEvent('vending:server:openManagement', function(coords)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if not Player then return end
local machineId = getMachineIdByCoords(preciseCoords)
if not machineId then
TriggerClientEvent('QBCore:Notify', src, 'Automat nicht gefunden!', 'error')
return
end
local machineId = getMachineIdByCoords(coords)
if not machineId then return end
local machine = vendingMachines[machineId]
@ -220,16 +187,13 @@ RegisterNetEvent('vending:server:openManagement', function(preciseCoords)
end)
-- Open stash
RegisterNetEvent('vending:server:openStash', function(preciseCoords)
RegisterNetEvent('vending:server:openStash', function(coords)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if not Player then return end
local machineId = getMachineIdByCoords(preciseCoords)
if not machineId then
TriggerClientEvent('QBCore:Notify', src, 'Automat nicht gefunden!', 'error')
return
end
local machineId = getMachineIdByCoords(coords)
if not machineId then return end
-- Check if player can manage
if not canManageMachine(src, machineId) then
@ -246,18 +210,14 @@ RegisterNetEvent('vending:server:openStash', function(preciseCoords)
label = 'Vending Machine #' .. machine.id
})
end)
-- Set item price
RegisterNetEvent('vending:server:setItemPrice', function(preciseCoords, itemName, price)
RegisterNetEvent('vending:server:setItemPrice', function(coords, itemName, price)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if not Player then return end
local machineId = getMachineIdByCoords(preciseCoords)
if not machineId then
TriggerClientEvent('QBCore:Notify', src, 'Automat nicht gefunden!', 'error')
return
end
local machineId = getMachineIdByCoords(coords)
if not machineId then return end
-- Check if player can manage
if not canManageMachine(src, machineId) then
@ -275,16 +235,13 @@ RegisterNetEvent('vending:server:setItemPrice', function(preciseCoords, itemName
end)
-- Withdraw money
RegisterNetEvent('vending:server:withdrawMoney', function(preciseCoords, amount)
RegisterNetEvent('vending:server:withdrawMoney', function(coords, amount)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if not Player then return end
local machineId = getMachineIdByCoords(preciseCoords)
if not machineId then
TriggerClientEvent('QBCore:Notify', src, 'Automat nicht gefunden!', 'error')
return
end
local machineId = getMachineIdByCoords(coords)
if not machineId then return end
-- Check if player can manage
if not canManageMachine(src, machineId) then
@ -309,16 +266,13 @@ RegisterNetEvent('vending:server:withdrawMoney', function(preciseCoords, amount)
end)
-- Buy item from vending machine with quantity selection
RegisterNetEvent('vending:server:buyItem', function(preciseCoords, itemName, amount)
RegisterNetEvent('vending:server:buyItem', function(coords, itemName, amount)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if not Player then return end
local machineId = getMachineIdByCoords(preciseCoords)
if not machineId then
TriggerClientEvent('QBCore:Notify', src, 'Automat nicht gefunden!', 'error')
return
end
local machineId = getMachineIdByCoords(coords)
if not machineId then return end
local machine = vendingMachines[machineId]
local price = machine.prices[itemName] or Config.DefaultPrice
@ -381,16 +335,13 @@ RegisterNetEvent('vending:server:buyItem', function(preciseCoords, itemName, amo
end)
-- Add manager to vending machine
RegisterNetEvent('vending:server:addManager', function(preciseCoords, targetId)
RegisterNetEvent('vending:server:addManager', function(coords, targetId)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if not Player then return end
local machineId = getMachineIdByCoords(preciseCoords)
if not machineId then
TriggerClientEvent('QBCore:Notify', src, 'Automat nicht gefunden!', 'error')
return
end
local machineId = getMachineIdByCoords(coords)
if not machineId then return end
local machine = vendingMachines[machineId]
@ -428,16 +379,13 @@ RegisterNetEvent('vending:server:addManager', function(preciseCoords, targetId)
end)
-- Remove manager from vending machine
RegisterNetEvent('vending:server:removeManager', function(preciseCoords, citizenid)
RegisterNetEvent('vending:server:removeManager', function(coords, citizenid)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if not Player then return end
local machineId = getMachineIdByCoords(preciseCoords)
if not machineId then
TriggerClientEvent('QBCore:Notify', src, 'Automat nicht gefunden!', 'error')
return
end
local machineId = getMachineIdByCoords(coords)
if not machineId then return end
local machine = vendingMachines[machineId]
@ -483,148 +431,9 @@ RegisterNetEvent('vending:server:removeManager', function(preciseCoords, citizen
end
end)
-- Start robbery
RegisterNetEvent('vending:server:startRobbery', function(preciseCoords)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if not Player then return end
local machineId = getMachineIdByCoords(preciseCoords)
if not machineId then
TriggerClientEvent('QBCore:Notify', src, 'Automat nicht gefunden!', 'error')
return
end
local machine = vendingMachines[machineId]
-- Check if player has required item
local hasItem = Player.Functions.GetItemByName(Config.RobberyItem)
if not hasItem or hasItem.amount < 1 then
TriggerClientEvent('QBCore:Notify', src, 'Du benötigst einen ' .. Config.RobberyItem, 'error')
return
end
-- Check if already being robbed
if robberyInProgress[machineId] then
TriggerClientEvent('QBCore:Notify', src, 'Dieser Automat wird bereits aufgebrochen!', 'error')
return
end
-- Check if machine has money
if machine.money < Config.MinRobberyAmount then
TriggerClientEvent('QBCore:Notify', src, 'Nicht genug Geld im Automaten!', 'error')
return
end
robberyInProgress[machineId] = true
-- Alert police
local streetHash = GetStreetNameAtCoord(machine.coords.x, machine.coords.y, machine.coords.z)
local streetName = GetStreetNameFromHashKey(streetHash)
local players = QBCore.Functions.GetQBPlayers()
for k, v in pairs(players) do
if v.PlayerData.job.name == 'police' and v.PlayerData.job.onduty then
TriggerClientEvent('vending:client:policeAlert', v.PlayerData.source, machine.coords, streetName)
end
end
-- Alert owner and managers
for _, playerId in ipairs(QBCore.Functions.GetPlayers()) do
local targetPlayer = QBCore.Functions.GetPlayer(playerId)
if targetPlayer then
if targetPlayer.PlayerData.citizenid == machine.owner then
TriggerClientEvent('QBCore:Notify', targetPlayer.PlayerData.source, 'Dein Verkaufsautomat wird gerade aufgebrochen! Standort: ' .. streetName, 'error', 10000)
elseif machine.managers then
for _, manager in pairs(machine.managers) do
if targetPlayer.PlayerData.citizenid == manager then
TriggerClientEvent('QBCore:Notify', targetPlayer.PlayerData.source, 'Ein Verkaufsautomat, den du verwaltest, wird gerade aufgebrochen! Standort: ' .. streetName, 'error', 10000)
break
end
end
end
end
end
TriggerClientEvent('vending:client:startRobbery', src, preciseCoords)
end)
-- Complete robbery
RegisterNetEvent('vending:server:completeRobbery', function(preciseCoords, success)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if not Player then return end
local machineId = getMachineIdByCoords(preciseCoords)
if not machineId then
TriggerClientEvent('QBCore:Notify', src, 'Automat nicht gefunden!', 'error')
return
end
local machine = vendingMachines[machineId]
robberyInProgress[machineId] = false
if success then
local stolenAmount = math.random(Config.MinRobberyAmount, math.min(machine.money, Config.MaxRobberyAmount))
-- Remove money from machine
machine.money = machine.money - stolenAmount
MySQL.update('UPDATE vending_machines SET money = ? WHERE id = ?', {machine.money, machineId})
-- Give money to player
Player.Functions.AddMoney('cash', stolenAmount)
TriggerClientEvent('QBCore:Notify', src, 'Du hast $' .. stolenAmount .. ' gestohlen!', 'success')
-- Remove robbery item with chance
if math.random(1, 100) <= Config.RobberyItemBreakChance then
Player.Functions.RemoveItem(Config.RobberyItem, 1)
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[Config.RobberyItem], 'remove')
TriggerClientEvent('QBCore:Notify', src, 'Dein ' .. Config.RobberyItem .. ' ist kaputt gegangen!', 'error')
end
else
TriggerClientEvent('QBCore:Notify', src, 'Aufbruch fehlgeschlagen!', 'error')
end
end)
-- Get machine data by coordinates
QBCore.Functions.CreateCallback('vending:server:getMachineByCoords', function(source, cb, preciseCoords)
local machineId = getMachineIdByCoords(preciseCoords)
if machineId then
cb(vendingMachines[machineId])
else
cb(nil)
end
end)
-- Get stash items for vending machine menu
QBCore.Functions.CreateCallback('vending:server:getStashItems', function(source, cb, preciseCoords)
local machineId = getMachineIdByCoords(preciseCoords)
if not machineId then
cb({})
return
end
local machine = vendingMachines[machineId]
-- Get stash items using correct export
local stashItems = exports["tgiann-inventory"]:GetSecondaryInventoryItems("stash", machine.stash)
local items = {}
if stashItems then
for slot, item in pairs(stashItems) do
if item.amount > 0 then
item.price = machine.prices[item.name] or Config.DefaultPrice
table.insert(items, item)
end
end
end
cb(items)
end)
-- Get managers list
QBCore.Functions.CreateCallback('vending:server:getManagers', function(source, cb, preciseCoords)
local machineId = getMachineIdByCoords(preciseCoords)
QBCore.Functions.CreateCallback('vending:server:getManagers', function(source, cb, coords)
local machineId = getMachineIdByCoords(coords)
if not machineId then
cb({})
return
@ -676,15 +485,177 @@ QBCore.Functions.CreateCallback('vending:server:getManagers', function(source, c
cb(managersList)
end)
-- Check if machine exists
QBCore.Functions.CreateCallback('vending:server:machineExists', function(source, cb, preciseCoords)
local machineId = getMachineIdByCoords(preciseCoords)
cb(machineId ~= nil)
-- Start robbery
RegisterNetEvent('vending:server:startRobbery', function(coords)
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]
-- Check if player has required item
local hasItem = Player.Functions.GetItemByName(Config.RobberyItem)
if not hasItem or hasItem.amount < 1 then
TriggerClientEvent('QBCore:Notify', src, 'Du benötigst einen ' .. Config.RobberyItem, 'error')
return
end
-- Check if already being robbed
if robberyInProgress[machineId] then
TriggerClientEvent('QBCore:Notify', src, 'Dieser Automat wird bereits aufgebrochen!', 'error')
return
end
-- Check if machine has money
if machine.money < Config.MinRobberyAmount then
TriggerClientEvent('QBCore:Notify', src, 'Nicht genug Geld im Automaten!', 'error')
return
end
robberyInProgress[machineId] = true
-- Alert police
local streetHash = GetStreetNameAtCoord(coords.x, coords.y, coords.z)
local streetName = GetStreetNameFromHashKey(streetHash)
local players = QBCore.Functions.GetQBPlayers()
for k, v in pairs(players) do
if v.PlayerData.job.name == 'police' and v.PlayerData.job.onduty then
TriggerClientEvent('vending:client:policeAlert', v.PlayerData.source, coords, streetName)
end
end
-- Alert owner and managers
for _, playerId in ipairs(QBCore.Functions.GetPlayers()) do
local targetPlayer = QBCore.Functions.GetPlayer(playerId)
if targetPlayer then
if targetPlayer.PlayerData.citizenid == machine.owner then
TriggerClientEvent('QBCore:Notify', targetPlayer.PlayerData.source, 'Dein Verkaufsautomat wird gerade aufgebrochen! Standort: ' .. streetName, 'error', 10000)
elseif machine.managers then
for _, manager in pairs(machine.managers) do
if targetPlayer.PlayerData.citizenid == manager then
TriggerClientEvent('QBCore:Notify', targetPlayer.PlayerData.source, 'Ein Verkaufsautomat, den du verwaltest, wird gerade aufgebrochen! Standort: ' .. streetName, 'error', 10000)
break
end
end
end
end
end
TriggerClientEvent('vending:client:startRobbery', src, coords)
end)
-- Complete robbery
RegisterNetEvent('vending:server:completeRobbery', function(coords, success)
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]
robberyInProgress[machineId] = false
if success then
local stolenAmount = math.random(Config.MinRobberyAmount, math.min(machine.money, Config.MaxRobberyAmount))
-- Remove money from machine
machine.money = machine.money - stolenAmount
MySQL.update('UPDATE vending_machines SET money = ? WHERE id = ?', {machine.money, machineId})
-- Give money to player
Player.Functions.AddMoney('cash', stolenAmount)
TriggerClientEvent('QBCore:Notify', src, 'Du hast $' .. stolenAmount .. ' gestohlen!', 'success')
-- Remove robbery item with chance
if math.random(1, 100) <= Config.RobberyItemBreakChance then
Player.Functions.RemoveItem(Config.RobberyItem, 1)
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[Config.RobberyItem], 'remove')
TriggerClientEvent('QBCore:Notify', src, 'Dein ' .. Config.RobberyItem .. ' ist kaputt gegangen!', 'error')
end
else
TriggerClientEvent('QBCore:Notify', src, 'Aufbruch fehlgeschlagen!', 'error')
end
end)
-- Helper function to get machine ID by coordinates
function getMachineIdByCoords(coords)
for id, machine in pairs(vendingMachines) do
local dist = #(vector3(coords.x, coords.y, coords.z) - vector3(machine.coords.x, machine.coords.y, machine.coords.z))
if dist < 2.0 then
return id
end
end
return nil
end
-- Get machine data by coordinates
QBCore.Functions.CreateCallback('vending:server:getMachineByCoords', function(source, cb, coords)
local machineId = getMachineIdByCoords(coords)
if machineId then
cb(vendingMachines[machineId])
else
cb(nil)
end
end)
-- Get stash items for vending machine menu
QBCore.Functions.CreateCallback('vending:server:getStashItems', function(source, cb, coords)
local machineId = getMachineIdByCoords(coords)
if not machineId then
cb({})
return
end
local machine = vendingMachines[machineId]
-- Get stash items using correct export
local stashItems = exports["tgiann-inventory"]:GetSecondaryInventoryItems("stash", machine.stash)
local items = {}
if stashItems then
for slot, item in pairs(stashItems) do
if item.amount > 0 then
item.price = machine.prices[item.name] or Config.DefaultPrice
table.insert(items, item)
end
end
end
cb(items)
end)
-- Check if player owns machine
QBCore.Functions.CreateCallback('vending:server:isOwner', function(source, cb, coords)
local Player = QBCore.Functions.GetPlayer(source)
if not Player then
cb(false)
return
end
local machineId = getMachineIdByCoords(coords)
if not machineId then
cb(false)
return
end
local machine = vendingMachines[machineId]
cb(machine.owner == Player.PlayerData.citizenid)
end)
-- Check if player can manage machine
QBCore.Functions.CreateCallback('vending:server:canManage', function(source, cb, preciseCoords)
local machineId = getMachineIdByCoords(preciseCoords)
QBCore.Functions.CreateCallback('vending:server:canManage', function(source, cb, coords)
local Player = QBCore.Functions.GetPlayer(source)
if not Player then
cb(false)
return
end
local machineId = getMachineIdByCoords(coords)
if not machineId then
cb(false)
return
@ -693,6 +664,12 @@ QBCore.Functions.CreateCallback('vending:server:canManage', function(source, cb,
cb(canManageMachine(source, machineId))
end)
-- Check if machine exists at coords
QBCore.Functions.CreateCallback('vending:server:machineExists', function(source, cb, coords)
local machineId = getMachineIdByCoords(coords)
cb(machineId ~= nil)
end)
-- Get online players for manager selection
QBCore.Functions.CreateCallback('vending:server:getOnlinePlayers', function(source, cb)
local src = source
@ -735,3 +712,4 @@ QBCore.Commands.Add('vendingdebug', 'Debug vending machines (Admin Only)', {}, f
end
end, 'admin')