From 03ad9be07887f1d1919576c7c2caa36adf6fb5e6 Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Fri, 23 Nov 2012 11:42:17 +0100 Subject: [PATCH] Be careful when registering commands. It shouldn't break the plugin. Found an error report in the wild suggesting that getCommand() might occationally fail (if plugin.yml isn't loaded properly, maybe) and return NULL instead. Since the commands are only for administrators and plugin authors, we'll simply ignore it if this occurs. --- .../comphenix/protocol/ProtocolLibrary.java | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java b/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java index ffe6b34d..e3f8e09d 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java @@ -23,6 +23,8 @@ import java.util.logging.LogRecord; import java.util.logging.Logger; import org.bukkit.Server; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.PluginCommand; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; @@ -187,8 +189,8 @@ public class ProtocolLibrary extends JavaPlugin { } // Set up command handlers - getCommand(CommandProtocol.NAME).setExecutor(commandProtocol); - getCommand(CommandPacket.NAME).setExecutor(commandPacket); + registerCommand(CommandProtocol.NAME, commandProtocol); + registerCommand(CommandPacket.NAME, commandPacket); // Notify server managers of incompatible plugins checkForIncompatibility(manager); @@ -217,6 +219,24 @@ public class ProtocolLibrary extends JavaPlugin { reporter.reportDetailed(this, "Metrics cannot be enabled. Incompatible Bukkit version.", e, statistisc); } } + + private void registerCommand(String name, CommandExecutor executor) { + try { + if (executor == null) + throw new RuntimeException("Executor was NULL."); + + PluginCommand command = getCommand(name); + + // Try to load the command + if (command != null) + command.setExecutor(executor); + else + throw new RuntimeException("plugin.yml might be corrupt."); + + } catch (RuntimeException e) { + reporter.reportWarning(this, "Cannot register command " + name + ": " + e.getMessage()); + } + } /** * Disable the current plugin.