Skip to content

Commit fdd60e0

Browse files
authored
feat: Document command restrictions (#610)
1 parent 7b982de commit fdd60e0

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed
207 KB
Loading
189 KB
Loading

src/content/docs/paper/dev/api/command-api/basics/requirements.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
title: Requirements
33
description: A guide to setting requirements for commands.
44
slug: paper/dev/command-api/basics/requirements
5+
version: 1.21.6
56
---
67

78
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")
7071
});
7172
```
7273

73-
## Automating command reloads
74+
### Automating command reloads
7475
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
7576
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
7677
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+
![](./assets/vanilla-restriction.png)
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+
![](./assets/custom-restriction.png)
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

Comments
 (0)