#Passing identifiers to a command

24 messages · Page 1 of 1 (latest)

sharp lily
#

Ok. i've got a command registered:

public void onInitialize() {

        CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
            LiteralCommandNode<ServerCommandSource> PlayerChatRangeMethodNode = CommandManager
                    .literal("PlayerChatRangeMethod")
                    .requires(source -> source.hasPermissionLevel(4))
                    .then(argument(MethodArgumentName, IdentifierArgumentType.identifier()))
                    .executes(this::PlayerRangeMethodCommand)
                    .build();

            dispatcher.getRoot().addChild(PlayerChatRangeMethodNode);
        });
    }

    private int PlayerRangeMethodCommand(CommandContext<ServerCommandSource> context) {
        Identifier methodName = IdentifierArgumentType.getIdentifier(context, MethodArgumentName);
        context.getSource().sendFeedback(() -> Text.literal("called /PlayerRangeMethod with argument " + methodName), false);

        return 1;
    }

At the moment, for testing, it doesn't care what identifier is passed but when i try to pass an identifier, say minecraft:stone, i get this error:

Unknown or incomplete command, see below for error
...raft:stone<--[HERE]

I assume I am getting something wrong about how to pass identifiers

slender quail
#

your executes() needs to be chained to argument()

sharp lily
#

do you mean this?

.literal("PlayerChatRangeMethod")
.requires(source -> source.hasPermissionLevel(4))
.then(argument(MethodArgumentName, IdentifierArgumentType.identifier()))
.argument(this::PlayerRangeMethodCommand)
.build();

because my IDE does not like that

winter parrot
#

why not

slender quail
#

no, you're chanining a new argument

#

chain an executes(...)

sharp lily
# slender quail chain an executes(...)

you mean this?

.literal("PlayerChatRangeMethod")
.requires(source -> source.hasPermissionLevel(4))
.then(argument(MethodArgumentName, IdentifierArgumentType.identifier())
        .executes(this::PlayerRangeMethodCommand)
)
.build();
slender quail
#

yes

#

what's that build()? i never seen that concern

sharp lily
#

no clue

#

it was in the totorial

slender quail
#

which one?

sharp lily
slender quail
#

this is fine when you have a complex command structure but it's an overkill for a simple command

sharp lily
#

this is what i had ordiginaly but i was having issues registing a command and that was one of the suggestions

#

turns out my issue wasent with the registration but that my code wasent running

slender quail
#

likely

sharp lily
#

hadent registed the class in fabric.mod.json

slender quail
#

the docs' commands examples should work because they're from the reference mod itself

tulip quail
slender quail
#

i'm very against that pattern

tulip quail