mSpigot-Parent/patches/server/0004-tps-command.patch

117 lines
4.9 KiB
Diff
Raw Permalink Normal View History

2023-07-21 06:11:34 +02:00
From da24e9809c9ee2254da026445735ee3dbe74aaf7 Mon Sep 17 00:00:00 2001
From: virtualWinter <winter@catmc.club>
Date: Fri, 21 Jul 2023 07:10:18 +0300
Subject: [PATCH] tps command
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index b400ce04..66334c12 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -117,6 +117,7 @@ public abstract class MinecraftServer implements Runnable, ICommandListener, IAs
public int autosavePeriod;
// CraftBukkit end
+
public MinecraftServer(OptionSet options, Proxy proxy, File file1) {
io.netty.util.ResourceLeakDetector.setEnabled( false ); // Spigot - disable
this.e = proxy;
diff --git a/src/main/java/org/spigotmc/TicksPerSecondCommand.java b/src/main/java/org/spigotmc/TicksPerSecondCommand.java
index 21fd7efc..eef3e0aa 100644
--- a/src/main/java/org/spigotmc/TicksPerSecondCommand.java
+++ b/src/main/java/org/spigotmc/TicksPerSecondCommand.java
@@ -1,14 +1,15 @@
package org.spigotmc;
+import net.minecraft.server.MinecraftServer;
+import org.apache.commons.lang.StringUtils;
+import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
-public class TicksPerSecondCommand extends Command
-{
+public class TicksPerSecondCommand extends Command {
- public TicksPerSecondCommand(String name)
- {
+ public TicksPerSecondCommand(String name) {
super( name );
this.description = "Gets the current ticks per second for the server";
this.usageMessage = "/tps";
@@ -16,28 +17,55 @@ public class TicksPerSecondCommand extends Command
}
@Override
- public boolean execute(CommandSender sender, String currentAlias, String[] args)
- {
- if ( !testPermission( sender ) )
- {
+ public boolean execute(CommandSender sender, String currentAlias, String[] args) {
+ if ( !testPermission( sender ) ) {
return true;
}
- // PaperSpigot start - Further improve tick handling
- double[] tps = org.bukkit.Bukkit.spigot().getTPS();
- String[] tpsAvg = new String[tps.length];
+ if (sender.hasPermission("bukkit.command.tps.advanced")) {
+ double[] tps = org.bukkit.Bukkit.spigot().getTPS();
+ String[] tpsAvg = new String[tps.length];
- for ( int i = 0; i < tps.length; i++) {
- tpsAvg[i] = format( tps[i] );
- }
- sender.sendMessage( ChatColor.GOLD + "TPS from last 1m, 5m, 15m: " + org.apache.commons.lang.StringUtils.join(tpsAvg, ", "));
+ for (int i = 0; i < tps.length; i++) {
+ tpsAvg[i] = formatAdvancedTps(tps[i]);
+ }
+
+ sender.sendMessage(ChatColor.GOLD + "TPS from last 1m, 5m, 15m: " + StringUtils.join(tpsAvg, ", "));
+ //sender.sendMessage(ChatColor.GOLD + "Full tick: " + formatTickTime(MinecraftServer.getServer().lastTickTime) + " ms");
+ sender.sendMessage(ChatColor.GOLD + "Online players: " + ChatColor.GREEN + Bukkit.getOnlinePlayers().size() + "/" + Bukkit.getMaxPlayers());
+ } else {
+ double tps = Bukkit.spigot().getTPS()[1];
+
+ StringBuilder tpsBuilder = new StringBuilder();
+
+ tpsBuilder.append(ChatColor.GOLD).append("Server performance: ");
+ tpsBuilder.append(formatBasicTps(tps)).append(ChatColor.GOLD).append("/20.0");
+ tpsBuilder.append(" [").append(tps > 18.0 ? ChatColor.GREEN : tps > 16.0 ? ChatColor.YELLOW : ChatColor.RED);
+
+ int i = 0;
+
+ for (; i < Math.round(tps); i++) {
+ tpsBuilder.append("|");
+ }
+ tpsBuilder.append(ChatColor.DARK_GRAY);
+
+ for (; i < 20; i++) {
+ tpsBuilder.append("|");
+ }
+ }
return true;
}
- private static String format(double tps) // PaperSpigot - made static
- {
- return ( ( tps > 18.0 ) ? ChatColor.GREEN : ( tps > 16.0 ) ? ChatColor.YELLOW : ChatColor.RED ).toString()
- + ( ( tps > 20.0 ) ? "*" : "" ) + Math.min( Math.round( tps * 100.0 ) / 100.0, 20.0 );
+ private static String formatTickTime(double time) {
+ return (time < 40.0D ? ChatColor.GREEN : time < 60.0D ? ChatColor.YELLOW : ChatColor.RED).toString() + Math.round(time * 10.0D) / 10.0D;
+ }
+
+ private static String formatAdvancedTps(double tps) {
+ return (tps > 18.0 ? ChatColor.GREEN : tps > 16.0 ? ChatColor.YELLOW : ChatColor.RED).toString() + Math.min(Math.round(tps * 100.0D) / 100.0, 20.0);
+ }
+
+ private String formatBasicTps(double tps) {
+ return (tps > 18.0 ? ChatColor.GREEN : tps > 16.0 ? ChatColor.YELLOW : ChatColor.RED).toString() + Math.min(Math.round(tps * 10.0D) / 10.0D, 20.0D);
}
}
--
2.41.0.windows.1