commands icon indicating copy to clipboard operation
commands copied to clipboard

Command Visibilty Conditions

Open PoQuatre opened this issue 4 years ago • 1 comments

Hi! I added this feature for one of my plugins, so I thought why not make a PR for it because people might find useful.

As the title suggest it adds a way to programmatically show or hide a subcommand, it only change the tab completion like @Private.

How it works

  1. You register a condition :
commandManager.getCommandVisibilityConditions().addCondition("creative", (context) -> {
    if (!context.getIssuer().isPlayer()) return true;
    return context.getIssuer().getPlayer().getGameMode() == GameMode.CREATIVE;
});

commandManager.getCommandVisibilityConditions().addCondition("flying", (context) -> {
    if (!context.getIssuer().isPlayer()) return true;
    return context.getIssuer().getPlayer().isFlying();
});
  1. You use it with either @ShowCommand or @HideCommand :
@Subcommand("example1")
@ShowCommand("creative")
public void firstExample(CommandSender sender) {
    ...
}

@Subcommand("example2")
@HideCommand("creative")
public void secondExample(CommandSender sender) {
    ...
}

@Subcommand("example3")
@ShowCommand("creative")
@HideCommand("flying")
public void thirdExample(CommandSender sender) {
    ...
}
  1. That's all you have to do to make it work.

So what is it doing in game :

  • In the first example, the subcommand example1 is only shown in the tab completion if the player is in gamemode creative, this is because with @ShowCommand the subcommand is hidden by default and is only shown when all conditions return true.
  • In the second example, the subcommand example2 is only shown in the tab completion if the player is not in gamemode creative, this is because with @HideCommand the subcommand is shown by default and is only hidden when all conditions return true.
  • And in the third example, the subcommand example3 is only shown in the tab completion if the player is in gamemode creative and not flying, this is because @HideCommand takes precedence over @ShowCommand.

Condition Configs

Like normal conditions, configs are available here is an example :

commandManager.getCommandVisibilityConditions().addCondition("height", (context) -> {
    if (!context.getIssuer().isPlayer()) return true;

    final int min = context.getConfigValue("min", -1);
    final int max = context.getConfigValue("max", -1);
    final double height = context.getIssuer().getPlayer().getHeight();

    return min < height && (max == -1 || max > height);
});
@Subcommand("example")
@ShowCommand("height:min=64,max=128")
public void example(CommandSender sender) {
    ...
}

In this example the subcommand example is only shown when the player is between Y 64 and Y 128.

Note : You can chain conditions using | between them.

PoQuatre avatar Oct 21 '21 18:10 PoQuatre

Nice feature would be very useful to have more control over that!

DevJoey avatar Jan 26 '23 18:01 DevJoey