Extending Player

Discussion in 'Plugin Development' started by Aza24, Mar 23, 2012.

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

    Aza24

    Is this possible? I would really like to extend it to use in my plugin as a unique type of player which has extra methods along with the ones in the Player class.
     
  2. Offline

    maxp0wer789

    can't think of a reason why it shouldn't be possible. Player is a Class so you can inherit from it.
     
  3. Offline

    hpdvs2

    Is it possible to access the event or location of initially constructing the player event, so we can have it load from the new Player class?

    I'm interested in this, bu have no clue where I would have the opportunity to start it.
     
  4. Offline

    maxp0wer789

    The instantiation of the Player Object happens somewhere between the PlayerPreLoginEvent and PlayerLoginEvent.
    But I'm pretty sure you would've to alter the bukkit code to create your own, new Player-object.

    I just took a look at the bukkit documentation and Player ist an interface. So forget anything i said :)

    Just create a new Interface

    public interface NewPlayer extends Player {
    //Add your new methods here
    }

    If u then want to use your new Player interface just cast the normal Player to your interface e.g.

    NewPlayer player = (NewPlayer) getServer().getPlayer("somePlayer");

    I hope this helps

    greetings maxp0wer

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  5. Offline

    Darkman2412

    You can't do that. Example:
    Code:
    - Interface A
    - Class CraftA implements A
    This is mainly how CraftBukkit works. You can cast CraftA, which is 'related' to A, to the interface A.
    What happens when you put a custom interface there?
    Code:
    - Interface A
    - Interface CustomA extends A
    - Class CraftA implements A
    Now, CraftA isn't related to CustomA => you can't cast it to CustomA.
     
    maxp0wer789 likes this.
  6. Offline

    maxp0wer789

    casting a parent to a child, stupid >.< thx darkman for the brain correction

    Hypothatical: does it work if you decompile bukkit, add an interface which inherits HumanEntity, Conversable, CommandSender, OfflinePlayer, PluginMessageRecipient
    then alter the Player class so inherits from your new interface and compile it again. Just thinking maybe I'm terribly wrong :)

    greetings maxp0wer


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  7. Offline

    Sagacious_Zed Bukkit Docs

    You can freely alter the source of bukkit and craftbukkit.
     
  8. Offline

    maxp0wer789

    So that's a way to solve the problem then...
     
  9. Offline

    Sagacious_Zed Bukkit Docs

    But if you want standard plugins to work with your edited version of the API you have to make non breaking changes. And if you make a plugin based on your modified API don't expect it to work on vanilla Bukkit servers.
     
  10. Offline

    maxp0wer789

    "interconnecting" an interface with correct inheritance shouldn't affect other plugins i guess. Your new Interface would have only the information it gets from the parent classes. So you wouldn't have the full functionality of the player interface though.
     
  11. Offline

    Double0negative

    Just extend Player and override the methods you want to and add the ones you want too

    Code:
    public class CustomPlayer extends Player{
     
    methods methods
     
    }
     
    
    then just use the CustomPlayer object instead of a Player object
     
  12. Offline

    Darkman2412

    You can always use some kind of wrapper, like so :)
    Code:
    public class CustomPlayer {
    
        private Player player;
        
        private static Map<String, CustomPlayer> customplayers = new HashMap<String, CustomPlayer>();
        
        private CustomPlayer(Player player) {
            this.player = player;
            customplayers.put(player.getName(), this);
        }
        
        // Return a running instance (or create a new one)
        public static CustomPlayer getInstanceOfPlayer(Player player) {
            if(!customplayers.containsKey(player.getName()) {
                return new CustomPlayer(player);
            }
            else if(customplayers.containsKey(player.getName()) {
                return customplayers.get(player.getName());
            }
            else {
               
            }
        }
        
        // Your special (non-static) methods defined here:
        public String getSpecialName() {
            if(player.getName().equalsIgnoreCase("notch")) {
                return ChatColor.GREEN + player.getName();
            }
            return player.getName();
        }
        
    }
    Usage:
    Code:
    Player player = blub;
    CustomPlayer cplayer = CustomPlayer.getInstanceOfPlayer(player);
    Bukkit.broadcastMessage(cplayer.getSpecialName());
    
    I don't actually know if it's allowed to have private constructors, but huh :p
     
    Aengo likes this.
  13. Offline

    Sagacious_Zed Bukkit Docs

    you can have private constructors, then the only class to instantiate itself is itself.
     
  14. Offline

    Aza24

    If Players a class in CraftBukkit, can I use CraftBukkit as my resource?
     
  15. Offline

    Technius

    You need to use the EntityPlayer NMS class, which frequently changes. And you'll also have to use CraftBukkit as your resource. In addition, you'll need to access a couple of private methods and other things. After that, you can make a class that extends CraftPlayer.
     
Thread Status:
Not open for further replies.

Share This Page