forked from Simnation/Main
Update client.lua
This commit is contained in:
parent
01c36a483f
commit
fdc0882b51
1 changed files with 113 additions and 86 deletions
|
@ -1,14 +1,21 @@
|
||||||
local QBCore = exports['qb-core']:GetCoreObject()
|
local QBCore = exports['qb-core']:GetCoreObject()
|
||||||
local nearbyMachine = nil
|
local nearbyMachine = nil
|
||||||
local currentMachineData = nil
|
local machineData = {}
|
||||||
local isInteracting = false
|
local isInteracting = false
|
||||||
|
|
||||||
-- Function to draw 3D text in the world
|
-- Function to draw 3D text in the world
|
||||||
function DrawText3D(x, y, z, text)
|
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)
|
SetTextScale(0.35, 0.35)
|
||||||
SetTextFont(4)
|
SetTextFont(4)
|
||||||
SetTextProportional(1)
|
SetTextProportional(1)
|
||||||
SetTextColour(255, 255, 255, 215)
|
SetTextColour(255, 255, 255, 215)
|
||||||
|
SetTextOutline()
|
||||||
SetTextEntry("STRING")
|
SetTextEntry("STRING")
|
||||||
SetTextCentre(1)
|
SetTextCentre(1)
|
||||||
AddTextComponentString(text)
|
AddTextComponentString(text)
|
||||||
|
@ -19,44 +26,67 @@ function DrawText3D(x, y, z, text)
|
||||||
ClearDrawOrigin()
|
ClearDrawOrigin()
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Function to check if machine is registered
|
-- Cache machine data to avoid constant callbacks
|
||||||
function isRegisteredMachine(entity)
|
function GetMachineData(entity)
|
||||||
|
local entityId = NetworkGetNetworkIdFromEntity(entity)
|
||||||
|
|
||||||
|
if not machineData[entityId] then
|
||||||
local coords = GetEntityCoords(entity)
|
local coords = GetEntityCoords(entity)
|
||||||
local isRegistered = false
|
local isRegistered = false
|
||||||
|
|
||||||
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
|
|
||||||
end
|
|
||||||
|
|
||||||
return isRegistered
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Check if player can manage machine
|
|
||||||
function canManageMachine(entity)
|
|
||||||
local coords = GetEntityCoords(entity)
|
|
||||||
local canManage = 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)
|
QBCore.Functions.TriggerCallback('vending:server:canManage', function(result)
|
||||||
canManage = 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)
|
end, coords)
|
||||||
|
|
||||||
-- Wait for callback
|
-- Wait for callbacks to complete
|
||||||
local timeout = 0
|
local timeout = 0
|
||||||
while canManage == false and timeout < 100 do
|
while not machineData[entityId] and timeout < 50 do
|
||||||
Wait(10)
|
Wait(10)
|
||||||
timeout = timeout + 1
|
timeout = timeout + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
return canManage
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Return cached data or default
|
||||||
|
return machineData[entityId] or {isRegistered = false, canManage = false}
|
||||||
|
end
|
||||||
|
|
||||||
|
-- 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
|
||||||
|
end)
|
||||||
|
|
||||||
-- Main thread to detect nearby vending machines
|
-- Main thread to detect nearby vending machines
|
||||||
CreateThread(function()
|
CreateThread(function()
|
||||||
while true do
|
while true do
|
||||||
|
@ -80,24 +110,23 @@ CreateThread(function()
|
||||||
foundMachine = true
|
foundMachine = true
|
||||||
nearbyMachine = obj
|
nearbyMachine = obj
|
||||||
|
|
||||||
-- Only check status if not already interacting
|
-- Get machine data from cache
|
||||||
if not isInteracting then
|
local data = GetMachineData(obj)
|
||||||
|
|
||||||
|
-- Display appropriate text based on machine status
|
||||||
local z = objCoords.z + 1.0
|
local z = objCoords.z + 1.0
|
||||||
local registered = isRegisteredMachine(obj)
|
|
||||||
|
|
||||||
if registered then
|
if data.isRegistered then
|
||||||
local canManage = canManageMachine(obj)
|
if data.canManage then
|
||||||
|
|
||||||
if canManage then
|
|
||||||
DrawText3D(objCoords.x, objCoords.y, z, "[E] Kaufen | [G] Verwalten")
|
DrawText3D(objCoords.x, objCoords.y, z, "[E] Kaufen | [G] Verwalten")
|
||||||
|
|
||||||
-- Handle key presses for management
|
-- Handle key presses for management
|
||||||
if IsControlJustPressed(0, 38) then -- E key
|
if IsControlJustPressed(0, 38) and not isInteracting then -- E key
|
||||||
isInteracting = true
|
isInteracting = true
|
||||||
TriggerEvent('vending:client:openBuyMenu', {entity = obj})
|
TriggerEvent('vending:client:openBuyMenu', {entity = obj})
|
||||||
Wait(500) -- Prevent multiple triggers
|
Wait(500) -- Prevent multiple triggers
|
||||||
isInteracting = false
|
isInteracting = false
|
||||||
elseif IsControlJustPressed(0, 47) then -- G key
|
elseif IsControlJustPressed(0, 47) and not isInteracting then -- G key
|
||||||
isInteracting = true
|
isInteracting = true
|
||||||
TriggerEvent('vending:client:openOwnerMenu', {entity = obj})
|
TriggerEvent('vending:client:openOwnerMenu', {entity = obj})
|
||||||
Wait(500) -- Prevent multiple triggers
|
Wait(500) -- Prevent multiple triggers
|
||||||
|
@ -107,12 +136,12 @@ CreateThread(function()
|
||||||
DrawText3D(objCoords.x, objCoords.y, z, "[E] Kaufen | [G] Aufbrechen")
|
DrawText3D(objCoords.x, objCoords.y, z, "[E] Kaufen | [G] Aufbrechen")
|
||||||
|
|
||||||
-- Handle key presses for buying/robbery
|
-- Handle key presses for buying/robbery
|
||||||
if IsControlJustPressed(0, 38) then -- E key
|
if IsControlJustPressed(0, 38) and not isInteracting then -- E key
|
||||||
isInteracting = true
|
isInteracting = true
|
||||||
TriggerEvent('vending:client:openBuyMenu', {entity = obj})
|
TriggerEvent('vending:client:openBuyMenu', {entity = obj})
|
||||||
Wait(500) -- Prevent multiple triggers
|
Wait(500) -- Prevent multiple triggers
|
||||||
isInteracting = false
|
isInteracting = false
|
||||||
elseif IsControlJustPressed(0, 47) then -- G key
|
elseif IsControlJustPressed(0, 47) and not isInteracting then -- G key
|
||||||
isInteracting = true
|
isInteracting = true
|
||||||
TriggerEvent('vending:client:startRobbery', {entity = obj})
|
TriggerEvent('vending:client:startRobbery', {entity = obj})
|
||||||
Wait(500) -- Prevent multiple triggers
|
Wait(500) -- Prevent multiple triggers
|
||||||
|
@ -123,14 +152,13 @@ CreateThread(function()
|
||||||
DrawText3D(objCoords.x, objCoords.y, z, "[E] Automaten kaufen ($" .. Config.VendingMachinePrice .. ")")
|
DrawText3D(objCoords.x, objCoords.y, z, "[E] Automaten kaufen ($" .. Config.VendingMachinePrice .. ")")
|
||||||
|
|
||||||
-- Handle key press for buying machine
|
-- Handle key press for buying machine
|
||||||
if IsControlJustPressed(0, 38) then -- E key
|
if IsControlJustPressed(0, 38) and not isInteracting then -- E key
|
||||||
isInteracting = true
|
isInteracting = true
|
||||||
TriggerEvent('vending:client:buyMachine', {entity = obj})
|
TriggerEvent('vending:client:buyMachine', {entity = obj})
|
||||||
Wait(500) -- Prevent multiple triggers
|
Wait(500) -- Prevent multiple triggers
|
||||||
isInteracting = false
|
isInteracting = false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -139,14 +167,19 @@ CreateThread(function()
|
||||||
if foundMachine then break end
|
if foundMachine then break end
|
||||||
end
|
end
|
||||||
|
|
||||||
if not foundMachine then
|
|
||||||
nearbyMachine = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
Wait(wait)
|
Wait(wait)
|
||||||
end
|
end
|
||||||
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
|
-- Buy vending machine
|
||||||
RegisterNetEvent('vending:client:buyMachine', function(data)
|
RegisterNetEvent('vending:client:buyMachine', function(data)
|
||||||
local entity = data.entity
|
local entity = data.entity
|
||||||
|
@ -727,9 +760,3 @@ RegisterCommand('vendingdebug', function()
|
||||||
end
|
end
|
||||||
end, coords)
|
end, coords)
|
||||||
end, false)
|
end, false)
|
||||||
|
|
||||||
-- Event to refresh machine data when a new machine is registered
|
|
||||||
RegisterNetEvent('vending:client:refreshTargets', function()
|
|
||||||
-- Clear cached data
|
|
||||||
currentMachineData = nil
|
|
||||||
end)
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue