1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Tutorial [Beginners] Separating Listener from PluginBase

Discussion in 'Resources' started by Diduhless, Nov 24, 2016.

  1. Diduhless

    Diduhless Baby Zombie

    Messages:
    199
    GitHub:
    Diduhless
    This guide will explain how to split the Listener from PluginBase, the reason behind this is to make the plugin cleaner and more readable.

    We'll start creating the main class that extends PluginBase, do not implement the listener.
    PHP:
    class Tutorial extends PluginBase {

    }
    Now we are going to create a global variable called listener, with this variable, the classes will be able to get the listener class.
    PHP:
    private $listener;
    Now we are going to make two public functions called registerListener() and getListener().
    registerListener(): This function will register the events of the specified listener class.
    getListener(): Returns the listener class (stored in global listener variable).
    PHP:
    public function registerListener() {
        
    $this->getServer()->getPluginManager()->registerEvents(new TutorialListener($this), $this);
    }

    public function 
    getListener() {
        return 
    $this->listener;
    }
    TutorialListener is the events class that implements Listener.
    Now we are going to override the onEnable function and we will define $this->listener as the listener class.
    PHP:
    public function onEnable() {
        
    $this->listener = new TutorialListener($this);
     
        
    $this->registerEvents();
    }
    Remember $this references to the class itself and we're using it inside new TutorialListener() because the listener constructor requires an instance of PluginBase (this main class does extend it).

    We must have this:
    PHP:
    <?php
    namespace Tutorial;


    use 
    pocketmine\plugin\PluginBase;

    class 
    Tutorial extends PluginBase {

        private 
    $listener;

        public function 
    onEnable() {
            
    $this->listener = new TutorialListener($this);

            
    $this->registerEvents();
        }

        public function 
    registerListener(): void {
            
    $this->getServer()->getPluginManager()->registerEvents(new TutorialListener($this), $this);
        }

        public function 
    getListener(): TutorialListener {
            return 
    $this->listener;
        }

    }
    So now let's create the events class, I'll call it TutorialListener (usually main class name + Listener)
    PHP:
    class TutorialListener implements Listener {

    }
    Now we will create another global variable called $plugin that will let us access to the main class functions (including PluginBase functions as that is what the main class extends, I repeat).
    PHP:
    private $plugin;
    Now we are going to call the magic method __construct to require the main instance when creating a new instance of the events class.
    We will define $this->plugin as the main class.
    PHP:
    public function __construct(Tutorial $plugin) {
        
    $this->plugin $plugin;
    }
    There's no need to register the events here because they are already registered on the Tutorial class.

    And this is the final result:
    PHP:
    <?php

    namespace Tutorial;

    use 
    pocketmine\event\Listener;

    class 
    TutorialListener implements Listener {

        private 
    $plugin;

        public function 
    __construct(Tutorial $plugin) {
            
    $this->plugin $plugin;
        }

    }
    Now it's all finished! $this->plugin refers to the main class so that's what you're going to use instead of only $this because you are not extending PluginBase.

    Leave a comment if there's something you have not understood! Thanks for reading.
     
    Last edited: Jun 14, 2020
    CaptainDuck likes this.
  2. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    And the most important thing - why do you want to separate event listener from the main class?
     
    jasonwynn10 likes this.
  3. Diduhless

    Diduhless Baby Zombie

    Messages:
    199
    GitHub:
    Diduhless
    Thanks. I added.
     
    jasonwynn10 likes this.
  4. Primus

    Primus Zombie Pigman

    Messages:
    749
    Not true. Having your PluginBase as Listener isn't bad for performance. I personally split them apart just to keep them organized.
     
  5. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    I can't stand it. I edited the post :/
     
    jasonwynn10, Primus and Thunder33345 like this.
  6. Diduhless

    Diduhless Baby Zombie

    Messages:
    199
    GitHub:
    Diduhless
    Are you serious? You can just delete what I wrote instead of putting that amount of s*** on my post
     
    Primus likes this.
  7. Primus

    Primus Zombie Pigman

    Messages:
    749
    He's kinda true. But you were totally wrong and mistakes may cost a lot sometimes.
     
  8. Diduhless

    Diduhless Baby Zombie

    Messages:
    199
    GitHub:
    Diduhless
    I even know separating Listener from PluginBase increases lag.
    Can we delete all of this posts to don't generate spam?
     
  9. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    well there still one use foe this as for people who want to be more organized
     
  10. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    It doesn't 'increase' lag if done correctly. It may punch the startup time by >1ms, but unless you don't pass and throw references or values around all classes you have in your plugin I don't think separating it is really noticeable at all. It may be something as ridiculous as 2CPU operations more...
     
  11. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    For beginners I don't think it is a good idea to separate the event listener from their main class if they do not even understand what an object is.
     
  12. basprohop

    basprohop Silverfish

    Messages:
    17
    An object is a material thing that can be seen and touched.
     
  13. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    Some do, sadly, really believe that...
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.