From f77ff6a0a212e5b24b23216a6e272545f4e3b856 Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Sun, 12 Jul 2015 21:07:09 -0400 Subject: [PATCH] Add /protocol dump, should help with debugging --- .../comphenix/protocol/CommandProtocol.java | 70 +++++++++++++++++++ .../protocol/error/DetailedErrorReporter.java | 6 +- ProtocolLib/src/main/resources/plugin.yml | 2 +- 3 files changed, 74 insertions(+), 4 deletions(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/CommandProtocol.java b/ProtocolLib/src/main/java/com/comphenix/protocol/CommandProtocol.java index d567a7ce..7fae25b5 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/CommandProtocol.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/CommandProtocol.java @@ -18,17 +18,25 @@ package com.comphenix.protocol; import java.io.File; +import java.io.FileWriter; import java.io.IOException; +import java.io.PrintWriter; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Set; +import java.util.logging.Level; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginDescriptionFile; +import com.comphenix.protocol.error.DetailedErrorReporter; import com.comphenix.protocol.error.ErrorReporter; import com.comphenix.protocol.events.PacketListener; import com.comphenix.protocol.timing.TimedListenerManager; import com.comphenix.protocol.timing.TimingReportGenerator; +import com.google.common.io.Closer; /** * Handles the "protocol" administration command. @@ -61,6 +69,8 @@ class CommandProtocol extends CommandBase { printListeners(sender); else if (subCommand.equalsIgnoreCase("version")) printVersion(sender); + else if (subCommand.equalsIgnoreCase("dump")) + dump(sender); else return false; return true; @@ -142,4 +152,64 @@ class CommandProtocol extends CommandBase { plugin.reloadConfig(); sender.sendMessage(ChatColor.YELLOW + "Reloaded configuration!"); } + + private static SimpleDateFormat FILE_FORMAT; + private static SimpleDateFormat TIMESTAMP_FORMAT; + + private void dump(CommandSender sender) { + Closer closer = Closer.create(); + + if (FILE_FORMAT == null) + FILE_FORMAT = new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss"); + if (TIMESTAMP_FORMAT == null) + TIMESTAMP_FORMAT = new SimpleDateFormat("MM/dd/yy HH:mm:ss"); + + try { + Date date = new Date(); + File file = new File(plugin.getDataFolder(), "dump-" + FILE_FORMAT.format(date) + ".txt"); + if (file.exists()) { + file.delete(); + } + + file.createNewFile(); + + FileWriter fw = closer.register(new FileWriter(file)); + PrintWriter pw = closer.register(new PrintWriter(fw)); + + pw.println("ProtocolLib Dump"); + pw.println("Timestamp: " + TIMESTAMP_FORMAT.format(date)); + pw.println(); + + pw.println("ProtocolLib Version: " + plugin.toString()); + pw.println("Bukkit Version: " + plugin.getServer().getBukkitVersion()); + pw.println("Server Version: " + plugin.getServer().getVersion()); + pw.println(); + + ProtocolManager manager = ProtocolLibrary.getProtocolManager(); + pw.println("ProtocolLib: " + DetailedErrorReporter.getStringDescription(plugin)); + pw.println("Manager: " + DetailedErrorReporter.getStringDescription(manager)); + pw.println(); + + Set listeners = manager.getPacketListeners(); + if (listeners.size() > 0) { + pw.println("Listeners:"); + + for (PacketListener listener : listeners) { + pw.println(DetailedErrorReporter.getStringDescription(listener)); + } + } else { + pw.println("No listeners"); + } + + sender.sendMessage("Data dump written to " + file.getAbsolutePath()); + } catch (IOException ex) { + ProtocolLibrary.getStaticLogger().log(Level.SEVERE, "Failed to create dump:", ex); + sender.sendMessage(ChatColor.RED + "Failed to create dump! Check console!"); + } finally { + try { + closer.close(); + } catch (IOException ex1) { + } + } + } } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/error/DetailedErrorReporter.java b/ProtocolLib/src/main/java/com/comphenix/protocol/error/DetailedErrorReporter.java index 615c1cc7..1ebe799f 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/error/DetailedErrorReporter.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/error/DetailedErrorReporter.java @@ -79,7 +79,7 @@ public class DetailedErrorReporter implements ErrorReporter { protected String pluginName; // Whether or not Apache Commons is not present - protected boolean apacheCommonsMissing; + protected static boolean apacheCommonsMissing; // Whether or not detailed errror reporting is enabled protected boolean detailedReporting; @@ -451,7 +451,7 @@ public class DetailedErrorReporter implements ErrorReporter { * @param value - object to convert. * @return String representation. */ - protected String getStringDescription(Object value) { + public static String getStringDescription(Object value) { // We can't only rely on toString. if (value == null) { return "[NULL]"; @@ -483,7 +483,7 @@ public class DetailedErrorReporter implements ErrorReporter { * @param test - the object to test. * @return TRUE if this object is simple enough to simply be printed, FALSE othewise. */ - protected boolean isSimpleType(Object test) { + protected static boolean isSimpleType(Object test) { return test instanceof String || Primitives.isWrapperType(test.getClass()); } diff --git a/ProtocolLib/src/main/resources/plugin.yml b/ProtocolLib/src/main/resources/plugin.yml index 6df02e42..fa35a50e 100644 --- a/ProtocolLib/src/main/resources/plugin.yml +++ b/ProtocolLib/src/main/resources/plugin.yml @@ -10,7 +10,7 @@ database: false commands: protocol: description: Performs administrative tasks regarding ProtocolLib. - usage: / config|timings|listeners|version + usage: / config|timings|listeners|version|dump permission: protocol.admin permission-message: You don't have packet: