2026-07-09 00:03:09 +08:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: MrHua269 <mrhua269@gmail.com>
Date: Thu, 9 Jul 2026 10:17:22 +0800
Subject: [PATCH] Add portal rate limiter
diff --git a/io/papermc/paper/threadedregions/RegionizedWorldData.java b/io/papermc/paper/threadedregions/RegionizedWorldData.java
2026-08-08 11:46:48 +08:00
index fbbc05e946b72da1079271810fdb3034510c6280..c04dbcd615c97cb6dcb5f14fec88daf73e6e9b4a 100644
2026-07-09 00:03:09 +08:00
--- a/io/papermc/paper/threadedregions/RegionizedWorldData.java
+++ b/io/papermc/paper/threadedregions/RegionizedWorldData.java
2026-08-08 11:46:48 +08:00
@@ -154,6 +154,10 @@ public final class RegionizedWorldData {
2026-07-09 00:03:09 +08:00
for (final ChunkHolder chunkHolder : from.chunkHoldersToBroadcast) {
into.chunkHoldersToBroadcast.add(chunkHolder);
}
2026-07-14 11:32:51 +08:00
+ // Shiroha start - Add portal rate limiter
2026-07-09 00:03:09 +08:00
+ into.portalRateThrottler.mergeWith(from.portalRateThrottler);
+ from.portalRateThrottler.destroy();
2026-07-14 11:32:51 +08:00
+ // Shiroha end
2026-07-09 00:03:09 +08:00
}
@Override
2026-08-08 11:46:48 +08:00
@@ -316,6 +320,12 @@ public final class RegionizedWorldData {
2026-07-09 00:03:09 +08:00
into.chunkHoldersToBroadcast.add(chunkHolder);
}
}
2026-07-14 11:32:51 +08:00
+ // Shiroha start - Add portal rate limiter
2026-07-09 00:03:09 +08:00
+ for (var worldData : dataSet) {
+ from.portalRateThrottler.splitInto(worldData.portalRateThrottler);
+ }
+ from.portalRateThrottler.destroy();
2026-07-14 11:32:51 +08:00
+ // Shiroha end
2026-07-09 00:03:09 +08:00
}
};
2026-08-08 11:46:48 +08:00
@@ -338,6 +348,35 @@ public final class RegionizedWorldData {
2026-07-09 00:03:09 +08:00
return this.isHandlingTick;
}
2026-07-14 11:32:51 +08:00
+ // Shiroha start - Add portal rate limiter
2026-07-14 12:22:17 +08:00
+ public final io.nanachiyo0721.shiroha.utils.RateThrottler portalRateThrottler = new io.nanachiyo0721.shiroha.utils.RateThrottler();
+ public final net.objecthunter.exp4j.Expression portalRateCapExpression = io.nanachiyo0721.shiroha.config.modules.function.PortalRateLimiterConfig.getExpressionIfConfigured();
2026-07-09 00:03:09 +08:00
+
+ private int computePortalRateCap() {
+ // expression mode is disabled
+ if (this.portalRateCapExpression == null) {
2026-07-14 12:22:17 +08:00
+ return io.nanachiyo0721.shiroha.config.modules.function.PortalRateLimiterConfig.maxPortalTeleportsPerTick;
2026-07-09 00:03:09 +08:00
+ }
+
+ final int tickingEntityCount = this.entityTickList.size();
+ final int tickingChunkCount = this.tickingChunks.size();
+ final int playerCount = this.localPlayers.size();
+
2026-07-14 12:22:17 +08:00
+ return io.nanachiyo0721.shiroha.config.modules.function.PortalRateLimiterConfig.computeExpression(
2026-07-09 00:03:09 +08:00
+ this.portalRateCapExpression,
+ tickingEntityCount,
+ tickingChunkCount,
+ playerCount
+ );
+ }
+ public boolean isPortalTeleportationOutOfRate() {
2026-07-14 12:22:17 +08:00
+ if (!io.nanachiyo0721.shiroha.config.modules.function.PortalRateLimiterConfig.enabled) {
2026-07-09 00:03:09 +08:00
+ return false;
+ }
+
+ return this.portalRateThrottler.isOutOfRate(this.computePortalRateCap());
+ }
2026-07-14 11:32:51 +08:00
+ // Shiroha end
2026-07-09 00:03:09 +08:00
// entities
// this is copy on write to allow packet processing to iterate safely
private final CopyOnWriteArrayList<ServerPlayer> localPlayers = new CopyOnWriteArrayList<>();
diff --git a/net/minecraft/server/MinecraftServer.java b/net/minecraft/server/MinecraftServer.java
2026-08-08 11:46:48 +08:00
index 28f5c28bdf9a3090b98451e559f807c4364ea259..170ff99a055d7ea582d6a18ef6acd57269d943c4 100644
2026-07-09 00:03:09 +08:00
--- a/net/minecraft/server/MinecraftServer.java
+++ b/net/minecraft/server/MinecraftServer.java
2026-08-08 11:46:48 +08:00
@@ -1938,6 +1938,9 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
2026-07-09 00:03:09 +08:00
//net.minecraft.world.level.block.entity.HopperBlockEntity.skipHopperEvents = level.paperConfig().hopper.disableMoveEvent || org.bukkit.event.inventory.InventoryMoveItemEvent.getHandlerList().getRegisteredListeners().length == 0; // Paper - Perf: Optimize Hoppers // Folia - region threading
profiler.push(() -> level + " " + level.dimension().identifier());
profiler.push("tick");
2026-07-14 11:32:51 +08:00
+ // Shiroha start - Add portal rate limiter
2026-07-09 00:03:09 +08:00
+ regionizedWorldData.portalRateThrottler.begin();
2026-07-14 11:32:51 +08:00
+ // Shiroha end
2026-07-09 00:03:09 +08:00
try {
foliaProfiler.startTimer(level.tickTimerId); try { // Folia - profiler
2026-08-08 11:46:48 +08:00
@@ -1952,6 +1955,9 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
2026-07-09 00:03:09 +08:00
profiler.pop();
profiler.pop();
regionizedWorldData.explosionDensityCache.clear(); // Paper - Optimize explosions // Folia - region threading
2026-07-14 11:32:51 +08:00
+ // Shiroha start - Add portal rate limiter
2026-07-09 00:03:09 +08:00
+ regionizedWorldData.portalRateThrottler.done();
2026-07-14 11:32:51 +08:00
+ // Shiroha end
2026-07-09 00:03:09 +08:00
}
//this.isIteratingOverLevels = false; // Paper - Throw exception on world create while being ticked // Folia - region threading
diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java
2026-08-15 16:14:13 +08:00
index 553395d07ec436280ff1415564ece10f544a37e7..4e1a035f8797196c765f59553ff3a0c9749bacfb 100644
2026-07-09 00:03:09 +08:00
--- a/net/minecraft/server/level/ServerLevel.java
+++ b/net/minecraft/server/level/ServerLevel.java
2026-08-15 16:14:13 +08:00
@@ -1534,8 +1534,15 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet
2026-07-09 00:03:09 +08:00
// removed from region while ticking
return;
}
2026-07-14 11:32:51 +08:00
+ // Shiroha start - Add portal rate limiter
2026-07-09 00:03:09 +08:00
+ var worldData = entity.level().getCurrentWorldData();
+ if (entity.portalProcess != null && worldData.isPortalTeleportationOutOfRate()) {
+ return;
+ }
2026-07-14 11:32:51 +08:00
+ // Shiroha end
2026-07-09 00:03:09 +08:00
if (entity.handlePortal()) {
// portalled
2026-07-14 11:32:51 +08:00
+ worldData.portalRateThrottler.increase(); // Shiroha - Add portal rate limiter
2026-07-09 00:03:09 +08:00
return;
}
}
2026-08-15 16:14:13 +08:00
@@ -1579,8 +1586,15 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet
2026-07-09 00:03:09 +08:00
// removed from region while ticking
return;
}
2026-07-14 11:32:51 +08:00
+ // Shiroha start - Add portal rate limiter
2026-07-09 00:03:09 +08:00
+ var worldData = entity.level().getCurrentWorldData();
+ if (entity.portalProcess != null && worldData.isPortalTeleportationOutOfRate()) {
+ return;
+ }
2026-07-14 11:32:51 +08:00
+ // Shiroha end
2026-07-09 00:03:09 +08:00
if (entity.handlePortal()) {
// portalled
2026-07-14 11:32:51 +08:00
+ worldData.portalRateThrottler.increase(); // Shiroha - Add portal rate limiter
2026-07-09 00:03:09 +08:00
return;
}
// Folia end - region threading