🎨 Add captions for Bukkit entity selector arguments

This commit is contained in:
jmp 2020-10-16 11:06:45 -07:00 • committed by Alexander Söderberg
parent 366c4f2ce5
commit c67cc35cf6
7 changed files with 178 additions and 24 deletions

View file

@ -57,6 +57,29 @@ public final class BukkitCaptionKeys {
* Variables: {input}
*/
public static final Caption ARGUMENT_PARSE_FAILURE_WORLD = of("argument.parse.failure.world");
/**
* Variables: {input}
*/
public static final Caption ARGUMENT_PARSE_FAILURE_SELECTOR_MALFORMED = of("argument.parse.failure.selector.malformed");
/**
* Variables: None
*/
public static final Caption ARGUMENT_PARSE_FAILURE_SELECTOR_UNSUPPORTED = of("argument.parse.failure.selector.unsupported");
/**
* Variables: None
*/
public static final Caption ARGUMENT_PARSE_FAILURE_SELECTOR_TOO_MANY_PLAYERS = of(
"argument.parse.failure.selector.too_many_players");
/**
* Variables: None
*/
public static final Caption ARGUMENT_PARSE_FAILURE_SELECTOR_TOO_MANY_ENTITIES = of(
"argument.parse.failure.selector.too_many_entities");
/**
* Variables: None
*/
public static final Caption ARGUMENT_PARSE_FAILURE_SELECTOR_NON_PLAYER = of(
"argument.parse.failure.selector.non_player_in_player_selector");
private BukkitCaptionKeys() {
}

View file

@ -37,21 +37,43 @@ public class BukkitCaptionRegistry<C> extends SimpleCaptionRegistry<C> {
*/
public static final String ARGUMENT_PARSE_FAILURE_ENCHANTMENT = "'{input}' is not a valid enchantment";
/**
* Default caption for {@link BukkitCaptionKeys#ARGUMENT_PARSE_FAILURE_ENCHANTMENT}
* Default caption for {@link BukkitCaptionKeys#ARGUMENT_PARSE_FAILURE_MATERIAL}
*/
public static final String ARGUMENT_PARSE_FAILURE_MATERIAL = "'{input}' is not a valid material name";
/**
* Default caption for {@link BukkitCaptionKeys#ARGUMENT_PARSE_FAILURE_ENCHANTMENT}
* Default caption for {@link BukkitCaptionKeys#ARGUMENT_PARSE_FAILURE_OFFLINEPLAYER}
*/
public static final String ARGUMENT_PARSE_FAILURE_OFFLINEPLAYER = "No player found for input '{input}'";
/**
* Default caption for {@link BukkitCaptionKeys#ARGUMENT_PARSE_FAILURE_ENCHANTMENT}
* Default caption for {@link BukkitCaptionKeys#ARGUMENT_PARSE_FAILURE_PLAYER}
*/
public static final String ARGUMENT_PARSE_FAILURE_PLAYER = "No player found for input '{input}'";
/**
* Default caption for {@link BukkitCaptionKeys#ARGUMENT_PARSE_FAILURE_ENCHANTMENT}
* Default caption for {@link BukkitCaptionKeys#ARGUMENT_PARSE_FAILURE_WORLD}
*/
public static final String ARGUMENT_PARSE_FAILURE_WORLD = "'{input}' is not a valid Minecraft world";
/**
* Default caption for {@link BukkitCaptionKeys#ARGUMENT_PARSE_FAILURE_SELECTOR_MALFORMED}
*/
public static final String ARGUMENT_PARSE_FAILURE_SELECTOR_MALFORMED = "Selector '{input}' is malformed.";
/**
* Default caption for {@link BukkitCaptionKeys#ARGUMENT_PARSE_FAILURE_SELECTOR_UNSUPPORTED}
*/
public static final String ARGUMENT_PARSE_FAILURE_SELECTOR_UNSUPPORTED =
"Entity selector argument type not supported below Minecraft 1.13.";
/**
* Default caption for {@link BukkitCaptionKeys#ARGUMENT_PARSE_FAILURE_SELECTOR_TOO_MANY_PLAYERS}
*/
public static final String ARGUMENT_PARSE_FAILURE_SELECTOR_TOO_MANY_PLAYERS = "More than 1 player selected in single player selector";
/**
* Default caption for {@link BukkitCaptionKeys#ARGUMENT_PARSE_FAILURE_SELECTOR_TOO_MANY_ENTITIES}
*/
public static final String ARGUMENT_PARSE_FAILURE_SELECTOR_TOO_MANY_ENTITIES = "More than 1 entity selected in single entity selector.";
/**
* Default caption for {@link BukkitCaptionKeys#ARGUMENT_PARSE_FAILURE_SELECTOR_NON_PLAYER}
*/
public static final String ARGUMENT_PARSE_FAILURE_SELECTOR_NON_PLAYER = "Non-player(s) selected in player selector.";
protected BukkitCaptionRegistry() {
super();
@ -75,6 +97,26 @@ public class BukkitCaptionRegistry<C> extends SimpleCaptionRegistry<C> {
BukkitCaptionKeys.ARGUMENT_PARSE_FAILURE_WORLD,
(caption, sender) -> ARGUMENT_PARSE_FAILURE_WORLD
);
this.registerMessageFactory(
BukkitCaptionKeys.ARGUMENT_PARSE_FAILURE_SELECTOR_MALFORMED,
(caption, sender) -> ARGUMENT_PARSE_FAILURE_SELECTOR_MALFORMED
);
this.registerMessageFactory(
BukkitCaptionKeys.ARGUMENT_PARSE_FAILURE_SELECTOR_UNSUPPORTED,
(caption, sender) -> ARGUMENT_PARSE_FAILURE_SELECTOR_UNSUPPORTED
);
this.registerMessageFactory(
BukkitCaptionKeys.ARGUMENT_PARSE_FAILURE_SELECTOR_TOO_MANY_PLAYERS,
(caption, sender) -> ARGUMENT_PARSE_FAILURE_SELECTOR_TOO_MANY_PLAYERS
);
this.registerMessageFactory(
BukkitCaptionKeys.ARGUMENT_PARSE_FAILURE_SELECTOR_TOO_MANY_ENTITIES,
(caption, sender) -> ARGUMENT_PARSE_FAILURE_SELECTOR_TOO_MANY_ENTITIES
);
this.registerMessageFactory(
BukkitCaptionKeys.ARGUMENT_PARSE_FAILURE_SELECTOR_NON_PLAYER,
(caption, sender) -> ARGUMENT_PARSE_FAILURE_SELECTOR_NON_PLAYER
);
}
}

View file

@ -132,8 +132,12 @@ public final class MultipleEntitySelectorArgument<C> extends CommandArgument<C,
) {
if (!commandContext.<Set<CloudBukkitCapabilities>>get("CloudBukkitCapabilities").contains(
CloudBukkitCapabilities.BRIGADIER)) {
return ArgumentParseResult.failure(
new IllegalArgumentException("Entity selector argument type not supported below Minecraft 1.13."));
return ArgumentParseResult.failure(new SelectorParseException(
"",
commandContext,
SelectorParseException.FailureReason.UNSUPPORTED_VERSION,
MultipleEntitySelectorParser.class
));
}
final String input = inputQueue.peek();
if (input == null) {
@ -145,7 +149,12 @@ public final class MultipleEntitySelectorArgument<C> extends CommandArgument<C,
try {
entities = Bukkit.selectEntities(commandContext.get("BukkitCommandSender"), input);
} catch (IllegalArgumentException e) {
return ArgumentParseResult.failure(new SelectorParseException(input));
return ArgumentParseResult.failure(new SelectorParseException(
input,
commandContext,
SelectorParseException.FailureReason.MALFORMED_SELECTOR,
MultipleEntitySelectorParser.class
));
}
return ArgumentParseResult.success(new MultipleEntitySelector(input, entities));

View file

@ -155,12 +155,22 @@ public final class MultiplePlayerSelectorArgument<C> extends CommandArgument<C,
try {
entities = Bukkit.selectEntities(commandContext.get("BukkitCommandSender"), input);
} catch (IllegalArgumentException e) {
return ArgumentParseResult.failure(new SelectorParseException(input));
return ArgumentParseResult.failure(new SelectorParseException(
input,
commandContext,
SelectorParseException.FailureReason.MALFORMED_SELECTOR,
MultiplePlayerSelectorParser.class
));
}
for (Entity e : entities) {
if (!(e instanceof Player)) {
return ArgumentParseResult.failure(new IllegalArgumentException("Non-players selected in player selector."));
return ArgumentParseResult.failure(new SelectorParseException(
input,
commandContext,
SelectorParseException.FailureReason.NON_PLAYER_IN_PLAYER_SELECTOR,
MultiplePlayerSelectorParser.class
));
}
}

View file

@ -23,12 +23,17 @@
//
package cloud.commandframework.bukkit.parsers.selector;
import cloud.commandframework.bukkit.BukkitCaptionKeys;
import cloud.commandframework.captions.Caption;
import cloud.commandframework.captions.CaptionVariable;
import cloud.commandframework.context.CommandContext;
import cloud.commandframework.exceptions.parsing.ParserException;
import org.checkerframework.checker.nullness.qual.NonNull;
/**
* EntitySelector parse exception
*/
public final class SelectorParseException extends IllegalArgumentException {
public final class SelectorParseException extends ParserException {
private final String input;
@ -36,8 +41,22 @@ public final class SelectorParseException extends IllegalArgumentException {
* Construct a new EntitySelector parse exception
*
* @param input String input
* @param context Command context
* @param reason Reason for parse failure
* @param parser The parser class
*/
public SelectorParseException(final @NonNull String input) {
public SelectorParseException(
final @NonNull String input,
final @NonNull CommandContext<?> context,
final @NonNull FailureReason reason,
final @NonNull Class<?> parser
) {
super(
parser,
context,
reason.getCaption(),
CaptionVariable.of("input", input)
);
this.input = input;
}
@ -50,9 +69,33 @@ public final class SelectorParseException extends IllegalArgumentException {
return input;
}
@Override
public String getMessage() {
return String.format("Selector '%s' is malformed.", input);
/**
* Reasons for which selector parsing may fail
*/
public enum FailureReason {
UNSUPPORTED_VERSION(BukkitCaptionKeys.ARGUMENT_PARSE_FAILURE_SELECTOR_UNSUPPORTED),
MALFORMED_SELECTOR(BukkitCaptionKeys.ARGUMENT_PARSE_FAILURE_SELECTOR_MALFORMED),
TOO_MANY_PLAYERS(BukkitCaptionKeys.ARGUMENT_PARSE_FAILURE_SELECTOR_TOO_MANY_PLAYERS),
TOO_MANY_ENTITIES(BukkitCaptionKeys.ARGUMENT_PARSE_FAILURE_SELECTOR_TOO_MANY_ENTITIES),
NON_PLAYER_IN_PLAYER_SELECTOR(BukkitCaptionKeys.ARGUMENT_PARSE_FAILURE_SELECTOR_NON_PLAYER);
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;
}
}
}

View file

@ -130,8 +130,12 @@ public final class SingleEntitySelectorArgument<C> extends CommandArgument<C, Si
) {
if (!commandContext.<Set<CloudBukkitCapabilities>>get("CloudBukkitCapabilities").contains(
CloudBukkitCapabilities.BRIGADIER)) {
return ArgumentParseResult.failure(
new IllegalArgumentException("Entity selector argument type not supported below Minecraft 1.13."));
return ArgumentParseResult.failure(new SelectorParseException(
"",
commandContext,
SelectorParseException.FailureReason.UNSUPPORTED_VERSION,
SingleEntitySelectorParser.class
));
}
final String input = inputQueue.peek();
if (input == null) {
@ -143,12 +147,21 @@ public final class SingleEntitySelectorArgument<C> extends CommandArgument<C, Si
try {
entities = Bukkit.selectEntities(commandContext.get("BukkitCommandSender"), input);
} catch (IllegalArgumentException e) {
return ArgumentParseResult.failure(new SelectorParseException(input));
return ArgumentParseResult.failure(new SelectorParseException(
input,
commandContext,
SelectorParseException.FailureReason.MALFORMED_SELECTOR,
SingleEntitySelectorParser.class
));
}
if (entities.size() > 1) {
return ArgumentParseResult.failure(
new IllegalArgumentException("More than 1 entity selected in single entity selector."));
return ArgumentParseResult.failure(new SelectorParseException(
input,
commandContext,
SelectorParseException.FailureReason.TOO_MANY_ENTITIES,
SingleEntitySelectorParser.class
));
}
return ArgumentParseResult.success(new SingleEntitySelector(input, entities));

View file

@ -153,17 +153,31 @@ public final class SinglePlayerSelectorArgument<C> extends CommandArgument<C, Si
try {
entities = Bukkit.selectEntities(commandContext.get("BukkitCommandSender"), input);
} catch (IllegalArgumentException e) {
return ArgumentParseResult.failure(new SelectorParseException(input));
return ArgumentParseResult.failure(new SelectorParseException(
input,
commandContext,
SelectorParseException.FailureReason.MALFORMED_SELECTOR,
SinglePlayerSelectorParser.class
));
}
for (Entity e : entities) {
if (!(e instanceof Player)) {
return ArgumentParseResult.failure(new IllegalArgumentException("Non-players selected in player selector."));
return ArgumentParseResult.failure(new SelectorParseException(
input,
commandContext,
SelectorParseException.FailureReason.NON_PLAYER_IN_PLAYER_SELECTOR,
SinglePlayerSelectorParser.class
));
}
}
if (entities.size() > 1) {
return ArgumentParseResult.failure(
new IllegalArgumentException("More than 1 player selected in single player selector"));
return ArgumentParseResult.failure(new SelectorParseException(
input,
commandContext,
SelectorParseException.FailureReason.TOO_MANY_PLAYERS,
SinglePlayerSelectorParser.class
));
}
return ArgumentParseResult.success(new SinglePlayerSelector(input, entities));