Exception handling in commands
This commit is contained in:
parent
64e82042a6
commit
2188275a00
27
src/main/java/org/bukkit/command/CommandException.java
Normal file
27
src/main/java/org/bukkit/command/CommandException.java
Normal file
@ -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 <code>CommandException</code> without detail message.
|
||||||
|
*/
|
||||||
|
public CommandException() {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs an instance of <code>CommandException</code> 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);
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,6 @@
|
|||||||
package org.bukkit.command;
|
package org.bukkit.command;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
|
|
||||||
public interface CommandMap {
|
public interface CommandMap {
|
||||||
/**
|
/**
|
||||||
@ -19,10 +18,12 @@ public interface CommandMap {
|
|||||||
*/
|
*/
|
||||||
public boolean register(String label, String fallbackPrefix, Command command);
|
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"
|
* @param cmdLine command + arguments. Example: "/test abc 123"
|
||||||
* @return targetFound returns false if no target is found.
|
* @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);
|
public boolean dispatch(CommandSender sender, String cmdLine);
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package org.bukkit.command;
|
package org.bukkit.command;
|
||||||
|
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
public final class PluginCommand extends Command {
|
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) {
|
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()) {
|
if (!cmdSuccess && !usageMessage.isEmpty()) {
|
||||||
String tmpMsg = usageMessage.replace("<command>", commandLabel);
|
String tmpMsg = usageMessage.replace("<command>", commandLabel);
|
||||||
String[] usageLines = tmpMsg.split("\\n");
|
String[] usageLines = tmpMsg.split("\\n");
|
||||||
|
@ -8,7 +8,6 @@ import java.util.Map;
|
|||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.Server;
|
import org.bukkit.Server;
|
||||||
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
import org.bukkit.plugin.PluginDescriptionFile;
|
import org.bukkit.plugin.PluginDescriptionFile;
|
||||||
|
|
||||||
@ -74,7 +73,13 @@ public final class SimpleCommandMap implements CommandMap {
|
|||||||
Command target = knownCommands.get(sentCommandLabel);
|
Command target = knownCommands.get(sentCommandLabel);
|
||||||
boolean isRegisteredCommand = (target != null);
|
boolean isRegisteredCommand = (target != null);
|
||||||
if (isRegisteredCommand) {
|
if (isRegisteredCommand) {
|
||||||
|
try {
|
||||||
target.execute(sender, sentCommandLabel, args);
|
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;
|
return isRegisteredCommand;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user