#Client-side command registration with BlockPos arguments

4 messages · Page 1 of 1 (latest)

halcyon horizon
#

Apologies if this is a stupid question; I'm new to mod dev. I'm writing a simple mod to register a client-side command that takes two block positions as arguments: /testcommand <pos1> <pos2>. This is my code currently:

import net.minecraft.command.argument.BlockPosArgumentType;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.minecraft.client.MinecraftClient;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;

public class TestClient implements ClientModInitializer {

    @Override
    public void onInitializeClient() {
        ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> {
            MinecraftClient client = MinecraftClient.getInstance();

            dispatcher.register(
                ClientCommandManager.literal("testcommand").then(
                    ClientCommandManager.argument("pos1", new BlockPosArgumentType()
                ).then(
                    ClientCommandManager.argument("pos2", new BlockPosArgumentType()).executes(
                        context -> {
                            BlockPos pos1 = context.getArgument("pos1", BlockPos.class);
                            BlockPos pos2 = context.getArgument("pos2", BlockPos.class);
                            context.getSource().sendFeedback(Text.literal("test"));
                            return 1;
                        }
                    )
                )
            )
        );
        });
    }
}

The mod compiles and runs without errors, but when I try to run the command in-game (/testcommand 10 10 10 20 20 20) I get the error:

Argument 'pos1' is defined as DefaultPosArgument, not class net.minecraft.util.math.BlockPos

What am I doing wrong, and how can I correctly read the BlockPos arguments?

glass tinsel
#

I think you need to use BlockPosArgumentType#getBlockPos or something

halcyon horizon
# glass tinsel I think you need to use `BlockPosArgumentType#getBlockPos` or something

Trying that

BlockPos pos1 = BlockPosArgumentType.getBlockPos(context, "pos1");
BlockPos pos2 = BlockPosArgumentType.getBlockPos(context, "pos2");

gives me the compile error

error: incompatible types: CommandContext<FabricClientCommandSource> cannot be converted to CommandContext<ServerCommandSource>
BlockPos pos1 = BlockPosArgumentType.getBlockPos(context, "pos1");

I assume that's because I'm trying to register a client command rather than a server command?

glass tinsel
#

Oh yeah most command arguments aren't made for client commands