commands icon indicating copy to clipboard operation
commands copied to clipboard

Could not find context resolver

Open McLive opened this issue 5 years ago • 10 comments

I'm seeing really strange issues when I try to implement a command Interface on my BaseCommand

It just started to happen since I added BaseCommand implements ISkinCommand<CommandSender, Player, OnlinePlayer> to my BaseCommand class.

This is the full class causing that error: https://github.com/SkinsRestorer/SkinsRestorerX/blob/c1840af58a68c199e53211390a25236f5aa2cee3/src/main/java/skinsrestorer/bukkit/commands/SkinCommand.java

This is where the error happens: https://github.com/SkinsRestorer/SkinsRestorerX/blob/c1840af58a68c199e53211390a25236f5aa2cee3/src/main/java/skinsrestorer/bukkit/SkinsRestorer.java#L169

What am I doing wrong?

McLive avatar Aug 19 '19 19:08 McLive

I am fairly confident I know what is happening. java.lang.reflect.Parameter; is not able to properly get the type of your method parameter at runtime - related to the generics involved. Grabbing the type of the interface instead of the implementing class. https://github.com/aikar/commands/blob/master/core/src/main/java/co/aikar/commands/CommandParameter.java#L72

As it stands, there would probably have to be some change to acf itself to support the behavior you are wanting. Looks like some research needs done in order to find the solution to this.

How to proceed from here. Either find an alternative to Parameter#getType() that gets the implementing method's parameter type and PR to acf. OR just not do this.

chickeneer avatar Aug 19 '19 22:08 chickeneer

@chickeneer Maybe something like this?

https://stackoverflow.com/a/1901275

McLive avatar Aug 22 '19 15:08 McLive

Maybe... Some testing needs done given this information. Not 100% certain this gives the full solution. Could get as simple as replacing this.type = param.getType(); with this.type = param.getClass();

chickeneer avatar Aug 22 '19 23:08 chickeneer

I can't even get anything to pass tests ¯_(ツ)_/¯

With this.type = param.getClass();:

CommandManagerTests.initEach:48 » InvalidCommandContext Parameter Parameter of...

With this.type = (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];:

CommandManagerTests.initEach:48 » ClassCast java.lang.Class cannot be cast to ...

Some comments on stackoverflow even say that something like this is not possible.. I have no idea

McLive avatar Aug 23 '19 13:08 McLive

@chickeneer that's not right. .getclass() will return Parameter.class which is completely wrong. the BukkitRegisteredCommand is getting passed as a PARAMETER somewhere and that isn't correct. I can't see that in any of this?

gotta find where BRC is even getting injected as a parameter.

aikar avatar Aug 23 '19 16:08 aikar

Derp. What was I thinking when I typed that. Definitely agree with the linked site not having the needed solution. Theoretically, the would be a plugin compile thing to prevent the type erasure for the generics.

chickeneer avatar Aug 23 '19 18:08 chickeneer

Let me know if there's something I can try to solve this

McLive avatar Aug 23 '19 18:08 McLive

I'm not sure where generics should even be a problem here. the error deals with parameters, and none of the parameters use generics.

aikar avatar Aug 23 '19 19:08 aikar

No... I think generics is exactly the problem. It is extending the ISkinCommand interface https://github.com/SkinsRestorer/SkinsRestorerX/blob/c1840af58a68c199e53211390a25236f5aa2cee3/src/main/java/skinsrestorer/shared/interfaces/ISkinCommand.java#L13

Implemented here. https://github.com/SkinsRestorer/SkinsRestorerX/blob/c1840af58a68c199e53211390a25236f5aa2cee3/src/main/java/skinsrestorer/bukkit/commands/SkinCommand.java#L33

From what I can tell. Parameter#getType is getting Object from the interface instead of the implementing class.

chickeneer avatar Aug 23 '19 21:08 chickeneer

Hello,

I seem to be experiencing this same issue in my own plugin. Has a solution been found?

andythetech avatar Dec 15 '20 05:12 andythetech