Skip to content

Commit 5867286

Browse files
committed
Update example command + small fixup
1 parent 326306c commit 5867286

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

docs/paper/dev/api/command-api/basics/argument-suggestions.mdx

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,19 @@ public interface SuggestionProvider<S> {
2020
}
2121
```
2222

23-
Similar to the `Command<S>` interface, this is a functional interface, which means that instead of passing in a class which implements this interface,
24-
we can just pass a lambda statement or a method reference.
23+
Similar to other classes or interfaces with a `<S>` generic parameter, for Paper, this usually is a `CommandSourceStack`. Furthermore, similar to the `Command<S>` interface,
24+
this is a functional interface, which means that instead of passing in a class which implements this interface, we can just pass a lambda statement or a method reference.
2525

26-
Our lambda/method requires two parameters: `CommandContext<S>` and `SuggestionsBuilder`. The lambda/method has to return a `CompletableFuture<Suggestions>`.
27-
In order to retrieve our return value, we can just run `SuggestionsBuilder#buildFuture()` (or `SuggestionsBuilder#build()`, if we already are inside a completeable
28-
future).
29-
30-
Similar to other classes or interfaces with a `<S>` generic parameter, for Paper, this usually is a `CommandSourceStack`.
26+
Our lambda/method requires two parameters, `CommandContext<S>` and `SuggestionsBuilder`, returning a `CompletableFuture<Suggestions>`.
27+
In order to retrieve our return value, we can just run `SuggestionsBuilder#buildFuture()` (or `SuggestionsBuilder#build()`, if we already are inside a CompletableFuture).
3128

3229
A very simple lambda for our `suggests` method might look like this:
3330
```java
3431
Commands.argument("name", StringArgumentType.word())
3532
.suggests((ctx, builder) -> builder.buildFuture())
3633
```
3734

38-
This suggests implementation obviously does not suggest anything, as we haven't added suggestions yet.
35+
This example obviously does not suggest anything, as we haven't added suggestions yet.
3936

4037
## A suggestion builder's methods
4138
The `SuggestionsBuilder` has a few methods we can use to construct our suggestions:
@@ -47,7 +44,7 @@ The following table displays what they return with the following input typed in
4744
| Method | Return Value | Description |
4845
|----------------------------|--------------------------------|---------------------------------------------------------------|
4946
| getInput() | /customsuggestions Asumm13Text | The full chat input |
50-
| getStart() | 19 | The index of the forfirstst character of the argument's input |
47+
| getStart() | 19 | The index of the first character of the argument's input |
5148
| getRemaining() | Asumm13Text | The input for the current argument |
5249
| getRemainingLowerCase() | asumm13text | The input for the current argument, lowercased |
5350

@@ -83,7 +80,7 @@ lambda/method and only returning the final `Suggestions` object asynchronously.
8380

8481
Here are the same suggestions declared in the two different ways mentioned above:
8582
```java
86-
// This one allows the usage of all Paper API inside the suggests method
83+
// Here, you are safe to use all Paper API
8784
Commands.argument("name", StringArgumentType.word())
8885
.suggests((ctx, builder) -> {
8986
builder.suggest("first");
@@ -92,7 +89,7 @@ Commands.argument("name", StringArgumentType.word())
9289
return builder.buildFuture();
9390
});
9491

95-
// This one does not allow the usage of most Paper API inside the suggests method
92+
// Here, most Paper API is not usable
9693
Commands.argument("name", StringArgumentType.word())
9794
.suggests((ctx, builder) -> CompletableFuture.supplyAsync(() -> {
9895
builder.suggest("first");
@@ -103,8 +100,8 @@ Commands.argument("name", StringArgumentType.word())
103100
```
104101

105102
## Example: Suggesting amounts in a give command
106-
In commands where you give players certain items, you oftentimes include an amount argument. We could suggest `1`, `32`, and `64` as common amounts for
107-
items given. This could look like this:
103+
In commands where you give players certain items, you oftentimes include an amount argument. We could suggest `1`, `16`, `32`, and `64` as common amounts for
104+
items given. The command implementation could look like this:
108105

109106
```java
110107
@NullMarked
@@ -113,22 +110,29 @@ public class SuggestionsTest {
113110
public static LiteralCommandNode<CommandSourceStack> constructGiveItemCommand() {
114111
// Create new command: /giveitem
115112
return Commands.literal("giveitem")
116-
// Requires a player executor
113+
114+
// Require a player to execute the command
117115
.requires(ctx -> ctx.getExecutor() instanceof Player)
116+
118117
// Declare a new ItemStack argument
119118
.then(Commands.argument("item", ArgumentTypes.itemStack())
119+
120120
// Declare a new integer argument with the bounds of 1 to 99
121121
.then(Commands.argument("stacksize", IntegerArgumentType.integer(1, 99))
122+
123+
// Here, we use method references, since otherwise, our command definition would grow too big
122124
.suggests(SuggestionsTest::getStackSizeSuggestions)
123125
.executes(SuggestionsTest::executeCommandLogic)
126+
124127
)
125128
)
126129
.build();
127130
}
128131

129132
private static CompletableFuture<Suggestions> getStackSizeSuggestions(final CommandContext<CommandSourceStack> ctx, final SuggestionsBuilder builder) {
130-
// Suggest 1, 32, and 64 to the user when they reach the 'stacksize' argument
133+
// Suggest 1, 16, 32, and 64 to the user when they reach the 'stacksize' argument
131134
builder.suggest(1);
135+
builder.suggest(16);
132136
builder.suggest(32);
133137
builder.suggest(64);
134138
return builder.buildFuture();
@@ -140,17 +144,22 @@ public class SuggestionsTest {
140144
return Command.SINGLE_SUCCESS;
141145
}
142146

147+
// If the player has no empty slot, we tell the player that they have no free inventory space
148+
if (player.getInventory().firstEmpty() == -1) {
149+
player.sendRichMessage("<light_purple>You do not have enough space in your inventory!");
150+
return Command.SINGLE_SUCCESS;
151+
}
152+
143153
// Retrieve our argument values
144154
final ItemStack item = ctx.getArgument("item", ItemStack.class);
145155
final int amount = IntegerArgumentType.getInteger(ctx, "stacksize");
146156

147-
// Set the item's max stack size to fit the stack size we defined, set the amount, and give it to the player
148-
item.setData(DataComponentTypes.MAX_STACK_SIZE, amount);
157+
// Set the item's amount and give it to the player
149158
item.setAmount(amount);
150-
player.getInventory().addItem(item);
159+
player.getInventory().setItem(player.getInventory().firstEmpty(), item);
151160

152161
// Send a confirmation message
153-
player.sendRichMessage("<gold>You have been given <white><amount>x</white> <aqua><item></aqua>",
162+
player.sendRichMessage("<light_purple>You have been given <white><amount>x</white> <aqua><item></aqua>!",
154163
Placeholder.component("amount", Component.text(amount)),
155164
Placeholder.component("item", Component.translatable(item).hoverEvent(item))
156165
);
@@ -159,7 +168,7 @@ public class SuggestionsTest {
159168
}
160169
```
161170

162-
And here is how the argument looks in-game:
171+
And here is how the command looks in-game:
163172
<FullWidthVideo src={GiveItemCommandMp4}/>
164173

165174
## Example: Filtering by user input
-351 KB
Binary file not shown.

0 commit comments

Comments
 (0)