#Registering commands and subcommands

9 messages · Page 1 of 1 (latest)

obsidian mirage
#

Is this the proper way to register multiple subcommands, or is there a cleaner form?

            ServerCommandSource source = context.getSource();
            source.sendFeedback(
                () -> Text.literal("bling-physics mod by SethBling").formatted(Formatting.GREEN),
                false
            );
    
            return 1;
        }));

        dispatcher.register(CommandManager.literal("physics")
            .then(CommandManager.literal("spawn").executes(context -> {
                ServerCommandSource source = context.getSource();
                ServerWorld world = source.getWorld();
                PhysicsWorld physicsWorld = physicsWorlds.get(world.getRegistryKey());
                if (physicsWorld != null) {
                    physicsWorld.onSpawnCommand(source);
                    return 1;
                } else {
                    source.sendFeedback(
                        () -> Text.literal("Physics world not found").formatted(Formatting.RED),
                        false
                    );
                    return -1;
                }
        })));


        dispatcher.register(CommandManager.literal("physics")
            .then(CommandManager.literal("clear").executes(context -> {
                ServerCommandSource source = context.getSource();
                // Iterate through all the worlds
                for (PhysicsWorld physicsWorld : physicsWorlds.values()) {
                    physicsWorld.onClearCommand(source);
                }
                return 1;
        })));```
drifting vapor
obsidian mirage
#

I used the second document as a primer, but it doesn't make mention of multiple subcommands. The first one looks like it might be helpful

obsidian mirage
#

very helpful, thank you

main wolf
#

command creation structure can get quite messy, especially with the indentation and many sub-commands, so what I like to do is delegate the execution away from the command structure to avoid accidentally moving a ( or ) to a different branch and creating an invalid command which are annoying to debug:

dispatcher.register(CommandManager.literal("base")
        .requires(source -> source.hasPermissionLevel(2))
        .then(CommandManager.literal("sub1")
                .then(CommandManager.literal("sub2")
                        .then(CommandManager.argument("arg", ArgumentType.arg())
                                .executes(MyCommandHandler::executeSub2)
                        )
                )
                .then(CommandManager.literal("otherSub2")
                    .executes(MyCommandHandler::executeOtherSub2)
                )
        )
        .then(CommandManager.literal("otherSub1")
            .executes(MyCommandHandler::executeOtherSub1)
        )
);
obsidian mirage
#

yeah, this is pretty much where I ended up

#

from sky's link

#

well, I'm using the node-based architecture

#

which makes it even harder to mess up the syntax tree construction