131 lines
6.4 KiB
Diff
131 lines
6.4 KiB
Diff
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
|
|
index f32d118e1e925b2155cef3fcdcb1e194c541e242..eef3f6b5d13d8a5ba3d66f5fe297b431cff28f58 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);
|
|
}
|
|
+ // Shiroha start - Add portal rate limiter
|
|
+ into.portalRateThrottler.mergeWith(from.portalRateThrottler);
|
|
+ from.portalRateThrottler.destroy();
|
|
+ // Shiroha end
|
|
}
|
|
|
|
@Override
|
|
@@ -322,6 +326,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
|
|
}
|
|
};
|
|
|
|
@@ -343,6 +353,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<ServerPlayer> localPlayers = new CopyOnWriteArrayList<>();
|
|
diff --git a/net/minecraft/server/MinecraftServer.java b/net/minecraft/server/MinecraftServer.java
|
|
index ddc34c954d7cd941d41f26ca00209509d1d96159..a3ff96cc86bd68dddabd4c49d990e1dce304268f 100644
|
|
--- a/net/minecraft/server/MinecraftServer.java
|
|
+++ b/net/minecraft/server/MinecraftServer.java
|
|
@@ -1922,6 +1922,9 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
|
|
//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");
|
|
+ // Shiroha start - Add portal rate limiter
|
|
+ regionizedWorldData.portalRateThrottler.begin();
|
|
+ // Shiroha end
|
|
|
|
try {
|
|
foliaProfiler.startTimer(level.tickTimerId); try { // Folia - profiler
|
|
@@ -1936,6 +1939,9 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
|
|
profiler.pop();
|
|
profiler.pop();
|
|
regionizedWorldData.explosionDensityCache.clear(); // Paper - Optimize explosions // Folia - region threading
|
|
+ // Shiroha start - Add portal rate limiter
|
|
+ regionizedWorldData.portalRateThrottler.done();
|
|
+ // Shiroha end
|
|
}
|
|
//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
|
|
index d348fa43e6703dbe461eabbab29699ed24957198..5cbe42636a3fd19d4e9b56fb3200e7d17b81bdad 100644
|
|
--- a/net/minecraft/server/level/ServerLevel.java
|
|
+++ b/net/minecraft/server/level/ServerLevel.java
|
|
@@ -1535,8 +1535,15 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet
|
|
// removed from region while ticking
|
|
return;
|
|
}
|
|
+ // Shiroha start - Add portal rate limiter
|
|
+ var worldData = entity.level().getCurrentWorldData();
|
|
+ if (entity.portalProcess != null && worldData.isPortalTeleportationOutOfRate()) {
|
|
+ return;
|
|
+ }
|
|
+ // Shiroha end
|
|
if (entity.handlePortal()) {
|
|
// portalled
|
|
+ worldData.portalRateThrottler.increase(); // Shiroha - Add portal rate limiter
|
|
return;
|
|
}
|
|
}
|
|
@@ -1580,8 +1587,15 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet
|
|
// removed from region while ticking
|
|
return;
|
|
}
|
|
+ // Shiroha start - Add portal rate limiter
|
|
+ var worldData = entity.level().getCurrentWorldData();
|
|
+ if (entity.portalProcess != null && worldData.isPortalTeleportationOutOfRate()) {
|
|
+ return;
|
|
+ }
|
|
+ // Shiroha end
|
|
if (entity.handlePortal()) {
|
|
// portalled
|
|
+ worldData.portalRateThrottler.increase(); // Shiroha - Add portal rate limiter
|
|
return;
|
|
}
|
|
// Folia end - region threading
|