From 105f6d6a21599d7f6951318e0d230495ac109ec9 Mon Sep 17 00:00:00 2001 From: Spottedleaf Date: Sat, 8 Apr 2023 19:14:59 -0700 Subject: [PATCH] Adjust CB async teleport dismount behavior The expected behavior is that the entity is only dismounted _if_ the teleport takes place, not regardless of whether the teleport takes place. To adhere to the expected behavior, we need to create a new teleport flag so that the NMS teleportAsync can perform the dismount. --- patches/server/0005-Threaded-Regions.patch | 58 ++++++++++--------- ...-getHandle-and-overrides-perform-thr.patch | 4 +- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/patches/server/0005-Threaded-Regions.patch b/patches/server/0005-Threaded-Regions.patch index b3fedcd..0a1c1a0 100644 --- a/patches/server/0005-Threaded-Regions.patch +++ b/patches/server/0005-Threaded-Regions.patch @@ -18047,7 +18047,7 @@ index 93a1e990b0a6caae4143c2f9d09bfb368fa1d6db..ad3166481dd37f4b5380f8bf28653bb4 itemstack = entityliving2.getMainHandItem(); diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java -index 4705d7066207250c03a5f98eef61554c901f2e35..580e75595d7fc8c5af5a0281b77f7be88a25a464 100644 +index 4705d7066207250c03a5f98eef61554c901f2e35..c659a574195f465ec3316f4c014621569e099cfc 100644 --- a/src/main/java/net/minecraft/world/entity/Entity.java +++ b/src/main/java/net/minecraft/world/entity/Entity.java @@ -165,7 +165,7 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource { @@ -18230,7 +18230,7 @@ index 4705d7066207250c03a5f98eef61554c901f2e35..580e75595d7fc8c5af5a0281b77f7be8 return; } // CraftBukkit end -@@ -3380,6 +3406,754 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource { +@@ -3380,6 +3406,763 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource { this.portalEntrancePos = original.portalEntrancePos; } @@ -18400,6 +18400,11 @@ index 4705d7066207250c03a5f98eef61554c901f2e35..580e75595d7fc8c5af5a0281b77f7be8 + * teleport will be refused. + */ + public static final long TELEPORT_FLAG_TELEPORT_PASSENGERS = 1L << 1; ++ /** ++ * The flag will dismount any passengers and dismout from the current vehicle ++ * to teleport if and only if dismounting would result in the teleport being allowed. ++ */ ++ public static final long TELEPORT_FLAG_UNMOUNT = 1L << 2; + + protected void placeSingleSync(ServerLevel originWorld, ServerLevel destination, EntityTreeNode treeNode, long teleportFlags) { + destination.addDuringTeleport(this); @@ -18535,6 +18540,21 @@ index 4705d7066207250c03a5f98eef61554c901f2e35..580e75595d7fc8c5af5a0281b77f7be8 + return false; + } + ++ if (!this.canTeleportAsync()) { ++ return false; ++ } ++ this.getBukkitEntity(); // force bukkit entity to be created before TPing ++ if ((teleportFlags & TELEPORT_FLAG_UNMOUNT) == 0L) { ++ for (Entity entity : this.getIndirectPassengers()) { ++ if (!entity.canTeleportAsync()) { ++ return false; ++ } ++ entity.getBukkitEntity(); // force bukkit entity to be created before TPing ++ } ++ } else { ++ this.unRide(); ++ } ++ + if ((teleportFlags & TELEPORT_FLAG_TELEPORT_PASSENGERS) != 0L) { + if (this.isPassenger()) { + return false; @@ -18545,17 +18565,6 @@ index 4705d7066207250c03a5f98eef61554c901f2e35..580e75595d7fc8c5af5a0281b77f7be8 + } + } + -+ this.getBukkitEntity(); // force bukkit entity to be created before TPing -+ if (!this.canTeleportAsync()) { -+ return false; -+ } -+ for (Entity entity : this.getIndirectPassengers()) { -+ entity.getBukkitEntity(); // force bukkit entity to be created before TPing -+ if (!entity.canTeleportAsync()) { -+ return false; -+ } -+ } -+ + // TODO any events that can modify go HERE + + // check for same region @@ -18985,7 +18994,7 @@ index 4705d7066207250c03a5f98eef61554c901f2e35..580e75595d7fc8c5af5a0281b77f7be8 @Nullable public Entity changeDimension(ServerLevel destination) { // CraftBukkit start -@@ -3388,6 +4162,11 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource { +@@ -3388,6 +4171,11 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource { @Nullable public Entity teleportTo(ServerLevel worldserver, PositionImpl location) { @@ -18997,7 +19006,7 @@ index 4705d7066207250c03a5f98eef61554c901f2e35..580e75595d7fc8c5af5a0281b77f7be8 // CraftBukkit end // Paper start - fix bad state entities causing dupes if (!isAlive() || !valid) { -@@ -3471,6 +4250,12 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource { +@@ -3471,6 +4259,12 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource { } } @@ -19010,7 +19019,7 @@ index 4705d7066207250c03a5f98eef61554c901f2e35..580e75595d7fc8c5af5a0281b77f7be8 protected void removeAfterChangingDimensions() { this.setRemoved(Entity.RemovalReason.CHANGED_DIMENSION); } -@@ -3910,17 +4695,13 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource { +@@ -3910,17 +4704,13 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource { // Paper start public void startSeenByPlayer(ServerPlayer player) { @@ -19030,7 +19039,7 @@ index 4705d7066207250c03a5f98eef61554c901f2e35..580e75595d7fc8c5af5a0281b77f7be8 } // Paper end -@@ -4411,7 +5192,8 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource { +@@ -4411,7 +5201,8 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource { } } // Paper end - fix MC-4 @@ -19040,7 +19049,7 @@ index 4705d7066207250c03a5f98eef61554c901f2e35..580e75595d7fc8c5af5a0281b77f7be8 synchronized (this.posLock) { // Paper this.position = new Vec3(x, y, z); } // Paper -@@ -4432,7 +5214,7 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource { +@@ -4432,7 +5223,7 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource { // Paper start - never allow AABB to become desynced from position // hanging has its own special logic @@ -19049,7 +19058,7 @@ index 4705d7066207250c03a5f98eef61554c901f2e35..580e75595d7fc8c5af5a0281b77f7be8 this.setBoundingBox(this.makeBoundingBox()); } // Paper end -@@ -4519,6 +5301,12 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource { +@@ -4519,6 +5310,12 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource { return this.removalReason != null; } @@ -19062,7 +19071,7 @@ index 4705d7066207250c03a5f98eef61554c901f2e35..580e75595d7fc8c5af5a0281b77f7be8 @Nullable public Entity.RemovalReason getRemovalReason() { return this.removalReason; -@@ -4543,7 +5331,23 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource { +@@ -4543,7 +5340,23 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource { if (reason != RemovalReason.UNLOADED_TO_CHUNK) this.getPassengers().forEach(Entity::stopRiding); // Paper - chunk system - don't adjust passenger state when unloading, it's just not safe (and messes with our logic in entity chunk unload) this.levelCallback.onRemove(reason); @@ -24496,7 +24505,7 @@ index cd4ad8261e56365850068db1d83d6a8454026737..78f7e72f2912dae503c2dab7d1992b65 List offers = waitable.get(); if (offers == null) { diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java -index 57a0dbb23a32123d30c3b3572f4d129be9d97847..2284570959dbaf9941e85caf723f06a032705e7e 100644 +index 57a0dbb23a32123d30c3b3572f4d129be9d97847..652c28a66d45680479a38463525afcf191c55ab4 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java @@ -203,6 +203,16 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity { @@ -24537,7 +24546,7 @@ index 57a0dbb23a32123d30c3b3572f4d129be9d97847..2284570959dbaf9941e85caf723f06a0 if (entityTracker == null) { return; -@@ -1308,30 +1323,46 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity { +@@ -1308,30 +1323,43 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity { Preconditions.checkArgument(location != null, "location"); location.checkFinite(); Location locationClone = location.clone(); // clone so we don't need to worry about mutations after this call. @@ -24562,15 +24571,12 @@ index 57a0dbb23a32123d30c3b3572f4d129be9d97847..2284570959dbaf9941e85caf723f06a0 - net.minecraft.server.MinecraftServer.LOGGER.error("Failed to teleport entity " + CraftEntity.this, throwable); - ret.completeExceptionally(throwable); + java.util.function.Consumer run = (Entity nmsEntity) -> { -+ // preserve behavior with old API: dismount the entity so it can teleport -+ nmsEntity.unRide(); -+ + boolean success = nmsEntity.teleportAsync( + ((CraftWorld)locationClone.getWorld()).getHandle(), + new net.minecraft.world.phys.Vec3(locationClone.getX(), locationClone.getY(), locationClone.getZ()), + null, null, net.minecraft.world.phys.Vec3.ZERO, + cause == null ? TeleportCause.UNKNOWN : cause, -+ Entity.TELEPORT_FLAG_LOAD_CHUNK, ++ Entity.TELEPORT_FLAG_LOAD_CHUNK | Entity.TELEPORT_FLAG_UNMOUNT, // preserve behavior with old API: dismount the entity so it can teleport + (Entity entityTp) -> { + ret.complete(Boolean.TRUE); } diff --git a/patches/server/0010-Make-CraftEntity-getHandle-and-overrides-perform-thr.patch b/patches/server/0010-Make-CraftEntity-getHandle-and-overrides-perform-thr.patch index c82b597..00cf57d 100644 --- a/patches/server/0010-Make-CraftEntity-getHandle-and-overrides-perform-thr.patch +++ b/patches/server/0010-Make-CraftEntity-getHandle-and-overrides-perform-thr.patch @@ -51,7 +51,7 @@ index d9687722e02dfd4088c7030abbf5008eb0a092c8..62484ebf4550b05182f693a3180bbac5 TickThread.ensureTickThread(thisEntity, "May not tick entity scheduler asynchronously"); final List toRun; diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java -index 580e75595d7fc8c5af5a0281b77f7be88a25a464..5a6926e7dd5b54e7ae928ee45058663e2a13274c 100644 +index c659a574195f465ec3316f4c014621569e099cfc..08e61a8940c142c68ed93359084ea46c7fd52310 100644 --- a/src/main/java/net/minecraft/world/entity/Entity.java +++ b/src/main/java/net/minecraft/world/entity/Entity.java @@ -2767,6 +2767,7 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource { @@ -932,7 +932,7 @@ index 75c7645fb5732c43d1da15181cf5c7ee4c3ecd6c..6d3325436a77153438bc40aa86819562 } diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java -index 2284570959dbaf9941e85caf723f06a032705e7e..beccbc2dd18174ca2586b9b67f1e2f1f26840901 100644 +index 652c28a66d45680479a38463525afcf191c55ab4..c6c31416fb21e180f5c05baca93630c5d14e0abb 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java @@ -827,7 +827,7 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {