Skip to content

Commit 817550c

Browse files
Add API to allow/disallow tick sleeping (#11611)
1 parent 8dc42fa commit 817550c

2 files changed

Lines changed: 92 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2+
From: Abel <abelvanhulst@gmail.com>
3+
Date: Tue, 12 Nov 2024 22:25:35 +0100
4+
Subject: [PATCH] API to allow/disallow tick sleeping
5+
6+
7+
diff --git a/src/main/java/org/bukkit/Server.java b/src/main/java/org/bukkit/Server.java
8+
index ba366576a571214e67bcc529dc1bca19e1d59ef8..f55638eb8b315864052f9fe17ab4846e5e9d8dbb 100644
9+
--- a/src/main/java/org/bukkit/Server.java
10+
+++ b/src/main/java/org/bukkit/Server.java
11+
@@ -2578,5 +2578,14 @@ public interface Server extends PluginMessageRecipient, net.kyori.adventure.audi
12+
* Returns whether the server is sleeping/paused.
13+
*/
14+
boolean isPaused();
15+
+
16+
+ /**
17+
+ * Allows or disallows the server to sleep/pause.
18+
+ * If any plugin disallows pausing, the server will never pause.
19+
+ *
20+
+ * @param plugin The {@link org.bukkit.plugin.Plugin} that's allowing or disallowing pausing.
21+
+ * @param value Whether to allow sleeping of the server (defaults to true).
22+
+ */
23+
+ void allowPausing(@NotNull org.bukkit.plugin.Plugin plugin, boolean value);
24+
// Paper end - API to check if the server is sleeping
25+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2+
From: Abel <abelvanhulst@gmail.com>
3+
Date: Tue, 12 Nov 2024 22:25:20 +0100
4+
Subject: [PATCH] API to allow/disallow tick sleeping
5+
6+
7+
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
8+
index 5d070f036dae6d93f863c55192b557419634456d..663b4ecd520e82aa108d44f2d5c2a20cfc7bc01f 100644
9+
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
10+
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
11+
@@ -332,6 +332,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
12+
public volatile Thread shutdownThread; // Paper
13+
public volatile boolean abnormalExit = false; // Paper
14+
public static final long SERVER_INIT = System.nanoTime(); // Paper - Lag compensation
15+
+ private List<String> pluginsBlockingSleep = new ArrayList<>(); // Paper - API to allow/disallow tick sleeping
16+
17+
public static <S extends MinecraftServer> S spin(Function<Thread, S> serverFactory) {
18+
AtomicReference<S> atomicreference = new AtomicReference();
19+
@@ -1623,8 +1624,9 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
20+
long i = Util.getNanos();
21+
int j = this.pauseWhileEmptySeconds() * 20;
22+
23+
+ this.removeDisabledPluginsBlockingSleep(); // Paper - API to allow/disallow tick sleeping
24+
if (j > 0) {
25+
- if (this.playerList.getPlayerCount() == 0 && !this.tickRateManager.isSprinting()) {
26+
+ if (this.playerList.getPlayerCount() == 0 && !this.tickRateManager.isSprinting() && this.pluginsBlockingSleep.isEmpty()) { // Paper - API to allow/disallow tick sleeping
27+
++this.emptyTicks;
28+
} else {
29+
this.emptyTicks = 0;
30+
@@ -3191,5 +3193,22 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
31+
public boolean isTickPaused() {
32+
return this.emptyTicks > 0 && this.emptyTicks >= this.pauseWhileEmptySeconds() * 20;
33+
}
34+
+
35+
+ public void addPluginAllowingSleep(final String pluginName, final boolean value) {
36+
+ if (!value) {
37+
+ this.pluginsBlockingSleep.add(pluginName);
38+
+ } else {
39+
+ this.pluginsBlockingSleep.remove(pluginName);
40+
+ }
41+
+ }
42+
+
43+
+ private void removeDisabledPluginsBlockingSleep() {
44+
+ if (this.pluginsBlockingSleep.isEmpty()) {
45+
+ return;
46+
+ }
47+
+ this.pluginsBlockingSleep.removeIf(plugin -> (
48+
+ !io.papermc.paper.plugin.manager.PaperPluginManagerImpl.getInstance().isPluginEnabled(plugin)
49+
+ ));
50+
+ }
51+
// Paper end - API to check if the server is sleeping
52+
}
53+
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
54+
index 605662917a7720a6c5134fd1d93aa2d26116b76d..cac8592e3a2f438fe9ca167a4fdcd65152bbb2de 100644
55+
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
56+
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
57+
@@ -3252,5 +3252,10 @@ public final class CraftServer implements Server {
58+
public boolean isPaused() {
59+
return this.console.isTickPaused();
60+
}
61+
+
62+
+ @Override
63+
+ public void allowPausing(final Plugin plugin, final boolean value) {
64+
+ this.console.addPluginAllowingSleep(plugin.getName(), value);
65+
+ }
66+
// Paper end - API to check if the server is sleeping
67+
}

0 commit comments

Comments
 (0)