From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: MrHua269 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 index f32d118e1e925b2155cef3fcdcb1e194c541e242..fe17157c4c6fc9ec745c5c11f6837e715d7949ab 100644 --- a/io/papermc/paper/threadedregions/RegionizedWorldData.java +++ b/io/papermc/paper/threadedregions/RegionizedWorldData.java @@ -157,6 +157,10 @@ public final class RegionizedWorldData { for (final ChunkHolder chunkHolder : from.chunkHoldersToBroadcast) { into.chunkHoldersToBroadcast.add(chunkHolder); } + // Camellia start - Add portal rate limiter + into.portalRateThrottler.mergeWith(from.portalRateThrottler); + from.portalRateThrottler.destroy(); + // Camellia end } @Override @@ -322,6 +326,12 @@ public final class RegionizedWorldData { into.chunkHoldersToBroadcast.add(chunkHolder); } } + // Camellia start - Add portal rate limiter + for (var worldData : dataSet) { + from.portalRateThrottler.splitInto(worldData.portalRateThrottler); + } + from.portalRateThrottler.destroy(); + // Camellia end } }; @@ -343,6 +353,35 @@ public final class RegionizedWorldData { return this.isHandlingTick; } + // Camellia start - Add portal rate limiter + public final moe.monnairealms.camellia.utils.RateThrottler portalRateThrottler = new moe.monnairealms.camellia.utils.RateThrottler(); + public final net.objecthunter.exp4j.Expression portalRateCapExpression = moe.monnairealms.camellia.config.modules.function.PortalRateLimiterConfig.getExpressionIfConfigured(); + + private int computePortalRateCap() { + // expression mode is disabled + if (this.portalRateCapExpression == null) { + return moe.monnairealms.camellia.config.modules.function.PortalRateLimiterConfig.maxPortalTeleportsPerTick; + } + + final int tickingEntityCount = this.entityTickList.size(); + final int tickingChunkCount = this.tickingChunks.size(); + final int playerCount = this.localPlayers.size(); + + return moe.monnairealms.camellia.config.modules.function.PortalRateLimiterConfig.computeExpression( + this.portalRateCapExpression, + tickingEntityCount, + tickingChunkCount, + playerCount + ); + } + public boolean isPortalTeleportationOutOfRate() { + if (!moe.monnairealms.camellia.config.modules.function.PortalRateLimiterConfig.enabled) { + return false; + } + + return this.portalRateThrottler.isOutOfRate(this.computePortalRateCap()); + } + // Camellia end // entities // this is copy on write to allow packet processing to iterate safely private final CopyOnWriteArrayList localPlayers = new CopyOnWriteArrayList<>(); diff --git a/net/minecraft/server/MinecraftServer.java b/net/minecraft/server/MinecraftServer.java index cdb1647146f526dc80b15a09d3a936b5dbc54ba3..4ce5bdd16b04f76c2e98a5465343a82ed5f28ab7 100644 --- a/net/minecraft/server/MinecraftServer.java +++ b/net/minecraft/server/MinecraftServer.java @@ -1922,6 +1922,9 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop level + " " + level.dimension().identifier()); profiler.push("tick"); + // Camellia start - Add portal rate limiter + regionizedWorldData.portalRateThrottler.begin(); + // Camellia end try { foliaProfiler.startTimer(level.tickTimerId); try { // Folia - profiler @@ -1936,6 +1939,9 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop