1
0
Fork 0
forked from Simnation/Main

Update client.lua

This commit is contained in:
Nordi98 2025-07-29 23:10:02 +02:00
parent fdc0882b51
commit 1973b32054

View file

@ -26,51 +26,55 @@ function DrawText3D(x, y, z, text)
ClearDrawOrigin()
end
-- Cache machine data to avoid constant callbacks
-- Get machine data with proper callback handling
function GetMachineData(entity)
local entityId = NetworkGetNetworkIdFromEntity(entity)
if not machineData[entityId] then
local coords = GetEntityCoords(entity)
local isRegistered = false
local canManage = false
local entityId = tostring(coords.x) .. tostring(coords.y) .. tostring(coords.z)
-- Check if machine is registered
QBCore.Functions.TriggerCallback('vending:server:machineExists', function(exists)
isRegistered = exists
-- Only check management if registered
if isRegistered then
QBCore.Functions.TriggerCallback('vending:server:canManage', function(result)
canManage = result
-- Store data in cache
machineData[entityId] = {
isRegistered = isRegistered,
canManage = canManage,
lastCheck = GetGameTimer()
}
end, coords)
else
-- Store data in cache
-- Check if we need to refresh the data
local currentTime = GetGameTimer()
if not machineData[entityId] or (currentTime - machineData[entityId].lastCheck > 10000) then
-- Initialize with default values
machineData[entityId] = {
isRegistered = false,
canManage = false,
lastCheck = GetGameTimer()
lastCheck = currentTime,
checking = true
}
-- Check if machine is registered
QBCore.Functions.TriggerCallback('vending:server:machineExists', function(exists)
if exists then
machineData[entityId].isRegistered = true
-- Only check management if registered
QBCore.Functions.TriggerCallback('vending:server:canManage', function(result)
machineData[entityId].canManage = result
machineData[entityId].checking = false
end, coords)
else
machineData[entityId].isRegistered = false
machineData[entityId].canManage = false
machineData[entityId].checking = false
end
end, coords)
end
-- Wait for callbacks to complete
-- Wait for callbacks to complete if currently checking
if machineData[entityId].checking then
local timeout = 0
while not machineData[entityId] and timeout < 50 do
while machineData[entityId].checking and timeout < 50 do
Wait(10)
timeout = timeout + 1
end
-- If timeout reached, set checking to false to avoid deadlock
if timeout >= 50 then
machineData[entityId].checking = false
end
end
-- Return cached data or default
return machineData[entityId] or {isRegistered = false, canManage = false}
return machineData[entityId]
end
-- Clear cache periodically
@ -110,7 +114,7 @@ CreateThread(function()
foundMachine = true
nearbyMachine = obj
-- Get machine data from cache
-- Get machine data
local data = GetMachineData(obj)
-- Display appropriate text based on machine status
@ -177,9 +181,6 @@ RegisterNetEvent('vending:client:refreshTargets', function()
machineData = {}
end)
-- Keep all the existing event handlers and functions below this point
-- They don't need to be modified since they work with ox_lib
-- Buy vending machine
RegisterNetEvent('vending:client:buyMachine', function(data)
local entity = data.entity
@ -207,6 +208,9 @@ RegisterNetEvent('vending:client:buyMachine', function(data)
icon = 'fas fa-check',
onSelect = function()
TriggerServerEvent('vending:server:registerMachine', coords, prop)
-- Clear cache for this machine
local entityId = tostring(coords.x) .. tostring(coords.y) .. tostring(coords.z)
machineData[entityId] = nil
end
},
{
@ -225,6 +229,13 @@ RegisterNetEvent('vending:client:openBuyMenu', function(data)
local entity = data.entity
local coords = GetEntityCoords(entity)
-- Double-check if machine is registered before proceeding
QBCore.Functions.TriggerCallback('vending:server:machineExists', function(exists)
if not exists then
QBCore.Functions.Notify('Dieser Automat ist nicht registriert!', 'error')
return
end
QBCore.Functions.TriggerCallback('vending:server:getStashItems', function(items)
if #items == 0 then
QBCore.Functions.Notify('Dieser Automat ist leer!', 'error')
@ -261,6 +272,7 @@ RegisterNetEvent('vending:client:openBuyMenu', function(data)
lib.showContext('vending_buy_menu')
end, coords)
end, coords)
end)
-- Open quantity dialog for buying items
@ -292,6 +304,13 @@ RegisterNetEvent('vending:client:openOwnerMenu', function(data)
local entity = data.entity
local coords = GetEntityCoords(entity)
-- Double-check if player can manage this machine before proceeding
QBCore.Functions.TriggerCallback('vending:server:canManage', function(canManage)
if not canManage then
QBCore.Functions.Notify('Du hast keine Berechtigung diesen Automaten zu verwalten!', 'error')
return
end
QBCore.Functions.TriggerCallback('vending:server:getMachineByCoords', function(machine)
if not machine then
QBCore.Functions.Notify('Automat nicht gefunden!', 'error')
@ -363,6 +382,7 @@ RegisterNetEvent('vending:client:openOwnerMenu', function(data)
lib.showContext('vending_owner_menu')
end, coords)
end, coords)
end)
-- Function to sell the vending machine
@ -378,11 +398,21 @@ function sellVendingMachine(coords, machineId)
if input and input[1] then
TriggerServerEvent('vending:server:sellMachine', coords, machineId)
-- Clear cache for this machine
local entityId = tostring(coords.x) .. tostring(coords.y) .. tostring(coords.z)
machineData[entityId] = nil
end
end
-- Open price menu
function openPriceMenu(coords)
-- Double-check if player can manage this machine before proceeding
QBCore.Functions.TriggerCallback('vending:server:canManage', function(canManage)
if not canManage then
QBCore.Functions.Notify('Du hast keine Berechtigung diesen Automaten zu verwalten!', 'error')
return
end
QBCore.Functions.TriggerCallback('vending:server:getStashItems', function(items)
if #items == 0 then
QBCore.Functions.Notify('Keine Items im Automaten!', 'error')
@ -413,6 +443,7 @@ function openPriceMenu(coords)
lib.showContext('vending_price_menu')
end, coords)
end, coords)
end
-- Set price for specific item
@ -435,6 +466,13 @@ end
-- Open withdraw menu
function openWithdrawMenu(coords, availableMoney)
-- Double-check if player can manage this machine before proceeding
QBCore.Functions.TriggerCallback('vending:server:canManage', function(canManage)
if not canManage then
QBCore.Functions.Notify('Du hast keine Berechtigung diesen Automaten zu verwalten!', 'error')
return
end
if availableMoney <= 0 then
QBCore.Functions.Notify('Kein Geld im Automaten!', 'error')
return
@ -454,6 +492,7 @@ function openWithdrawMenu(coords, availableMoney)
if input and input[1] then
TriggerServerEvent('vending:server:withdrawMoney', coords, tonumber(input[1]))
end
end, coords)
end
-- Open stats menu
@ -486,6 +525,13 @@ end
-- Open managers menu
function openManagersMenu(coords)
-- Double-check if player is owner of this machine before proceeding
QBCore.Functions.TriggerCallback('vending:server:isOwner', function(isOwner)
if not isOwner then
QBCore.Functions.Notify('Nur der Besitzer kann Verwalter verwalten!', 'error')
return
end
-- Get current managers
QBCore.Functions.TriggerCallback('vending:server:getManagers', function(managers)
local options = {
@ -547,10 +593,18 @@ function openManagersMenu(coords)
lib.showContext('managers_menu')
end, coords)
end, coords)
end
-- Open add manager menu
function openAddManagerMenu(coords)
-- Double-check if player is owner of this machine before proceeding
QBCore.Functions.TriggerCallback('vending:server:isOwner', function(isOwner)
if not isOwner then
QBCore.Functions.Notify('Nur der Besitzer kann Verwalter hinzufügen!', 'error')
return
end
QBCore.Functions.TriggerCallback('vending:server:getOnlinePlayers', function(players)
if #players == 0 then
QBCore.Functions.Notify('Keine Spieler online!', 'error')
@ -582,6 +636,7 @@ function openAddManagerMenu(coords)
lib.showContext('add_manager_menu')
end)
end, coords)
end
-- Robbery menu
@ -589,6 +644,19 @@ RegisterNetEvent('vending:client:startRobbery', function(data)
local entity = data.entity
local coords = GetEntityCoords(entity)
-- Double-check if machine is registered and player cannot manage it
QBCore.Functions.TriggerCallback('vending:server:machineExists', function(exists)
if not exists then
QBCore.Functions.Notify('Dieser Automat ist nicht registriert!', 'error')
return
end
QBCore.Functions.TriggerCallback('vending:server:canManage', function(canManage)
if canManage then
QBCore.Functions.Notify('Du kannst deinen eigenen Automaten nicht aufbrechen!', 'error')
return
end
lib.registerContext({
id = 'vending_robbery_confirm',
title = 'Verkaufsautomat aufbrechen',
@ -610,6 +678,8 @@ RegisterNetEvent('vending:client:startRobbery', function(data)
})
lib.showContext('vending_robbery_confirm')
end, coords)
end, coords)
end)
-- Start robbery animation and progress
@ -677,6 +747,13 @@ end)
-- Management menu (alternative opening method)
RegisterNetEvent('vending:client:openManagement', function(machine)
-- Double-check if player can manage this machine
QBCore.Functions.TriggerCallback('vending:server:canManage', function(canManage)
if not canManage then
QBCore.Functions.Notify('Du hast keine Berechtigung diesen Automaten zu verwalten!', 'error')
return
end
lib.registerContext({
id = 'vending_management',
title = 'Verkaufsautomat #' .. machine.id,
@ -701,6 +778,7 @@ RegisterNetEvent('vending:client:openManagement', function(machine)
})
lib.showContext('vending_management')
end, machine.coords)
end)
-- Debug command to check props
@ -760,3 +838,37 @@ RegisterCommand('vendingdebug', function()
end
end, coords)
end, false)
-- Clear cache command for debugging
RegisterCommand('clearvendingcache', function()
machineData = {}
QBCore.Functions.Notify('Vending machine cache cleared', 'success')
end, false)
-- Event handler for when player loads
RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function()
-- Clear cache when player loads
machineData = {}
end)
-- Event handler for when player unloads
RegisterNetEvent('QBCore:Client:OnPlayerUnload', function()
-- Clear cache when player unloads
machineData = {}
end)
-- Event handler for resource start
AddEventHandler('onResourceStart', function(resourceName)
if resourceName == GetCurrentResourceName() then
-- Clear cache when resource starts
machineData = {}
end
end)
-- Event handler for resource stop
AddEventHandler('onResourceStop', function(resourceName)
if resourceName == GetCurrentResourceName() then
-- Nothing to do here, but good to have for completeness
end
end)