From 20018f2b4f9533f019b17d2d77e27c2ffd2eb6c1 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Sun, 28 Jun 2020 01:29:54 -0400
Subject: [PATCH] Disable the memory information in /tps unless argument is
 passed

Memory usage is a pretty useless value in any modern garbage collector
as the way memory is used makes this information not representive of
any actionable data.

In the recommended GC flags, this memory value will constantly rise until
it is near max and then goes down. This is perfectly normal and expected.

Having this information shown will lead to confusion.

Plus, many servers give this command to end users, which now really might
not want to expose this memory data.

So this disables it unless /tps mem is used, and also requires a permission
node to even run this subcommand.
---
 .../Further-improve-server-tick-loop.patch            | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/Spigot-Server-Patches/Further-improve-server-tick-loop.patch b/Spigot-Server-Patches/Further-improve-server-tick-loop.patch
index 609a0eb41c..9963ca01c4 100644
--- a/Spigot-Server-Patches/Further-improve-server-tick-loop.patch
+++ b/Spigot-Server-Patches/Further-improve-server-tick-loop.patch
@@ -180,18 +180,25 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +
 +        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, ", "));
++        if (args.length > 0 && args[0].equals("mem") && sender.hasPermission("bukkit.command.tpsmemory")) {
++            sender.sendMessage(ChatColor.GOLD + "Current Memory Usage: " + ChatColor.GREEN + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / (1024 * 1024)) + "/" + (Runtime.getRuntime().totalMemory() / (1024 * 1024)) + " mb (Max: " + (Runtime.getRuntime().maxMemory() / (1024 * 1024)) + " mb)");
++            if (!hasShownMemoryWarning) {
++                sender.sendMessage(ChatColor.RED + "Warning: " + ChatColor.GOLD + " Memory usage on modern garbage collectors is not a stable value and it is perfectly normal to see it reach max. Please do not pay it much attention.");
++                hasShownMemoryWarning = true;
++            }
          }
 -        sender.sendMessage( sb.substring( 0, sb.length() - 2 ) );
 -        sender.sendMessage(ChatColor.GOLD + "Current Memory Usage: " + ChatColor.GREEN + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / (1024 * 1024)) + "/" + (Runtime.getRuntime().totalMemory() / (1024 * 1024)) + " mb (Max: "
 -                + (Runtime.getRuntime().maxMemory() / (1024 * 1024)) + " mb)");
-+        sender.sendMessage(ChatColor.GOLD + "TPS from last 1m, 5m, 15m: " + org.apache.commons.lang.StringUtils.join(tpsAvg, ", "));
-+        sender.sendMessage(ChatColor.GOLD + "Current Memory Usage: " + ChatColor.GREEN + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / (1024 * 1024)) + "/" + (Runtime.getRuntime().totalMemory() / (1024 * 1024)) + " mb (Max: " + (Runtime.getRuntime().maxMemory() / (1024 * 1024)) + " mb)");
 +        // Paper end
  
          return true;
      }
  
 -    private String format(double tps)
++    private boolean hasShownMemoryWarning; // Paper
 +    private static String format(double tps) // Paper - Made static
      {
          return ( ( tps > 18.0 ) ? ChatColor.GREEN : ( tps > 16.0 ) ? ChatColor.YELLOW : ChatColor.RED ).toString()