Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Is it safe to extend Player class?

Comments in 'Plugin Development' started by TigerHix, May 13, 2015.

  1. TigerHix
    Offline

    TigerHix Active Member

    Joined:
    May 11, 2015
    Posts:
    61
    Minecraft User:
    TigerHix
    The title says it all :p

    Or a wrapper class would be better?

    EDIT: Just found that I can modify player class in PlayerCreationEvent. It seems certainly possible!
    Last edited: May 13, 2015
  2. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,706
    Plugins:
    22
    Minecraft User:
    Falkirknh
    It is a possible, and "safe". However, it is bad practice. If you are running your own SourceInterface, it makes sense, but for other problems, there should be a better solution.
    TigerHix likes this.
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    What do you want to do?
  4. TigerHix
    Offline

    TigerHix Active Member

    Joined:
    May 11, 2015
    Posts:
    61
    Minecraft User:
    TigerHix
    Add custom methods / fields to the class? :p

    I am trying to write a minigame plugin, and of course the code will involve things like reading and writing player data, getting information from database, etc. Originally wanted to implement features using a wrapper class, like:

    PHP:
    class PlayerInfo {

        private static 
    $playerInfos;
       
        public static function 
    getPlayerInfo(Player $player)
        {
            
    $info $this->playerInfos[$player->getName()];
            if (
    $info === null)
            {
                
    $info = new PlayerInfo($player);
                
    $this->playerInfos[$player->getName()] = $info;
            }
            return 
    $info;
        }


        private 
    $player;

        public function 
    __construct(Player $player) {...}

        
    // Custom fields and methods here

    }
    Something like that. But it is a pain in the ass to call PlayerInfo::getPlayerInfo(Player) everytime when I need access to certain data. Therefore I posted the question.

    And yes, it is possible, and PocketMine even has a built-in support for replacing the Player class, through PlayerCreationEvent. :D
  5. 64FF00
    Offline

    64FF00 Active Member Plugin Developer

    Joined:
    May 11, 2014
    Posts:
    297
    Plugins:
    2
    Minecraft User:
    64FF00
    At least it would be safe as long as it doesn't blow up your phone.
    PEMapModder and TigerHix like this.
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    The PocketMine extension, pthreads, breaks something when you use static object properties. Avoid using static properties containing objects or arrays of objects.
    64FF00 and TigerHix like this.
  7. Samueljh1H8sLag
    Offline

    Samueljh1H8sLag Active Member

    Joined:
    Jun 20, 2015
    Posts:
    168
    Minecraft User:
    Samueljh1_
    how did you manage to do that? (playercreationevent)
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    I would just like to add a note on this half a year old threads (bumping!), that unless you are coding a private plugin, or unless it is impossible to otherwise do so, never use PlayerCreationEvent. Two plugins doing so will clash and only one can survive.

Share This Page

Advertisement