papermc/Bukkit-Patches/0003-Timings-Paste-Command.patch

140 lines
6.1 KiB
Diff

From af9e9b5585b81ee36211d584d8678a47e4ffa3c4 Mon Sep 17 00:00:00 2001
From: md_5 <md_5@live.com.au>
Date: Sun, 2 Jun 2013 10:55:20 +1000
Subject: [PATCH] Timings Paste Command
diff --git a/src/main/java/org/bukkit/command/defaults/TimingsCommand.java b/src/main/java/org/bukkit/command/defaults/TimingsCommand.java
index 3c4ef89..e3777ea 100644
--- a/src/main/java/org/bukkit/command/defaults/TimingsCommand.java
+++ b/src/main/java/org/bukkit/command/defaults/TimingsCommand.java
@@ -19,6 +19,15 @@ import org.bukkit.util.StringUtil;
import com.google.common.collect.ImmutableList;
+// Spigot start
+import java.io.ByteArrayOutputStream;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLEncoder;
+import java.util.logging.Level;
+// Spigot end
+
public class TimingsCommand extends BukkitCommand {
private static final List<String> TIMINGS_SUBCOMMANDS = ImmutableList.of("merged", "reset", "separate");
public static long timingStart = 0; // Spigot
@@ -26,14 +35,14 @@ public class TimingsCommand extends BukkitCommand {
public TimingsCommand(String name) {
super(name);
this.description = "Records timings for all plugin events";
- this.usageMessage = "/timings <reset|merged|separate>";
+ this.usageMessage = "/timings <reset|merged|separate> [paste]"; // Spigot
this.setPermission("bukkit.command.timings");
}
@Override
public boolean execute(CommandSender sender, String currentAlias, String[] args) {
if (!testPermission(sender)) return true;
- if (args.length != 1) {
+ if (args.length < 1) { // Spigot
sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
return false;
}
@@ -43,6 +52,7 @@ public class TimingsCommand extends BukkitCommand {
}
boolean separate = "separate".equals(args[0]);
+ boolean paste = "paste".equals( args[0] ); // Spigot
if ("reset".equals(args[0])) {
for (HandlerList handlerList : HandlerList.getHandlerLists()) {
for (RegisteredListener listener : handlerList.getRegisteredListeners()) {
@@ -53,8 +63,7 @@ public class TimingsCommand extends BukkitCommand {
}
timingStart = System.nanoTime(); // Spigot
sender.sendMessage("Timings reset");
- } else if ("merged".equals(args[0]) || separate) {
-
+ } else if ("merged".equals(args[0]) || separate || paste) { // Spigot
long sampleTime = System.nanoTime() - timingStart; // Spigot
int index = 0;
int pluginIdx = 0;
@@ -62,11 +71,12 @@ public class TimingsCommand extends BukkitCommand {
timingFolder.mkdirs();
File timings = new File(timingFolder, "timings.txt");
File names = null;
+ ByteArrayOutputStream bout = ( paste ) ? new ByteArrayOutputStream() : null; // Spigot
while (timings.exists()) timings = new File(timingFolder, "timings" + (++index) + ".txt");
PrintStream fileTimings = null;
PrintStream fileNames = null;
try {
- fileTimings = new PrintStream(timings);
+ fileTimings = ( paste ) ? new PrintStream( bout ) : new PrintStream( timings );
if (separate) {
names = new File(timingFolder, "names" + index + ".txt");
fileNames = new PrintStream(names);
@@ -96,6 +106,13 @@ public class TimingsCommand extends BukkitCommand {
fileTimings.println(" Total time " + totalTime + " (" + totalTime / 1000000000 + "s)");
}
fileTimings.println( "Sample time " + sampleTime + " (" + sampleTime / 1E9 + "s)" ); // Spigot
+ // Spigot start
+ if ( paste )
+ {
+ new PasteThread( sender, bout ).start();
+ return true;
+ }
+ // Spigot end
sender.sendMessage("Timings written to " + timings.getPath());
if (separate) sender.sendMessage("Names written to " + names.getPath());
} catch (IOException e) {
@@ -122,4 +139,47 @@ public class TimingsCommand extends BukkitCommand {
}
return ImmutableList.of();
}
+
+ // Spigot start
+ private static class PasteThread extends Thread
+ {
+
+ private final CommandSender sender;
+ private final ByteArrayOutputStream bout;
+
+ public PasteThread(CommandSender sender, ByteArrayOutputStream bout)
+ {
+ super( "Timings paste thread" );
+ this.sender = sender;
+ this.bout = bout;
+ }
+
+ @Override
+ public void run()
+ {
+ try
+ {
+ HttpURLConnection con = (HttpURLConnection) new URL( "http://paste.ubuntu.com/" ).openConnection();
+ con.setDoOutput( true );
+ con.setRequestMethod( "POST" );
+ con.setInstanceFollowRedirects( false );
+
+ OutputStream out = con.getOutputStream();
+ out.write( "poster=Spigot&syntax=text&content=".getBytes( "UTF-8" ) );
+ out.write( URLEncoder.encode( bout.toString( "UTF-8" ), "UTF-8" ).getBytes( "UTF-8" ) );
+ out.close();
+ con.getInputStream().close();
+
+ String location = con.getHeaderField( "Location" );
+ String pasteID = location.substring( "http://paste.ubuntu.com/".length(), location.length() - 1 );
+ sender.sendMessage( ChatColor.GREEN + "Your timings have been pasted to " + location );
+ sender.sendMessage( ChatColor.GREEN + "You can view the results at http://aikar.co/timings.php?url=" + pasteID );
+ } catch ( IOException ex )
+ {
+ sender.sendMessage( ChatColor.RED + "Error pasting timings, check your console for more information" );
+ Bukkit.getServer().getLogger().log( Level.WARNING, "Could not paste timings", ex );
+ }
+ }
+ }
+ // Spigot end
}
--
1.8.1.2