personthecat.catlib.command.arguments.EnumArgument Maven / Gradle / Ivy
package personthecat.catlib.command.arguments;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import personthecat.catlib.exception.Exceptions;
import personthecat.catlib.util.LibReference;
import personthecat.catlib.util.McUtils;
import personthecat.catlib.util.Shorthand;
import personthecat.fresult.Void;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;
import net.minecraft.class_2172;
import net.minecraft.class_2316;
import net.minecraft.class_2319;
public class EnumArgument> implements ArgumentType {
public static void register() {
class_2316.method_10017(LibReference.MOD_ID + ":enum_argument", EnumArgument.class,
new class_2319<>(() -> new EnumArgument<>(Void.class)));
}
private final Class enumClass;
private EnumArgument(final Class enumClass) {
this.enumClass = enumClass;
}
public static > EnumArgument> of(final Class enumClass) {
return new EnumArgument<>(enumClass);
}
@Override
public E parse(final StringReader reader) throws CommandSyntaxException {
return Shorthand.getEnumConstant(reader.readUnquotedString(), this.enumClass)
.orElseThrow(() -> Exceptions.cmdSyntax(reader, "No such value"));
}
@Override
public CompletableFuture listSuggestions(final CommandContext ctx, final SuggestionsBuilder sb) {
return class_2172.method_9264(
Stream.of(this.enumClass.getEnumConstants()).map(e -> e.toString().toLowerCase()), sb);
}
}