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

Multiple Classes?

Discussion in 'Facepalm' started by WreckagePE / ZAYD, Apr 29, 2017.

  1. WreckagePE / ZAYD

    WreckagePE / ZAYD Slime

    Messages:
    82
    How can I include multiple classes/files in my plugin.
    I have included the main in the constructor of my second file.
    However in the Main class, do I have to registerEvents? and if so, how?
     
  2. TheDiamondYT

    TheDiamondYT Zombie

    Messages:
    298
    GitHub:
    TheDiamondYT1
    To register events to another class

    PHP:
    public function onEnable() {
        
    $this->getServer()->getPluginManager()->registerEvents(new EventListener($this), $this);
    }
    EventListener.php
    PHP:
    <?php

    use pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerJoinEvent;

    class 
    EventListener implements Listener {
        private 
    $plugin;

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

        public function 
    onJoin(PlayerJoinEvent $event) {

        }
    }
     
    WreckagePE / ZAYD likes this.
  3. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    Try to only use that when there's good reasons to do so...
     
  4. TheDiamondYT

    TheDiamondYT Zombie

    Messages:
    298
    GitHub:
    TheDiamondYT1
    Why
     
  5. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    because it's pointless to if there's no real reason to decouple it
     
    Primus likes this.
  6. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Because you don't "include" another class in your plugin. Plugins are in object-oriented design and you don't and cannot do something like #include in C to run the code in another class.

    You have to understand that by creating another class in your plugin, you don't magically "inject" your code in another class into your main class. It's not like it automagically moves the functions into the main class such that they are also registered as event handlers (if you even qualitatively understand this)
     
  7. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks
    I believe it was relevant to your question, who are you to tell someone what they can and can't say? SOFe was just trying to explain that you can't include classes in a plugin.
     
  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.