diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java b/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java index 97a65df2..0804fe51 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java @@ -42,6 +42,7 @@ import com.comphenix.protocol.metrics.Statistics; import com.comphenix.protocol.metrics.Updater; import com.comphenix.protocol.metrics.Updater.UpdateResult; import com.comphenix.protocol.reflect.compiler.BackgroundCompiler; +import com.comphenix.protocol.utility.ChatExtensions; /** * The main entry point for ProtocolLib. @@ -216,6 +217,21 @@ public class ProtocolLibrary extends JavaPlugin { // Don't do anything else! if (manager == null) return; + // Silly plugin reloaders! + if (protocolManager == null) { + Logger directLogging = Logger.getLogger("Minecraft"); + String[] message = new String[] { + " PROTOCOLLIB DOES NOT SUPPORT PLUGIN RELOADERS. ", + " PLEASE USE THE BUILT-IN RELOAD COMMAND. ", + }; + + // Print as severe + for (String line : ChatExtensions.toFlowerBox(message, "*", 3, 1)) { + directLogging.severe(line); + } + disablePlugin(); + return; + } // Perform logic when the world has loaded protocolManager.postWorldLoaded(); @@ -261,7 +277,7 @@ public class ProtocolLibrary extends JavaPlugin { reporter.reportDetailed(this, "Metrics cannot be enabled. Incompatible Bukkit version.", e, statistisc); } } - + // Used to check Minecraft version private void verifyMinecraftVersion() { try { @@ -426,9 +442,13 @@ public class ProtocolLibrary extends JavaPlugin { if (redirectHandler != null) { logger.removeHandler(redirectHandler); } - - unhookTask.close(); - protocolManager.close(); + if (protocolManager != null) + protocolManager.close(); + else + return; // Plugin reloaders! + + if (unhookTask != null) + unhookTask.close(); protocolManager = null; statistisc = null; reporter = null; diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/utility/ChatExtensions.java b/ProtocolLib/src/main/java/com/comphenix/protocol/utility/ChatExtensions.java index 92af6cca..7fc274d0 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/utility/ChatExtensions.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/utility/ChatExtensions.java @@ -27,6 +27,7 @@ import com.comphenix.protocol.Packets; import com.comphenix.protocol.ProtocolManager; import com.comphenix.protocol.injector.PacketConstructor; import com.comphenix.protocol.reflect.FieldAccessException; +import com.google.common.base.Strings; /** * Utility methods for sending chat messages. @@ -98,4 +99,48 @@ public class ChatExtensions { } } } + + /** + * Print a flower box around a given message. + * @param message - the message to print. + * @param marginChar - the character to use as margin. + * @param marginWidth - the width (in characters) of the left and right margin. + * @param marginHeight - the height (in characters) of the top and buttom margin. + */ + public static String[] toFlowerBox(String[] message, String marginChar, int marginWidth, int marginHeight) { + String[] output = new String[message.length + marginHeight * 2]; + int width = getMaximumLength(message); + + // Margins + String topButtomMargin = Strings.repeat(marginChar, width + marginWidth * 2); + String leftRightMargin = Strings.repeat(marginChar, marginWidth); + + // Add left and right margin + for (int i = 0; i < message.length; i++) { + output[i + marginHeight] = leftRightMargin + Strings.padEnd(message[i], width, ' ') + leftRightMargin; + } + + // Insert top and bottom margin + for (int i = 0; i < marginHeight; i++) { + output[i] = topButtomMargin; + output[output.length - i - 1] = topButtomMargin; + } + return output; + } + + /** + * Retrieve the longest line lenght in a list of strings. + * @param lines - the lines. + * @return Longest line lenght. + */ + private static int getMaximumLength(String[] lines) { + int current = 0; + + // Find the longest line + for (int i = 0; i < lines.length; i++) { + if (current < lines[i].length()) + current = lines[i].length(); + } + return current; + } }