From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Bacteriawa Date: Tue, 21 Apr 2026 02:27:38 +0800 Subject: [PATCH] Add Portal rate limiter diff --git a/io/papermc/paper/threadedregions/RegionizedWorldData.java b/io/papermc/paper/threadedregions/RegionizedWorldData.java index 981458f6697a79dbc9fc8cf41c088c98e556c6dd..4dbbe03d949811bf6140a35faa5e8cee8f7e28f0 100644 --- a/io/papermc/paper/threadedregions/RegionizedWorldData.java +++ b/io/papermc/paper/threadedregions/RegionizedWorldData.java @@ -154,6 +154,10 @@ public final class RegionizedWorldData { for (final ChunkHolder chunkHolder : from.chunkHoldersToBroadcast) { into.chunkHoldersToBroadcast.add(chunkHolder); } + // Luminol start - Portal rate limiter + into.portalRateThrottler.mergeWith(from.portalRateThrottler); + from.portalRateThrottler.destroy(); + // Luminol end } @Override @@ -316,6 +320,12 @@ public final class RegionizedWorldData { into.chunkHoldersToBroadcast.add(chunkHolder); } } + // Luminol start - Portal rate limiter + for (var worldData : dataSet) { + from.portalRateThrottler.splitInto(worldData.portalRateThrottler); + } + from.portalRateThrottler.destroy(); + // Luminol end } }; @@ -338,6 +348,35 @@ public final class RegionizedWorldData { return this.isHandlingTick; } + // Luminol start - Portal rate limiter + public final me.earthme.luminol.utils.RateThrottler portalRateThrottler = new me.earthme.luminol.utils.RateThrottler(); + public final net.objecthunter.exp4j.Expression portalRateCapExpression = me.earthme.luminol.config.modules.function.PortalRateLimiterConfig.getExpressionIfConfigured(); + + private int computePortalRateCap() { + // expression mode is disabled + if (this.portalRateCapExpression == null) { + return me.earthme.luminol.config.modules.function.PortalRateLimiterConfig.maxPortalTeleportsPerTick; + } + + final int tickingEntityCount = this.entityTickList.size(); + final int tickingChunkCount = this.tickingChunks.size(); + final int playerCount = this.localPlayers.size(); + + return me.earthme.luminol.config.modules.function.PortalRateLimiterConfig.computeExpression( + this.portalRateCapExpression, + tickingEntityCount, + tickingChunkCount, + playerCount + ); + } + public boolean isPortalTeleportationOutOfRate() { + if (!me.earthme.luminol.config.modules.function.PortalRateLimiterConfig.enabled) { + return false; + } + + return this.portalRateThrottler.isOutOfRate(this.computePortalRateCap()); + } + // Luminol 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 ff6809944c2dd8a2cc7c11a8b05b0f7c17b5fb4c..fe1b70edb24e2ded42f8ca1f5ddfbae7151b147a 100644 --- a/net/minecraft/server/MinecraftServer.java +++ b/net/minecraft/server/MinecraftServer.java @@ -1978,6 +1978,9 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop level + " " + level.dimension().identifier()); profiler.push("tick"); + // Luminol start - Portal rate limiter + regionizedWorldData.portalRateThrottler.begin(); + // Luminol end try { foliaProfiler.startTimer(level.tickTimerId); try { // Folia - profiler @@ -1992,6 +1995,9 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop