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 fbbc05e946b72da1079271810fdb3034510c6280..c04dbcd615c97cb6dcb5f14fec88daf73e6e9b4a 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); } + // Shiroha start - Add portal rate limiter + into.portalRateThrottler.mergeWith(from.portalRateThrottler); + from.portalRateThrottler.destroy(); + // Shiroha end } @Override @@ -316,6 +320,12 @@ public final class RegionizedWorldData { into.chunkHoldersToBroadcast.add(chunkHolder); } } + // Shiroha start - Add portal rate limiter + for (var worldData : dataSet) { + from.portalRateThrottler.splitInto(worldData.portalRateThrottler); + } + from.portalRateThrottler.destroy(); + // Shiroha end } }; @@ -338,6 +348,35 @@ public final class RegionizedWorldData { return this.isHandlingTick; } + // Shiroha start - Add portal rate limiter + 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(); + + private int computePortalRateCap() { + // expression mode is disabled + if (this.portalRateCapExpression == null) { + return io.nanachiyo0721.shiroha.config.modules.function.PortalRateLimiterConfig.maxPortalTeleportsPerTick; + } + + final int tickingEntityCount = this.entityTickList.size(); + final int tickingChunkCount = this.tickingChunks.size(); + final int playerCount = this.localPlayers.size(); + + return io.nanachiyo0721.shiroha.config.modules.function.PortalRateLimiterConfig.computeExpression( + this.portalRateCapExpression, + tickingEntityCount, + tickingChunkCount, + playerCount + ); + } + public boolean isPortalTeleportationOutOfRate() { + if (!io.nanachiyo0721.shiroha.config.modules.function.PortalRateLimiterConfig.enabled) { + return false; + } + + return this.portalRateThrottler.isOutOfRate(this.computePortalRateCap()); + } + // Shiroha 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 28f5c28bdf9a3090b98451e559f807c4364ea259..170ff99a055d7ea582d6a18ef6acd57269d943c4 100644 --- a/net/minecraft/server/MinecraftServer.java +++ b/net/minecraft/server/MinecraftServer.java @@ -1938,6 +1938,9 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop level + " " + level.dimension().identifier()); profiler.push("tick"); + // Shiroha start - Add portal rate limiter + regionizedWorldData.portalRateThrottler.begin(); + // Shiroha end try { foliaProfiler.startTimer(level.tickTimerId); try { // Folia - profiler @@ -1952,6 +1955,9 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop