From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: "Sofiane H. Djerbi" <46628754+kugge@users.noreply.github.com> Date: Sat, 8 Jul 2023 03:31:04 +0300 Subject: [PATCH] Teleport async if we cannot move entity off-main Entities with huge velocity (100k+ velocity anarchy travel exploit) might disappear / crash the server because they travel a region each tick. TODO: Entities with huge velocity still throw stacktraces because they are ticked in "null" regions. diff --git a/src/main/java/dev/kaiijumc/kaiiju/KaiijuWorldConfig.java b/src/main/java/dev/kaiijumc/kaiiju/KaiijuWorldConfig.java index e2fb7d7a7b3126d386b46442c115085d1974ac4e..44f5540a6a5733cf6f10f6b04fc9611ac4e53685 100644 --- a/src/main/java/dev/kaiijumc/kaiiju/KaiijuWorldConfig.java +++ b/src/main/java/dev/kaiijumc/kaiiju/KaiijuWorldConfig.java @@ -169,6 +169,7 @@ public class KaiijuWorldConfig { public boolean fixTripWireStateInconsistency = true; public boolean safeTeleporting = true; public boolean sandDuplication = false; + public boolean teleportAsyncOnHighVelocity = false; private void gameplaySettings() { fixVoidTrading = getBoolean("gameplay.fix-void-trading", fixVoidTrading); @@ -176,5 +177,6 @@ public class KaiijuWorldConfig { fixTripWireStateInconsistency = getBoolean("gameplay.fix-tripwire-state-inconsistency", fixTripWireStateInconsistency); safeTeleporting = getBoolean("gameplay.safe-teleportation", safeTeleporting); sandDuplication = getBoolean("gameplay.sand-duplication", sandDuplication); + teleportAsyncOnHighVelocity = getBoolean("gameplay.teleport-async-on-high-velocity", teleportAsyncOnHighVelocity); } } diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java index 135573308662845ecc73fde1c620345e1f372538..812b2c216003b8decedef1353acab1b44d146e08 100644 --- a/src/main/java/net/minecraft/world/entity/Entity.java +++ b/src/main/java/net/minecraft/world/entity/Entity.java @@ -1126,7 +1126,20 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource { } } + try { // Kaiiju - Teleport async if we cannot move entity off-main this.setPos(this.getX() + vec3d1.x, this.getY() + vec3d1.y, this.getZ() + vec3d1.z); + // Kaiiju start - Teleport async if we cannot move entity off-main + } catch (IllegalStateException e) { + if (this.level().kaiijuConfig.teleportAsyncOnHighVelocity) + this.teleportAsync((ServerLevel) this.level(), this.position().add(vec3d1), + this.getYRot(), this.getXRot(), + null, PlayerTeleportEvent.TeleportCause.UNKNOWN, + Entity.TELEPORT_FLAG_LOAD_CHUNK | Entity.TELEPORT_FLAG_TELEPORT_PASSENGERS, + null + ); + else LOGGER.error("High velocity entity caused off-main setPos: ", e); + } + // Kaiiju end } this.level().getProfiler().pop(); @@ -3868,13 +3881,15 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource { // check for same region if (destination == this.level()) { Vec3 currPos = this.position(); - if ( - destination.regioniser.getRegionAtUnsynchronised( - io.papermc.paper.util.CoordinateUtils.getChunkX(currPos), io.papermc.paper.util.CoordinateUtils.getChunkZ(currPos) - ) == destination.regioniser.getRegionAtUnsynchronised( - io.papermc.paper.util.CoordinateUtils.getChunkX(pos), io.papermc.paper.util.CoordinateUtils.getChunkZ(pos) - ) - ) { + // Kaiiju start - We shouldn't teleport when regions are null + io.papermc.paper.threadedregions.ThreadedRegionizer.ThreadedRegion + currRegion = destination.regioniser.getRegionAtUnsynchronised(io.papermc.paper.util.CoordinateUtils.getChunkX(currPos), io.papermc.paper.util.CoordinateUtils.getChunkZ(currPos)); + io.papermc.paper.threadedregions.ThreadedRegionizer.ThreadedRegion + destRegion = destination.regioniser.getRegionAtUnsynchronised(io.papermc.paper.util.CoordinateUtils.getChunkX(pos), io.papermc.paper.util.CoordinateUtils.getChunkZ(pos)); + if (currRegion == destRegion && currRegion != null) { + // Kaiiju end EntityTreeNode passengerTree = this.detachPassengers(); // Note: The client does not accept position updates for controlled entities. So, we must // perform a lot of tracker updates here to make it all work out.