|
2 | 2 | title: Requirements |
3 | 3 | description: A guide to setting requirements for commands. |
4 | 4 | slug: paper/dev/command-api/basics/requirements |
| 5 | +version: 1.21.6 |
5 | 6 | --- |
6 | 7 |
|
7 | 8 | Sometimes you want to limit a player's ability to use and/or view certain commands or subcommands. Exactly for this purpose, |
@@ -70,7 +71,44 @@ Commands.literal("reloadcommands") |
70 | 71 | }); |
71 | 72 | ``` |
72 | 73 |
|
73 | | -## Automating command reloads |
| 74 | +### Automating command reloads |
74 | 75 | Forcing a player to reload their own commands is not a viable option for user experience. For this reason, you can **automate** this behavior. It is safe to call |
75 | 76 | the update commands method as often as required, but it should generally be avoided as it can cost a great deal of bandwidth. If possible, you should instead place |
76 | 77 | these in very specific spots. Furthermore, this method is completely thread safe, meaning you are free to call it from an asynchronous context. |
| 78 | + |
| 79 | +## Restricted commands |
| 80 | +From 1.21.6 onwards, commands can now be restricted. This feature is used by Vanilla in order to make a player confirm whether they |
| 81 | +really want to run a command from a run-command click event. That includes ones on text components or dialog buttons. |
| 82 | +All Vanilla commands, which require operator status by default, are restricted: |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +### Restricting your commands |
| 87 | +You can apply the same behavior to your commands by wrapping the predicate inside your `.requires` with `Commands.restricted(...)`. |
| 88 | +A simple implementation might look like this: |
| 89 | + |
| 90 | +```java |
| 91 | +Commands.literal("test-req") |
| 92 | + .requires(Commands.restricted(source -> true)) |
| 93 | + .executes(ctx -> { |
| 94 | + ctx.getSource().getSender().sendRichMessage("You passed!"); |
| 95 | + return Command.SINGLE_SUCCESS; |
| 96 | + }); |
| 97 | +``` |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | +<br /> |
| 102 | + |
| 103 | +Inside the `.restricted` method you can put any logic which you would put into your `.requires` method. |
| 104 | +It is nothing more but a simple wrapper around the usual `.requires` predicate: |
| 105 | + |
| 106 | +```java |
| 107 | +Commands.literal("mycommand") |
| 108 | + .requires(Commands.restricted(source -> source.getSender().hasPermission("my.custom.permission") |
| 109 | + && source.getExecutor() instanceof Player player |
| 110 | + && player.getGameMode() == GameMode.ADVENTURE)) |
| 111 | + .executes(ctx -> { |
| 112 | + // Command logic |
| 113 | + }); |
| 114 | +``` |
0 commit comments