Casting Player to another class

Discussion in 'Plugin Development' started by Ahniolator, Oct 1, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Ahniolator

    For a "Secret" project that I am working on, I need to cast Player onto another class, where I have created a few extra methods to make my life easier. I know that Spout does something similar to this, but I don't know how it works or how I could do it. Is there anyone that could explain this to me?

    Sorry if this is not all that clear. It's hard (at least for me) to be clear with what I want if I don't understand large parts of it.
     
  2. Offline

    stelar7

    //first class
    public static Player player = something
    //2nd class
    Player player = firstclass.player

    ?
     
  3. Offline

    Ahniolator

    That's not what I mean... I want to cast Player onto (for example) EPlayer, and have EPlayer inherit all of the methods, etc. from Player while gaining the new methods specified in EPlayer. That way I could use EPlayer.getDisplayName(); and also EPlayer.insertNewMethodHere();
     
  4. Offline

    ZNickq

    I think you just write something like:
    SpecialPlayer extends Player

    and then you just cast it
    SpecialPlayer splr=(SpecialPlayer) player;
     
  5. Offline

    stelar7

    EPlayer Eplayer = (Player) sender; or sumething?
     
  6. Offline

    Ahniolator

    Here's what my two relevant headers look like:

    Code:java
    1. public abstract interface bPlayer extends Player {
    2. //contains abstract methods
    3. }
    4.  
    5. //In a separate class file
    6. public class bConPlayer extends CraftPlayer implements bPlayer {
    7. //Contains actual abstract method code from bPlayer
    8. }


    Every time I try to cast Player to bPlayer, I get a ClassCastException, and I'm not sure how to fix it.
     
  7. Offline

    ZNickq

    try:
    Code:
    bPlayer extends [SIZE=12px][FONT=Bitstream Vera Sans Mono][COLOR=rgb(0, 0, 0)]org[B].[/B][COLOR=rgb(0, 128, 128)]bukkit[/COLOR][B].[/B][COLOR=rgb(0, 128, 128)]entity[/COLOR][B].[/B][COLOR=rgb(0, 128, 128)]Player[/COLOR][/COLOR][/FONT][/SIZE]
    
    That's what Spout does! :D
     
  8. Offline

    Ahniolator

    Maybe @Afforess will help me with this... He's on the Spout team, so maybe he could shed some light on how I can do this.

    Edit: In all likelihood, I probably need to learn more Java to understand what I need to do here as well...
     
  9. Offline

    ZerothAngel

    The reason for the ClassCastException is because the Player object you receive from CraftBukkit is still a CraftPlayer. It doesn't implement bPlayer.

    The way to fix this is to create a wrapper class around Player. This wrapper should accept a Player object in its constructor and then delegate all the normal Player method calls to that object.

    Then you can add your custom bPlayer methods to this wrapper.

    Anytime you encounter a Player from CraftBukkit (e.g. Server.getPlayer(), or event.getPlayer()), you wrap it with your wrapper class and then pass that wrapped object on to your methods.
     
  10. Offline

    Afforess

    I can tell you how, but I recommend you don't. The problem is that only one plugin can extend and replace players w/o problems. If two plugins do it, they will break each other.

    If you really do want to know, we extend craftplayer and replace the contents of the bukkitentity field for entity players.

    I suggest using a wrapper, like others have suggested instead though.
     
  11. Offline

    Ahniolator

    Thanks for the suggestions, and I've decided to go with the wrapper. And that really is a long list of methods that are in Player :eek:
     
  12. Offline

    ZerothAngel

    Yeah, about that... is there any reason bPlayer must extend Player? Wrappers are fragile and are definitely a pain to write/maintain.

    Would be nice if Bukkit itself had some sort of abstract "PlayerWrapper" class to make this process easier and shield plugin developers from changes to the Player interface...

    Anyway, proxying would also save work delegating all those Player methods, plus shield you from future changes. Might be worth a look.
     
  13. Offline

    Ahniolator

    No, it did not need to extend Player, and actually, upon further investigation I created a proxy class instead of a wrapper. And it did indeed make it a lot easier to delegate all of the methods, even though there are so many. Thanks for all of the help, it made my life a lot easier ;)
     
Thread Status:
Not open for further replies.

Share This Page