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**
This commit is contained in:
Mycroft
2022-12-09 01:07:53 +00:00
parent 89194761e5
commit 51280a094c
3 changed files with 24 additions and 27 deletions
-21
View File
@@ -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
+23 -5
View File
@@ -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)
+1 -1
View File
@@ -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