Creating Your Own Player Class

Discussion in 'Plugin Development' started by Kodate, Apr 6, 2013.

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

    Kodate

    Hello!
    I'm trying to create my own player class, so each player can have their own attributes that I create. I can't figure out how to make each player that logins declare a new HNSPlayer class, with their name as the identifier.
    I also can't figure out how to convert from bukkit's player class to my own HNSPlayer class, i.e. when I use commands like bukkit.getonlineplayers(), I'd like to have that list be of HNSPlayer, not of bukkit's player class.

    Please help! Thanks.
     
  2. Offline

    raGan.

    Can't you just create separate object and store it in hashmap with player's name as a key ?
     
  3. Offline

    chasechocolate

    It would probably be easier to save player names in a HashMap/Map/List than to create a class that extends Player.
     
  4. Offline

    CubixCoders

    Just make a new class thats called HSNPlayer with the methods in it you like, have these variables and methods
    Code:java
    1.  
    2. protected Player player;
    3. protected String name;
    4. //Any other variables
    5.  
    6.  
    7. //The constructor
    8. public HSNPlayer(Player player){
    9. this.player = player;
    10. this.name = player.getName();
    11. }
    12.  
    13. public boolean isHNS(){
    14. //if your list contains the player or whatever return true, else false
    15. }
    16.  
    17. //Other methods you want
    18. public Player[] getHNSPlayers(){
    19. Player[] online;
    20. int i = 0;
    21. for(Player p: Bukkit.getOnlinePlayers()){
    22. HNSPlayer pl = new HNSPlayer(p);
    23. if(p.isHNS()){
    24. online[i] = p;
    25. i++;
    26. }
    27. }
    28. return online;
    29. }
    30. [/i]
     
  5. Offline

    Kodate

    Thanks! I've tried what you suggested, I just don't understand how the method isHNS() would work. Don't I need to have isHNS(Player player) and then compare it to a list of players?

    How would I do that? Thanks. CubixCoders
     
  6. Offline

    ZeusAllMighty11

    You should really look into OOP (Object oriented programming)

    It takes a while to learn, but it's sooo worth it
     
  7. Offline

    Jogy34

    For one, don't store player objects that can easily cause memory leaks.

    For two, for storing and retrieving the HNSPlayers I would suggest having a static HashMap of <String, HNSPlayer> then on the last line of your constructor you can store the HNSPlayer like this:
    Code:
    //Above constructor
    private static HashMap<String, HNSPlayer> allHNSPlayers = new Hashmap<String, HNSPlayer>();
     
    //Last line of constructor
    allHNSPlayers.put(playerName, this);
    
    then you can have a method like this to get the player:
    Code:
    public static HNSPlayer getHNSPlayer(String playerName)
    {
        if(allHNSPlayers.containsKey(playerName))
        {
            return allHNSPlayers.get(playerName);
        }
        return null;
    }
    
    That way you can get the players from anywhere fairly easily. I do a similar type of thing for my TARDIS plugin and it has worked really well so far.
     
  8. Offline

    Austy

    It's not a good practice to publish this reference before an object is constructed. You can use a static factory method instead, that will create an object and then will add it to a static map.
     
  9. Offline

    Jogy34

    That's why I said to put it as the last line in the constructor so as long as he isn't using multiple threads and not using it to extend from another class he should be fine. The problem I see with the static factory method is if there are multiple arguments so each time he wants to retrieve a player he would have to send in each argument even if the HNSPlayer for that Player is already created. If you only have to send in the name then you should probably do it that way unless you don't want to create an HNSPlayer for every Player but only a few select ones.
     
  10. Offline

    lenis0012

Thread Status:
Not open for further replies.

Share This Page