Improve the exception message for LocationArgument

This commit is contained in:
jmp 2020-10-23 13:44:57 -07:00 committed by Alexander Söderberg
parent cfac2639ad
commit e26d01388d
3 changed files with 53 additions and 14 deletions

View file

@ -83,7 +83,13 @@ public final class BukkitCaptionKeys {
/** /**
* Variables: {input} * Variables: {input}
*/ */
public static final Caption ARGUMENT_PARSE_FAILURE_LOCATION = of("argument.parse.failure.location"); public static final Caption ARGUMENT_PARSE_FAILURE_LOCATION_INVALID_FORMAT = of(
"argument.parse.failure.location.invalid_format");
/**
* Variables: None
*/
public static final Caption ARGUMENT_PARSE_FAILURE_LOCATION_MIXED_LOCAL_ABSOLUTE = of(
"argument.parse.failure.location.mixed_local_absolute");
private BukkitCaptionKeys() { private BukkitCaptionKeys() {
} }

View file

@ -74,10 +74,15 @@ public class BukkitCaptionRegistry<C> extends SimpleCaptionRegistry<C> {
*/ */
public static final String ARGUMENT_PARSE_FAILURE_SELECTOR_NON_PLAYER = "Non-player(s) selected in player selector."; public static final String ARGUMENT_PARSE_FAILURE_SELECTOR_NON_PLAYER = "Non-player(s) selected in player selector.";
/** /**
* Default caption for {@link BukkitCaptionKeys#ARGUMENT_PARSE_FAILURE_LOCATION} * Default caption for {@link BukkitCaptionKeys#ARGUMENT_PARSE_FAILURE_LOCATION_INVALID_FORMAT}
*/ */
public static final String ARGUMENT_PARSE_FAILURE_LOCATION = public static final String ARGUMENT_PARSE_FAILURE_LOCATION_INVALID_FORMAT =
"'{input}' is not a valid location. Required format is '<x> <y> <z>'"; "'{input}' is not a valid location. Required format is '<x> <y> <z>'";
/**
* Default caption for {@link BukkitCaptionKeys#ARGUMENT_PARSE_FAILURE_LOCATION_MIXED_LOCAL_ABSOLUTE}
*/
public static final String ARGUMENT_PARSE_FAILURE_LOCATION_MIXED_LOCAL_ABSOLUTE =
"Cannot mix local and absolute coordinates. (either all coordinates use '^' or none do)";
protected BukkitCaptionRegistry() { protected BukkitCaptionRegistry() {
super(); super();
@ -122,8 +127,12 @@ public class BukkitCaptionRegistry<C> extends SimpleCaptionRegistry<C> {
(caption, sender) -> ARGUMENT_PARSE_FAILURE_SELECTOR_NON_PLAYER (caption, sender) -> ARGUMENT_PARSE_FAILURE_SELECTOR_NON_PLAYER
); );
this.registerMessageFactory( this.registerMessageFactory(
BukkitCaptionKeys.ARGUMENT_PARSE_FAILURE_LOCATION, BukkitCaptionKeys.ARGUMENT_PARSE_FAILURE_LOCATION_INVALID_FORMAT,
(caption, sender) -> ARGUMENT_PARSE_FAILURE_LOCATION (caption, sender) -> ARGUMENT_PARSE_FAILURE_LOCATION_INVALID_FORMAT
);
this.registerMessageFactory(
BukkitCaptionKeys.ARGUMENT_PARSE_FAILURE_LOCATION_MIXED_LOCAL_ABSOLUTE,
(caption, sender) -> ARGUMENT_PARSE_FAILURE_LOCATION_MIXED_LOCAL_ABSOLUTE
); );
} }

View file

@ -28,6 +28,7 @@ import cloud.commandframework.arguments.parser.ArgumentParseResult;
import cloud.commandframework.arguments.parser.ArgumentParser; import cloud.commandframework.arguments.parser.ArgumentParser;
import cloud.commandframework.arguments.standard.IntegerArgument; import cloud.commandframework.arguments.standard.IntegerArgument;
import cloud.commandframework.bukkit.BukkitCaptionKeys; import cloud.commandframework.bukkit.BukkitCaptionKeys;
import cloud.commandframework.captions.Caption;
import cloud.commandframework.captions.CaptionVariable; import cloud.commandframework.captions.CaptionVariable;
import cloud.commandframework.context.CommandContext; import cloud.commandframework.context.CommandContext;
import cloud.commandframework.exceptions.parsing.ParserException; import cloud.commandframework.exceptions.parsing.ParserException;
@ -167,6 +168,7 @@ public final class LocationArgument<C> extends CommandArgument<C, Location> {
return ArgumentParseResult.failure( return ArgumentParseResult.failure(
new LocationParseException( new LocationParseException(
commandContext, commandContext,
LocationParseException.FailureReason.WRONG_FORMAT,
input.toString() input.toString()
) )
); );
@ -200,17 +202,11 @@ public final class LocationArgument<C> extends CommandArgument<C, Location> {
|| ((coordinates[0].getType() == LocationCoordinateType.LOCAL) || ((coordinates[0].getType() == LocationCoordinateType.LOCAL)
!= (coordinates[2].getType() == LocationCoordinateType.LOCAL)) != (coordinates[2].getType() == LocationCoordinateType.LOCAL))
) { ) {
final StringBuilder input = new StringBuilder();
for (int i = 0; i < inputQueue.size(); i++) {
input.append(((LinkedList<String>) inputQueue).get(i));
if ((i + 1) < inputQueue.size()) {
input.append(" ");
}
}
return ArgumentParseResult.failure( return ArgumentParseResult.failure(
new LocationParseException( new LocationParseException(
commandContext, commandContext,
input.toString() LocationParseException.FailureReason.MIXED_LOCAL_ABSOLUTE,
""
) )
); );
} }
@ -293,16 +289,44 @@ public final class LocationArgument<C> extends CommandArgument<C, Location> {
protected LocationParseException( protected LocationParseException(
final @NonNull CommandContext<?> context, final @NonNull CommandContext<?> context,
final @NonNull FailureReason reason,
final @NonNull String input final @NonNull String input
) { ) {
super( super(
LocationParser.class, LocationParser.class,
context, context,
BukkitCaptionKeys.ARGUMENT_PARSE_FAILURE_LOCATION, reason.getCaption(),
CaptionVariable.of("input", input) CaptionVariable.of("input", input)
); );
} }
/**
* Reasons for which location parsing may fail
*/
public enum FailureReason {
WRONG_FORMAT(BukkitCaptionKeys.ARGUMENT_PARSE_FAILURE_LOCATION_INVALID_FORMAT),
MIXED_LOCAL_ABSOLUTE(BukkitCaptionKeys.ARGUMENT_PARSE_FAILURE_LOCATION_MIXED_LOCAL_ABSOLUTE);
private final Caption caption;
FailureReason(final @NonNull Caption caption) {
this.caption = caption;
}
/**
* Get the caption used for this failure reason
*
* @return The caption
*/
public @NonNull Caption getCaption() {
return this.caption;
}
}
} }
} }