From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Spottedleaf Date: Tue, 20 Feb 2024 18:24:16 -0800 Subject: [PATCH] Fix entity tracker desync when new players are added to the tracker The delta position packet instructs the client to update the entity position by a position difference. However, this position difference is relative to the last position in the entity tracker state, not the last position which has been sent to the player. As a result, if the last position the player has recorded is different than the one stored in the entity tracker (which occurs when a new player is added to an existing entity tracker state) then the sent position difference will cause a position desync for the client. We can resolve this problem by either tracking the last position sent per-player, or by simply resetting the last sent position in the entity tracker state every time a new player is added. Resetting the last sent position every time a new player is added to the tracker is just easier to do, so that is what this patch does. This patch also fixes entities appearing to disappear when teleporting to players by changing the initial position in the spawn packet to the entities current tracking position. When teleporting, the spawn packet will contain the old position which is most likely in an unloaded chunk - which means that the client will not tick the entity and thus not lerp the entity from its old position to its new position. diff --git a/src/main/java/net/minecraft/network/protocol/game/ClientboundAddEntityPacket.java b/src/main/java/net/minecraft/network/protocol/game/ClientboundAddEntityPacket.java index 854baa554da2215a656f976ae2973341c3b2d61c..792b9a72a610cc512a8920d61013b6ba02f71e47 100644 --- a/src/main/java/net/minecraft/network/protocol/game/ClientboundAddEntityPacket.java +++ b/src/main/java/net/minecraft/network/protocol/game/ClientboundAddEntityPacket.java @@ -42,9 +42,11 @@ public class ClientboundAddEntityPacket implements Packet 32767L || l < -32768L || l > 32767L || i1 < -32768L || i1 > 32767L; - if (!flag6 && this.teleportDelay <= 400 && !this.wasRiding && this.wasOnGround == this.entity.onGround()) { + if (!this.forceStateResync && !flag6 && this.teleportDelay <= 400 && !this.wasRiding && this.wasOnGround == this.entity.onGround()) { // Paper - fix desync when a player is added to the tracker if ((!flag2 || !flag3) && !(this.entity instanceof AbstractArrow)) { if (flag2) { packet1 = new ClientboundMoveEntityPacket.Pos(this.entity.getId(), (short) ((int) k), (short) ((int) l), (short) ((int) i1), this.entity.onGround()); @@ -249,6 +256,7 @@ public class ServerEntity { } this.entity.hasImpulse = false; + this.forceStateResync = false; // Paper - fix desync when a player is added to the tracker } ++this.tickCount;