Skip to content

Commit

Permalink
Update Leaves tick command, add freeze and config
Browse files Browse the repository at this point in the history
  • Loading branch information
s-yh-china committed Aug 20, 2023
1 parent 0796006 commit 1ab2f97
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 39 deletions.
11 changes: 8 additions & 3 deletions patches/server/0004-Leaves-Server-Config-And-Command.patch
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ index a2f71a6d1a9e98133dff6cd0f625da9435a8af14..45897b81a822b9523c16658ca46a5077

new TimingsExport(listeners, parent, history).start();
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index 670f25370554c3f8c40d2fcc4f5a31d7a372f452..18b10f1d1d62e1184a562ef52e1625b4927aec45 100644
index bd87bfcf52da4059b9e345c2d2d7abc725a111a8..c13dccd56b81b88e984e866da199dd8fd7e005e8 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -1118,6 +1118,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
Expand Down Expand Up @@ -128,10 +128,10 @@ index e1c99d941c7bb954bf3ac83d5002dbf58fd833b0..4760b943da08771a42fcb22eba4d586d
.withRequiredArg()
diff --git a/src/main/java/top/leavesmc/leaves/LeavesConfig.java b/src/main/java/top/leavesmc/leaves/LeavesConfig.java
new file mode 100644
index 0000000000000000000000000000000000000000..c5c89964d60f291a4625545bb39f314850945ae6
index 0000000000000000000000000000000000000000..03f2fd1958b8f2c7d97232240f05affcc6f56b7f
--- /dev/null
+++ b/src/main/java/top/leavesmc/leaves/LeavesConfig.java
@@ -0,0 +1,985 @@
@@ -0,0 +1,990 @@
+package top.leavesmc.leaves;
+
+import com.destroystokyo.paper.util.SneakyThrow;
Expand Down Expand Up @@ -942,6 +942,11 @@ index 0000000000000000000000000000000000000000..c5c89964d60f291a4625545bb39f3148
+ }
+ }
+
+ public static boolean tickCommand = false;
+ private static void tickCommand() {
+ tickCommand = getBoolean("settings.modify.tick-command", tickCommand);
+ }
+
+ public static final class WorldConfig {
+
+ public final String worldName;
Expand Down
10 changes: 7 additions & 3 deletions patches/server/0078-Bladeren-mspt-sync-protocol.patch
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ index e50305b51a3759f53a9425ba6487db30c5d5d788..b00a84a73b7682d1579a8b6350a11f32

diff --git a/src/main/java/top/leavesmc/leaves/protocol/bladeren/MsptSyncProtocol.java b/src/main/java/top/leavesmc/leaves/protocol/bladeren/MsptSyncProtocol.java
new file mode 100644
index 0000000000000000000000000000000000000000..0f9062d409267f7ea9df9bac4e4323b497448545
index 0000000000000000000000000000000000000000..f04ea3ad31ac8d9d820599a09c002d4c5db6104d
--- /dev/null
+++ b/src/main/java/top/leavesmc/leaves/protocol/bladeren/MsptSyncProtocol.java
@@ -0,0 +1,63 @@
@@ -0,0 +1,67 @@
+package top.leavesmc.leaves.protocol.bladeren;
+
+import io.netty.buffer.Unpooled;
Expand Down Expand Up @@ -108,7 +108,11 @@ index 0000000000000000000000000000000000000000..0f9062d409267f7ea9df9bac4e4323b4
+
+ public static void tick(MinecraftServer server) {
+ if (LeavesConfig.msptSyncProtocol) {
+ if (server.getTickCount() % LeavesConfig.msptSyncTickInterval == 0 && !players.isEmpty()) {
+ if (players.isEmpty()) {
+ return;
+ }
+
+ if (server.getTickCount() % LeavesConfig.msptSyncTickInterval == 0) {
+ FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
+ double mspt = Arrays.stream(server.tickTimes).average().getAsDouble() * 1.0E-6D;
+ double tps = 1000.0D / Math.max(mspt, 50);
Expand Down
221 changes: 188 additions & 33 deletions patches/server/0118-Leaves-tick-command.patch
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,122 @@ Subject: [PATCH] Leaves tick command


diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index 87bb9de0403339d3f14fa9065d0a0515fad3e767..75a73d5797dd9655b646de00435d750c5fc63f68 100644
index 49fc9c750d7696789649081430b262b084a21022..d3caf75c726a88b6ace1161550057c124e7fdd9c 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -1100,6 +1100,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
@@ -194,6 +194,7 @@ import org.bukkit.event.server.ServerLoadEvent;
// CraftBukkit end

import co.aikar.timings.MinecraftTimings; // Paper
+import top.leavesmc.leaves.util.TickStatus; // Leaves - tick command

public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTask> implements CommandSource, AutoCloseable {

@@ -1100,6 +1101,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
// Spigot End

public static volatile RuntimeException chunkSystemCrash; // Paper - rewrite chunk system
+ public static boolean skipTickWait = false; // Leaves - tick command - warp
+ public static TickStatus tickStatus = TickStatus.NORMAL; // Leaves - tick command

protected void runServer() {
try {
@@ -1179,7 +1180,13 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
@@ -1148,21 +1150,24 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
this.lastOverloadWarning = this.nextTickTime;
}

- ++MinecraftServer.currentTickLong; // Paper
- if ( ++MinecraftServer.currentTick % MinecraftServer.SAMPLE_INTERVAL == 0 )
- {
- final long diff = curTime - tickSection;
- java.math.BigDecimal currentTps = TPS_BASE.divide(new java.math.BigDecimal(diff), 30, java.math.RoundingMode.HALF_UP);
- tps1.add(currentTps, diff);
- tps5.add(currentTps, diff);
- tps15.add(currentTps, diff);
- // Backwards compat with bad plugins
- this.recentTps[0] = tps1.getAverage();
- this.recentTps[1] = tps5.getAverage();
- this.recentTps[2] = tps15.getAverage();
- // Paper end
- tickSection = curTime;
+ // Leaves start - tick command
+ if (tickStatus != TickStatus.FREEZE) {
+ ++MinecraftServer.currentTickLong; // Paper
+ if (++MinecraftServer.currentTick % MinecraftServer.SAMPLE_INTERVAL == 0) {
+ final long diff = curTime - tickSection;
+ java.math.BigDecimal currentTps = TPS_BASE.divide(new java.math.BigDecimal(diff), 30, java.math.RoundingMode.HALF_UP);
+ tps1.add(currentTps, diff);
+ tps5.add(currentTps, diff);
+ tps15.add(currentTps, diff);
+ // Backwards compat with bad plugins
+ this.recentTps[0] = tps1.getAverage();
+ this.recentTps[1] = tps5.getAverage();
+ this.recentTps[2] = tps15.getAverage();
+ // Paper end
+ tickSection = curTime;
+ }
}
+ // Leaves end - tick command
// Spigot end

if (this.debugCommandProfilerDelayStart) {
@@ -1175,11 +1180,23 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
this.nextTickTime += 50L;
this.startMetricsRecordingTick();
this.profiler.push("tick");
- this.tickServer(this::haveTime);
+ // Leaves start - tick command
+ if (tickStatus == TickStatus.FREEZE) {
+ this.tickFreeze();
+ } else {
+ this.tickServer(this::haveTime);
+ }
+ // Leaves end - tick command
this.profiler.popPush("nextTickWait");
this.mayHaveDelayedTasks = true;
this.delayedTasksMaxNextTickTime = Math.max(Util.getMillis() + 50L, this.nextTickTime);
- this.waitUntilNextTick();
+ // Leaves - tick command - warp
+ if (skipTickWait) {
+ // Leaves start - tick command
+ if (tickStatus == TickStatus.WARP) {
+ this.nextTickTime = Util.getMillis();
+ } else {
+ this.waitUntilNextTick();
+ }
+ // Leaves - tick command - warp
+ // Leaves end - tick command
this.profiler.pop();
this.endMetricsRecordingTick();
this.isReady = true;
@@ -1236,6 +1253,18 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa

}

+ // Leaves start - tick command - freeze
+ private void tickFreeze() {
+ this.getConnection().tick();
+ top.leavesmc.leaves.protocol.bladeren.MsptSyncProtocol.tick(this);
+ this.runAllTasks();
+ while (!this.processQueue.isEmpty()) {
+ this.processQueue.remove().run();
+ }
+ org.spigotmc.WatchdogThread.tick();
+ }
+ // Leaves end - tick command - freeze
+
private static CrashReport constructOrExtractCrashReport(Throwable throwable) {
ReportedException reportedexception = null;

diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java
index a5d306e9e8081480275ab6a59b4c0681976913df..c5f5cbf8fe88c42993dbcea1d9514d407024348c 100644
--- a/src/main/java/net/minecraft/server/level/ServerPlayer.java
+++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java
@@ -2736,7 +2736,7 @@ public class ServerPlayer extends Player {
}

public boolean allowOperation() {
- return (instaBreakCountPerTick == 0 || placeBlockCountPerTick == 0) && (instaBreakCountPerTick <= 1 && placeBlockCountPerTick <= 2);
+ return MinecraftServer.tickStatus == top.leavesmc.leaves.util.TickStatus.FREEZE || (instaBreakCountPerTick == 0 || placeBlockCountPerTick == 0) && (instaBreakCountPerTick <= 1 && placeBlockCountPerTick <= 2); // Leaves - tick command
}
// Leaves end - player operation limiter
// CraftBukkit end
diff --git a/src/main/java/top/leavesmc/leaves/command/LeavesCommand.java b/src/main/java/top/leavesmc/leaves/command/LeavesCommand.java
index da1f7ce2db23c509c70f673c8bb1c46af2ed7279..0259a1472bba36b5bb903e559a99f791e8a9317a 100644
--- a/src/main/java/top/leavesmc/leaves/command/LeavesCommand.java
Expand All @@ -53,10 +143,10 @@ index da1f7ce2db23c509c70f673c8bb1c46af2ed7279..0259a1472bba36b5bb903e559a99f791
.flatMap(entry -> entry.getKey().stream().map(s -> Map.entry(s, entry.getValue())))
diff --git a/src/main/java/top/leavesmc/leaves/command/subcommands/TickCommand.java b/src/main/java/top/leavesmc/leaves/command/subcommands/TickCommand.java
new file mode 100644
index 0000000000000000000000000000000000000000..a6dcf7b1c5df95d7f8dcccda14674195317f22a8
index 0000000000000000000000000000000000000000..162c3c005130bfa07c7b924015356430b1e1ac2a
--- /dev/null
+++ b/src/main/java/top/leavesmc/leaves/command/subcommands/TickCommand.java
@@ -0,0 +1,67 @@
@@ -0,0 +1,82 @@
+package top.leavesmc.leaves.command.subcommands;
+
+import io.papermc.paper.command.CommandUtil;
Expand All @@ -65,7 +155,9 @@ index 0000000000000000000000000000000000000000..a6dcf7b1c5df95d7f8dcccda14674195
+import net.kyori.adventure.text.format.NamedTextColor;
+import net.minecraft.server.MinecraftServer;
+import org.bukkit.command.CommandSender;
+import top.leavesmc.leaves.LeavesConfig;
+import top.leavesmc.leaves.command.LeavesSubcommand;
+import top.leavesmc.leaves.util.TickStatus;
+
+import java.util.Collections;
+import java.util.List;
Expand All @@ -74,6 +166,10 @@ index 0000000000000000000000000000000000000000..a6dcf7b1c5df95d7f8dcccda14674195
+
+ @Override
+ public boolean execute(CommandSender sender, String subCommand, String[] args) {
+ if (!LeavesConfig.tickCommand) {
+ return true;
+ }
+
+ if (args.length < 1) {
+ sender.sendMessage(Component.join(JoinConfiguration.noSeparators(),
+ Component.text("Tick Status: ", NamedTextColor.GRAY),
Expand All @@ -82,45 +178,104 @@ index 0000000000000000000000000000000000000000..a6dcf7b1c5df95d7f8dcccda14674195
+ return true;
+ }
+
+ TickStatus oldStatus = this.getStatus();
+
+ switch (args[0]) {
+ case "warp" -> {
+ TickStatus oldStatus = this.getStatus();
+ MinecraftServer.skipTickWait = !MinecraftServer.skipTickWait;
+ sender.sendMessage(Component.join(JoinConfiguration.noSeparators(),
+ Component.text("Tick Status: ", NamedTextColor.GRAY),
+ oldStatus.getMessage(),
+ Component.text(" -> ", NamedTextColor.GRAY),
+ this.getStatus().getMessage()
+ ));
+ MinecraftServer.tickStatus = TickStatus.WARP;
+ sender.sendMessage(this.getStatusChangeMessage(oldStatus));
+ }
+
+ case "freeze" -> {
+ MinecraftServer.tickStatus = TickStatus.FREEZE;
+ sender.sendMessage(this.getStatusChangeMessage(oldStatus));
+ }
+
+ case "normal" -> {
+ MinecraftServer.tickStatus = TickStatus.NORMAL;
+ sender.sendMessage(this.getStatusChangeMessage(oldStatus));
+ }
+ }
+ return true;
+ }
+
+ public TickStatus getStatus() {
+ return MinecraftServer.skipTickWait ? TickStatus.WARP : TickStatus.NORMAL;
+ public Component getStatusChangeMessage(TickStatus old) {
+ return Component.join(JoinConfiguration.noSeparators(),
+ Component.text("Tick Status: ", NamedTextColor.GRAY),
+ old.getMessage(),
+ Component.text(" -> ", NamedTextColor.GRAY),
+ this.getStatus().getMessage()
+ );
+ }
+
+ public enum TickStatus {
+ NORMAL(Component.text("normal", NamedTextColor.GRAY)),
+ WARP(Component.text("warp", NamedTextColor.AQUA));
+
+ private final Component message;
+
+ private TickStatus(Component message) {
+ this.message = message;
+ }
+
+ public Component getMessage() {
+ return message;
+ }
+ public TickStatus getStatus() {
+ return MinecraftServer.tickStatus;
+ }
+
+ @Override
+ public List<String> tabComplete(final CommandSender sender, final String subCommand, final String[] args) {
+ if (!LeavesConfig.tickCommand) {
+ return Collections.emptyList();
+ }
+
+ if (args.length == 1) {
+ return CommandUtil.getListMatchingLast(sender, args, "warp");
+ return CommandUtil.getListMatchingLast(sender, args, "normal", "warp", "freeze");
+ }
+ return Collections.emptyList();
+ }
+
+ @Override
+ public boolean tabCompletes() {
+ return LeavesConfig.tickCommand;
+ }
+}
diff --git a/src/main/java/top/leavesmc/leaves/protocol/bladeren/MsptSyncProtocol.java b/src/main/java/top/leavesmc/leaves/protocol/bladeren/MsptSyncProtocol.java
index f04ea3ad31ac8d9d820599a09c002d4c5db6104d..93478c3163910eb190e4fd0aded51ae0e31d6be3 100644
--- a/src/main/java/top/leavesmc/leaves/protocol/bladeren/MsptSyncProtocol.java
+++ b/src/main/java/top/leavesmc/leaves/protocol/bladeren/MsptSyncProtocol.java
@@ -10,6 +10,7 @@ import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import top.leavesmc.leaves.LeavesConfig;
import top.leavesmc.leaves.util.ProtocolUtils;
+import top.leavesmc.leaves.util.TickStatus;

import java.util.ArrayList;
import java.util.Arrays;
@@ -61,6 +62,11 @@ public class MsptSyncProtocol {
buf.writeDouble(mspt);
buf.writeDouble(tps);
players.forEach(player -> ProtocolUtils.sendPayloadPacket(player, MSPT_SYNC, buf));
+ } else if (MinecraftServer.tickStatus == TickStatus.FREEZE) {
+ FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
+ buf.writeDouble(0);
+ buf.writeDouble(0);
+ players.forEach(player -> ProtocolUtils.sendPayloadPacket(player, MSPT_SYNC, buf));
}
}
}
diff --git a/src/main/java/top/leavesmc/leaves/util/TickStatus.java b/src/main/java/top/leavesmc/leaves/util/TickStatus.java
new file mode 100644
index 0000000000000000000000000000000000000000..460307c6d477704417dbfe38364afbc5efe8c767
--- /dev/null
+++ b/src/main/java/top/leavesmc/leaves/util/TickStatus.java
@@ -0,0 +1,20 @@
+package top.leavesmc.leaves.util;
+
+import net.kyori.adventure.text.Component;
+import net.kyori.adventure.text.format.NamedTextColor;
+
+public enum TickStatus {
+ NORMAL(Component.text("normal", NamedTextColor.GRAY)),
+ WARP(Component.text("warp", NamedTextColor.GREEN)),
+ FREEZE(Component.text("freeze", NamedTextColor.AQUA));
+
+ private final Component message;
+
+ private TickStatus(Component message) {
+ this.message = message;
+ }
+
+ public Component getMessage() {
+ return message;
+ }
+}

0 comments on commit 1ab2f97

Please sign in to comment.