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

Add methods to players

Comments in 'Plugin Development' started by Tethered_, Jun 19, 2015.

  1. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    Is there a way to extend a class or do something that I can add variables and methods to players, so I could do for example:
    PHP:
    public function onBlockBreak(BlockBreakEvent $event) {
        
    $event->getPlayer()->myMethod();
    }
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    No. Create your own class that references to the Player, initialize it at login and finalize it at quit.

    Take a look at the BulkCommands plugin for example.

    Of course, you can as well do a $player->_______myname_myvar to define a dynamic property and get it later, but it is very bad practice.
    EvolSoft and Tethered_ like this.
  3. EvolSoft
    Offline

    EvolSoft Notable Member Plugin Developer

    Joined:
    Sep 10, 2014
    Posts:
    821
    Plugins:
    15
    Minecraft User:
    Flavius12
    Another way to do this is editing PocketMine source but @PEMapModder gave the best solution especially if you want to redistribute this part of code
  4. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    If I'm running my own private server can I add my own methods? And is it legal?
  5. EvolSoft
    Offline

    EvolSoft Notable Member Plugin Developer

    Joined:
    Sep 10, 2014
    Posts:
    821
    Plugins:
    15
    Minecraft User:
    Flavius12
    Yes of course, you can fully edit PocketMine source: don't forget that PocketMine is licensed under GNU GPL.
    But if you add methods to PocketMine classes and then use those on your plugins, you can't redistribute them unless you redistribute also your modified PocketMine version
    Tethered_ likes this.
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    And the important thing is, you won't be able to update your PocketMine version fluently.

    Let me explain my suggestion with some code:
    PHP:
    // main class that implements Listener
    private $sessions = [];
    public function 
    onJoin(PlayerLoginEvent $event){
      
    $this->sessions[$event->getPlayer()->getId()] = new Session($this$event->getPlayer()); // from my experience, getting the main class object is almost necessary for every object that interacts with your plugin, so my practice is to make it pass it even if you don't foresee the need to use it yet.
    }
    public function 
    onQuit(PlayerQuitEvent $event){
      if(isset(
    $this->sessions[$id $event->getPlayer()->getId()])){
        
    $this->sessions[$id]->finalize();
        unset(
    $this->sessions[$id]);
      }
    }
    public function 
    getSession(Player $player){
      return isset(
    $this->sessions[$id $player->getId()]) ? $this->sessions[$id] : null;
    }
    P.S. I have written this framework so many times that it gets so boring.
    PHP:
    class Session{
      public 
    $main;
      public 
    $player;
      public 
    $isUsingCommand false;
      public function 
    __construct(Main $mainPlayer $player){
        
    $this->main $main;
        
    $this->player $player;
        
    // tip: if you use PHPStorm, move your pointer to one of the parameters
        // of the constructor and type Alt+Enter (or whatever your keymap is for auto
        // intentions), and there would be an "Initialize fields" button. Click it
        // to automatically add the private fields and the $this->*=$* lines
      
    }
      public function 
    finalize(){
        
    // when player quits
      
    }
    }
    Then an example command handler inside your main class:
    PHP:
    /** @var CommandSender $send */
    if(!($send instanceof Player)){
      
    $send->sendMessage("please run this command in-game");
      return 
    true;
    }
    $ses $this->getSession($send);
    // we don't need to check whether getSession returns null or a real object, because a player wouldn't issue a command until he has logged in.
    if(!$ses->isUsingCommand){
      
    $ses->isUsingCommand true;
      
    $send->sendMessage("Are you sure? Run this command again to confirm");
      return 
    true;
    }
    // execute the command
    $ses->isUsingCommand false;
    EvolSoft likes this.

Share This Page

Advertisement