Skip to content

Commit 3cf908f

Browse files
committed
Make Entity#teleportAsync respect load chunk flag in same region
This avoids sync loading chunks in the same region. Additionally, use a larger radius for checking same-region teleports.
1 parent 834b549 commit 3cf908f

File tree

1 file changed

+37
-44
lines changed

1 file changed

+37
-44
lines changed

folia-server/minecraft-patches/features/0001-Region-Threading-Base.patch

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14212,7 +14212,7 @@ index 6471ff779dd1672db769538bad1d00c8c5724be3..956782df0a3a5e221422c072d7c1c67e
1421214212
return blockToFallLocation(blockState);
1421314213
} else {
1421414214
diff --git a/net/minecraft/world/entity/Entity.java b/net/minecraft/world/entity/Entity.java
14215-
index 21d0de44d9055a5e80c551402abdb7e352dcb0c9..d150493ac4414f0d68d351ab2a4d68c3431e0171 100644
14215+
index 21d0de44d9055a5e80c551402abdb7e352dcb0c9..3df00628e64f55f86095f4b7d35a116515e49628 100644
1421614216
--- a/net/minecraft/world/entity/Entity.java
1421714217
+++ b/net/minecraft/world/entity/Entity.java
1421814218
@@ -160,7 +160,7 @@ public abstract class Entity implements SyncedDataHolder, DebugValueSource, Name
@@ -14419,7 +14419,7 @@ index 21d0de44d9055a5e80c551402abdb7e352dcb0c9..d150493ac4414f0d68d351ab2a4d68c3
1441914419
if (!this.level().paperConfig().scoreboards.allowNonPlayerEntitiesOnScoreboards && !(this instanceof Player)) { return null; } // Paper - Perf: Disable Scoreboards for non players by default
1442014420
return this.level().getScoreboard().getPlayersTeam(this.getScoreboardName());
1442114421
}
14422-
@@ -3992,7 +4020,794 @@ public abstract class Entity implements SyncedDataHolder, DebugValueSource, Name
14422+
@@ -3992,7 +4020,787 @@ public abstract class Entity implements SyncedDataHolder, DebugValueSource, Name
1442314423
this.portalProcess = entity.portalProcess;
1442414424
}
1442514425

@@ -14803,47 +14803,40 @@ index 21d0de44d9055a5e80c551402abdb7e352dcb0c9..d150493ac4414f0d68d351ab2a4d68c3
1480314803
+ // TODO any events that can modify go HERE
1480414804
+
1480514805
+ // check for same region
14806-
+ if (destination == this.level()) {
14807-
+ Vec3 currPos = this.position();
14808-
+ if (
14809-
+ destination.regioniser.getRegionAtUnsynchronised(
14810-
+ ca.spottedleaf.moonrise.common.util.CoordinateUtils.getChunkX(currPos), ca.spottedleaf.moonrise.common.util.CoordinateUtils.getChunkZ(currPos)
14811-
+ ) == destination.regioniser.getRegionAtUnsynchronised(
14812-
+ ca.spottedleaf.moonrise.common.util.CoordinateUtils.getChunkX(pos), ca.spottedleaf.moonrise.common.util.CoordinateUtils.getChunkZ(pos)
14813-
+ )
14814-
+ ) {
14815-
+ boolean hasPassengers = !this.passengers.isEmpty();
14816-
+ EntityTreeNode passengerTree = this.detachPassengers();
14806+
+ if (destination == this.level()
14807+
+ && ca.spottedleaf.moonrise.common.util.TickThread.isTickThreadFor(destination, ca.spottedleaf.moonrise.common.util.CoordinateUtils.getChunkX(pos), ca.spottedleaf.moonrise.common.util.CoordinateUtils.getChunkZ(pos), 8)
14808+
+ && (((teleportFlags & TELEPORT_FLAG_LOAD_CHUNK) == 0L) || destination.areChunksLoadedForMove(this.getBoundingBoxAt(pos.x, pos.y, pos.z)))) {
14809+
+ boolean hasPassengers = !this.passengers.isEmpty();
14810+
+ EntityTreeNode passengerTree = this.detachPassengers();
1481714811
+
14818-
+ if (hasPassengers) {
14819-
+ // Note: The client does not accept position updates for controlled entities. So, we must
14820-
+ // perform a lot of tracker updates here to make it all work out.
14812+
+ if (hasPassengers) {
14813+
+ // Note: The client does not accept position updates for controlled entities. So, we must
14814+
+ // perform a lot of tracker updates here to make it all work out.
1482114815
+
14822-
+ // first, clear the tracker
14823-
+ passengerTree.clearTracker();
14824-
+ }
14816+
+ // first, clear the tracker
14817+
+ passengerTree.clearTracker();
14818+
+ }
1482514819
+
14826-
+ for (EntityTreeNode entity : passengerTree.getFullTree()) {
14827-
+ entity.root.teleportSyncSameRegion(pos, yaw, pitch, velocity);
14828-
+ }
14820+
+ for (EntityTreeNode entity : passengerTree.getFullTree()) {
14821+
+ entity.root.teleportSyncSameRegion(pos, yaw, pitch, velocity);
14822+
+ }
1482914823
+
14830-
+ if (hasPassengers) {
14831-
+ passengerTree.restore();
14832-
+ // re-add to the tracker once the tree is restored
14833-
+ passengerTree.addTracker();
14824+
+ if (hasPassengers) {
14825+
+ passengerTree.restore();
14826+
+ // re-add to the tracker once the tree is restored
14827+
+ passengerTree.addTracker();
1483414828
+
14835-
+ // adjust entities to final position
14836-
+ passengerTree.adjustRiders(true);
14829+
+ // adjust entities to final position
14830+
+ passengerTree.adjustRiders(true);
1483714831
+
14838-
+ // the tracker clear/add logic is only used in the same region, as the other logic
14839-
+ // performs add/remove from world logic which will also perform add/remove tracker logic
14840-
+ }
14832+
+ // the tracker clear/add logic is only used in the same region, as the other logic
14833+
+ // performs add/remove from world logic which will also perform add/remove tracker logic
14834+
+ }
1484114835
+
14842-
+ if (teleportComplete != null) {
14843-
+ teleportComplete.accept(this);
14844-
+ }
14845-
+ return true;
14836+
+ if (teleportComplete != null) {
14837+
+ teleportComplete.accept(this);
1484614838
+ }
14839+
+ return true;
1484714840
+ }
1484814841
+
1484914842
+ EntityTreeNode passengerTree = this.detachPassengers();
@@ -15214,7 +15207,7 @@ index 21d0de44d9055a5e80c551402abdb7e352dcb0c9..d150493ac4414f0d68d351ab2a4d68c3
1521415207
// Paper start - Fix item duplication and teleport issues
1521515208
if ((!this.isAlive() || !this.valid) && (teleportTransition.newLevel() != this.level)) {
1521615209
LOGGER.warn("Illegal Entity Teleport {} to {}:{}", this, teleportTransition.newLevel(), teleportTransition.position(), new Throwable());
15217-
@@ -4197,6 +5012,12 @@ public abstract class Entity implements SyncedDataHolder, DebugValueSource, Name
15210+
@@ -4197,6 +5005,12 @@ public abstract class Entity implements SyncedDataHolder, DebugValueSource, Name
1521815211
}
1521915212
}
1522015213

@@ -15227,7 +15220,7 @@ index 21d0de44d9055a5e80c551402abdb7e352dcb0c9..d150493ac4414f0d68d351ab2a4d68c3
1522715220
protected void removeAfterChangingDimensions() {
1522815221
this.setRemoved(Entity.RemovalReason.CHANGED_DIMENSION, null); // CraftBukkit - add Bukkit remove cause
1522915222
if (this instanceof Leashable leashable && leashable.isLeashed()) { // Paper - only call if it is leashed
15230-
@@ -4497,6 +5318,12 @@ public abstract class Entity implements SyncedDataHolder, DebugValueSource, Name
15223+
@@ -4497,6 +5311,12 @@ public abstract class Entity implements SyncedDataHolder, DebugValueSource, Name
1523115224
}
1523215225

1523315226
public void startSeenByPlayer(ServerPlayer player) {
@@ -15240,7 +15233,7 @@ index 21d0de44d9055a5e80c551402abdb7e352dcb0c9..d150493ac4414f0d68d351ab2a4d68c3
1524015233
}
1524115234

1524215235
public void stopSeenByPlayer(ServerPlayer player) {
15243-
@@ -4506,6 +5333,12 @@ public abstract class Entity implements SyncedDataHolder, DebugValueSource, Name
15236+
@@ -4506,6 +5326,12 @@ public abstract class Entity implements SyncedDataHolder, DebugValueSource, Name
1524415237
new io.papermc.paper.event.player.PlayerUntrackEntityEvent(player.getBukkitEntity(), this.getBukkitEntity()).callEvent();
1524515238
}
1524615239
// Paper end - entity tracking events
@@ -15253,7 +15246,7 @@ index 21d0de44d9055a5e80c551402abdb7e352dcb0c9..d150493ac4414f0d68d351ab2a4d68c3
1525315246
}
1525415247

1525515248
public float rotate(Rotation transformRotation) {
15256-
@@ -5032,7 +5865,8 @@ public abstract class Entity implements SyncedDataHolder, DebugValueSource, Name
15249+
@@ -5032,7 +5858,8 @@ public abstract class Entity implements SyncedDataHolder, DebugValueSource, Name
1525715250
return;
1525815251
}
1525915252
// Paper end - Block invalid positions and bounding box
@@ -15263,7 +15256,7 @@ index 21d0de44d9055a5e80c551402abdb7e352dcb0c9..d150493ac4414f0d68d351ab2a4d68c3
1526315256
synchronized (this.posLock) { // Paper - detailed watchdog information
1526415257
this.position = new Vec3(x, y, z);
1526515258
} // Paper - detailed watchdog information
15266-
@@ -5065,7 +5899,7 @@ public abstract class Entity implements SyncedDataHolder, DebugValueSource, Name
15259+
@@ -5065,7 +5892,7 @@ public abstract class Entity implements SyncedDataHolder, DebugValueSource, Name
1526715260
}
1526815261
// Paper start - Block invalid positions and bounding box; don't allow desync of pos and AABB
1526915262
// hanging has its own special logic
@@ -15272,7 +15265,7 @@ index 21d0de44d9055a5e80c551402abdb7e352dcb0c9..d150493ac4414f0d68d351ab2a4d68c3
1527215265
this.setBoundingBox(this.makeBoundingBox());
1527315266
}
1527415267
// Paper end - Block invalid positions and bounding box
15275-
@@ -5171,6 +6005,12 @@ public abstract class Entity implements SyncedDataHolder, DebugValueSource, Name
15268+
@@ -5171,6 +5998,12 @@ public abstract class Entity implements SyncedDataHolder, DebugValueSource, Name
1527615269
return this.removalReason != null;
1527715270
}
1527815271

@@ -15285,7 +15278,7 @@ index 21d0de44d9055a5e80c551402abdb7e352dcb0c9..d150493ac4414f0d68d351ab2a4d68c3
1528515278
public Entity.@Nullable RemovalReason getRemovalReason() {
1528615279
return this.removalReason;
1528715280
}
15288-
@@ -5185,6 +6025,9 @@ public abstract class Entity implements SyncedDataHolder, DebugValueSource, Name
15281+
@@ -5185,6 +6018,9 @@ public abstract class Entity implements SyncedDataHolder, DebugValueSource, Name
1528915282
// Paper end - rewrite chunk system
1529015283
org.bukkit.craftbukkit.event.CraftEventFactory.callEntityRemoveEvent(this, cause); // CraftBukkit
1529115284
final boolean alreadyRemoved = this.removalReason != null; // Paper - Folia schedulers
@@ -15295,7 +15288,7 @@ index 21d0de44d9055a5e80c551402abdb7e352dcb0c9..d150493ac4414f0d68d351ab2a4d68c3
1529515288
if (this.removalReason == null) {
1529615289
this.removalReason = removalReason;
1529715290
}
15298-
@@ -5208,6 +6051,10 @@ public abstract class Entity implements SyncedDataHolder, DebugValueSource, Name
15291+
@@ -5208,6 +6044,10 @@ public abstract class Entity implements SyncedDataHolder, DebugValueSource, Name
1529915292
this.removalReason = null;
1530015293
}
1530115294

@@ -15306,7 +15299,7 @@ index 21d0de44d9055a5e80c551402abdb7e352dcb0c9..d150493ac4414f0d68d351ab2a4d68c3
1530615299
// Paper start - Folia schedulers
1530715300
/**
1530815301
* Invoked only when the entity is truly removed from the server, never to be added to any world.
15309-
@@ -5219,7 +6066,7 @@ public abstract class Entity implements SyncedDataHolder, DebugValueSource, Name
15302+
@@ -5219,7 +6059,7 @@ public abstract class Entity implements SyncedDataHolder, DebugValueSource, Name
1531015303
// Paper end - Folia schedulers
1531115304
// Paper start - optimise Folia entity scheduler
1531215305
public final void registerScheduler() {

0 commit comments

Comments
 (0)