Skip to content

feat: Document command restrictions #610

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
title: Requirements
description: A guide to setting requirements for commands.
slug: paper/dev/command-api/basics/requirements
version: 1.21.6
---

Sometimes you want to limit a player's ability to use and/or view certain commands or subcommands. Exactly for this purpose,
Expand Down Expand Up @@ -70,7 +71,44 @@ Commands.literal("reloadcommands")
});
```

## Automating command reloads
### Automating command reloads
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
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
these in very specific spots. Furthermore, this method is completely thread safe, meaning you are free to call it from an asynchronous context.

## Restricted commands
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
really want to run a command from a run-command click event. That includes ones on text components or dialog buttons.
All Vanilla commands, which require operator status by default, are restricted:

![](./assets/vanilla-restriction.png)

### Restricting your commands
You can apply the same behavior to your commands by wrapping the predicate inside your `.requires` with `Commands.restricted(...)`.
A simple implementation might look like this:

```java
Commands.literal("test-req")
.requires(Commands.restricted(source -> true))
.executes(ctx -> {
ctx.getSource().getSender().sendRichMessage("You passed!");
return Command.SINGLE_SUCCESS;
});
```

![](./assets/custom-restriction.png)

<br />

Inside the `.restricted` method you can put any logic which you would put into your `.requires` method.
It is nothing more but a simple wrapper around the usual `.requires` predicate:

```java
Commands.literal("mycommand")
.requires(Commands.restricted(source -> source.getSender().hasPermission("my.custom.permission")
&& source.getExecutor() instanceof Player player
&& player.getGameMode() == GameMode.ADVENTURE))
.executes(ctx -> {
// Command logic
});
```