diff --git a/[core]/es_extended/client/imports/point.lua b/[core]/es_extended/client/imports/point.lua index 5c505cc82..af0facad6 100644 --- a/[core]/es_extended/client/imports/point.lua +++ b/[core]/es_extended/client/imports/point.lua @@ -1,53 +1,76 @@ local Point = ESX.Class() -local nearby, loop = {} +local points, nearbyPoints, mainThread, nearbyThread = {}, {}, false, false + +local function UnloadPoint(handle, point) + nearbyPoints[handle] = nil + if point.leave then + point:leave() + end + if #nearbyPoints == 0 then + nearbyThread = false + end +end function Point:constructor(properties) - self.coords = properties.coords - self.hidden = properties.hidden - self.enter = properties.enter - self.leave = properties.leave - self.inside = properties.inside - self.handle = ESX.CreatePointInternal(properties.coords, properties.distance, properties.hidden, function() - nearby[self.handle] = self - if self.enter then - self:enter() - end - if not loop then - loop = true - CreateThread(function() - while loop do - local coords = GetEntityCoords(ESX.PlayerData.ped) - for handle, point in pairs(nearby) do - if point.inside then - point:inside(#(coords - point.coords)) - end - end - Wait() - end - end) - end - end, function() - nearby[self.handle] = nil - if self.leave then - self:leave() - end - if #nearby == 0 then - loop = false - end - end) + self.coords = properties.coords + self.distance = properties.distance + self.hidden = properties.hidden + self.enter = properties.enter + self.leave = properties.leave + self.inside = properties.inside + self.handle = ESX.Table.SizeOf(points) + 1 + points[self.handle] = self + if not mainThread then + mainThread = true + CreateThread(function() + while mainThread do Wait(500) + local coords = GetEntityCoords(ESX.PlayerData.ped) + for handle, point in pairs(points) do + if not point.hidden and #(coords - point.coords) <= point.distance then + if not nearbyPoints[handle] then + nearbyPoints[handle] = point + if point.enter then + point:enter() + end + if not nearbyThread then + nearbyThread = true + CreateThread(function() + while nearbyThread do Wait(0) + coords = GetEntityCoords(ESX.PlayerData.ped) + for _, nearbyPoint in pairs(nearbyPoints) do + if nearbyPoint.inside then + nearbyPoint:inside(#(coords - nearbyPoint.coords)) + end + end + end + end) + end + end + elseif nearbyPoints[handle] then + UnloadPoint(handle, point) + end + end + end + end) + end end function Point:delete() - ESX.RemovePointInternal(self.handle) + points[self.handle] = nil + if #points == 0 then + mainThread = false + end + if nearbyPoints[self.handle] then + UnloadPoint(self.handle, self) + end end function Point:toggle(hidden) - if hidden == nil then - hidden = not self.hidden - end - self.hidden = hidden - ESX.HidePointInternal(self.handle, hidden) + if hidden == nil then + hidden = not self.hidden + end + self.hidden = hidden end -return Point \ No newline at end of file +return Point diff --git a/[core]/es_extended/client/modules/events.lua b/[core]/es_extended/client/modules/events.lua index 08d85b78b..518cdc89b 100644 --- a/[core]/es_extended/client/modules/events.lua +++ b/[core]/es_extended/client/modules/events.lua @@ -42,7 +42,6 @@ RegisterNetEvent("esx:playerLoaded", function(xPlayer, _, skin) end Actions:Init() - StartPointsLoop() StartServerSyncLoops() NetworkSetLocalPlayerSyncLookAt(true) end) diff --git a/[core]/es_extended/client/modules/points.lua b/[core]/es_extended/client/modules/points.lua deleted file mode 100644 index bbcaf8a82..000000000 --- a/[core]/es_extended/client/modules/points.lua +++ /dev/null @@ -1,54 +0,0 @@ -local points = {} - -function ESX.CreatePointInternal(coords, distance, hidden, enter, leave) - local point = { - coords = coords, - distance = distance, - hidden = hidden, - enter = enter, - leave = leave, - resource = GetInvokingResource() - } - local handle = ESX.Table.SizeOf(points) + 1 - points[handle] = point - return handle -end - -function ESX.RemovePointInternal(handle) - points[handle] = nil -end - -function ESX.HidePointInternal(handle, hidden) - if points[handle] then - points[handle].hidden = hidden - end -end - -function StartPointsLoop() - CreateThread(function() - while true do - local coords = GetEntityCoords(ESX.PlayerData.ped) - for handle, point in pairs(points) do - if not point.hidden and #(coords - point.coords) <= point.distance then - if not point.nearby then - points[handle].nearby = true - points[handle].enter() - end - elseif point.nearby then - points[handle].nearby = false - points[handle].leave() - end - end - Wait(500) - end - end) -end - - -AddEventHandler('onResourceStop', function(resource) - for handle, point in pairs(points) do - if point.resource == resource then - points[handle] = nil - end - end -end) \ No newline at end of file diff --git a/[core]/es_extended/fxmanifest.lua b/[core]/es_extended/fxmanifest.lua index 5bcca0968..8386e7a29 100644 --- a/[core]/es_extended/fxmanifest.lua +++ b/[core]/es_extended/fxmanifest.lua @@ -44,7 +44,6 @@ client_scripts { 'client/modules/wrapper.lua', 'client/modules/callback.lua', 'client/modules/adjustments.lua', - 'client/modules/points.lua', 'client/modules/events.lua',