|
| 1 | +package core.paper.command.argument; |
| 2 | + |
| 3 | +import com.mojang.brigadier.StringReader; |
| 4 | +import com.mojang.brigadier.arguments.ArgumentType; |
| 5 | +import com.mojang.brigadier.arguments.StringArgumentType; |
| 6 | +import com.mojang.brigadier.context.CommandContext; |
| 7 | +import com.mojang.brigadier.exceptions.CommandSyntaxException; |
| 8 | +import com.mojang.brigadier.suggestion.Suggestions; |
| 9 | +import com.mojang.brigadier.suggestion.SuggestionsBuilder; |
| 10 | +import core.paper.command.ComponentCommandExceptionType; |
| 11 | +import core.paper.command.argument.codec.EnumStringCodec; |
| 12 | +import io.papermc.paper.command.brigadier.argument.CustomArgumentType; |
| 13 | +import net.kyori.adventure.text.Component; |
| 14 | +import org.jetbrains.annotations.Contract; |
| 15 | +import org.jspecify.annotations.NullMarked; |
| 16 | + |
| 17 | +import java.util.Arrays; |
| 18 | +import java.util.concurrent.CompletableFuture; |
| 19 | + |
| 20 | +/** |
| 21 | + * Represents an argument type for parsing and suggesting enum constants. |
| 22 | + * This class allows for conversion between string representations and enum constants of a specified type, |
| 23 | + * enabling the creation of command arguments based on enums. |
| 24 | + * |
| 25 | + * @param <E> the type of the enum |
| 26 | + * @since 2.3.0 |
| 27 | + */ |
| 28 | +@NullMarked |
| 29 | +public final class EnumArgumentType<E extends Enum<E>> implements CustomArgumentType<E, String> { |
| 30 | + private final Class<E> enumClass; |
| 31 | + private final EnumStringCodec codec; |
| 32 | + |
| 33 | + private EnumArgumentType(Class<E> enumClass, EnumStringCodec codec) { |
| 34 | + this.enumClass = enumClass; |
| 35 | + this.codec = codec; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Creates a new instance of {@link EnumArgumentType} for the specified enum class and string codec. |
| 40 | + * |
| 41 | + * @param <E> the type of the enum |
| 42 | + * @param enumClass the class of the enum type for which the argument type is being created |
| 43 | + * @param codec the {@link EnumStringCodec} responsible for converting between strings and enum constants |
| 44 | + * @return a new {@link EnumArgumentType} instance |
| 45 | + */ |
| 46 | + @Contract(value = "_, _ -> new", pure = true) |
| 47 | + public static <E extends Enum<E>> EnumArgumentType<E> of(Class<E> enumClass, EnumStringCodec codec) { |
| 48 | + return new EnumArgumentType<>(enumClass, codec); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public E parse(StringReader reader) throws CommandSyntaxException { |
| 53 | + var type = getNativeType().parse(reader); |
| 54 | + try { |
| 55 | + return codec.fromString(enumClass, type); |
| 56 | + } catch (IllegalArgumentException ignore) { |
| 57 | + throw new ComponentCommandExceptionType( |
| 58 | + Component.text("No such enum constant: " + type) |
| 59 | + ).createWithContext(reader); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) { |
| 65 | + Arrays.stream(enumClass.getEnumConstants()) |
| 66 | + .map(codec::toString) |
| 67 | + .map(StringArgumentType::escapeIfRequired) |
| 68 | + .filter(s -> s.contains(builder.getRemaining())) |
| 69 | + .forEach(builder::suggest); |
| 70 | + return builder.buildFuture(); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public ArgumentType<String> getNativeType() { |
| 75 | + return StringArgumentType.string(); |
| 76 | + } |
| 77 | +} |
0 commit comments