JorelAli / CommandAPI

A Bukkit/Spigot API for the command UI introduced in Minecraft 1.13

Home Page:https://commandapi.jorel.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Hidden Command

tanyaofei opened this issue · comments

Description

Sometimes I want to create ClickEvent to execute commands, and I don't want them show up in suggestions.
It's there a way to create hidden subcommands that will not show up on tab complete?

Expected code

new CommandAPICommand("system")
                .withSubcommands(
                        new CommandAPICommand("reload")
                                .executes((CommandExecutor) (sender, args) -> sender
                                        .sendMessage(Component
                                                .text("Are you sure?")
                                                .clickEvent(ClickEvent.runCommand("/system reload-confirm"))
                                        )
                                ),
                        new CommandAPICommand("reload-confirm")
                                .hidden(true) // this command will not show up in suggestions
                                .executes((CommandExecutor) (sender, args) -> Bukkit.getServer().reload())
                ).register();

Extra details

No response

We already have a similar issue open: #409
A subcommand is converted to a literal value internally which in turn is basically suggested instantly when Brigadier sees one of them.
You may also want to look into the linked issue as @willkroboth explained it a bit better than I did here.

Yeah, subcommands are converted into LiteralArguments, and you can't control the suggestions of literal arguments.

However, in this case, you could use a StringArgument instead with a CommandTree, like so:

new CommandTree("system")
    .then(
        new LiteralArgument("reload")
            .executes((CommandExecutor) (sender, args) -> sender
                    .sendMessage(Component
                            .text("Are you sure?")
                            .clickEvent(ClickEvent.runCommand("/system reload-confirm"))
                    )
            )
    )
    .then(
        new StringArgument("option")
            .executes((sender, args) -> {
                String choice = args.get("option");
                if("reload-confirm".equals(choice)) {
                    Bukkit.getServer().reload();
                } else {
                    throw CommandAPI.failWithString("Unknown choice: " + choice);
                }
            })
    )
    .register();

It's a bit jank, but it might work for this situation? StringArgument can have its suggestions controlled and, by default, it doesn't suggest anything.