From d9b50f59c23429bd9f167bc5f80bebab80b8d619 Mon Sep 17 00:00:00 2001 From: Nordi98 Date: Tue, 29 Jul 2025 22:35:35 +0200 Subject: [PATCH] Update client.lua --- .../[inventory]/nordi_vending/client.lua | 191 +++++++++--------- 1 file changed, 96 insertions(+), 95 deletions(-) diff --git a/resources/[inventory]/nordi_vending/client.lua b/resources/[inventory]/nordi_vending/client.lua index a6c7167a3..50dde2966 100644 --- a/resources/[inventory]/nordi_vending/client.lua +++ b/resources/[inventory]/nordi_vending/client.lua @@ -1,97 +1,28 @@ local QBCore = exports['qb-core']:GetCoreObject() +local nearbyMachine = nil +local displayingText = false --- Function to initialize targets -function InitializeTargets() - -- Remove existing targets first to avoid duplicates - exports['qb-target']:RemoveTargetModel(Config.VendingProps) - Wait(100) +-- Function to draw 3D text in the world +function DrawText3D(x, y, z, text) + local onScreen, _x, _y = World3dToScreen2d(x, y, z) + local px, py, pz = table.unpack(GetGameplayCamCoords()) + local scale = 0.35 + local font = 4 - -- Add targets - exports['qb-target']:AddTargetModel(Config.VendingProps, { - options = { - { - type = "client", - event = "vending:client:buyMachine", - icon = "fas fa-dollar-sign", - label = "Automaten kaufen ($" .. Config.VendingMachinePrice .. ")", - canInteract = function(entity) - return not isRegisteredMachine(entity) - end - }, - { - type = "client", - event = "vending:client:openBuyMenu", - icon = "fas fa-shopping-cart", - label = "Kaufen", - canInteract = function(entity) - return isRegisteredMachine(entity) - end - }, - { - type = "client", - event = "vending:client:openOwnerMenu", - icon = "fas fa-cog", - label = "Verwalten", - canInteract = function(entity) - return canManageMachine(entity) - end - }, - { - type = "client", - event = "vending:client:startRobbery", - icon = "fas fa-mask", - label = "Aufbrechen", - canInteract = function(entity) - return isRegisteredMachine(entity) and not canManageMachine(entity) - end - } - }, - distance = 2.0 - }) - - print("^2[VENDING]^7 Added targets to " .. #Config.VendingProps .. " vending machine props") + if onScreen then + SetTextScale(scale, scale) + SetTextFont(font) + SetTextProportional(1) + SetTextColour(255, 255, 255, 215) + SetTextOutline() + SetTextEntry("STRING") + SetTextCentre(1) + AddTextComponentString(text) + DrawText(_x, _y) + end end --- Add targets to all vending machine props with multiple attempts (Option 1) -CreateThread(function() - -- First attempt - Wait(2000) - InitializeTargets() - - -- Second attempt after a delay - Wait(5000) - InitializeTargets() - - -- Third attempt after server is fully loaded - Wait(10000) - InitializeTargets() -end) - --- Event-based initialization (Option 2) -RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function() - Wait(1000) - InitializeTargets() -end) - -RegisterNetEvent('QBCore:Client:OnPlayerUnload', function() - -- Nothing to do here, but good to have for completeness -end) - --- Listen for resource start/stop events -AddEventHandler('onResourceStart', function(resourceName) - if resourceName == 'qb-target' or resourceName == GetCurrentResourceName() then - Wait(1000) - InitializeTargets() - end -end) - --- Command to manually refresh targets -RegisterCommand('refreshvendingtargets', function() - InitializeTargets() - QBCore.Functions.Notify('Vending machine targets refreshed', 'success') -end, false) - --- Check if machine is registered +-- Function to check if machine is registered function isRegisteredMachine(entity) local coords = GetEntityCoords(entity) local isRegistered = false @@ -129,6 +60,81 @@ function canManageMachine(entity) return canManage end +-- Main thread to detect nearby vending machines +CreateThread(function() + while true do + local playerPed = PlayerPedId() + local playerCoords = GetEntityCoords(playerPed) + local wait = 1000 + nearbyMachine = nil + + -- Check for nearby vending machines + for _, propName in ipairs(Config.VendingProps) do + local hash = GetHashKey(propName) + local objects = GetGamePool('CObject') + + for _, obj in ipairs(objects) do + if GetEntityModel(obj) == hash then + local objCoords = GetEntityCoords(obj) + local dist = #(playerCoords - objCoords) + + if dist < 2.0 then + wait = 0 + nearbyMachine = obj + + -- Display interaction options based on conditions + if not displayingText then + displayingText = true + CreateThread(function() + while nearbyMachine == obj and #(GetEntityCoords(PlayerPedId()) - GetEntityCoords(obj)) < 2.0 do + local z = objCoords.z + 1.0 + + -- Check conditions and display appropriate text + if isRegisteredMachine(obj) then + if canManageMachine(obj) then + DrawText3D(objCoords.x, objCoords.y, z, "~g~E~w~ - Kaufen | ~y~G~w~ - Verwalten") + + -- Handle key presses for management + if IsControlJustReleased(0, 38) then -- E key + TriggerEvent('vending:client:openBuyMenu', {entity = obj}) + elseif IsControlJustReleased(0, 47) then -- G key + TriggerEvent('vending:client:openOwnerMenu', {entity = obj}) + end + else + DrawText3D(objCoords.x, objCoords.y, z, "~g~E~w~ - Kaufen | ~r~G~w~ - Aufbrechen") + + -- Handle key presses for buying/robbery + if IsControlJustReleased(0, 38) then -- E key + TriggerEvent('vending:client:openBuyMenu', {entity = obj}) + elseif IsControlJustReleased(0, 47) then -- G key + TriggerEvent('vending:client:startRobbery', {entity = obj}) + end + end + else + DrawText3D(objCoords.x, objCoords.y, z, "~g~E~w~ - Automaten kaufen ($" .. Config.VendingMachinePrice .. ")") + + -- Handle key press for buying machine + if IsControlJustReleased(0, 38) then -- E key + TriggerEvent('vending:client:buyMachine', {entity = obj}) + end + end + Wait(0) + end + displayingText = false + end) + end + break + end + end + end + + if nearbyMachine then break end + end + + Wait(wait) + end +end) + -- Buy vending machine RegisterNetEvent('vending:client:buyMachine', function(data) local entity = data.entity @@ -314,7 +320,7 @@ RegisterNetEvent('vending:client:openOwnerMenu', function(data) end, coords) end) --- Funktion zum Verkaufen des Automaten +-- Function to sell the vending machine function sellVendingMachine(coords, machineId) local input = lib.inputDialog('Automaten verkaufen', { { @@ -624,11 +630,6 @@ RegisterNetEvent('vending:client:policeAlert', function(coords, streetName) QBCore.Functions.Notify('Verkaufsautomat Aufbruch gemeldet: ' .. streetName, 'error', 8000) end) --- Refresh targets (called when new machine is registered) -RegisterNetEvent('vending:client:refreshTargets', function() - InitializeTargets() -end) - -- Management menu (alternative opening method) RegisterNetEvent('vending:client:openManagement', function(machine) lib.registerContext({