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.
This commit is contained in:
Spottedleaf 2023-04-08 19:14:59 -07:00
parent c9ded2de1c
commit 105f6d6a21
2 changed files with 34 additions and 28 deletions

View File

@ -18047,7 +18047,7 @@ index 93a1e990b0a6caae4143c2f9d09bfb368fa1d6db..ad3166481dd37f4b5380f8bf28653bb4
itemstack = entityliving2.getMainHandItem(); 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 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 --- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/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 { @@ -165,7 +165,7 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
@ -18230,7 +18230,7 @@ index 4705d7066207250c03a5f98eef61554c901f2e35..580e75595d7fc8c5af5a0281b77f7be8
return; return;
} }
// CraftBukkit end // 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; this.portalEntrancePos = original.portalEntrancePos;
} }
@ -18400,6 +18400,11 @@ index 4705d7066207250c03a5f98eef61554c901f2e35..580e75595d7fc8c5af5a0281b77f7be8
+ * teleport will be refused. + * teleport will be refused.
+ */ + */
+ public static final long TELEPORT_FLAG_TELEPORT_PASSENGERS = 1L << 1; + 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) { + protected void placeSingleSync(ServerLevel originWorld, ServerLevel destination, EntityTreeNode treeNode, long teleportFlags) {
+ destination.addDuringTeleport(this); + destination.addDuringTeleport(this);
@ -18535,6 +18540,21 @@ index 4705d7066207250c03a5f98eef61554c901f2e35..580e75595d7fc8c5af5a0281b77f7be8
+ return false; + 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 ((teleportFlags & TELEPORT_FLAG_TELEPORT_PASSENGERS) != 0L) {
+ if (this.isPassenger()) { + if (this.isPassenger()) {
+ return false; + 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 + // TODO any events that can modify go HERE
+ +
+ // check for same region + // check for same region
@ -18985,7 +18994,7 @@ index 4705d7066207250c03a5f98eef61554c901f2e35..580e75595d7fc8c5af5a0281b77f7be8
@Nullable @Nullable
public Entity changeDimension(ServerLevel destination) { public Entity changeDimension(ServerLevel destination) {
// CraftBukkit start // 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 @Nullable
public Entity teleportTo(ServerLevel worldserver, PositionImpl location) { public Entity teleportTo(ServerLevel worldserver, PositionImpl location) {
@ -18997,7 +19006,7 @@ index 4705d7066207250c03a5f98eef61554c901f2e35..580e75595d7fc8c5af5a0281b77f7be8
// CraftBukkit end // CraftBukkit end
// Paper start - fix bad state entities causing dupes // Paper start - fix bad state entities causing dupes
if (!isAlive() || !valid) { 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() { protected void removeAfterChangingDimensions() {
this.setRemoved(Entity.RemovalReason.CHANGED_DIMENSION); 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 // Paper start
public void startSeenByPlayer(ServerPlayer player) { public void startSeenByPlayer(ServerPlayer player) {
@ -19030,7 +19039,7 @@ index 4705d7066207250c03a5f98eef61554c901f2e35..580e75595d7fc8c5af5a0281b77f7be8
} }
// Paper end // 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 // Paper end - fix MC-4
@ -19040,7 +19049,7 @@ index 4705d7066207250c03a5f98eef61554c901f2e35..580e75595d7fc8c5af5a0281b77f7be8
synchronized (this.posLock) { // Paper synchronized (this.posLock) { // Paper
this.position = new Vec3(x, y, z); this.position = new Vec3(x, y, z);
} // Paper } // 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 // Paper start - never allow AABB to become desynced from position
// hanging has its own special logic // hanging has its own special logic
@ -19049,7 +19058,7 @@ index 4705d7066207250c03a5f98eef61554c901f2e35..580e75595d7fc8c5af5a0281b77f7be8
this.setBoundingBox(this.makeBoundingBox()); this.setBoundingBox(this.makeBoundingBox());
} }
// Paper end // 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; return this.removalReason != null;
} }
@ -19062,7 +19071,7 @@ index 4705d7066207250c03a5f98eef61554c901f2e35..580e75595d7fc8c5af5a0281b77f7be8
@Nullable @Nullable
public Entity.RemovalReason getRemovalReason() { public Entity.RemovalReason getRemovalReason() {
return this.removalReason; 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) 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); this.levelCallback.onRemove(reason);
@ -24496,7 +24505,7 @@ index cd4ad8261e56365850068db1d83d6a8454026737..78f7e72f2912dae503c2dab7d1992b65
List<String> offers = waitable.get(); List<String> offers = waitable.get();
if (offers == null) { 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 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 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
+++ b/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 { @@ -203,6 +203,16 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {
@ -24537,7 +24546,7 @@ index 57a0dbb23a32123d30c3b3572f4d129be9d97847..2284570959dbaf9941e85caf723f06a0
if (entityTracker == null) { if (entityTracker == null) {
return; 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"); Preconditions.checkArgument(location != null, "location");
location.checkFinite(); location.checkFinite();
Location locationClone = location.clone(); // clone so we don't need to worry about mutations after this call. 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); - net.minecraft.server.MinecraftServer.LOGGER.error("Failed to teleport entity " + CraftEntity.this, throwable);
- ret.completeExceptionally(throwable); - ret.completeExceptionally(throwable);
+ java.util.function.Consumer<Entity> run = (Entity nmsEntity) -> { + java.util.function.Consumer<Entity> run = (Entity nmsEntity) -> {
+ // preserve behavior with old API: dismount the entity so it can teleport
+ nmsEntity.unRide();
+
+ boolean success = nmsEntity.teleportAsync( + boolean success = nmsEntity.teleportAsync(
+ ((CraftWorld)locationClone.getWorld()).getHandle(), + ((CraftWorld)locationClone.getWorld()).getHandle(),
+ new net.minecraft.world.phys.Vec3(locationClone.getX(), locationClone.getY(), locationClone.getZ()), + new net.minecraft.world.phys.Vec3(locationClone.getX(), locationClone.getY(), locationClone.getZ()),
+ null, null, net.minecraft.world.phys.Vec3.ZERO, + null, null, net.minecraft.world.phys.Vec3.ZERO,
+ cause == null ? TeleportCause.UNKNOWN : cause, + 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) -> { + (Entity entityTp) -> {
+ ret.complete(Boolean.TRUE); + ret.complete(Boolean.TRUE);
} }

View File

@ -51,7 +51,7 @@ index d9687722e02dfd4088c7030abbf5008eb0a092c8..62484ebf4550b05182f693a3180bbac5
TickThread.ensureTickThread(thisEntity, "May not tick entity scheduler asynchronously"); TickThread.ensureTickThread(thisEntity, "May not tick entity scheduler asynchronously");
final List<ScheduledTask> toRun; final List<ScheduledTask> toRun;
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java 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 --- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/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 { @@ -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 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 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
+++ b/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 { @@ -827,7 +827,7 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {