Skip to content

Commit 31771b5

Browse files
jacky8399zlataovce
andauthored
feat: Add command suggestions docs (#486)
Co-authored-by: Matouš Kučera <mk@kcra.me>
1 parent 7f6cdc8 commit 31771b5

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

docs/paper/dev/api/commands.mdx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,42 @@ For now, you can find specific examples of arguments, among other things, on the
124124
Custom arguments can be created by implementing the <Javadoc name={"io.papermc.paper.command.brigadier.argument.CustomArgumentType"}>CustomArgumentType</Javadoc>
125125
interface. See the Javadocs for more information on how they work.
126126

127+
### Command suggestions
128+
129+
Custom command suggestions can be supplied by either overriding the
130+
<Javadoc name={"io.papermc.paper.command.brigadier.argument.CustomArgumentType#listSuggestions(com.mojang.brigadier.context.CommandContext,com.mojang.brigadier.suggestion.SuggestionsBuilder)"}>listSuggestions</Javadoc>
131+
method in `CustomArgumentType`, or using the `suggests(SuggestionProvider)` method to define a `SuggestionProvider` for the argument.
132+
133+
An example suggestion provider might look like this:
134+
```java
135+
Commands.argument("count", IntegerArgumentType.integer())
136+
.suggests((ctx, builder) -> {
137+
builder.suggest(1);
138+
builder.suggest(10);
139+
return builder.buildFuture();
140+
})
141+
```
142+
143+
Suggestions can also be contextual since they have access to the `CommandContext`.
144+
145+
:::note
146+
147+
To access other arguments when building suggestions, make sure the correct `CommandContext`
148+
is obtained with `CommandContext.getLastChild()`.
149+
150+
:::
151+
152+
An example contextual suggestion provider:
153+
```java
154+
Commands.argument("min", IntegerArgumentType.integer())
155+
.then(Commands.argument("max", IntegerArgumentType.integer())
156+
.suggests((ctx, builder) -> {
157+
int min = ctx.getLastChild().getArgument("min", Integer.class);
158+
builder.suggest(min + 1);
159+
return builder.buildFuture();
160+
}))
161+
```
162+
127163
## Lifecycle
128164

129165
Commands are not just registered once at the start, but anytime a reload happens. This can be

0 commit comments

Comments
 (0)