All Downloads are FREE. Search and download functionalities are using the official Maven repository.

personthecat.catlib.command.arguments.PathArgument Maven / Gradle / Ivy

Go to download

Utilities for serialization, commands, noise generation, IO, and some new data types.

The newest version!
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.command.CommandUtils;
import personthecat.catlib.serialization.json.JsonPath;
import personthecat.catlib.serialization.json.XjsUtils;
import personthecat.catlib.util.LibReference;
import xjs.core.JsonObject;

import java.util.Collections;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import net.minecraft.class_2172;
import net.minecraft.class_2316;
import net.minecraft.class_2319;

@SuppressWarnings("unused")
public class PathArgument implements ArgumentType {

    public static void register() {
        class_2316.method_10017(LibReference.MOD_ID + ":path_argument", PathArgument.class,
            new class_2319<>(PathArgument::new));
    }

    @Override
    public  CompletableFuture listSuggestions(final CommandContext ctx, final SuggestionsBuilder builder) {
        final Optional json = CommandUtils.getLastArg(ctx, JsonArgument.class, JsonArgument.Result.class)
            .map(arg -> arg.json.get());
        if (json.isEmpty()) {
            return Suggestions.empty();
        }
        final JsonPath path = CommandUtils.getLastArg(ctx, PathArgument.class, JsonPath.class)
            .orElseGet(() -> new JsonPath(Collections.emptyList()));
        return class_2172.method_9265(XjsUtils.getPaths(json.get(), path), builder);
    }

    @Override
    public JsonPath parse(final StringReader reader) throws CommandSyntaxException {
        return JsonPath.parse(reader);
    }
}