[1.18] Fix EnumArgument to use enum names for suggestions (#8746)

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).

Using Enum#name fixes this discrepancy, so now the suggestions are
always valid inputs regardless of the return of Enum#toString.

Fixes #8618
This commit is contained in:
sciwhiz12 authored and GitHub committed 2022-07-17 15:32:10 +01:00
1 parent af9b4c1fe5
commit d264674a85
1 file changed
+3 -3
@@ -42,18 +42,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 Serializer implements ArgumentSerializer<EnumArgument<?>>