✨ Add exception messages so invalid input doesn't result in 'Invalid Command Argument: null' (#13)
Co-authored-by: Alexander Söderberg <Sauilitired@users.noreply.github.com>
This commit is contained in:
parent
bea9c54841
commit
09e3e7aa13
7 changed files with 48 additions and 10 deletions
|
|
@ -23,6 +23,9 @@
|
|||
//
|
||||
package cloud.commandframework.arguments.parser;
|
||||
|
||||
import cloud.commandframework.arguments.standard.UUIDArgument;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.reflect.TypeToken;
|
||||
import cloud.commandframework.annotations.specifier.Completions;
|
||||
import cloud.commandframework.annotations.specifier.Range;
|
||||
import cloud.commandframework.arguments.standard.BooleanArgument;
|
||||
|
|
@ -44,6 +47,7 @@ import java.util.Collection;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
|
||||
|
|
@ -103,7 +107,8 @@ public final class StandardParserRegistry<C> implements ParserRegistry<C> {
|
|||
StringArgument.StringMode.SINGLE, (context, s) ->
|
||||
Arrays.asList(options.get(StandardParameters.COMPLETIONS, new String[0]))));
|
||||
/* Add options to this */
|
||||
this.registerParserSupplier(TypeToken.get(Boolean.class), options -> new BooleanArgument.BooleanParser<>(false));
|
||||
this.registerParserSupplier(TypeToken.of(Boolean.class), options -> new BooleanArgument.BooleanParser<>(false));
|
||||
this.registerParserSupplier(TypeToken.of(UUID.class), options -> new UUIDArgument.UUIDParser<>());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -241,5 +241,10 @@ public final class BooleanArgument<C> extends CommandArgument<C, Boolean> {
|
|||
public boolean isLiberal() {
|
||||
return liberal;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return String.format("Could not parse boolean from '%s'.", input);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -145,7 +145,7 @@ public final class CharArgument<C> extends CommandArgument<C, Character> {
|
|||
private final String input;
|
||||
|
||||
/**
|
||||
* Construct a new boolean parse exception
|
||||
* Construct a new Char parse exception
|
||||
*
|
||||
* @param input String input
|
||||
*/
|
||||
|
|
@ -161,5 +161,10 @@ public final class CharArgument<C> extends CommandArgument<C, Character> {
|
|||
public @NonNull String getInput() {
|
||||
return input;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return String.format("'%s' is not a valid character.", input);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -112,7 +112,7 @@ public final class UUIDArgument<C> extends CommandArgument<C, UUID> {
|
|||
}
|
||||
|
||||
|
||||
private static final class UUIDParser<C> implements ArgumentParser<C, UUID> {
|
||||
public static final class UUIDParser<C> implements ArgumentParser<C, UUID> {
|
||||
|
||||
@Override
|
||||
public @NonNull ArgumentParseResult<UUID> parse(
|
||||
|
|
@ -141,14 +141,20 @@ public final class UUIDArgument<C> extends CommandArgument<C, UUID> {
|
|||
|
||||
public static final class UUIDParseException extends IllegalArgumentException {
|
||||
|
||||
private final String input;
|
||||
|
||||
/**
|
||||
* Construct a new example parse exception
|
||||
* Construct a new UUID parse exception
|
||||
*
|
||||
* @param input String input
|
||||
*/
|
||||
public UUIDParseException(@NonNull final String input) {
|
||||
super(input);
|
||||
this.input = input;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return String.format("Could not parse UUID from '%s'.", input);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,13 +26,17 @@ package cloud.commandframework.bukkit;
|
|||
import cloud.commandframework.CommandManager;
|
||||
import cloud.commandframework.CommandTree;
|
||||
import cloud.commandframework.bukkit.parsers.MaterialArgument;
|
||||
import cloud.commandframework.bukkit.parsers.OfflinePlayerArgument;
|
||||
import cloud.commandframework.bukkit.parsers.PlayerArgument;
|
||||
import cloud.commandframework.bukkit.parsers.WorldArgument;
|
||||
import cloud.commandframework.execution.CommandExecutionCoordinator;
|
||||
import io.leangen.geantyref.TypeToken;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
|
|
@ -87,6 +91,11 @@ public class BukkitCommandManager<C> extends CommandManager<C> {
|
|||
this.getParserRegistry().registerParserSupplier(TypeToken.get(World.class), params -> new WorldArgument.WorldParser<>());
|
||||
this.getParserRegistry().registerParserSupplier(TypeToken.get(Material.class),
|
||||
params -> new MaterialArgument.MaterialParser<>());
|
||||
this.getParserRegistry()
|
||||
.registerParserSupplier(TypeToken.of(Player.class), params -> new PlayerArgument.PlayerParser<>());
|
||||
this.getParserRegistry()
|
||||
.registerParserSupplier(TypeToken.of(OfflinePlayer.class),
|
||||
params -> new OfflinePlayerArgument.OfflinePlayerParser<>());
|
||||
|
||||
/* Try to determine the Minecraft version */
|
||||
int version = -1;
|
||||
|
|
|
|||
|
|
@ -124,7 +124,7 @@ public final class OfflinePlayerArgument<C> extends CommandArgument<C, OfflinePl
|
|||
}
|
||||
|
||||
|
||||
private static final class OfflinePlayerParser<C> implements ArgumentParser<C, OfflinePlayer> {
|
||||
public static final class OfflinePlayerParser<C> implements ArgumentParser<C, OfflinePlayer> {
|
||||
|
||||
@Override
|
||||
public @NonNull ArgumentParseResult<OfflinePlayer> parse(@NonNull final CommandContext<C> commandContext,
|
||||
|
|
@ -160,14 +160,14 @@ public final class OfflinePlayerArgument<C> extends CommandArgument<C, OfflinePl
|
|||
|
||||
|
||||
/**
|
||||
* Player parse exception
|
||||
* OfflinePlayer parse exception
|
||||
*/
|
||||
public static final class OfflinePlayerParseException extends IllegalArgumentException {
|
||||
|
||||
private final String input;
|
||||
|
||||
/**
|
||||
* Construct a new boolean parse exception
|
||||
* Construct a new OfflinePlayer parse exception
|
||||
*
|
||||
* @param input String input
|
||||
*/
|
||||
|
|
@ -184,5 +184,9 @@ public final class OfflinePlayerArgument<C> extends CommandArgument<C, OfflinePl
|
|||
return input;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return String.format("No player found for input '%s'.", input);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -119,7 +119,7 @@ public final class PlayerArgument<C> extends CommandArgument<C, Player> {
|
|||
}
|
||||
|
||||
|
||||
private static final class PlayerParser<C> implements ArgumentParser<C, Player> {
|
||||
public static final class PlayerParser<C> implements ArgumentParser<C, Player> {
|
||||
|
||||
@Override
|
||||
public @NonNull ArgumentParseResult<Player> parse(@NonNull final CommandContext<C> commandContext,
|
||||
|
|
@ -162,7 +162,7 @@ public final class PlayerArgument<C> extends CommandArgument<C, Player> {
|
|||
private final String input;
|
||||
|
||||
/**
|
||||
* Construct a new boolean parse exception
|
||||
* Construct a new Player parse exception
|
||||
*
|
||||
* @param input String input
|
||||
*/
|
||||
|
|
@ -179,5 +179,9 @@ public final class PlayerArgument<C> extends CommandArgument<C, Player> {
|
|||
return input;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return String.format("No player found for input '%s'.", input);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue