aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/1066-API-to-allow-disallow-tick-sleeping.patch
blob: 83c055d4a023302a06fa0d906a6be8c709406700 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Abel <abelvanhulst@gmail.com>
Date: Tue, 12 Nov 2024 22:25:20 +0100
Subject: [PATCH] API to allow/disallow tick sleeping


diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index f07661d09f21b8691ef2e09a1e8ddce6ad53b473..3ea668e3b33353d8a6353ebc4f18e7103b573353 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -332,6 +332,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
     public volatile Thread shutdownThread; // Paper
     public volatile boolean abnormalExit = false; // Paper
     public static final long SERVER_INIT = System.nanoTime(); // Paper - Lag compensation
+    private final Set<String> pluginsBlockingSleep = new java.util.HashSet<>(); // Paper - API to allow/disallow tick sleeping
 
     public static <S extends MinecraftServer> S spin(Function<Thread, S> serverFactory) {
         ca.spottedleaf.dataconverter.minecraft.datatypes.MCTypeRegistry.init(); // Paper - rewrite data converter system
@@ -1624,8 +1625,9 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
         long i = Util.getNanos();
         int j = this.pauseWhileEmptySeconds() * 20;
 
+        this.removeDisabledPluginsBlockingSleep(); // Paper - API to allow/disallow tick sleeping
         if (j > 0) {
-            if (this.playerList.getPlayerCount() == 0 && !this.tickRateManager.isSprinting()) {
+            if (this.playerList.getPlayerCount() == 0 && !this.tickRateManager.isSprinting() && this.pluginsBlockingSleep.isEmpty()) { // Paper - API to allow/disallow tick sleeping
                 ++this.emptyTicks;
             } else {
                 this.emptyTicks = 0;
@@ -3192,5 +3194,22 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
     public boolean isTickPaused() {
         return this.emptyTicks > 0 && this.emptyTicks >= this.pauseWhileEmptySeconds() * 20;
     }
+
+    public void addPluginAllowingSleep(final String pluginName, final boolean value) {
+        if (!value) {
+            this.pluginsBlockingSleep.add(pluginName);
+        } else {
+            this.pluginsBlockingSleep.remove(pluginName);
+        }
+    }
+
+    private void removeDisabledPluginsBlockingSleep() {
+        if (this.pluginsBlockingSleep.isEmpty()) {
+            return;
+        }
+        this.pluginsBlockingSleep.removeIf(plugin -> (
+            !io.papermc.paper.plugin.manager.PaperPluginManagerImpl.getInstance().isPluginEnabled(plugin)
+        ));
+    }
     // Paper end - API to check if the server is sleeping
 }
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
index 7afc3d4244c096f78d48338da2eb65c4e834b6f1..ac8af406180bc680d46e8edc3da0fc2e5211345a 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
@@ -3264,5 +3264,10 @@ public final class CraftServer implements Server {
     public boolean isPaused() {
         return this.console.isTickPaused();
     }
+
+    @Override
+    public void allowPausing(final Plugin plugin, final boolean value) {
+        this.console.addPluginAllowingSleep(plugin.getName(), value);
+    }
     // Paper end - API to check if the server is sleeping
 }