Skip to content

Commit 26c1d82

Browse files
committed
Start implementing "safe-casting"
1 parent bb043bb commit 26c1d82

File tree

1 file changed

+56
-3
lines changed

1 file changed

+56
-3
lines changed

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

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package dev.jorel.commandapi.executors;
22

3+
import dev.jorel.commandapi.arguments.AbstractArgument;
4+
35
import javax.annotation.Nullable;
46

57
import java.util.Collection;
@@ -79,7 +81,7 @@ public String getFullInput() {
7981
public int count() {
8082
return args.length;
8183
}
82-
84+
8385
// Main accessing methods. In Kotlin, methods named get() allows it to
8486
// access these methods using array notation, as a part of operator overloading.
8587
// More information about operator overloading in Kotlin can be found here:
@@ -342,7 +344,7 @@ public Optional<String> getRawOptional(String nodeName) {
342344
}
343345
return Optional.of(rawArgsMap.get(nodeName));
344346
}
345-
347+
346348
/** Unchecked methods. These are the same as the methods above, but use
347349
* unchecked generics to conform to the type they are declared as. In Java,
348350
* the normal methods (checked) require casting:
@@ -367,7 +369,7 @@ public Optional<String> getRawOptional(String nodeName) {
367369
public <T> T getUnchecked(int index) {
368370
return (T) get(index);
369371
}
370-
372+
371373
/**
372374
* Returns an argument by its node name
373375
*
@@ -443,4 +445,55 @@ public <T> Optional<T> getOptionalUnchecked(String nodeName) {
443445
return (Optional<T>) getOptional(nodeName);
444446
}
445447

448+
/**
449+
* 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()}
450+
*
451+
* @param argumentType The argument instance used to create the argument
452+
* @return The argument represented by the CommandAPI argument, or null if the argument's clas cannot be cast to the type represented by {@link dev.jorel.commandapi.arguments.AbstractArgument#getPrimitiveType()}
453+
*/
454+
@Nullable
455+
public <T> T getByArgument(AbstractArgument<T, ?, ?, ?> argumentType) {
456+
Object argument = get(argumentType.getNodeName());
457+
return castArgument(argument, argumentType.getPrimitiveType());
458+
}
459+
460+
/**
461+
* 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.
462+
*
463+
* @param nodeName The node name of the argument
464+
* @param argumentType The class that represents the argument
465+
* @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
466+
*/
467+
@Nullable
468+
public <T> T getByClass(String nodeName, Class<T> argumentType) {
469+
Object argument = get(nodeName);
470+
return castArgument(argument, argumentType);
471+
}
472+
473+
/**
474+
* Returns an argument based on its index. This also attempts to directly cast the argument to the type represented by the {@code argumentType} parameter.
475+
*
476+
* @param index The position of the argument
477+
* @param argumentType The class that represents the argument
478+
* @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
479+
*/
480+
@Nullable
481+
public <T> T getByClass(int index, Class<T> argumentType) {
482+
Object argument = get(index);
483+
return castArgument(argument, argumentType);
484+
}
485+
486+
private <T> T castArgument(Object argument, Class<T> argumentType) {
487+
if (argument == null) {
488+
return null;
489+
}
490+
if (!argument.getClass().equals(argumentType)) {
491+
return null;
492+
}
493+
if (!argumentType.isAssignableFrom(argument.getClass())) {
494+
return null;
495+
}
496+
return (T) argument;
497+
}
498+
446499
}

0 commit comments

Comments
 (0)