diff --git a/src/main/java/com/sk89q/minecraft/util/commands/CommandException.java b/src/main/java/com/sk89q/minecraft/util/commands/CommandException.java index b6e30825f..968c31e6a 100644 --- a/src/main/java/com/sk89q/minecraft/util/commands/CommandException.java +++ b/src/main/java/com/sk89q/minecraft/util/commands/CommandException.java @@ -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); diff --git a/src/main/java/com/sk89q/worldedit/extension/platform/CommandManager.java b/src/main/java/com/sk89q/worldedit/extension/platform/CommandManager.java index 56fc1754b..94e7dec03 100644 --- a/src/main/java/com/sk89q/worldedit/extension/platform/CommandManager.java +++ b/src/main/java/com/sk89q/worldedit/extension/platform/CommandManager.java @@ -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(); diff --git a/src/main/java/com/sk89q/worldedit/util/command/InvalidUsageException.java b/src/main/java/com/sk89q/worldedit/util/command/InvalidUsageException.java index 56195cdf2..9795aaaef 100644 --- a/src/main/java/com/sk89q/worldedit/util/command/InvalidUsageException.java +++ b/src/main/java/com/sk89q/worldedit/util/command/InvalidUsageException.java @@ -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. + *

+ * 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. + *

+ * 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; } }