[Q] Player Interface

Discussion in 'Bukkit Discussion' started by Wingzzz, May 18, 2013.

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

    Wingzzz

    Hey, the thought had never crossed my mind before, but I was coding and I realized we use an interface (Player) to "make things happen" with well... players. I have an understanding of inheritance and such, but I had never known you could use an interface's blank methods to execute code. Now, I see that CraftPlayer implements Player, is this is what is linking all of our code?

    Question:
    How can you use an interface's method(s) to execute code? (More specifically, can someone explain the Player interface's situation. Interfaces we all know have no default implementation, only blank methods and static final variables which is where my confusion on how calling player.getLocation() even does anything. Although explaining by use of other Bukkit interfaces used in a similar way is just as good.).

    Answers here, or links to external information is much appreciated.

    Thanks again.
     
  2. Offline

    Syd

    When you have a Player object in your Code it actually a CraftPlayer instance. But as it implements the Player interface you can also express it as Player.

    -> A interface can never stand alone. You always need a instance of a class which implements the interface.

    Basicly OO and inheritance.

    (I hope it's understandable, I'm horrible at explaining...)
     
    MrBluebear3 likes this.
  3. Offline

    desht

  4. Offline

    Wingzzz

    So if I were to make interface Group and have a class implement it called GroupBase, I can then just simply make a Group group and call methods from there? Where it confuses me is yes I know they implement and everything, I just don't understand how an interface's blank methods can be called to then be used by another class IE: Player being a CraftPlayer instance. Would you mind showing me an example of how this works / is done?

    Thanks, but I understand what an interface is for, when to use it over abstract classes and all of that, the part that confuses me is how using a Player object actually does things in CraftPlayer etc... Does an interface know what classes have implemented it, otherwise how can it execute code from another class?

    Thanks for the quick responses guys :)
     
  5. Offline

    Syd

    When you use a Player object it's actually a CraftPlayer object. As CraftPlayer is a instance of Player it can also be stored as Player while programming.
    In runtime however, it is seen as a object of CraftPlayer and calls CraftPlayer#doSomething() instead of Player#doSomething().

    What counts is the actual type of the object, not as what it is referred during coding.

    It's one of the core features of object orientation. Look for polymorphism.
     
  6. Offline

    Wingzzz

    Yes ex: Animal dog = new Dog(); etc... Though, I never knew that you could use interfaces in a way like interface.doSomething();... Now, since player.doSomething(); is just going to go to craftPlayer.doSomething(); how does it know to go to the CraftPlayer class to execute code instead of another class that implements the interface(in the case that multiple classes implement the interface)?
     
  7. Offline

    Syd

    In runtime it knows that the object, that wants to call doSomething() is a CraftPlayer, so it needs to use CraftPlayer#doSomething()

    Also the interface should not know which classes are implementing it.
     
  8. Offline

    Wingzzz

    So then at runtime how does player.doSomething() know which class to execute the code from, IE: player.doSomething() okay we could do craftPlayer.doSomething() or anotherPlayer.doSomething().... The interface may be implemented by a few classes, how does the JVM know specifically what implementing class to execute from when only calling the actual interfaces methods over calling a method from a class that implements the interface?

    (Thanks a ton for helping me understand this, couldn't really find much anywhere on the topic)
     
  9. Offline

    Syd

    When you write
    Code:
    Player p = new CraftPlayer ("dontknowwhattheconstructorwantsinreal")
    the JVM will create a new CraftPlayer object in memory and a reference to this part of memory at the varibale p.
    So if you call p, you actually call the section of memory where the created CraftPlayer object is stored.

    If you do p.doSomething() now, you'll go to the adress of the object and then go to the adress of the method, specified in this object.
    So it doesn't matter as what you save it in code, it does matter as what it is stored in memory.

    The methods Player#doSomething() and CraftPlayer#doSomething() are somehow linked by the compiler, as CraftPlayer implements Player. But I don't know how this stuff works exactly...

    All you have to know is:
    If you want to store a object in a variable the object needs to be related to the type of the variable. (inheritance)
    You have there only the methods avaible the variable type declares, but if you execute them it will take the methods out of the actual object type (which can, but not must, differ from the method of variable type, when this method is overwritten).

    PS: I don't guranteee that this is complety correct. ^^
     
  10. Offline

    Wingzzz

    Syd
    I would understand if we were to do Player player = new CraftPlayer(...); but we're not creating a CraftPlayer object, we're creating Player player = Bukkit.getPlayer(yada yada) / Player player = event.getPlayer(); / Player player = (Player) cs; which all return of type Player. So since we didn't explicitly create a CraftPlayer, we created an object of type Player(interface) is where my confusion goes. I understand inheritance a great deal, this concept of having code being executed by an interface is just not making much sense.


    How can
    Code:
    Player player = Bukkit.getPlayer("Wingzzz");
    player.teleport(location);
    
    Actually in fact get the player "Wingzzz" (I used this way just for convenience, there are better ways depending on the situation) and teleport the player to the location defined. First of all the player is of type Player, so I've got a interface variable "player", and I've then executed the blank method teleport(location);. I'm confused on how it even did a single thing...

    By the way, the reason I want a deeper understanding of this concept is because I am looking to replicate it (IE: an API).
     
  11. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    The Bukkit method getPlayer returns an object which implements Player. It doesn't matter what the implementation of Player is, just that it implements Player and thus implements all the methods of Player. Then, on that object, you call the teleport method. The implementation of Player that Bukkit passed you will then execute the teleportation however the implementation defines it.

    It does not matter to you that the Player you got happens to be a CraftPlayer (Which is CraftBukkit's implementation of Player), just that it has a teleport() method and that when you use it, it teleports the player. Doesn't matter how it does it, just that it happens.
     
  12. Offline

    Wingzzz

    mbaxter
    So getPlayer(); returns CraftPlayer not Player?

    I guess I don't need to know exactly how it's done for Player, I just would like a deeper understanding of how it works because I would like to replicate this concept.

    EDIT: Bukkit.getPlayer() returns of type Player, so how do we end up getting a CraftPlayer?
     
  13. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    A CraftPlayer is an implementation of Player.
     
  14. Offline

    Wingzzz

    I know, but how can calling player.doSomething(); actually do something from the CraftPlayer's implementation?
     
  15. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    Because the variable 'player' is currently holding an instance of a CraftPlayer (Which is a Player)
     
  16. Offline

    Wingzzz

    (Thanks a bunch for holding with my confusion, the only concept I've actually gotten stuck on so far)
    So Player player = Bukkit.getPlayer("guy"); returns of type Player, but Player since is implemented by CraftPlayer is an instance of CraftPlayer? I understand the explicitly defined ones such as Vehicle boat = new Boat(); but what if Player is implemented by multiple classes, which class does it know to be an instance of or execute code from etc..?
     
  17. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    The getPlayer method is implemented by CraftBukkit. Inside CraftBukkit's getPlayer, it acquires a CraftPlayer (Because that's the implementation of Player that CraftBukkit uses) and passes it to you.
     
  18. Offline

    Wingzzz

    The full process being laid out got me to understand it much better, thanks for explaining that!
     
    mbaxter likes this.
Thread Status:
Not open for further replies.

Share This Page