9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-19 15:09:25 +00:00
Files
Leaf/leaf-server/minecraft-patches/features/0218-SparklyPaper-Track-each-world-MSPT.patch
2025-06-25 23:05:25 +09:00

130 lines
6.0 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Taiyou06 <kaandindar21@gmail.com>
Date: Thu, 6 Mar 2025 00:26:19 +0100
Subject: [PATCH] SparklyPaper: Track each world MSPT
Original project: https://github.com/SparklyPower/SparklyPaper
diff --git a/net/minecraft/server/MinecraftServer.java b/net/minecraft/server/MinecraftServer.java
index 940aad1282c48dfeaa906d1c9abfc269fbe05bd7..50da03adea9b496ef3622ee163daf169fd045c2d 100644
--- a/net/minecraft/server/MinecraftServer.java
+++ b/net/minecraft/server/MinecraftServer.java
@@ -1685,7 +1685,16 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
// Leaf start - SparklyPaper - parallel world ticking mod (move level ticking logic out for branch convergence)
private void tickLevel(ServerLevel serverLevel, BooleanSupplier hasTimeLeft) {
try {
+ long i = Util.getNanos(); // SparklyPaper - track world's MSPT
serverLevel.tick(hasTimeLeft);
+ // SparklyPaper start - track world's MSPT
+ long j = Util.getNanos() - i;
+
+ // These are from the "tickServer" function
+ serverLevel.tickTimes5s.add(this.tickCount, j);
+ serverLevel.tickTimes10s.add(this.tickCount, j);
+ serverLevel.tickTimes60s.add(this.tickCount, j);
+ // SparklyPaper end - track world's MSPT
} catch (Throwable levelTickingException) {
CrashReport crashReport = CrashReport.forThrowable(levelTickingException, "Exception ticking world");
serverLevel.fillReportDetails(crashReport);
diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java
index 61ccb98e02f30b143226b110d5b5f7316606844d..4e1ff6c3ae2dea86c51c286876371d491d9f66de 100644
--- a/net/minecraft/server/level/ServerLevel.java
+++ b/net/minecraft/server/level/ServerLevel.java
@@ -570,6 +570,12 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe
}
// Paper end - chunk tick iteration
+ // SparklyPaper start - track world's MSPT
+ public final MinecraftServer.TickTimes tickTimes5s = new MinecraftServer.TickTimes(100);
+ public final MinecraftServer.TickTimes tickTimes10s = new MinecraftServer.TickTimes(200);
+ public final MinecraftServer.TickTimes tickTimes60s = new MinecraftServer.TickTimes(1200);
+ // SparklyPaper end - track world's MSPT
+
public ServerLevel(
MinecraftServer server,
Executor dispatcher,
diff --git a/org/purpurmc/purpur/task/TPSBarTask.java b/org/purpurmc/purpur/task/TPSBarTask.java
index 8769993e7ca59da309087051a3cd38fc562c15d1..b65c839e45402c618020c77fe63b89bd19cc7d96 100644
--- a/org/purpurmc/purpur/task/TPSBarTask.java
+++ b/org/purpurmc/purpur/task/TPSBarTask.java
@@ -28,6 +28,44 @@ public class TPSBarTask extends BossBarTask {
@Override
void updateBossBar(BossBar bossbar, Player player) {
+ // SparklyPaper start - track world's MSPT
+ if (org.dreeam.leaf.config.modules.async.SparklyPaperParallelWorldTicking.enabled) {
+ // Get player's current world
+ net.minecraft.server.level.ServerLevel serverLevel = ((org.bukkit.craftbukkit.CraftWorld) player.getWorld()).getHandle();
+
+ // Calculate world-specific MSPT and TPS
+ double worldMspt = calculateWorldMSPT(serverLevel);
+ double worldTps = Math.min(20.0, 1000.0 / Math.max(worldMspt, 0.001)); // Avoid division by zero
+
+ // Store original values
+ double originalTps = this.tps;
+ double originalMspt = this.mspt;
+
+ try {
+ // Temporarily set to world values
+ this.tps = worldTps;
+ this.mspt = worldMspt;
+
+ // Create MSPT component with world name
+ Component msptWithWorld = Component.empty()
+ .append(getMSPTColor())
+ .append(Component.text(" [" + player.getWorld().getName() + "]").color(net.kyori.adventure.text.format.NamedTextColor.GRAY));
+
+ // Update the boss bar using the methods that depend on the fields
+ bossbar.progress(getBossBarProgress());
+ bossbar.color(getBossBarColor());
+ bossbar.name(MiniMessage.miniMessage().deserialize(PurpurConfig.commandTPSBarTitle,
+ Placeholder.component("tps", getTPSColor()),
+ Placeholder.component("mspt", msptWithWorld),
+ Placeholder.component("ping", getPingColor(player.getPing()))
+ ));
+ } finally {
+ // Restore original values
+ this.tps = originalTps;
+ this.mspt = originalMspt;
+ }
+ } else {
+ // Default behavior
bossbar.progress(getBossBarProgress());
bossbar.color(getBossBarColor());
bossbar.name(MiniMessage.miniMessage().deserialize(PurpurConfig.commandTPSBarTitle,
@@ -35,6 +73,8 @@ public class TPSBarTask extends BossBarTask {
Placeholder.component("mspt", getMSPTColor()),
Placeholder.component("ping", getPingColor(player.getPing()))
));
+ }
+ // SparklyPaper end - track world's MSPT
}
@Override
@@ -136,6 +176,25 @@ public class TPSBarTask extends BossBarTask {
return MiniMessage.miniMessage().deserialize(color, Placeholder.parsed("text", String.format("%s", ping)));
}
+ // SparklyPaper start - track world's MSPT
+ private double calculateWorldMSPT(net.minecraft.server.level.ServerLevel serverLevel) {
+ long[] times = serverLevel.tickTimes5s.getTimes();
+ long total = 0L;
+ int count = 0;
+
+ for (long value : times) {
+ if (value > 0L) {
+ total += value;
+ count++;
+ }
+ }
+
+ if (count == 0) return 0.0;
+
+ return (double) total / (double) count * 1.0E-6D;
+ }
+ // SparklyPaper end - track world's MSPT
+
public enum FillMode {
TPS, MSPT, PING
}