javaminecraftbungeecord

Problems with casting a class to a player


I was working on a bungeecord plugin, when i ran into a issue. I was working with this API , when i ran into an issue. To obtain the PlayerParty object i need to use this method

PartyManager.getInstance().getParty()

But it takes in a OnlinePAFPlayer, so i tried this:

OnlinePAFPlayer player = (OnlinePAFPlayer) sender;
PartyManager.getInstance().getParty(player)

But this didn't work, and i got this exception:

java.lang.ClassCastException: net.md_5.bungee.UserConnection cannot be cast to de.simonsator.partyandfriends.api.pafplayers.OnlinePAFPlayer

Any ideas on how to fix this?


Solution

  • The reason for your exception is that the class OnlinePAFPlayer does not extend from UserConnection or that there is a conversion method given in the API, which is the type of your sender-object.

    To get a PAFPlayer you can use PAFPlayerManager.getInstance().getPlayer("Notch"). If the player is online you can cast it to OnlinePAFPlayer. Alternatively you can convert ProxiedPlayer-objects to OnlinePAFPlayer's, similar to the method above.

    I'd suggest you try to convert your sender-object to ProxiedPlayer first, then convert it using the method mentioned above :)