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: <?phpnamespace 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: <?phpnamespace 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.
Not true. Having your PluginBase as Listener isn't bad for performance. I personally split them apart just to keep them organized.
I even know separating Listener from PluginBase increases lag. Can we delete all of this posts to don't generate spam?
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...
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.