diff --git a/src/main/java/org/bukkit/command/CommandException.java b/src/main/java/org/bukkit/command/CommandException.java
new file mode 100644
index 00000000..7111517c
--- /dev/null
+++ b/src/main/java/org/bukkit/command/CommandException.java
@@ -0,0 +1,27 @@
+
+package org.bukkit.command;
+
+/**
+ * Thrown when an unhandled exception occurs during the execution of a Command
+ */
+public class CommandException extends RuntimeException {
+
+ /**
+ * Creates a new instance of CommandException
without detail message.
+ */
+ public CommandException() {
+ }
+
+
+ /**
+ * Constructs an instance of CommandException
with the specified detail message.
+ * @param msg the detail message.
+ */
+ public CommandException(String msg) {
+ super(msg);
+ }
+
+ public CommandException(String msg, Throwable cause) {
+ super(msg, cause);
+ }
+}
diff --git a/src/main/java/org/bukkit/command/CommandMap.java b/src/main/java/org/bukkit/command/CommandMap.java
index 884fea2d..6acc4acf 100644
--- a/src/main/java/org/bukkit/command/CommandMap.java
+++ b/src/main/java/org/bukkit/command/CommandMap.java
@@ -1,7 +1,6 @@
package org.bukkit.command;
import java.util.List;
-import org.bukkit.entity.Player;
public interface CommandMap {
/**
@@ -19,10 +18,12 @@ public interface CommandMap {
*/
public boolean register(String label, String fallbackPrefix, Command command);
- /** Looks for the requested command and executes it if found.
+ /**
+ * Looks for the requested command and executes it if found.
*
- * @param cmdLine command + arguments. Example: "/test abc 123"
- * @return targetFound returns false if no target is found.
+ * @param cmdLine command + arguments. Example: "/test abc 123"
+ * @return targetFound returns false if no target is found.
+ * @throws CommandException Thrown when the executor for the given command fails with an unhandled exception
*/
public boolean dispatch(CommandSender sender, String cmdLine);
diff --git a/src/main/java/org/bukkit/command/PluginCommand.java b/src/main/java/org/bukkit/command/PluginCommand.java
index 16ea3654..1d0f8209 100644
--- a/src/main/java/org/bukkit/command/PluginCommand.java
+++ b/src/main/java/org/bukkit/command/PluginCommand.java
@@ -1,7 +1,6 @@
package org.bukkit.command;
import org.bukkit.ChatColor;
-import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
public final class PluginCommand extends Command {
@@ -14,7 +13,14 @@ public final class PluginCommand extends Command {
}
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
- boolean cmdSuccess = owningPlugin.onCommand(sender, this, commandLabel, args);
+ boolean cmdSuccess = false;
+
+ try {
+ owningPlugin.onCommand(sender, this, commandLabel, args);
+ } catch (Throwable ex) {
+ throw new CommandException("Unhandled exception executing command '" + commandLabel + "' in plugin " + owningPlugin.getDescription().getFullName(), ex);
+ }
+
if (!cmdSuccess && !usageMessage.isEmpty()) {
String tmpMsg = usageMessage.replace("", commandLabel);
String[] usageLines = tmpMsg.split("\\n");
diff --git a/src/main/java/org/bukkit/command/SimpleCommandMap.java b/src/main/java/org/bukkit/command/SimpleCommandMap.java
index 92682791..c63fd34e 100644
--- a/src/main/java/org/bukkit/command/SimpleCommandMap.java
+++ b/src/main/java/org/bukkit/command/SimpleCommandMap.java
@@ -8,7 +8,6 @@ import java.util.Map;
import org.bukkit.ChatColor;
import org.bukkit.Server;
-import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
@@ -74,7 +73,13 @@ public final class SimpleCommandMap implements CommandMap {
Command target = knownCommands.get(sentCommandLabel);
boolean isRegisteredCommand = (target != null);
if (isRegisteredCommand) {
- target.execute(sender, sentCommandLabel, args);
+ try {
+ target.execute(sender, sentCommandLabel, args);
+ } catch (CommandException ex) {
+ throw ex;
+ } catch (Throwable ex) {
+ throw new CommandException("Unhandled exception executing '" + commandLine + "' in " + target, ex);
+ }
}
return isRegisteredCommand;
}