From b6dad9c903a997154100814bbaf7c4ba80dfd307 Mon Sep 17 00:00:00 2001
From: Owen <23108066+Owen1212055@users.noreply.github.com>
Date: Tue, 22 Feb 2022 21:20:29 -0500
Subject: [PATCH] Fix desync on teleporting entity on first tick (#7183)

---
 .../0866-Fix-Entity-Position-Desync.patch     | 31 +++++++++++++++++++
 1 file changed, 31 insertions(+)
 create mode 100644 patches/server/0866-Fix-Entity-Position-Desync.patch

diff --git a/patches/server/0866-Fix-Entity-Position-Desync.patch b/patches/server/0866-Fix-Entity-Position-Desync.patch
new file mode 100644
index 0000000000..c877249ade
--- /dev/null
+++ b/patches/server/0866-Fix-Entity-Position-Desync.patch
@@ -0,0 +1,31 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: Owen1212055 <23108066+Owen1212055@users.noreply.github.com>
+Date: Thu, 23 Dec 2021 23:59:12 -0500
+Subject: [PATCH] Fix Entity Position Desync
+
+If entities were teleported in the first tick it would not be send to the client.
+
+This also fixes desync caused be relatively teleporting paintings.
+This is caused by the fact that setPacketCoordinates isn't called on paintings when they are spawned (unlike every other entity, like XP orbs)
+on the client for some reason. This causes it to be relatively teleported to the world origin (0,0,0).
+
+diff --git a/src/main/java/net/minecraft/server/level/ServerEntity.java b/src/main/java/net/minecraft/server/level/ServerEntity.java
+index 9a6c67b614944f841813ec2892381c3342bc365c..b0a79e335ac9dc24fc6f18010bf40716ecc724bf 100644
+--- a/src/main/java/net/minecraft/server/level/ServerEntity.java
++++ b/src/main/java/net/minecraft/server/level/ServerEntity.java
+@@ -164,13 +164,14 @@ public class ServerEntity {
+                 }
+                 // CraftBukkit end
+ 
+-                if (this.tickCount > 0 || this.entity instanceof AbstractArrow) {
++                if (true || this.tickCount > 0 || this.entity instanceof AbstractArrow) { // Paper - Always update position
+                     // Paper start - remove allocation of Vec3D here
+                     long k = ClientboundMoveEntityPacket.entityToPacket(vec3d_dx);
+                     long l = ClientboundMoveEntityPacket.entityToPacket(vec3d_dy);
+                     long i1 = ClientboundMoveEntityPacket.entityToPacket(vec3d_dz);
+                     // Paper end - remove allocation of Vec3D here
+                     boolean flag4 = k < -32768L || k > 32767L || l < -32768L || l > 32767L || i1 < -32768L || i1 > 32767L;
++                    if (this.entity instanceof net.minecraft.world.entity.decoration.Painting) {flag4 = true;} // Paper - Always send exact position for paintings
+ 
+                     if (!flag4 && this.teleportDelay <= 400 && !this.wasRiding && this.wasOnGround == this.entity.isOnGround() && !(com.destroystokyo.paper.PaperConfig.sendFullPosForHardCollidingEntities && this.entity.hardCollides())) { // Paper - send full pos for hard colliding entities to prevent collision problems due to desync
+                         if ((!flag2 || !flag3) && !(this.entity instanceof AbstractArrow)) {