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 f32d118e1e925b2155cef3fcdcb1e194c541e242..2d5d4170e1f45ae9113c37623b4066861dc79d64 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); } + // Luminol start - Portal rate limiter + into.portalRateThrottler.mergeWith(from.portalRateThrottler); + from.portalRateThrottler.destroy(); + // Luminol end } @Override @@ -322,6 +326,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 } }; @@ -343,6 +353,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 f0a3fdaf7e9d342b472b3db42f5b59b0f6691b49..ac49bcc53951c025de0160fd8afd319b1f3d1347 100644 --- a/net/minecraft/server/MinecraftServer.java +++ b/net/minecraft/server/MinecraftServer.java @@ -1973,6 +1973,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 @@ -1987,6 +1990,9 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop