From fdc0882b519b50077b25de65d611e7ee35ab64ce Mon Sep 17 00:00:00 2001 From: Nordi98 Date: Tue, 29 Jul 2025 23:02:29 +0200 Subject: [PATCH] Update client.lua --- .../[inventory]/nordi_vending/client.lua | 199 ++++++++++-------- 1 file changed, 113 insertions(+), 86 deletions(-) diff --git a/resources/[inventory]/nordi_vending/client.lua b/resources/[inventory]/nordi_vending/client.lua index 8bcdb8007..a02786216 100644 --- a/resources/[inventory]/nordi_vending/client.lua +++ b/resources/[inventory]/nordi_vending/client.lua @@ -1,14 +1,21 @@ local QBCore = exports['qb-core']:GetCoreObject() local nearbyMachine = nil -local currentMachineData = nil +local machineData = {} local isInteracting = false -- Function to draw 3D text in the world function DrawText3D(x, y, z, text) + -- Calculate distance to reduce computation when far away + local playerCoords = GetEntityCoords(PlayerPedId()) + local dist = #(vector3(x, y, z) - playerCoords) + if dist > 5.0 then return end + + -- Set text properties SetTextScale(0.35, 0.35) SetTextFont(4) SetTextProportional(1) SetTextColour(255, 255, 255, 215) + SetTextOutline() SetTextEntry("STRING") SetTextCentre(1) AddTextComponentString(text) @@ -19,43 +26,66 @@ function DrawText3D(x, y, z, text) ClearDrawOrigin() end --- Function to check if machine is registered -function isRegisteredMachine(entity) - local coords = GetEntityCoords(entity) - local isRegistered = false +-- Cache machine data to avoid constant callbacks +function GetMachineData(entity) + local entityId = NetworkGetNetworkIdFromEntity(entity) - QBCore.Functions.TriggerCallback('vending:server:machineExists', function(exists) - isRegistered = exists - end, coords) - - -- Wait for callback - local timeout = 0 - while isRegistered == false and timeout < 100 do - Wait(10) - timeout = timeout + 1 + if not machineData[entityId] then + local coords = GetEntityCoords(entity) + local isRegistered = false + local canManage = false + + -- 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 + machineData[entityId] = { + isRegistered = false, + canManage = false, + lastCheck = GetGameTimer() + } + end + end, coords) + + -- Wait for callbacks to complete + local timeout = 0 + while not machineData[entityId] and timeout < 50 do + Wait(10) + timeout = timeout + 1 + end end - return isRegistered + -- Return cached data or default + return machineData[entityId] or {isRegistered = false, canManage = false} end --- Check if player can manage machine -function canManageMachine(entity) - local coords = GetEntityCoords(entity) - local canManage = false - - QBCore.Functions.TriggerCallback('vending:server:canManage', function(result) - canManage = result - end, coords) - - -- Wait for callback - local timeout = 0 - while canManage == false and timeout < 100 do - Wait(10) - timeout = timeout + 1 +-- Clear cache periodically +CreateThread(function() + while true do + Wait(60000) -- Clear cache every minute + local currentTime = GetGameTimer() + + for entityId, data in pairs(machineData) do + if currentTime - data.lastCheck > 60000 then + machineData[entityId] = nil + end + end end - - return canManage -end +end) -- Main thread to detect nearby vending machines CreateThread(function() @@ -80,55 +110,53 @@ CreateThread(function() foundMachine = true nearbyMachine = obj - -- Only check status if not already interacting - if not isInteracting then - local z = objCoords.z + 1.0 - local registered = isRegisteredMachine(obj) - - if registered then - local canManage = canManageMachine(obj) + -- Get machine data from cache + local data = GetMachineData(obj) + + -- Display appropriate text based on machine status + local z = objCoords.z + 1.0 + + if data.isRegistered then + if data.canManage then + DrawText3D(objCoords.x, objCoords.y, z, "[E] Kaufen | [G] Verwalten") - if canManage then - DrawText3D(objCoords.x, objCoords.y, z, "[E] Kaufen | [G] Verwalten") - - -- Handle key presses for management - if IsControlJustPressed(0, 38) then -- E key - isInteracting = true - TriggerEvent('vending:client:openBuyMenu', {entity = obj}) - Wait(500) -- Prevent multiple triggers - isInteracting = false - elseif IsControlJustPressed(0, 47) then -- G key - isInteracting = true - TriggerEvent('vending:client:openOwnerMenu', {entity = obj}) - Wait(500) -- Prevent multiple triggers - isInteracting = false - end - else - DrawText3D(objCoords.x, objCoords.y, z, "[E] Kaufen | [G] Aufbrechen") - - -- Handle key presses for buying/robbery - if IsControlJustPressed(0, 38) then -- E key - isInteracting = true - TriggerEvent('vending:client:openBuyMenu', {entity = obj}) - Wait(500) -- Prevent multiple triggers - isInteracting = false - elseif IsControlJustPressed(0, 47) then -- G key - isInteracting = true - TriggerEvent('vending:client:startRobbery', {entity = obj}) - Wait(500) -- Prevent multiple triggers - isInteracting = false - end - end - else - DrawText3D(objCoords.x, objCoords.y, z, "[E] Automaten kaufen ($" .. Config.VendingMachinePrice .. ")") - - -- Handle key press for buying machine - if IsControlJustPressed(0, 38) then -- E key + -- Handle key presses for management + if IsControlJustPressed(0, 38) and not isInteracting then -- E key isInteracting = true - TriggerEvent('vending:client:buyMachine', {entity = obj}) + TriggerEvent('vending:client:openBuyMenu', {entity = obj}) + Wait(500) -- Prevent multiple triggers + isInteracting = false + elseif IsControlJustPressed(0, 47) and not isInteracting then -- G key + isInteracting = true + TriggerEvent('vending:client:openOwnerMenu', {entity = obj}) Wait(500) -- Prevent multiple triggers isInteracting = false end + else + DrawText3D(objCoords.x, objCoords.y, z, "[E] Kaufen | [G] Aufbrechen") + + -- Handle key presses for buying/robbery + if IsControlJustPressed(0, 38) and not isInteracting then -- E key + isInteracting = true + TriggerEvent('vending:client:openBuyMenu', {entity = obj}) + Wait(500) -- Prevent multiple triggers + isInteracting = false + elseif IsControlJustPressed(0, 47) and not isInteracting then -- G key + isInteracting = true + TriggerEvent('vending:client:startRobbery', {entity = obj}) + Wait(500) -- Prevent multiple triggers + isInteracting = false + end + end + else + DrawText3D(objCoords.x, objCoords.y, z, "[E] Automaten kaufen ($" .. Config.VendingMachinePrice .. ")") + + -- Handle key press for buying machine + if IsControlJustPressed(0, 38) and not isInteracting then -- E key + isInteracting = true + TriggerEvent('vending:client:buyMachine', {entity = obj}) + Wait(500) -- Prevent multiple triggers + isInteracting = false end end break @@ -139,14 +167,19 @@ CreateThread(function() if foundMachine then break end end - if not foundMachine then - nearbyMachine = nil - end - Wait(wait) end end) +-- Event to refresh machine data when a new machine is registered +RegisterNetEvent('vending:client:refreshTargets', function() + -- Clear cached data + 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 @@ -727,9 +760,3 @@ RegisterCommand('vendingdebug', function() end end, coords) end, false) - --- Event to refresh machine data when a new machine is registered -RegisterNetEvent('vending:client:refreshTargets', function() - -- Clear cached data - currentMachineData = nil -end)