Custom Player class

Discussion in 'Spigot Plugin Development' started by fishb6nes, Jan 25, 2015.

  1. In the Server object there is a method:
    Code (Text):
    Collection<? extends Player> getOnlinePlayers()
    Now I'm starting to wonder if that means that I can define a custom class that extends Player that I can use to store my players on the server during runtime. Anyone able to give me some insight into why this returns objects that extend the Player class rather than just returning a collection of Player objects given the fact that Player doesn't have any pre-defined subinterfaces?
     
  2. Why would you want to do this again?
     
  3. Keeping server efficiency in mind I can think of countless reasons why I'd want to be able to assign custom member fields and methods to Player objects.
     
  4. Make a wrapper instead. There are numerous reasons to why you shouldn't extend the Player object.
     
  5. @PickNChew And how would I go about having the server store the wrapper for me as the Player object?
     
  6. Just store a reference to a player (Through UUID or Name) in a new class. You'll be using oop.

    A very simplistic version would be this:

    Code (Text):
    public class MyWrapper{

    private String name;

    public MyWrapper(Player p){
         this.name = p.getName();
    }

    public Player getPlayer(){
         return Bukkit.getPlayer(name);
    }
    }
    Then you can add any special values you want to the player there.
     
  7. Are you seriously asking me this?
     
  8. Ok my question is clearly missunderstood.

    I was wondering after reading that
    Code (Text):
    Collection<? extends Player> getOnlinePlayers()
    returns any class that extends Player if there was a way for me to tell the server: "hey, we're gonna be using this Player class, rather than your default one". This way I'd be getting my own player class sent to me in events and what not. Again I was only wondering, it's purely hypothetical.
     
  9. As said above it would be best to make a wrapper class for this.

    Just make a method in the wrapper class that converts a Bukkit player to your wrapper player (If it exists).

    For example:
    (This is just pseudo code, you would most likely need to modify it to suit how you are storing the wrapped player objects)

    Code (Text):
    public PlayerWrapper getWrapperPlayer(Player p){
        return wrappedPlayers.get(p);
    }
     
  10. So my initial question is "Can I tell the server to use my own <? extends Player> class" then I get suggested twice to implement it as a wrapper class in stead, guy gets mad at me for asking how I then tell the server to use my wrapper class in stead of its default Player classes. I get told again to use a wrapper class, am I supposed to ask how the server is going to use my wrapper class again now or?

    edit: forget it guys, I appreciate the effort, but this isn't going anywhere. I'll assume it's not possible, never was, and never will be.
     
  11. @fishb6nes one of the reasons it would be ? extends Player is type erasure. Collection<Player> will be Collection<Object> after compilation, Collection<? extends Player> will be Collection<Player> after compilation.
     
    • Informative Informative x 1
    • Useful Useful x 1