Skip to content

Commit 7117830

Browse files
committed
Adds documentation for safe arguments
1 parent 129dd78 commit 7117830

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,31 @@ void commandArguments() {
13661366
})
13671367
.register();
13681368
/* ANCHOR_END: commandArguments3 */
1369+
1370+
/* ANCHOR: commandArguments4 */
1371+
StringArgument nameArgument = new StringArgument("name");
1372+
IntegerArgument amountArgument = new IntegerArgument("amount");
1373+
PlayerArgument playerArgument = new PlayerArgument("player");
1374+
PlayerArgument targetArgument = new PlayerArgument("target");
1375+
GreedyStringArgument messageArgument = new GreedyStringArgument("message");
1376+
1377+
new CommandAPICommand("mycommand")
1378+
.withArguments(nameArgument)
1379+
.withArguments(amountArgument)
1380+
.withOptionalArguments(playerArgument)
1381+
.withOptionalArguments(targetArgument)
1382+
.withOptionalArguments(messageArgument)
1383+
.executesPlayer((player, args) -> {
1384+
String name = args.getByArgument(nameArgument);
1385+
int amount = args.getByArgument(amountArgument);
1386+
Player p = args.getByArgumentOrDefault(playerArgument, player);
1387+
Player target = args.getByArgumentOrDefault(targetArgument, player);
1388+
String message = args.getOptionalByArgument(messageArgument).orElse("Hello!");
1389+
1390+
// Do whatever with these values
1391+
})
1392+
.register();
1393+
/* ANCHOR_END: commandArguments4 */
13691394
}
13701395

13711396
void commandFailures() {

commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,31 @@ CommandAPICommand("mycommand")
12871287
})
12881288
.register();
12891289
/* ANCHOR_END: commandArguments3 */
1290+
1291+
/* ANCHOR: commandArguments4 */
1292+
val nameArgument = StringArgument("name")
1293+
val amountArgument = IntegerArgument("amount")
1294+
val playerArgument = PlayerArgument("player")
1295+
val targetArgument = PlayerArgument("target")
1296+
val messageArgument = GreedyStringArgument("message")
1297+
1298+
CommandAPICommand("mycommand")
1299+
.withArguments(nameArgument)
1300+
.withArguments(amountArgument)
1301+
.withOptionalArguments(playerArgument)
1302+
.withOptionalArguments(targetArgument)
1303+
.withOptionalArguments(messageArgument)
1304+
.executesPlayer(PlayerCommandExecutor { player, args ->
1305+
val name: String = args.getByArgument(nameArgument)!!
1306+
val amount: Int = args.getByArgument(amountArgument)!!
1307+
val p: Player = args.getByArgumentOrDefault(playerArgument, player)
1308+
val target: Player = args.getByArgumentOrDefault(targetArgument, player)
1309+
val message: String = args.getOptionalByArgument(messageArgument).orElse("Hello!")
1310+
1311+
// Do whatever with these values
1312+
})
1313+
.register();
1314+
/* ANCHOR_END: commandArguments4 */
12901315
}
12911316

12921317
fun commandFailures() {

docssrc/src/commandarguments.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ While the argument array just gives the possibility to access the arguments via
88
- [Access arguments](#access-arguments)
99
- [Access raw arguments](#access-raw-arguments)
1010
- [Access unsafe arguments](#access-unsafe-arguments)
11+
- [Access safe arguments](#access-safe-arguments)
1112

1213
-----
1314

@@ -254,3 +255,67 @@ Here, we don't actually want to cast the argument, so we use unsafe arguments to
254255
</div>
255256

256257
</div>
258+
259+
-----
260+
261+
## Access safe arguments
262+
263+
<div class="warning">
264+
265+
**Developer's Note:**
266+
267+
The following methods can not be used to access a value returned by a `CustomArgument` as its return type depends on the base argument for it.
268+
269+
</div>
270+
271+
Lastly, the CommandArguments class offers you a way to access your arguments in a more safe way by using internal casts. Again, methods are offered to access arguments by their
272+
index or their node name:
273+
274+
```java
275+
T getByClass(String nodeName, Class<T> argumentType);
276+
T getByClassOrDefault(String nodeName, Class<T> argumentType, T defaultValue);
277+
T getOptionalByClass(String nodeName, Class<T> argumentType);
278+
T getByClass(int index, Class<T> argumentType);
279+
T getByClassOrDefault(int index, Class<T> argumentType, T defaultValue);
280+
T getOptionalByClass(int index, Class<T> argumentType);
281+
```
282+
283+
The main addition in contrast to all the other methods the `CommandArguments` class offers, these methods take an additional parameter of type `Class<T>` where `T` is the return type
284+
of the argument with the given node name or index.
285+
286+
For example, say you declared a `new StringArgument("value")` and you now want to access the return value of this argument using safe casting. This would be done as follows:
287+
288+
```java
289+
String value = args.getByClass("value", String.class);
290+
```
291+
292+
### Access safe arguments using an argument instance
293+
294+
Finally, there is one more, even safer way of accessing safe arguments: by using an argument instance:
295+
296+
```java
297+
T getByArgument(Argument<T> argumentType);
298+
T getByArgumentOrDefault(Argument<T> argumentType, T defaultValue);
299+
T getOptionalByArgument(Argument<T> argumentType);
300+
```
301+
302+
However, while safer, this also introduces the need to first initialize your arguments before you can start implementing your command.
303+
To visualize this, we want to implement the command from [Access arguments by node name and index](#example---access-arguments-by-node-name-and-index) again, this time using safe arguments with an argument instance:
304+
305+
<div class="example">
306+
307+
### Example - Access safe arguments using an argument instance
308+
309+
<div class="multi-pre">
310+
311+
```java,Java
312+
{{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:commandArguments4}}
313+
```
314+
315+
```kotlin,Kotlin
316+
{{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:commandArguments4}}
317+
```
318+
319+
</div>
320+
321+
</div>

docssrc/src/intro.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Here's the list of changes to the documentation between each update. You can vie
4040
### Documentation changes 9.3.0 \\(\rightarrow\\) 9.4.0
4141

4242
- Adds [Velocity](./velocity_intro.md) page to outline how to setup the CommandAPI for Velocity
43+
- Updates [CommandArguments](./commandarguments.md) to document new additions for safe arguments
4344

4445
### Documentation changes 9.2.0 \\(\rightarrow\\) 9.3.0
4546

0 commit comments

Comments
 (0)