Skip to content

Commit 4878c8e

Browse files
committed
Add more methods to safe-cast an argument
1 parent 5f69e3d commit 4878c8e

File tree

1 file changed

+93
-4
lines changed

1 file changed

+93
-4
lines changed

commandapi-core/src/main/java/dev/jorel/commandapi/executors/CommandArguments.java

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -457,37 +457,97 @@ public <T> Optional<T> getOptionalUnchecked(String nodeName) {
457457
return (Optional<T>) getOptional(nodeName);
458458
}
459459

460+
/*****************************************
461+
********** SAFE-CAST ARGUMENTS **********
462+
*****************************************/
463+
460464
/**
461465
* 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()}
462466
*
463467
* @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.
465469
*/
466470
@Nullable
467471
public <T> T getByArgument(AbstractArgument<T, ?, ?, ?> argumentType) {
468472
Object argument = get(argumentType.getNodeName());
469473
return castArgument(argument, argumentType.getPrimitiveType(), argumentType.getNodeName());
470474
}
471475

476+
/**
477+
* 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> T getByArgumentOrDefault(AbstractArgument<T, ?, ?, ?> argumentType, T defaultValue) {
486+
T argument = getByArgument(argumentType);
487+
return (argument != null) ? argument : defaultValue;
488+
}
489+
490+
/**
491+
* 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+
T argument = getByArgument(argumentType);
500+
return Optional.ofNullable(argument);
501+
}
502+
472503
/**
473504
* 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.
474505
*
475506
* @param nodeName The node name of the argument
476507
* @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.
478509
*/
479510
@Nullable
480511
public <T> T getByClass(String nodeName, Class<T> argumentType) {
481512
Object argument = get(nodeName);
482513
return castArgument(argument, argumentType, nodeName);
483514
}
484515

516+
/**
517+
* Returns an argument based on its node name or a default value if the argument wasn't found.
518+
* <p>
519+
* If the argument was found, this method attempts to directly cast the argument to the type represented by the {@code argumentType} parameter.
520+
*
521+
* @param nodeName The node name of the argument
522+
* @param argumentType The class that represents the argument
523+
* @param defaultValue The default value to return if the argument wasn't found
524+
* @return The argument with the given node name, or the default value if the argument was not found.
525+
*/
526+
public <T> T getByClassOrDefault(String nodeName, Class<T> argumentType, T defaultValue) {
527+
T argument = getByClass(nodeName, argumentType);
528+
return (argument != null) ? argument : defaultValue;
529+
}
530+
531+
/**
532+
* 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(String nodeName, Class<T> argumentType) {
541+
T argument = getByClass(nodeName, argumentType);
542+
return Optional.ofNullable(argument);
543+
}
544+
485545
/**
486546
* Returns an argument based on its index. This also attempts to directly cast the argument to the type represented by the {@code argumentType} parameter.
487547
*
488-
* @param index The position of the argument
548+
* @param index The index of the argument
489549
* @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.
491551
*/
492552
@Nullable
493553
public <T> T getByClass(int index, Class<T> argumentType) {
@@ -516,6 +576,35 @@ public <T> T getByClass(int index, Class<T> argumentType) {
516576
"contact the developers of the CommandAPI, we'd love to know how you managed to get this error!");
517577
}
518578

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> T getByClassOrDefault(int index, Class<T> argumentType, T defaultValue) {
590+
T argument = getByClass(index, argumentType);
591+
return (argument != null) ? argument : defaultValue;
592+
}
593+
594+
/**
595+
* 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(int index, Class<T> argumentType) {
604+
T argument = getByClass(index, argumentType);
605+
return Optional.ofNullable(argument);
606+
}
607+
519608
private <T> T castArgument(Object argument, Class<T> argumentType, String name) {
520609
if (argument == null) {
521610
return null;

0 commit comments

Comments
 (0)