forked from Simnation/Main
ed
This commit is contained in:
parent
a81f1d58f8
commit
90d92129ee
2 changed files with 94 additions and 43 deletions
|
@ -1,4 +1,6 @@
|
||||||
local QBCore = exports['qb-core']:GetCoreObject()
|
local QBCore = exports['qb-core']:GetCoreObject()
|
||||||
|
local machineCache = {} -- Cache für registrierte Maschinen
|
||||||
|
local ownerCache = {} -- Cache für Besitzer
|
||||||
|
|
||||||
-- Add targets to all vending machine props
|
-- Add targets to all vending machine props
|
||||||
CreateThread(function()
|
CreateThread(function()
|
||||||
|
@ -6,6 +8,52 @@ CreateThread(function()
|
||||||
refreshTargets()
|
refreshTargets()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
-- Update cache periodically
|
||||||
|
CreateThread(function()
|
||||||
|
while true do
|
||||||
|
updateCache()
|
||||||
|
Wait(5000) -- Update every 5 seconds
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
-- Update machine and owner cache
|
||||||
|
function updateCache()
|
||||||
|
local playerPed = PlayerPedId()
|
||||||
|
local playerCoords = GetEntityCoords(playerPed)
|
||||||
|
|
||||||
|
-- Clear old cache
|
||||||
|
machineCache = {}
|
||||||
|
ownerCache = {}
|
||||||
|
|
||||||
|
-- Check all vending machine props in range
|
||||||
|
for i = 1, #Config.VendingProps do
|
||||||
|
local hash = GetHashKey(Config.VendingProps[i])
|
||||||
|
local objects = GetGamePool('CObject')
|
||||||
|
|
||||||
|
for j = 1, #objects do
|
||||||
|
local obj = objects[j]
|
||||||
|
if GetEntityModel(obj) == hash then
|
||||||
|
local objCoords = GetEntityCoords(obj)
|
||||||
|
local distance = #(playerCoords - objCoords)
|
||||||
|
|
||||||
|
if distance < 50.0 then -- Only check nearby objects
|
||||||
|
-- Check if machine exists
|
||||||
|
QBCore.Functions.TriggerCallback('vending:server:machineExists', function(exists)
|
||||||
|
machineCache[obj] = exists
|
||||||
|
|
||||||
|
if exists then
|
||||||
|
-- Check if player is owner
|
||||||
|
QBCore.Functions.TriggerCallback('vending:server:isOwner', function(isOwner)
|
||||||
|
ownerCache[obj] = isOwner
|
||||||
|
end, objCoords)
|
||||||
|
end
|
||||||
|
end, objCoords)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- Refresh all targets
|
-- Refresh all targets
|
||||||
function refreshTargets()
|
function refreshTargets()
|
||||||
-- Remove old targets first
|
-- Remove old targets first
|
||||||
|
@ -22,7 +70,7 @@ function refreshTargets()
|
||||||
icon = "fas fa-dollar-sign",
|
icon = "fas fa-dollar-sign",
|
||||||
label = "Automaten kaufen ($" .. Config.VendingMachinePrice .. ")",
|
label = "Automaten kaufen ($" .. Config.VendingMachinePrice .. ")",
|
||||||
canInteract = function(entity)
|
canInteract = function(entity)
|
||||||
return not isMachineRegistered(entity)
|
return not (machineCache[entity] == true)
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -31,7 +79,7 @@ function refreshTargets()
|
||||||
icon = "fas fa-shopping-cart",
|
icon = "fas fa-shopping-cart",
|
||||||
label = "Kaufen",
|
label = "Kaufen",
|
||||||
canInteract = function(entity)
|
canInteract = function(entity)
|
||||||
return isMachineRegistered(entity)
|
return machineCache[entity] == true
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -40,7 +88,7 @@ function refreshTargets()
|
||||||
icon = "fas fa-cog",
|
icon = "fas fa-cog",
|
||||||
label = "Verwalten",
|
label = "Verwalten",
|
||||||
canInteract = function(entity)
|
canInteract = function(entity)
|
||||||
return isMachineRegistered(entity) and isOwner(entity)
|
return machineCache[entity] == true and ownerCache[entity] == true
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -49,7 +97,7 @@ function refreshTargets()
|
||||||
icon = "fas fa-mask",
|
icon = "fas fa-mask",
|
||||||
label = "Aufbrechen",
|
label = "Aufbrechen",
|
||||||
canInteract = function(entity)
|
canInteract = function(entity)
|
||||||
return isMachineRegistered(entity) and hasRobberyItem() and not isOwner(entity)
|
return machineCache[entity] == true and ownerCache[entity] ~= true and hasRobberyItem()
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -57,43 +105,11 @@ function refreshTargets()
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Check if machine is registered
|
|
||||||
function isMachineRegistered(entity)
|
|
||||||
local coords = GetEntityCoords(entity)
|
|
||||||
local registered = false
|
|
||||||
|
|
||||||
QBCore.Functions.TriggerCallback('vending:server:machineExists', function(exists)
|
|
||||||
registered = exists
|
|
||||||
end, coords)
|
|
||||||
|
|
||||||
-- Wait for callback (not ideal but necessary for sync check)
|
|
||||||
while registered == false do
|
|
||||||
Wait(10)
|
|
||||||
end
|
|
||||||
|
|
||||||
return registered
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Check if player is owner
|
|
||||||
function isOwner(entity)
|
|
||||||
local coords = GetEntityCoords(entity)
|
|
||||||
local owner = false
|
|
||||||
|
|
||||||
QBCore.Functions.TriggerCallback('vending:server:isOwner', function(isOwner)
|
|
||||||
owner = isOwner
|
|
||||||
end, coords)
|
|
||||||
|
|
||||||
-- Wait for callback
|
|
||||||
while owner == false do
|
|
||||||
Wait(10)
|
|
||||||
end
|
|
||||||
|
|
||||||
return owner
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Check if player has robbery item
|
-- Check if player has robbery item
|
||||||
function hasRobberyItem()
|
function hasRobberyItem()
|
||||||
local PlayerData = QBCore.Functions.GetPlayerData()
|
local PlayerData = QBCore.Functions.GetPlayerData()
|
||||||
|
if not PlayerData or not PlayerData.items then return false end
|
||||||
|
|
||||||
for k, v in pairs(PlayerData.items) do
|
for k, v in pairs(PlayerData.items) do
|
||||||
if v.name == Config.RobberyItem and v.amount > 0 then
|
if v.name == Config.RobberyItem and v.amount > 0 then
|
||||||
return true
|
return true
|
||||||
|
@ -158,8 +174,9 @@ RegisterNetEvent('vending:client:openBuyMenu', function(data)
|
||||||
for i = 1, #items do
|
for i = 1, #items do
|
||||||
local item = items[i]
|
local item = items[i]
|
||||||
if item.amount > 0 then
|
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, {
|
table.insert(options, {
|
||||||
title = QBCore.Shared.Items[item.name].label,
|
title = itemLabel,
|
||||||
description = 'Preis: $' .. item.price .. ' | Verfügbar: ' .. item.amount,
|
description = 'Preis: $' .. item.price .. ' | Verfügbar: ' .. item.amount,
|
||||||
icon = 'nui://qb-inventory/html/images/' .. item.name .. '.png',
|
icon = 'nui://qb-inventory/html/images/' .. item.name .. '.png',
|
||||||
onSelect = function()
|
onSelect = function()
|
||||||
|
@ -169,6 +186,11 @@ RegisterNetEvent('vending:client:openBuyMenu', function(data)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if #options == 0 then
|
||||||
|
QBCore.Functions.Notify('Keine verfügbaren Items!', 'error')
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
lib.registerContext({
|
lib.registerContext({
|
||||||
id = 'vending_buy_menu',
|
id = 'vending_buy_menu',
|
||||||
title = 'Verkaufsautomat',
|
title = 'Verkaufsautomat',
|
||||||
|
@ -238,8 +260,9 @@ function openPriceMenu(coords)
|
||||||
|
|
||||||
for i = 1, #items do
|
for i = 1, #items do
|
||||||
local item = items[i]
|
local item = items[i]
|
||||||
|
local itemLabel = QBCore.Shared.Items[item.name] and QBCore.Shared.Items[item.name].label or item.name
|
||||||
table.insert(options, {
|
table.insert(options, {
|
||||||
title = QBCore.Shared.Items[item.name].label,
|
title = itemLabel,
|
||||||
description = 'Aktueller Preis: $' .. item.price,
|
description = 'Aktueller Preis: $' .. item.price,
|
||||||
icon = 'nui://qb-inventory/html/images/' .. item.name .. '.png',
|
icon = 'nui://qb-inventory/html/images/' .. item.name .. '.png',
|
||||||
onSelect = function()
|
onSelect = function()
|
||||||
|
@ -260,10 +283,12 @@ end
|
||||||
|
|
||||||
-- Set price for specific item
|
-- Set price for specific item
|
||||||
function setPriceForItem(coords, itemName, currentPrice)
|
function setPriceForItem(coords, itemName, currentPrice)
|
||||||
|
local itemLabel = QBCore.Shared.Items[itemName] and QBCore.Shared.Items[itemName].label or itemName
|
||||||
|
|
||||||
local input = lib.inputDialog('Preis festlegen', {
|
local input = lib.inputDialog('Preis festlegen', {
|
||||||
{
|
{
|
||||||
type = 'number',
|
type = 'number',
|
||||||
label = 'Neuer Preis für ' .. (QBCore.Shared.Items[itemName] and QBCore.Shared.Items[itemName].label or itemName),
|
label = 'Neuer Preis für ' .. itemLabel,
|
||||||
description = 'Aktueller Preis: $' .. currentPrice,
|
description = 'Aktueller Preis: $' .. currentPrice,
|
||||||
required = true,
|
required = true,
|
||||||
min = 1,
|
min = 1,
|
||||||
|
@ -280,7 +305,15 @@ end
|
||||||
-- Open withdraw menu
|
-- Open withdraw menu
|
||||||
function openWithdrawMenu(coords)
|
function openWithdrawMenu(coords)
|
||||||
QBCore.Functions.TriggerCallback('vending:server:getMachineByCoords', function(machine)
|
QBCore.Functions.TriggerCallback('vending:server:getMachineByCoords', function(machine)
|
||||||
if not machine then return end
|
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', {
|
local input = lib.inputDialog('Geld abheben', {
|
||||||
{
|
{
|
||||||
|
@ -408,5 +441,23 @@ end)
|
||||||
|
|
||||||
-- Refresh targets when called from server
|
-- Refresh targets when called from server
|
||||||
RegisterNetEvent('vending:client:refreshTargets', function()
|
RegisterNetEvent('vending:client:refreshTargets', function()
|
||||||
|
updateCache()
|
||||||
|
Wait(1000)
|
||||||
refreshTargets()
|
refreshTargets()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
-- Update cache when player data changes
|
||||||
|
RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function()
|
||||||
|
Wait(2000)
|
||||||
|
updateCache()
|
||||||
|
refreshTargets()
|
||||||
|
end)
|
||||||
|
|
||||||
|
-- Debug command to check targets
|
||||||
|
RegisterCommand('checktargets', function()
|
||||||
|
print('Refreshing vending machine targets...')
|
||||||
|
updateCache()
|
||||||
|
Wait(1000)
|
||||||
|
refreshTargets()
|
||||||
|
print('Targets refreshed!')
|
||||||
|
end, false)
|
||||||
|
|
|
@ -2,7 +2,7 @@ Config = {}
|
||||||
|
|
||||||
-- Vending Machine Settings
|
-- Vending Machine Settings
|
||||||
Config.VendingProps = {
|
Config.VendingProps = {
|
||||||
'prop_boxpile_03a',
|
'bzzz_vending_candy_a',
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue