You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -428,6 +428,8 @@ This is the current roadmap for the CommandAPI (as of 11th May 2023):
428
428
<li>Fixed implementation issues with <code>FunctionArgument</code></li>
429
429
<li>https://github.com/JorelAli/CommandAPI/issues/490 Adds (experimental) support for Mojang-mapped servers via the CommandAPI config</li>
430
430
<li>https://github.com/JorelAli/CommandAPI/issues/524 Fixes <code>CommandAPIBukkit.get().getTags()</code> erroring in 1.20.4</li>
431
+
<li>https://github.com/JorelAli/CommandAPI/issues/536, https://github.com/JorelAli/CommandAPI/pull/537 Fixes <code>MultiLiteralArgument</code>help displaying the node name instead of the literal text</li>
432
+
<li>https://github.com/JorelAli/CommandAPI/pull/540 Add methods to "safe-cast" arguments to <code>CommandArguments</code></li>
// Main accessing methods. In Kotlin, methods named get() allows it to
84
98
// access these methods using array notation, as a part of operator overloading.
85
99
// More information about operator overloading in Kotlin can be found here:
@@ -342,7 +356,7 @@ public Optional<String> getRawOptional(String nodeName) {
342
356
}
343
357
returnOptional.of(rawArgsMap.get(nodeName));
344
358
}
345
-
359
+
346
360
/** Unchecked methods. These are the same as the methods above, but use
347
361
* unchecked generics to conform to the type they are declared as. In Java,
348
362
* the normal methods (checked) require casting:
@@ -367,7 +381,7 @@ public Optional<String> getRawOptional(String nodeName) {
367
381
public <T> TgetUnchecked(intindex) {
368
382
return (T) get(index);
369
383
}
370
-
384
+
371
385
/**
372
386
* Returns an argument by its node name
373
387
*
@@ -443,4 +457,147 @@ public <T> Optional<T> getOptionalUnchecked(String nodeName) {
443
457
return (Optional<T>) getOptional(nodeName);
444
458
}
445
459
460
+
/*****************************************
461
+
********** SAFE-CAST ARGUMENTS **********
462
+
*****************************************/
463
+
464
+
/**
465
+
* Returns an argument purely based on its CommandAPI representation. This also attempts to directly cast the argument to the type represented by {@link dev.jorel.commandapi.arguments.AbstractArgument#getPrimitiveType()}
466
+
*
467
+
* @param argumentType The argument instance used to create the argument
468
+
* @return The argument represented by the CommandAPI argument, or null if the argument was not found.
469
+
*/
470
+
@Nullable
471
+
public <T> TgetByArgument(AbstractArgument<T, ?, ?, ?> argumentType) {
* Returns an argument purely based on its CommandAPI representation or a default value if the argument wasn't found.
477
+
* <p>
478
+
* If the argument was found, this also attempts to directly cast the argument to the type represented by {@link dev.jorel.commandapi.arguments.AbstractArgument#getPrimitiveType()}
479
+
*
480
+
* @param argumentType The argument instance used to create the argument
481
+
* @param defaultValue The default value to return if the argument wasn't found
482
+
* @return The argument represented by the CommandAPI argument, or the default value if the argument was not found.
483
+
*/
484
+
public <T> TgetByArgumentOrDefault(AbstractArgument<T, ?, ?, ?> argumentType, TdefaultValue) {
* Returns an <code>Optional</code> holding the provided argument. This <code>Optional</code> can be empty if the argument was not given when running the command.
491
+
* <p>
492
+
* This attempts to directly cast the argument to the type represented by {@link dev.jorel.commandapi.arguments.AbstractArgument#getPrimitiveType()}
493
+
*
494
+
* @param argumentType The argument instance used to create the argument
495
+
* @return An <code>Optional</code> holding the argument, or an empty <code>Optional</code> if the argument was not found.
496
+
*/
497
+
public <T> Optional<T> getOptionalByArgument(AbstractArgument<T, ?, ?, ?> argumentType) {
* Returns an argument based on its node name. This also attempts to directly cast the argument to the type represented by the {@code argumentType} parameter.
503
+
*
504
+
* @param nodeName The node name of the argument
505
+
* @param argumentType The class that represents the argument
506
+
* @return The argument with the given node name, or null if the argument was not found.
507
+
*/
508
+
@Nullable
509
+
public <T> TgetByClass(StringnodeName, Class<T> argumentType) {
* Returns an <code>Optional</code> holding the argument with the given node name. This <code>Optional</code> can be empty if the argument was not given when running the command.
530
+
* <p>
531
+
* This attempts to directly cast the argument to the type represented by the {@code argumentType} parameter.
532
+
*
533
+
* @param nodeName The node name of the argument
534
+
* @param argumentType The class that represents the argument
535
+
* @return An <code>Optional</code> holding the argument, or an empty <code>Optional</code> if the argument was not found.
536
+
*/
537
+
public <T> Optional<T> getOptionalByClass(StringnodeName, Class<T> argumentType) {
* Returns an argument based on its index. This also attempts to directly cast the argument to the type represented by the {@code argumentType} parameter.
543
+
*
544
+
* @param index The index of the argument
545
+
* @param argumentType The class that represents the argument
546
+
* @return The argument at the given index, or null if the argument was not found.
547
+
*/
548
+
@Nullable
549
+
public <T> TgetByClass(intindex, Class<T> argumentType) {
* Returns an <code>Optional</code> holding the argument at the given index. This <code>Optional</code> can be empty if the argument was not given when running the command.
570
+
* <p>
571
+
* This attempts to directly cast the argument to the type represented by the {@code argumentType} parameter.
572
+
*
573
+
* @param index The index of the argument
574
+
* @param argumentType The class that represents the argument
575
+
* @return An <code>Optional</code> holding the argument, or an empty <code>Optional</code> if the argument was not found.
576
+
*/
577
+
public <T> Optional<T> getOptionalByClass(intindex, Class<T> argumentType) {
@@ -254,3 +255,75 @@ Here, we don't actually want to cast the argument, so we use unsafe arguments to
254
255
</div>
255
256
256
257
</div>
258
+
259
+
-----
260
+
261
+
## Access safe arguments
262
+
263
+
<divclass="warning">
264
+
265
+
**Developer's Note:**
266
+
267
+
The following methods cannot 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
+
Compared to 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
+
<divclass="multi-pre">
289
+
290
+
```java,Java
291
+
String value = args.getByClass("value", String.class);
292
+
```
293
+
294
+
```kotlin,Kotlin
295
+
val value = args.getByClass("value", String::class.java)
296
+
```
297
+
298
+
</div>
299
+
300
+
### Access safe arguments using an argument instance
301
+
302
+
Finally, there is one more, even safer way of accessing safe arguments: by using an argument instance:
303
+
304
+
```java
305
+
T getByArgument(Argument<T> argumentType);
306
+
T getByArgumentOrDefault(Argument<T> argumentType, T defaultValue);
307
+
T getOptionalByArgument(Argument<T> argumentType);
308
+
```
309
+
310
+
However, while safer, this also introduces the need to first initialize your arguments before you can start implementing your command.
311
+
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, but this time using safe arguments with an argument instance:
312
+
313
+
<divclass="example">
314
+
315
+
### Example - Access safe arguments using an argument instance
0 commit comments