@@ -552,28 +552,7 @@ public <T> Optional<T> getOptionalByClass(String nodeName, Class<T> argumentType
552552 @ Nullable
553553 public <T > T getByClass (int index , Class <T > argumentType ) {
554554 Object argument = get (index );
555- // This seems like an extremely bogus implementation but I really don't want
556- // to figure the node name vs index stuff out in the castArgument method
557- int argumentIndex = 0 ;
558- for (String nodeName : argsMap .keySet ()) {
559- if (argumentIndex != index ) {
560- argumentIndex ++;
561- continue ;
562- }
563- // Ensure the found node name points to the right argument
564- if (argument == null ) {
565- return null ;
566- }
567- if (!argument .equals (argsMap .get (nodeName ))) {
568- throw new IllegalStateException ("Unexpected behaviour detected while retrieving argument (arguments don't match up)!" +
569- "This should never happen - if you're seeing this message, please" +
570- "contact the developers of the CommandAPI, we'd love to know how you managed to get this error!" );
571- }
572- return castArgument (argument , argumentType , nodeName );
573- }
574- throw new IllegalStateException ("Unexpected behaviour detected while retrieving argument (didn't find the node name)!" +
575- "This should never happen - if you're seeing this message, please" +
576- "contact the developers of the CommandAPI, we'd love to know how you managed to get this error!" );
555+ return castArgument (argument , argumentType , index );
577556 }
578557
579558 /**
@@ -605,14 +584,26 @@ public <T> Optional<T> getOptionalByClass(int index, Class<T> argumentType) {
605584 return Optional .ofNullable (argument );
606585 }
607586
608- private <T > T castArgument (Object argument , Class <T > argumentType , String name ) {
587+ private <T > T castArgument (Object argument , Class <T > argumentType , Object argumentNameOrIndex ) {
609588 if (argument == null ) {
610589 return null ;
611590 }
612591 if (!PRIMITIVE_TO_WRAPPER .getOrDefault (argumentType , argumentType ).isAssignableFrom (argument .getClass ())) {
613- throw new IllegalArgumentException ("Argument '" + name + "' is defined as " + argument .getClass ().getSimpleName () + ", not " + argumentType );
592+ throw new IllegalArgumentException (buildExceptionMessage ( argumentNameOrIndex , argument .getClass ().getSimpleName (), argumentType . getSimpleName ()) );
614593 }
615594 return (T ) argument ;
616595 }
617596
597+ private String buildExceptionMessage (Object argumentNameOrIndex , String expectedClass , String actualClass ) {
598+ if (argumentNameOrIndex instanceof Integer i ) {
599+ return "Argument at index '" + i + "' is defined as " + expectedClass + ", not " + actualClass ;
600+ }
601+ if (argumentNameOrIndex instanceof String s ) {
602+ return "Argument '" + s + "' is defined as " + expectedClass + ", not " + actualClass ;
603+ }
604+ throw new IllegalStateException ("Unexpected behaviour detected while building exception message!" +
605+ "This should never happen - if you're seeing this message, please" +
606+ "contact the developers of the CommandAPI, we'd love to know how you managed to get this error!" );
607+ }
608+
618609}
0 commit comments