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 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,44 +26,67 @@ function DrawText3D(x, y, z, text)
|
|||
ClearDrawOrigin()
|
||||
end
|
||||
|
||||
-- Function to check if machine is registered
|
||||
function isRegisteredMachine(entity)
|
||||
-- Cache machine data to avoid constant callbacks
|
||||
function GetMachineData(entity)
|
||||
local entityId = NetworkGetNetworkIdFromEntity(entity)
|
||||
|
||||
if not machineData[entityId] then
|
||||
local coords = GetEntityCoords(entity)
|
||||
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
|
||||
|
||||
-- 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 callback
|
||||
-- Wait for callbacks to complete
|
||||
local timeout = 0
|
||||
while canManage == false and timeout < 100 do
|
||||
while not machineData[entityId] and timeout < 50 do
|
||||
Wait(10)
|
||||
timeout = timeout + 1
|
||||
end
|
||||
end
|
||||
|
||||
return canManage
|
||||
-- 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
|
||||
CreateThread(function()
|
||||
while true do
|
||||
|
@ -80,24 +110,23 @@ CreateThread(function()
|
|||
foundMachine = true
|
||||
nearbyMachine = obj
|
||||
|
||||
-- Only check status if not already interacting
|
||||
if not isInteracting then
|
||||
-- Get machine data from cache
|
||||
local data = GetMachineData(obj)
|
||||
|
||||
-- Display appropriate text based on machine status
|
||||
local z = objCoords.z + 1.0
|
||||
local registered = isRegisteredMachine(obj)
|
||||
|
||||
if registered then
|
||||
local canManage = canManageMachine(obj)
|
||||
|
||||
if canManage then
|
||||
if data.isRegistered then
|
||||
if data.canManage then
|
||||
DrawText3D(objCoords.x, objCoords.y, z, "[E] Kaufen | [G] Verwalten")
|
||||
|
||||
-- Handle key presses for management
|
||||
if IsControlJustPressed(0, 38) then -- E key
|
||||
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) then -- G key
|
||||
elseif IsControlJustPressed(0, 47) and not isInteracting then -- G key
|
||||
isInteracting = true
|
||||
TriggerEvent('vending:client:openOwnerMenu', {entity = obj})
|
||||
Wait(500) -- Prevent multiple triggers
|
||||
|
@ -107,12 +136,12 @@ CreateThread(function()
|
|||
DrawText3D(objCoords.x, objCoords.y, z, "[E] Kaufen | [G] Aufbrechen")
|
||||
|
||||
-- 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
|
||||
TriggerEvent('vending:client:openBuyMenu', {entity = obj})
|
||||
Wait(500) -- Prevent multiple triggers
|
||||
isInteracting = false
|
||||
elseif IsControlJustPressed(0, 47) then -- G key
|
||||
elseif IsControlJustPressed(0, 47) and not isInteracting then -- G key
|
||||
isInteracting = true
|
||||
TriggerEvent('vending:client:startRobbery', {entity = obj})
|
||||
Wait(500) -- Prevent multiple triggers
|
||||
|
@ -123,14 +152,13 @@ CreateThread(function()
|
|||
DrawText3D(objCoords.x, objCoords.y, z, "[E] Automaten kaufen ($" .. Config.VendingMachinePrice .. ")")
|
||||
|
||||
-- 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
|
||||
TriggerEvent('vending:client:buyMachine', {entity = obj})
|
||||
Wait(500) -- Prevent multiple triggers
|
||||
isInteracting = false
|
||||
end
|
||||
end
|
||||
end
|
||||
break
|
||||
end
|
||||
end
|
||||
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue