How to implement arguments into custom commands - Minecraft Forge 1.16.5
11:57 20 Oct 2021

I am trying to implement simple SetHome and Home commands into my mod using the Forge 1.16.5 MDK. I followed TutorialsByKaupenjoe's custom commands video (https://youtu.be/bYH2i-KOLgk) but they didn't describe the use of arguments in commands. I have found very brief and limited descriptions on how to use argument types and even looked at the vanilla commands. The best resource I could find was (https://github.com/TheGreyGhost/MinecraftByExample/blob/master/src/main/java/minecraftbyexample/mbe45_commands/MBEquoteCommand.java).

this is my SetHomeCommand.java class

package net.caret.goggleutils.commands;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.caret.goggleutils.GoggleUtils;
import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.StringTextComponent;

public class SetHomeCommand {
    public SetHomeCommand(CommandDispatcher dispatcher) {
        dispatcher.register(
                Commands.literal("sethome")
                .then(Commands.argument("number", IntegerArgumentType.integer(1, 10)))
                        .executes(commandContext -> setHome(commandContext.getSource(), IntegerArgumentType.getInteger(commandContext, "number")))
        );
    }

    private int setHome(CommandSource source, int number) throws CommandSyntaxException{
        System.out.println("test");
        ServerPlayerEntity player = source.asPlayer();
        BlockPos playerPos = player.getPosition();
        String pos = "(" + playerPos.getX() + ", " + playerPos.getY() + "," + playerPos.getZ() + ")";

        player.getPersistentData().putIntArray(GoggleUtils.MODID + String.valueOf(number) + "home", new int[]{playerPos.getX(), playerPos.getY(), playerPos.getZ()});

        source.sendFeedback(new StringTextComponent("Set home at " + pos), true);
        return 1;
    }
}

Everything runs properly (I have also subscribed to the RegisterCommandsEvent in another class) and the game autocompletes as it should, but when I try /sethome 1 I get

Unknown or incomplete command, see below for error
/sethome 1<--[HERE]

This seems to be a problem with the builder chain (as the console does not print "test").

java minecraft minecraft-forge