Skip to content

Commit 5f69e3d

Browse files
committed
Update to throw an exception if arguments don't match
1 parent 49fcb04 commit 5f69e3d

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

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

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ public <T> Optional<T> getOptionalUnchecked(String nodeName) {
466466
@Nullable
467467
public <T> T getByArgument(AbstractArgument<T, ?, ?, ?> argumentType) {
468468
Object argument = get(argumentType.getNodeName());
469-
return castArgument(argument, argumentType.getPrimitiveType());
469+
return castArgument(argument, argumentType.getPrimitiveType(), argumentType.getNodeName());
470470
}
471471

472472
/**
@@ -479,7 +479,7 @@ public <T> T getByArgument(AbstractArgument<T, ?, ?, ?> argumentType) {
479479
@Nullable
480480
public <T> T getByClass(String nodeName, Class<T> argumentType) {
481481
Object argument = get(nodeName);
482-
return castArgument(argument, argumentType);
482+
return castArgument(argument, argumentType, nodeName);
483483
}
484484

485485
/**
@@ -492,18 +492,36 @@ public <T> T getByClass(String nodeName, Class<T> argumentType) {
492492
@Nullable
493493
public <T> T getByClass(int index, Class<T> argumentType) {
494494
Object argument = get(index);
495-
return castArgument(argument, argumentType);
495+
// This seems like an extremely bogus implementation but I really don't want
496+
// to figure the node name vs index stuff out in the castArgument method
497+
int argumentIndex = 0;
498+
for (String nodeName : argsMap.keySet()) {
499+
if (argumentIndex != index) {
500+
argumentIndex++;
501+
continue;
502+
}
503+
// Ensure the found node name points to the right argument
504+
if (argument == null) {
505+
return null;
506+
}
507+
if (!argument.equals(argsMap.get(nodeName))) {
508+
throw new IllegalStateException("Unexpected behaviour detected while retrieving argument (arguments don't match up)!" +
509+
"This should never happen - if you're seeing this message, please" +
510+
"contact the developers of the CommandAPI, we'd love to know how you managed to get this error!");
511+
}
512+
return castArgument(argument, argumentType, nodeName);
513+
}
514+
throw new IllegalStateException("Unexpected behaviour detected while retrieving argument (didn't find the node name)!" +
515+
"This should never happen - if you're seeing this message, please" +
516+
"contact the developers of the CommandAPI, we'd love to know how you managed to get this error!");
496517
}
497518

498-
private <T> T castArgument(Object argument, Class<T> argumentType) {
519+
private <T> T castArgument(Object argument, Class<T> argumentType, String name) {
499520
if (argument == null) {
500521
return null;
501522
}
502-
if (!argument.getClass().equals(PRIMITIVE_TO_WRAPPER.getOrDefault(argumentType, argumentType))) {
503-
return null;
504-
}
505523
if (!PRIMITIVE_TO_WRAPPER.getOrDefault(argumentType, argumentType).isAssignableFrom(argument.getClass())) {
506-
return null;
524+
throw new IllegalArgumentException("Argument '" + name + "' is defined as " + argument.getClass().getSimpleName() + ", not " + argumentType);
507525
}
508526
return (T) argument;
509527
}

0 commit comments

Comments
 (0)