mirror of
https://github.com/PurpurMC/Purpur.git
synced 2026-02-19 17:37:42 +01:00
MORE WORK
This commit is contained in:
208
patches/server/0143-Implement-TPSBar.patch
Normal file
208
patches/server/0143-Implement-TPSBar.patch
Normal file
@@ -0,0 +1,208 @@
|
||||
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
||||
From: William Blake Galbreath <Blake.Galbreath@GMail.com>
|
||||
Date: Sat, 12 Dec 2020 21:19:05 -0600
|
||||
Subject: [PATCH] Implement TPSBar
|
||||
|
||||
|
||||
diff --git a/src/main/java/net/minecraft/commands/Commands.java b/src/main/java/net/minecraft/commands/Commands.java
|
||||
index 9cbaaf4cd138e6c1d2d51afcef24a81a3f2973c5..13e90bf9506862a4830b50dcce06cc95a6ada5e3 100644
|
||||
--- a/src/main/java/net/minecraft/commands/Commands.java
|
||||
+++ b/src/main/java/net/minecraft/commands/Commands.java
|
||||
@@ -200,6 +200,7 @@ public class Commands {
|
||||
WhitelistCommand.register(this.dispatcher);
|
||||
net.pl3x.purpur.command.DemoCommand.register(this.dispatcher); // Purpur
|
||||
net.pl3x.purpur.command.PingCommand.register(this.dispatcher); // Purpur
|
||||
+ net.pl3x.purpur.command.TPSBarCommand.register(this.dispatcher); // Purpur
|
||||
}
|
||||
|
||||
if (environment.includeIntegrated) {
|
||||
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
|
||||
index 91e6d0b6560ffa8e870eca9707d5cd117fcb2347..d525bd371bedf75c24afd4a8a1240b31f91aa9da 100644
|
||||
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
|
||||
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
|
||||
@@ -1040,6 +1040,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
|
||||
this.safeShutdown(flag, false);
|
||||
}
|
||||
public void safeShutdown(boolean flag, boolean isRestarting) {
|
||||
+ net.pl3x.purpur.task.TPSBarTask.stop(); // Purpur
|
||||
this.isRestarting = isRestarting;
|
||||
this.hasLoggedStop = true; // Paper
|
||||
if (isDebugging()) io.papermc.paper.util.TraceUtil.dumpTraceForThread("Server stopped"); // Paper
|
||||
diff --git a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
|
||||
index 5fa2723c9899c69ac326f95acf49cd47406378f9..a9098119009b60ea8dbdb4c118cfc20b112d44e0 100644
|
||||
--- a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
|
||||
+++ b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
|
||||
@@ -345,6 +345,8 @@ public class DedicatedServer extends MinecraftServer implements ServerInterface
|
||||
DedicatedServer.LOGGER.info("JMX monitoring enabled");
|
||||
}
|
||||
|
||||
+ net.pl3x.purpur.task.TPSBarTask.start(); // Purpur
|
||||
+
|
||||
return true;
|
||||
}
|
||||
}
|
||||
diff --git a/src/main/java/net/minecraft/server/players/PlayerList.java b/src/main/java/net/minecraft/server/players/PlayerList.java
|
||||
index 7bab62ac11d31f9eb0936f8c8540ad19685b49ee..f71097f1386de7ba6aaaffe8728e9ac18c5270c5 100644
|
||||
--- a/src/main/java/net/minecraft/server/players/PlayerList.java
|
||||
+++ b/src/main/java/net/minecraft/server/players/PlayerList.java
|
||||
@@ -593,6 +593,8 @@ public abstract class PlayerList {
|
||||
if (entityplayer.didPlayerJoinEvent) this.cserver.getPluginManager().callEvent(playerQuitEvent); // Paper - if we disconnected before join ever fired, don't fire quit
|
||||
entityplayer.getBukkitEntity().disconnect(playerQuitEvent.getQuitMessage());
|
||||
|
||||
+ net.pl3x.purpur.task.TPSBarTask.removePlayer(cserver.getPlayer(entityplayer)); // Purpur
|
||||
+
|
||||
if (server.isSameThread()) entityplayer.doTick(); // SPIGOT-924 // Paper - don't tick during emergency shutdowns (Watchdog)
|
||||
// CraftBukkit end
|
||||
|
||||
diff --git a/src/main/java/net/pl3x/purpur/command/TPSBarCommand.java b/src/main/java/net/pl3x/purpur/command/TPSBarCommand.java
|
||||
new file mode 100644
|
||||
index 0000000000000000000000000000000000000000..2803a1b95121fbff3066fd2d1abaf8723386781f
|
||||
--- /dev/null
|
||||
+++ b/src/main/java/net/pl3x/purpur/command/TPSBarCommand.java
|
||||
@@ -0,0 +1,24 @@
|
||||
+package net.pl3x.purpur.command;
|
||||
+
|
||||
+import com.mojang.brigadier.CommandDispatcher;
|
||||
+import net.minecraft.commands.CommandSourceStack;
|
||||
+import net.minecraft.commands.Commands;
|
||||
+import net.minecraft.server.level.ServerPlayer;
|
||||
+import net.pl3x.purpur.task.TPSBarTask;
|
||||
+
|
||||
+public class TPSBarCommand {
|
||||
+ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
|
||||
+ dispatcher.register(Commands.literal("tpsbar")
|
||||
+ .requires(listener -> listener.hasPermission(2))
|
||||
+ .executes(context -> execute(context.getSource(), context.getSource().getPlayerOrException()))
|
||||
+ ).setPermission("bukkit.command.tpsbar");
|
||||
+ }
|
||||
+
|
||||
+ private static int execute(CommandSourceStack source, ServerPlayer player) {
|
||||
+ if (player != null) {
|
||||
+ TPSBarTask.togglePlayer(player.getBukkitEntity());
|
||||
+ return 1;
|
||||
+ }
|
||||
+ return 0;
|
||||
+ }
|
||||
+}
|
||||
diff --git a/src/main/java/net/pl3x/purpur/task/TPSBarTask.java b/src/main/java/net/pl3x/purpur/task/TPSBarTask.java
|
||||
new file mode 100644
|
||||
index 0000000000000000000000000000000000000000..11791bbd4dbe4037f7a5b630be2ff1fa4d599513
|
||||
--- /dev/null
|
||||
+++ b/src/main/java/net/pl3x/purpur/task/TPSBarTask.java
|
||||
@@ -0,0 +1,116 @@
|
||||
+package net.pl3x.purpur.task;
|
||||
+
|
||||
+import org.bukkit.Bukkit;
|
||||
+import org.bukkit.ChatColor;
|
||||
+import org.bukkit.NamespacedKey;
|
||||
+import org.bukkit.boss.BarColor;
|
||||
+import org.bukkit.boss.BarStyle;
|
||||
+import org.bukkit.boss.BossBar;
|
||||
+import org.bukkit.craftbukkit.scheduler.MinecraftInternalPlugin;
|
||||
+import org.bukkit.entity.Player;
|
||||
+import org.bukkit.scheduler.BukkitRunnable;
|
||||
+
|
||||
+public class TPSBarTask extends BukkitRunnable {
|
||||
+ private static TPSBarTask instance;
|
||||
+ private final NamespacedKey key;
|
||||
+ private BossBar bossbar;
|
||||
+
|
||||
+ private TPSBarTask() {
|
||||
+ this.key = new NamespacedKey("purpur", "tpsbar");
|
||||
+ }
|
||||
+
|
||||
+ private static TPSBarTask instance() {
|
||||
+ if (instance == null) {
|
||||
+ instance = new TPSBarTask();
|
||||
+ }
|
||||
+ return instance;
|
||||
+ }
|
||||
+
|
||||
+ private BossBar bossbar() {
|
||||
+ if (bossbar == null) {
|
||||
+ bossbar = Bukkit.getBossBar(key);
|
||||
+ if (bossbar == null) {
|
||||
+ bossbar = Bukkit.createBossBar(key, "TPS: 20.0", BarColor.RED, BarStyle.SEGMENTED_20);
|
||||
+ }
|
||||
+ bossbar.setVisible(true);
|
||||
+ bossbar.setProgress(1.0D);
|
||||
+ }
|
||||
+ return bossbar;
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public void run() {
|
||||
+ BossBar bossbar = bossbar();
|
||||
+ if (bossbar.getPlayers().isEmpty()) {
|
||||
+ return;
|
||||
+ }
|
||||
+
|
||||
+ double tps = Bukkit.getTPS()[0];
|
||||
+ if (tps > 20.0D) {
|
||||
+ tps = 20.0D;
|
||||
+ } else if (tps < 0.0D) {
|
||||
+ tps = 0.0D;
|
||||
+ }
|
||||
+
|
||||
+ bossbar.setVisible(true);
|
||||
+ bossbar.setProgress(Math.max(Math.min(tps / 20.0D, 1.0D), 0.0D));
|
||||
+
|
||||
+ String tpsColor;
|
||||
+ if (tps >= 18) {
|
||||
+ tpsColor = "&2";
|
||||
+ bossbar.setColor(BarColor.GREEN);
|
||||
+ } else if (tps >= 15) {
|
||||
+ tpsColor = "&e";
|
||||
+ bossbar.setColor(BarColor.YELLOW);
|
||||
+ } else {
|
||||
+ tpsColor = "&4";
|
||||
+ bossbar.setColor(BarColor.RED);
|
||||
+ }
|
||||
+
|
||||
+ double mspt = Bukkit.getAverageTickTime();
|
||||
+ String msptColor;
|
||||
+ if (mspt < 40) {
|
||||
+ msptColor = "&2";
|
||||
+ } else if (mspt < 50) {
|
||||
+ msptColor = "&e";
|
||||
+ } else {
|
||||
+ msptColor = "&4";
|
||||
+ }
|
||||
+
|
||||
+ bossbar.setTitle(ChatColor.translateAlternateColorCodes('&', "&eTPS&3: " + tpsColor + String.format("%.2f", tps) + " &eMSPT&3: " + msptColor + String.format("%.3f", mspt)));
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public void cancel() {
|
||||
+ super.cancel();
|
||||
+ BossBar bossbar = bossbar();
|
||||
+ bossbar.setVisible(false);
|
||||
+ bossbar.removeAll();
|
||||
+ Bukkit.removeBossBar(key);
|
||||
+ }
|
||||
+
|
||||
+ public static void removePlayer(Player player) {
|
||||
+ instance().bossbar().removePlayer(player);
|
||||
+ }
|
||||
+
|
||||
+ public static void togglePlayer(Player player) {
|
||||
+ BossBar bossbar = instance().bossbar();
|
||||
+ if (bossbar.getPlayers().contains(player)) {
|
||||
+ bossbar.removePlayer(player);
|
||||
+ } else {
|
||||
+ bossbar.addPlayer(player);
|
||||
+ instance.run();
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ public static void start() {
|
||||
+ stop();
|
||||
+ instance().runTaskTimerAsynchronously(new MinecraftInternalPlugin(), 20L, 20L);
|
||||
+ }
|
||||
+
|
||||
+ public static void stop() {
|
||||
+ if (instance != null) {
|
||||
+ instance.cancel();
|
||||
+ }
|
||||
+ }
|
||||
+}
|
||||
Reference in New Issue
Block a user