Solved Unable to cast player to custom player exitsing craft player explaination

Discussion in 'Plugin Development' started by Lightspeed, Dec 20, 2015.

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

    Lightspeed

    I think I didn't learn somthing or remember it but, if you can please explain why.

    Code:
    LightPlayer lp = (LightPlayer) s;
    If I change s to craftplayer it's the same thing yet LightPlayer extends it so why can't I just use it?
    I don't know about you but, this bothers me. :confused:[skeleton]

    This thread isn't important so it's not gonna finish my plugin or anything it's just a question.
     
    Last edited: Dec 20, 2015
  2. Offline

    teej107

    @Lightspeed Let's take a look at this with an (best as I can do) inheritance tree. I'm assuming "s" is CraftPlayer or Player.

    CraftPlayer extends Player. LightPlayer extends Player. LightPlayer is not a CraftPlayer because LightPlayer doesn't extend it. CraftBukkit always passes an instance of CraftPlayer but we always reference it as Player. CraftPlayer and LightPlayer both extend Player but that is the upper most thing they have in common. LightPlayer is not a CraftPlayer!
    https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
     
  3. Offline

    Lightspeed

    I have LightPlayer extend CraftPlayer?
     
  4. Offline

    teej107

    So let me modify the inheritance tree a bit.
    Remember when I said that CraftBukkit passes a CraftPlayer object? CraftPlayer is an instance of Player which is an instance of Object but you can't go up the inheritance tree.
     
  5. Offline

    Lightspeed

    @teej107 So I can have LightPlayer lp = new LightPlayer(cp.getHandle());
    to init it but, never cast it?
     
  6. Offline

    teej107

    Mrs. bwfctower likes this.
  7. Offline

    Lightspeed

    I did this just is confusing and I've never had this before. :L
     
  8. Offline

    Mrs. bwfctower

    If it's confusing for you that shows that you haven't fully learned it and come to understand it. Just practice it without using the Bukkit API and really master the idea of Objects.
     
  9. Offline

    Lightspeed

    @Mrs. bwfctower @teej107 Ok thanks with that explaination I'm now able to understand why I can't cast thank you :D
    It feels a ton better understanding why. :D
     
Thread Status:
Not open for further replies.

Share This Page