Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ef00068

Browse files
committed
Implement a map to map primitive types to their wrapper classes
1 parent 99a2ab8 commit ef00068

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.util.Collection;
88
import java.util.Collections;
9+
import java.util.HashMap;
910
import java.util.Map;
1011
import java.util.Optional;
1112
import java.util.function.Supplier;
@@ -48,6 +49,17 @@ public record CommandArguments(
4849
String fullInput
4950
) {
5051

52+
private static final Map<Class<?>, Class<?>> PRIMITIVE_TO_WRAPPER = new HashMap<>() {{
53+
put(boolean.class, Boolean.class);
54+
put(char.class, Character.class);
55+
put(byte.class, Byte.class);
56+
put(short.class, Short.class);
57+
put(int.class, Integer.class);
58+
put(long.class, Long.class);
59+
put(float.class, Float.class);
60+
put(double.class, Double.class);
61+
}};
62+
5163
// Access the inner structure directly
5264

5365
/**
@@ -487,10 +499,10 @@ private <T> T castArgument(Object argument, Class<T> argumentType) {
487499
if (argument == null) {
488500
return null;
489501
}
490-
if (!argument.getClass().equals(argumentType)) {
502+
if (!argument.getClass().equals(PRIMITIVE_TO_WRAPPER.getOrDefault(argumentType, argumentType))) {
491503
return null;
492504
}
493-
if (!argumentType.isAssignableFrom(argument.getClass())) {
505+
if (!PRIMITIVE_TO_WRAPPER.getOrDefault(argumentType, argumentType).isAssignableFrom(argument.getClass())) {
494506
return null;
495507
}
496508
return (T) argument;

0 commit comments

Comments
 (0)