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: commandapi-core/src/main/java/dev/jorel/commandapi/executors/CommandArguments.java
+93-4Lines changed: 93 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -457,37 +457,97 @@ public <T> Optional<T> getOptionalUnchecked(String nodeName) {
457
457
return (Optional<T>) getOptional(nodeName);
458
458
}
459
459
460
+
/*****************************************
461
+
********** SAFE-CAST ARGUMENTS **********
462
+
*****************************************/
463
+
460
464
/**
461
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()}
462
466
*
463
467
* @param argumentType The argument instance used to create the argument
464
-
* @return The argument represented by the CommandAPI argument, or null if the argument's class cannot be cast to the type represented by {@link dev.jorel.commandapi.arguments.AbstractArgument#getPrimitiveType()}
468
+
* @return The argument represented by the CommandAPI argument, or null if the argument was not found.
465
469
*/
466
470
@Nullable
467
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.
478
+
* <p>
479
+
* 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()}
480
+
*
481
+
* @param argumentType The argument instance used to create the argument
482
+
* @param defaultValue The default value to return if the argument wasn't found
483
+
* @return The argument represented by the CommandAPI argument, or the default value if the argument was not found.
484
+
*/
485
+
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.
492
+
* <p>
493
+
* This attempts to directly cast the argument to the type represented by {@link dev.jorel.commandapi.arguments.AbstractArgument#getPrimitiveType()}
494
+
*
495
+
* @param argumentType The argument instance used to create the argument
496
+
* @return An <code>Optional</code> holding the argument, or an empty <code>Optional</code> if the argument was not found.
497
+
*/
498
+
public <T> Optional<T> getOptionalByArgument(AbstractArgument<T, ?, ?, ?> argumentType) {
499
+
Targument = getByArgument(argumentType);
500
+
returnOptional.ofNullable(argument);
501
+
}
502
+
472
503
/**
473
504
* 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.
474
505
*
475
506
* @param nodeName The node name of the argument
476
507
* @param argumentType The class that represents the argument
477
-
* @return The argument with the given node name, or null if the argument's class cannot be cast to the type represented by the given {@code argumentType} argument
508
+
* @return The argument with the given node name, or null if the argument was not found.
478
509
*/
479
510
@Nullable
480
511
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.
533
+
* <p>
534
+
* This attempts to directly cast the argument to the type represented by the {@code argumentType} parameter.
535
+
*
536
+
* @param nodeName The node name of the argument
537
+
* @param argumentType The class that represents the argument
538
+
* @return An <code>Optional</code> holding the argument, or an empty <code>Optional</code> if the argument was not found.
539
+
*/
540
+
public <T> Optional<T> getOptionalByClass(StringnodeName, Class<T> argumentType) {
541
+
Targument = getByClass(nodeName, argumentType);
542
+
returnOptional.ofNullable(argument);
543
+
}
544
+
485
545
/**
486
546
* Returns an argument based on its index. This also attempts to directly cast the argument to the type represented by the {@code argumentType} parameter.
487
547
*
488
-
* @param index The position of the argument
548
+
* @param index The index of the argument
489
549
* @param argumentType The class that represents the argument
490
-
* @return The argument at the given index, or null if the argument's class cannot be cast to the type represented by the given {@code argumentType} argument
550
+
* @return The argument at the given index, or null if the argument was not found.
491
551
*/
492
552
@Nullable
493
553
public <T> TgetByClass(intindex, Class<T> argumentType) {
@@ -516,6 +576,35 @@ public <T> T getByClass(int index, Class<T> argumentType) {
516
576
"contact the developers of the CommandAPI, we'd love to know how you managed to get this error!");
517
577
}
518
578
579
+
/**
580
+
* Returns an argument based on its index or a default value if the argument wasn't found.
581
+
* <p>
582
+
* If the argument was found, this method attempts to directly cast the argument to the type represented by the {@code argumentType} parameter.
583
+
*
584
+
* @param index The index of the argument
585
+
* @param argumentType The class that represents the argument
586
+
* @param defaultValue The default value to return if the argument wasn't found
587
+
* @return The argument at the given index, or the default value if the argument was not found.
588
+
*/
589
+
public <T> TgetByClassOrDefault(intindex, Class<T> argumentType, TdefaultValue) {
* 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.
596
+
* <p>
597
+
* This attempts to directly cast the argument to the type represented by the {@code argumentType} parameter.
598
+
*
599
+
* @param index The index of the argument
600
+
* @param argumentType The class that represents the argument
601
+
* @return An <code>Optional</code> holding the argument, or an empty <code>Optional</code> if the argument was not found.
602
+
*/
603
+
public <T> Optional<T> getOptionalByClass(intindex, Class<T> argumentType) {
0 commit comments