From 51280a094c9b813225cb27037f21607ef964a09e Mon Sep 17 00:00:00 2001 From: Mycroft Date: Fri, 9 Dec 2022 01:07:53 +0000 Subject: [PATCH] refactor(es_extended): Server sided Coord Saving this is as thread less design that allows both xPlayer.coords and xPlayer.getCoords to correctly function, aswell as removing the need for the client to trigger a server event this is once again, a change that **requires** Onesync **infinity** --- [esx]/es_extended/client/main.lua | 21 ---------------- [esx]/es_extended/server/classes/player.lua | 28 +++++++++++++++++---- [esx]/es_extended/server/main.lua | 2 +- 3 files changed, 24 insertions(+), 27 deletions(-) diff --git a/[esx]/es_extended/client/main.lua b/[esx]/es_extended/client/main.lua index b613f741..6d46a79d 100644 --- a/[esx]/es_extended/client/main.lua +++ b/[esx]/es_extended/client/main.lua @@ -438,27 +438,6 @@ function StartServerSyncLoops() end end) end - - -- sync current player coords with server - CreateThread(function() - local previousCoords = vector3(ESX.PlayerData.coords.x, ESX.PlayerData.coords.y, ESX.PlayerData.coords.z) - - while ESX.PlayerLoaded do - local playerPed = PlayerPedId() - if ESX.PlayerData.ped ~= playerPed then ESX.SetPlayerData('ped', playerPed) end - - if DoesEntityExist(ESX.PlayerData.ped) then - local playerCoords = GetEntityCoords(ESX.PlayerData.ped) - local distance = #(playerCoords - previousCoords) - - if distance > 1 then - previousCoords = playerCoords - TriggerServerEvent('esx:updateCoords') - end - end - Wait(1500) - end - end) end if not Config.OxInventory and Config.EnableDefaultInventory then diff --git a/[esx]/es_extended/server/classes/player.lua b/[esx]/es_extended/server/classes/player.lua index 73634829..8f3eb8e9 100644 --- a/[esx]/es_extended/server/classes/player.lua +++ b/[esx]/es_extended/server/classes/player.lua @@ -1,3 +1,9 @@ +local SetTimeout = SetTimeout +local GetPlayerPed = GetPlayerPed +local DoesEntityExist = DoesEntityExist +local GetEntityCoords = GetEntityCoords +local GetEntityHeading = GetEntityHeading + function CreateExtendedPlayer(playerId, identifier, group, accounts, inventory, weight, job, loadout, name, coords) local targetOverrides = Config.PlayerFunctionOverride and Core.PlayerFunctionOverrides[Config.PlayerFunctionOverride] or {} @@ -31,7 +37,6 @@ function CreateExtendedPlayer(playerId, identifier, group, accounts, inventory, end function self.setCoords(coords) - self.updateCoords(coords) local Ped = GetPlayerPed(self.source) local vector = type(coords) == "vector4" and coords or type(coords) == "vector3" and vector4(coords, 0.0) or vec(coords.x, coords.y, coords.z, coords.heading or 0.0) @@ -40,10 +45,23 @@ function CreateExtendedPlayer(playerId, identifier, group, accounts, inventory, end function self.updateCoords() - local Ped = GetPlayerPed(self.source) - local coords = GetEntityCoords(Ped) - local heading = GetEntityHeading(Ped) - self.coords = {x = ESX.Math.Round(coords.x, 1), y = ESX.Math.Round(coords.y, 1), z = ESX.Math.Round(coords.z, 1), heading = ESX.Math.Round(heading or 0.0, 1)} + SetTimeout(1000,function() + local Ped = GetPlayerPed(self.source) + if DoesEntityExist(Ped) then + local coords = GetEntityCoords(Ped) + local distance = #(coords - vector3(self.coords.x, self.coords.y, self.coords.z)) + if distance > 1.5 then + local heading = GetEntityHeading(Ped) + self.coords = { + x = coords.x, + y = coords.y, + z = coords.z, + heading = heading or 0.0 + } + end + end + self.updateCoords() + end) end function self.getCoords(vector) diff --git a/[esx]/es_extended/server/main.lua b/[esx]/es_extended/server/main.lua index 87ffeb71..ddfb98dd 100644 --- a/[esx]/es_extended/server/main.lua +++ b/[esx]/es_extended/server/main.lua @@ -317,7 +317,7 @@ function loadESXPlayer(identifier, playerId, isNew) else exports.ox_inventory:setPlayerInventory(xPlayer, userData.inventory) end - + xPlayer.updateCoords() xPlayer.triggerEvent('esx:registerSuggestions', Core.RegisteredCommands) print(('[^2INFO^0] Player ^5"%s"^0 has connected to the server. ID: ^5%s^7'):format(xPlayer.getName(), playerId)) end