commands
commands copied to clipboard
Command Visibilty Conditions
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
- 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();
});
- You use it with either
@ShowCommandor@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) {
...
}
- That's all you have to do to make it work.
So what is it doing in game :
- In the first example, the subcommand
example1is only shown in the tab completion if the player is in gamemode creative, this is because with@ShowCommandthe subcommand is hidden by default and is only shown when all conditions return true. - In the second example, the subcommand
example2is only shown in the tab completion if the player is not in gamemode creative, this is because with@HideCommandthe subcommand is shown by default and is only hidden when all conditions return true. - And in the third example, the subcommand
example3is only shown in the tab completion if the player is in gamemode creative and not flying, this is because@HideCommandtakes 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.
Nice feature would be very useful to have more control over that!