How to create classes such as Bukkit's "Player" class?

Discussion in 'Plugin Development' started by Randy Schouten, Nov 10, 2011.

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

    Randy Schouten

    I'm trying to broaden my knowledge of Java, and so I feel like I have to learn this.

    So what I'd like to know is, how would I set something as a player?
    Where is all the info stored (such as .getDisplayName())?

    Sorry for the short questions, but I'm not quite sure how to start with this.
    Thanks in advance. :)
     
  2. Offline

    nisovin

    It's stored in the class object in memory. I'm not sure I fully understand your question. If you want to make your own Player, you could just create a new class that implements Player.
     
  3. Offline

    Randy Schouten

    I want to create a class such as Player from scratch. I used Player as example of what I want to learn.
    My question is basically how this works.

    I've taken a look at Bukkit's source code, but I don't quite understand how it works.

    If you give me the name of such things, I will look it up first and then ask for help.
    But I have no idea what these things are actually called, which is why I ask here.
     
  4. public class myClass {
    }
    ?
     
  5. Offline

    Randy Schouten

    @tips48
    Right.
    I know how to make classes.

    Like I said, I don't even know how these classes are called.

    Let me see if I can explain it a bit differently.

    A player joins the server, it will be assigned to the class "Player".
    How does a player get assigned to that?
    Where does it store all the info of the player that just joined (for instance his name)?

    When I looked at the source, it gave the method "getDisplayName()", nothing else.
    Thus I'm not sure where it gets the name from.
     
  6. Thats all defined by the Minecraft NetServerHandler... Its pretty complicated, but if you really want help I suggest talking to the Spout team (#Spout)
     
  7. Offline

    Evenprime

    I understand what you mean. When you lookup "Player.java" in the bukkit project, you will only find an interface called "Player".

    Code:
    public [B]interface[/B] Player { ...
    
    That's already something you might want to look up in some online Java tutorial: "Interfaces and Classes". To answer the rest of your question on how to find the actual class that is used to do everything. The class is called "CraftPlayer". You can find it in the "CraftBukkit" project (while "Player" is part of the "Bukkit" project). It looks like this

    Code:
    public [B]class[/B] CraftPlayer extends ... [B]implements[/B] Player {
    ...
    }
    
    That class (and its parents) contain the actual code that is used by the CraftBukkit server. But be warned: CraftPlayer has about a dozen parent classes and implements lots of interfaces. It is probably the most complex class that CraftBukkit has to offer.
     
  8. Offline

    Randy Schouten

    Ah thank you very much.
    That cleared it up.
     
  9. Offline

    Celeixen

    Code:
    public class Randy{
    private String Name;
    private int Health;
    private double Experience;
    
    public Randy(String Name,int Health,double Experience){
    this.Name = Name;
    this.Health = Health;
    this.Experience = Experience;
    }
    
    public double GetExperience(){
    return Experience;
    }
    
    public int GetHealth(){
    return Health;
    }
    
    public String GetName(){
    return Name;}
    
    }
    There is an example that i wrote just for you with no IDE :) pretty basic...

    Edit:
    Just realised what you were talking about :/ ooh well
     
  10. Offline

    Randy Schouten

    I suppose that's also pretty handy to know.
    I haven't really thought about using a class that way. :)
     
  11. Offline

    Celeixen

    Well trust me when your making client mods, object orientated design makes it way better and easier when your want to upgrade certain parts :)
     
  12. Offline

    Randy Schouten

    Yeah, I see what you mean :D

    I have to broaden my java knowledge haha.
    I only recently started working with lists as interfaces, and they make things a lot easier.
    For instance, normally I used a stringarray for groups (or parties, whatever you prefer).
    But when you use a list for that, instead of arrays, you can easily remove a player from it for instance.
     
  13. Offline

    Celeixen

    Yep, its much easier that way xD

    Btw not sure if you already use them but my favorite "containers" are hashmaps & hashsets. They are so efficient and you can add/remove objects quickly without getting modify errors(when you edit an array twice at the same time) that you do with arrays.
     
Thread Status:
Not open for further replies.

Share This Page