From 42d9d82f76270eda5a1b97ec727413ee10d03472 Mon Sep 17 00:00:00 2001 From: Goodlyay Date: Wed, 28 Feb 2024 01:26:26 -0800 Subject: [PATCH] Immediately update server-side position when TPing player --- MCGalaxy/Network/ClassicProtocol.cs | 21 ++++++++++------- MCGalaxy/Player/Player.Handlers.cs | 35 ++++++++++++++++++++--------- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/MCGalaxy/Network/ClassicProtocol.cs b/MCGalaxy/Network/ClassicProtocol.cs index 2639b1d71..59f7d1843 100644 --- a/MCGalaxy/Network/ClassicProtocol.cs +++ b/MCGalaxy/Network/ClassicProtocol.cs @@ -361,26 +361,31 @@ namespace MCGalaxy.Network if (id == Entities.SelfID) pos.Y -= 22; Send(Packet.Teleport(id, pos, rot, player.hasExtPositions)); - DoIgnorePositionPing(id); + OnTeleported(id, pos, rot); } public override bool SendTeleport(byte id, Position pos, Orientation rot, Packet.TeleportMoveMode moveMode, bool usePos = true, bool interpolateOri = false, bool useOri = true) { if (!Supports(CpeExt.ExtEntityTeleport)) { return false; } + bool absoluteSelf = (moveMode == Packet.TeleportMoveMode.AbsoluteInstant || + moveMode == Packet.TeleportMoveMode.AbsoluteSmooth) && id == Entities.SelfID; // NOTE: Classic clients require offseting own entity by 22 units vertically when using absolute location updates - if ((moveMode == Packet.TeleportMoveMode.AbsoluteInstant || - moveMode == Packet.TeleportMoveMode.AbsoluteSmooth) && id == Entities.SelfID) - { pos.Y -= 22; } + if (absoluteSelf) pos.Y -= 22; Send(Packet.TeleportExt(id, usePos, moveMode, useOri, interpolateOri, pos, rot, player.hasExtPositions)); - DoIgnorePositionPing(id); + if (absoluteSelf) OnTeleported(id, pos, rot); return true; } - void DoIgnorePositionPing(byte id) { - if (!hasTwoWayPing || id != Entities.SelfID) { return; } + + void OnTeleported(byte id, Position pos, Orientation rot) { + if (id != Entities.SelfID || !hasTwoWayPing) { return; } + ushort data = Ping.NextTwoWayPingData(true); SendTwoWayPing(data); + + //Update server-side position and check MB/portals/zones + player.ProcessMovementCore(pos, rot.RotY, rot.HeadX, false); } public override void SendRemoveEntity(byte id) { @@ -576,7 +581,7 @@ namespace MCGalaxy.Network } else { Send(Packet.AddEntity(id, name, pos, rot, player.hasCP437, player.hasExtPositions)); } - DoIgnorePositionPing(id); + OnTeleported(id, pos, rot); } public override void SendLevel(Level prev, Level level) { diff --git a/MCGalaxy/Player/Player.Handlers.cs b/MCGalaxy/Player/Player.Handlers.cs index d4a798b14..52317ff5d 100644 --- a/MCGalaxy/Player/Player.Handlers.cs +++ b/MCGalaxy/Player/Player.Handlers.cs @@ -225,24 +225,37 @@ namespace MCGalaxy if (held >= 0) ClientHeldBlock = (BlockID)held; if (Session.Ping.IgnorePosition || Loading) { return; } + if (trainGrab || following.Length > 0) { CheckBlocks(Pos, Pos); return; } // Doesn't check zones? Potential bug - if (trainGrab || following.Length > 0) { CheckBlocks(Pos, Pos); return; } Position next = new Position(x, y, z); + ProcessMovementCore(next, yaw, pitch, true); + } + + /// + /// Called to update player's position and check blocks and zones. + /// If fromClient is true, calls OnPlayerMove event and updates AFK status. + /// + internal void ProcessMovementCore(Position next, byte yaw, byte pitch, bool fromClient) { + CheckBlocks(Pos, next); - bool cancel = false; - OnPlayerMoveEvent.Call(this, next, yaw, pitch, ref cancel); - if (cancel) { cancel = false; return; } - + if (fromClient) { + bool cancel = false; + OnPlayerMoveEvent.Call(this, next, yaw, pitch, ref cancel); + if (cancel) { cancel = false; return; } + } + Pos = next; SetYawPitch(yaw, pitch); CheckZones(next); - - if (!Moved()) return; - if (DateTime.UtcNow < AFKCooldown) return; - - LastAction = DateTime.UtcNow; - if (IsAfk) CmdAfk.ToggleAfk(this, ""); + + if (fromClient) { + if (!Moved()) return; + if (DateTime.UtcNow < AFKCooldown) return; + + LastAction = DateTime.UtcNow; + if (IsAfk) CmdAfk.ToggleAfk(this, ""); + } } void CheckZones(Position pos) {