Fix EnumArgument to use enum names for suggestions (#8728)

Previously, the suggestions used the string representation of the enum
through Enum#toString, which can differ from the name of the enum as
required by Enum#valueOf, causing invalid suggestions (both in gui and
through the error message).
This commit is contained in:
sciwhiz12 authored and GitHub committed 2022-06-15 14:15:32 -07:00
1 parent 5979636ee5
commit 915d570b17
1 file changed
+3 -3
@@ -43,18 +43,18 @@ public class EnumArgument<T extends Enum<T>> implements ArgumentType<T> {
try {
return Enum.valueOf(enumClass, name);
} catch (IllegalArgumentException e) {
throw INVALID_ENUM.createWithContext(reader, name, Arrays.toString(enumClass.getEnumConstants()));
throw INVALID_ENUM.createWithContext(reader, name, Arrays.toString(Arrays.stream(enumClass.getEnumConstants()).map(Enum::name).toArray()));
}
}
@Override
public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
return SharedSuggestionProvider.suggest(Stream.of(enumClass.getEnumConstants()).map(Object::toString), builder);
return SharedSuggestionProvider.suggest(Stream.of(enumClass.getEnumConstants()).map(Enum::name), builder);
}
@Override
public Collection<String> getExamples() {
return Stream.of(enumClass.getEnumConstants()).map(Object::toString).collect(Collectors.toList());
return Stream.of(enumClass.getEnumConstants()).map(Enum::name).collect(Collectors.toList());
}
public static class Info<T extends Enum<T>> implements ArgumentTypeInfo<EnumArgument<T>, Info<T>.Template>