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

personthecat.catlib.command.CommandContextWrapper 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;

import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.context.ParsedCommandNode;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.class_124;
import net.minecraft.class_1297;
import net.minecraft.class_1657;
import net.minecraft.class_1792;
import net.minecraft.class_1934;
import net.minecraft.class_1937;
import net.minecraft.class_2168;
import net.minecraft.class_2170;
import net.minecraft.class_2247;
import net.minecraft.class_2290;
import net.minecraft.class_243;
import net.minecraft.class_2561;
import net.minecraft.class_2583;
import net.minecraft.class_2585;
import net.minecraft.class_2680;
import net.minecraft.class_310;
import net.minecraft.class_3222;
import net.minecraft.class_437;
import net.minecraft.class_746;
import net.minecraft.server.MinecraftServer;
import personthecat.catlib.command.arguments.JsonArgument;
import personthecat.catlib.event.lifecycle.ClientTickEvent;
import personthecat.catlib.serialization.json.JsonPath;
import personthecat.catlib.data.ModDescriptor;
import personthecat.catlib.exception.CommandExecutionException;
import personthecat.catlib.util.PathUtils;
import personthecat.catlib.linting.SyntaxLinter;
import personthecat.fresult.Result;

import javax.annotation.Nullable;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static personthecat.catlib.exception.Exceptions.cmdEx;
import static personthecat.catlib.util.Shorthand.f;

@Log4j2
@SuppressWarnings("unused")
@AllArgsConstructor
public class CommandContextWrapper {

    /** The style used when printing error messages. */
    private static final class_2583 ERROR_STYLE = class_2583.field_24360.method_10977(class_124.field_1061);

    private final CommandContext ctx;
    private final SyntaxLinter linter;
    private final ModDescriptor mod;

    public String getString(final String key) {
        return this.get(key, String.class);
    }

    public int getInt(final String key) {
        return this.get(key, Integer.class);
    }

    public double getDouble(final String key) {
        return this.get(key, Double.class);
    }

    public boolean getBoolean(final String key) {
        return this.get(key, Boolean.class);
    }

    public class_2680 getBlock(final String key) {
        return this.get(key, class_2247.class).method_9494();
    }

    public class_1792 getItem(final String key) {
        return this.get(key, class_2290.class).method_9785();
    }

    public File getFile(final String key) {
        return this.get(key, File.class);
    }

    public JsonArgument.Result getJsonFile(final String key) {
        return this.get(key, JsonArgument.Result.class);
    }

    public JsonPath getJsonPath(final String key) {
        return this.get(key, JsonPath.class);
    }

    public  Optional getOptional(final String key, final Class type) {
        return Result.suppress(() -> this.get(key, type)).get(Result::IGNORE);
    }

    public  List getList(final String key, final Class type) {
        final List list = new ArrayList<>();
        for (int i = 0; true; i++) {
            final Optional arg = this.getOptional(key + i, type);
            if (arg.isPresent()) {
                list.add(arg.get());
            } else {
                return list;
            }
        }
    }

    public  T get(final String key, final Class type) {
        return this.ctx.getArgument(key, type);
    }

    public String getActual(final String key) {
        return this.tryGetActual(key).orElseThrow(() -> cmdEx("No such argument: {}", key));
    }

    public Optional tryGetActual(final String key) {
        for (final ParsedCommandNode node : this.ctx.getNodes()) {
            if (key.equals(node.getNode().getName())) {
                return Optional.of(node.getRange().get(this.getInput()));
            }
        }
        return Optional.empty();
    }

    public void sendMessage(final String msg) {
        this.sendMessage(new class_2585(msg));
    }

    public void sendMessage(final String msg, final Object... args) {
        this.sendMessage(f(msg, args));
    }

    public void sendMessage(final String msg, final class_124 color) {
        this.sendMessage(msg, class_2583.field_24360.method_10977(color));
    }

    public void sendMessage(final String msg, final class_2583 style) {
        this.sendMessage(new class_2585(msg).method_10862(style));
    }

    public void sendMessage(final class_2561 msg) {
        this.ctx.getSource().method_9226(msg, true);
    }

    public void sendLintedMessage(final String message) {
        this.sendMessage(this.linter.lint(message));
    }

    public void sendError(final String msg) {
        this.ctx.getSource().method_9213(new class_2585(msg));
    }

    public void sendError(final String msg, final Object... args) {
        this.sendError(f(msg, args));
    }

    public void sendError(final String msg, final class_2583 style) {
        this.ctx.getSource().method_9213(new class_2585(msg).method_10862(style));
    }

    public void sendError(final class_2561 msg) {
        this.ctx.getSource().method_9213(msg);
    }

    public PendingMessageWrapper generateMessage(final String msg) {
        return new PendingMessageWrapper(this, new class_2585(msg));
    }

    public PendingMessageWrapper generateMessage(final String msg, final Object... args) {
        return this.generateMessage(f(msg, args));
    }

    public PendingMessageWrapper generateMessage(final String msg, final class_2583 style) {
        return new PendingMessageWrapper(this, (class_2585) new class_2585(msg).method_10862(style));
    }

    public PendingMessageWrapper generateMessage(final class_2585 component) {
        return new PendingMessageWrapper(this, component);
    }

    public class_2585 createText(final String msg) {
        return new class_2585(msg);
    }

    public class_2585 createText(final String msg, final Object... args) {
        return new class_2585(f(msg, args));
    }

    public class_2561 lintMessage(final String msg) {
        return this.linter.lint(msg);
    }

    public class_2561 lintMessage(final String msg, final Object... args) {
        return this.linter.lint(f(msg, args));
    }

    @Environment(EnvType.CLIENT)
    public void setScreen(final class_437 screen) {
        ClientTickEvent.registerSingle(minecraft -> minecraft.method_1507(screen));
    }

    @Nullable
    public class_1297 getEntity() {
        return this.ctx.getSource().method_9228();
    }

    public class_1297 assertEntity() {
        final class_1297 entity = this.getEntity();
        if (entity == null) throw new CommandExecutionException("Expected an entity");
        return entity;
    }

    @Nullable
    public class_1657 getPlayer() {
        final class_1297 entity = this.getEntity();
        return entity instanceof class_1657 ? (class_1657) entity : null;
    }

    public class_1657 assertPlayer() {
        final class_1657 player = this.getPlayer();
        if (player == null) throw new CommandExecutionException("Expected a player");
        return player;
    }

    public boolean isPlayer() {
        return this.getEntity() instanceof class_1657;
    }

    public class_243 getPos() {
        return this.ctx.getSource().method_9222();
    }

    @SuppressWarnings("ConstantConditions")
    public void setGameMode(final class_1934 type) {
        if (this.isClientSide()) {
            if (this.getPlayer() instanceof class_746) {
                class_310.method_1551().field_1761.method_2907(type);
            }
        } else if (this.getPlayer() instanceof class_3222 player) {
            player.method_7336(type);
        }
    }

    public class_1937 getLevel() {
        return this.ctx.getSource().method_9225();
    }

    public MinecraftServer getServer() {
        return this.ctx.getSource().method_9211();
    }

    public boolean isClientSide() {
        return this.getLevel().method_8608();
    }

    public void execute(final String command) {
        this.tryExecute(command).throwIfErr();
    }

    public void execute(final String command, final Object... args) {
        this.execute(f(command, args));
    }

    public Result tryExecute(final String command) {
        final class_2170 manager = this.getServer().method_3734();
        return Result.suppress(() -> manager.method_9249(this.ctx.getSource(), command))
            .mapErr(CommandExecutionException::new)
            .filter(i -> i >= 0, () -> cmdEx("Running nested command"));
    }

    public String getInput() {
        return this.ctx.getInput();
    }

    public ModDescriptor getMod() {
        return this.mod;
    }

    public File getBackupsFolder() {
        return this.mod.getBackupFolder();
    }

    public File getModConfigFolder() {
        return this.mod.getConfigFolder();
    }

    public File getModFile(final String path) {
        return new File(this.mod.getConfigFolder(), path);
    }

    public String getModPath(final String path) {
        return this.getModPath(new File(path));
    }

    public String getModPath(final File file) {
        return PathUtils.getRelativePath(this.mod.getConfigFolder(), file);
    }

    public CommandContext getCtx() {
        return this.ctx;
    }

    public class_2168 getSource() {
        return this.ctx.getSource();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy