Cleaned up InvalidUsageException and CommandException to be less confusing.

This commit is contained in:
sk89q 2014-06-30 23:02:04 -07:00
parent 08ad5f4451
commit 88f0f1061a
3 changed files with 65 additions and 12 deletions

View File

@ -19,10 +19,13 @@
package com.sk89q.minecraft.util.commands;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import static com.google.common.base.Preconditions.checkNotNull;
public class CommandException extends Exception {
private static final long serialVersionUID = 870638193072101739L;
@ -48,7 +51,16 @@ public class CommandException extends Exception {
commandStack.add(name);
}
public String toStackString(String prefix, String spacedSuffix) {
/**
* Gets the command that was called, which will include the sub-command
* (i.e. "/br sphere").
*
* @param prefix the command shebang character (such as "/") -- may be empty
* @param spacedSuffix a suffix to put at the end (optional) -- may be null
* @return the command that was used
*/
public String getCommandUsed(String prefix, @Nullable String spacedSuffix) {
checkNotNull(prefix);
StringBuilder builder = new StringBuilder();
if (prefix != null) {
builder.append(prefix);

View File

@ -222,8 +222,8 @@ public final class CommandManager {
} catch (CommandPermissionsException e) {
actor.printError("You don't have permission to do this.");
} catch (InvalidUsageException e) {
if (e.isFullUsageSuggested()) {
actor.printRaw(ColorCodeBuilder.asColorCodes(new CommandUsageBox(e.getCommand(), e.toStackString("/", ""), locals)));
if (e.isFullHelpSuggested()) {
actor.printRaw(ColorCodeBuilder.asColorCodes(new CommandUsageBox(e.getCommand(), e.getCommandUsed("/", ""), locals)));
String message = e.getMessage();
if (message != null) {
actor.printError(message);
@ -231,7 +231,7 @@ public final class CommandManager {
} else {
String message = e.getMessage();
actor.printError(message != null ? message : "The command was not used properly (no more help available).");
actor.printError(e.getUsage("/"));
actor.printError(e.getSimpleUsageString("/"));
}
} catch (WrappedCommandException e) {
Throwable t = e.getCause();

View File

@ -21,38 +21,79 @@ package com.sk89q.worldedit.util.command;
import com.sk89q.minecraft.util.commands.CommandException;
import javax.annotation.Nullable;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Thrown when a command is not used properly.
* </p>
* When handling this exception, print the error message if it is not null.
* Print a one line help instruction unless {@link #isFullHelpSuggested()}
* is true, which, in that case, the full help of the command should be shown.
* </p>
* If no error message is set and full help is not to be shown, then a generic
* "you used this command incorrectly" message should be shown.
*/
public class InvalidUsageException extends CommandException {
private static final long serialVersionUID = -3222004168669490390L;
private final CommandCallable command;
private final boolean fullUsageSuggested;
private final boolean fullHelpSuggested;
/**
* Create a new instance with no error message and with no suggestion
* that full and complete help for the command should be shown. This will
* result in a generic error message.
*
* @param command the command
*/
public InvalidUsageException(CommandCallable command) {
this(null, command);
}
public InvalidUsageException(String message, CommandCallable command) {
/**
* Create a new instance with a message and with no suggestion
* that full and complete help for the command should be shown.
*
* @param message the message
* @param command the command
*/
public InvalidUsageException(@Nullable String message, CommandCallable command) {
this(message, command, false);
}
public InvalidUsageException(String message, CommandCallable command, boolean fullUsageSuggested) {
/**
* Create a new instance with a message.
*
* @param message the message
* @param command the command
* @param fullHelpSuggested true if the full help for the command should be shown
*/
public InvalidUsageException(@Nullable String message, CommandCallable command, boolean fullHelpSuggested) {
super(message);
checkNotNull(command);
this.command = command;
this.fullUsageSuggested = fullUsageSuggested;
this.fullHelpSuggested = fullHelpSuggested;
}
/**
* Get the command.
*
* @return the command
*/
public CommandCallable getCommand() {
return command;
}
public String getUsage(String prefix) {
return toStackString(prefix, command.getDescription().getUsage());
/**
* Get a simple usage string.
*
* @param prefix the command shebang (such as "/") -- may be blank
* @return a usage string
*/
public String getSimpleUsageString(String prefix) {
return getCommandUsed(prefix, command.getDescription().getUsage());
}
/**
@ -60,7 +101,7 @@ public class InvalidUsageException extends CommandException {
*
* @return show full usage
*/
public boolean isFullUsageSuggested() {
return fullUsageSuggested;
public boolean isFullHelpSuggested() {
return fullHelpSuggested;
}
}