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

Any other way of using this

Discussion in 'Development' started by NotRedd, May 22, 2017.

  1. NotRedd

    NotRedd Silverfish

    Messages:
    17
    Is there a better way of registrating.
    PHP:
    this->getServer()->getPluginManager()->registerEvents(new CatEyes($this), $this);
            
    $this->getServer()->getPluginManager()->registerEvents(new Fireless($this), $this);
            
    $this->getServer()->getPluginManager()->registerEvents(new CutClean($this), $this);
            
    $this->getServer()->getPluginManager()->registerEvents(new Nofall($this), $this);
    Instead of putting all of them ,
    PHP:
    public function onEnable()
        {
            @
    mkdir($this->getDataFolder());
            
    $this->config = new Config($this->getDataFolder() . "/scenarios.yml"CONFIG::YAML, array(
                
    "cutclean" => true,
                
    "applerate" => true,
                
    "nofall" => true,
                
    "noenchant" => false,
                
    "noanvil" => false,
                
    "goldless" => false,
                
    "doubleores" => false,
                
    "diamondless" => false,
                
    "fastsmelt" => false,
                
    "fireless" => false,
                
    "cateyes" => true,
            ));
    $this->saveDefaultConfig();
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    $this->getServer()->getNetwork()->registerPacket(BossEventPacket::NETWORK_IDBossEventPacket::class);
            
    $this->getServer()->getScheduler()->scheduleRepeatingTask(new BossTask($this), $this->getConfig()->get("interval") * 20);
            
    $this->getServer()->getPluginManager()->registerEvents(new CatEyes($this), $this);
            
    $this->getServer()->getPluginManager()->registerEvents(new Fireless($this), $this);
            
    $this->getServer()->getPluginManager()->registerEvents(new CutClean($this), $this);
            
    $this->getServer()->getPluginManager()->registerEvents(new Nofall($this), $this);
            
    $this->getServer()->getPluginManager()->registerEvents(new Applerate($this), $this);
            
    $this->getServer()->getPluginManager()->registerEvents(new BloodDiamonds($this), $this);
            
    $this->getServer()->getPluginManager()->registerEvents(new Diamondless($this), $this);
            
    $this->getServer()->getPluginManager()->registerEvents(new NoEnchant($this), $this);
            
    $this->getServer()->getPluginManager()->registerEvents(new NoAnvil($this), $this);
            
    $this->getServer()->getPluginManager()->registerEvents(new Goldless($this), $this);
            
    $this->getServer()->getPluginManager()->registerEvents(new Diamondless($this), $this);
    $this->getServer()->getPluginManager()->registerEvents(new FastSmelt($this), $this);
            
    $this->getServer()->getPluginManager()->registerEvents(new DoubleOres($this), $this);
    $this->getLogger()->info("has been Enabled");
         }
       
       public function 
    onDisable(){
         
    $this->getLogger()->info("has been Disabled");
        }
        
    I have multiple files in the Scenarios Folder and how can I register them all at once?
     
    Last edited by a moderator: May 22, 2017
  2. McpeBooster

    McpeBooster Baby Zombie

    Messages:
    190
    GitHub:
    mcpebooster
    use Code Tags!!!
     
  3. NotRedd

    NotRedd Silverfish

    Messages:
    17
    Im new to php, how do I register code tags?
     
  4. SavionLegendZzz

    SavionLegendZzz Slime

    Messages:
    75
    GitHub:
    savionlegends
    PHP:
    $eventListeners = [new ClassName($args), new SecondClassName($args)];
    foreach(
    $eventListeners as $event){
      
    $this->getServer()->getPluginManager()->registerEvents($event/*Main class*/):
    }
     
  5. NotRedd

    NotRedd Silverfish

    Messages:
    17
    So
    PHP:
    eventListeners = [new ClassName($args), new SecondClassName($args)];
    foreach(
    $eventListeners as $event){
      
    $this->getServer()->getPluginManager()->registerEvents($eventMain);
    }
     
  6. NotRedd

    NotRedd Silverfish

    Messages:
    17
    Im basically working on a UHC core I just have a messy coding for the Main.php file
     
  7. NotRedd

    NotRedd Silverfish

    Messages:
    17
    Example of one Scenarios File
    PHP:
    use UHCReload\Main;
    use 
    pocketmine\event\block\BlockBreakEvent;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\Player;

    class 
    DoubleOres implements Listener {
        
      public 
    $scenarios;
      private 
    $plugin;
     
      public function 
    __construct(Main $plugin){
        
    $this->plugin $plugin;
        
    $this->scenarios = new Config($this->plugin->getDataFolder() . "scenarios.yml");
      }

      public function 
    getPlugin(){
        return 
    $this->plugin;
      }
     
     
  8. SavionLegendZzz

    SavionLegendZzz Slime

    Messages:
    75
    GitHub:
    savionlegends
    no no
    PHP:
    <?php

    namespace YourFileNameSpaceHere;


    use 
    pocketmine\event\Listener;
    use 
    pocketmine\plugin\PluginBase;

    class 
    Main extends PluginBase implements Listener{


        public function 
    onEnable(){
            
    $eventListeners = [new ClassName($args), new SecondClassName($args)];
            foreach ($
    $eventListeners as $event) {
                
    $this->getServer()->getPluginManager()->registerEvents($event$this);
            }
            
        }
     
  9. NotRedd

    NotRedd Silverfish

    Messages:
    17
    What would I input for ClassNane and Second ClassNane?
     
  10. SavionLegendZzz

    SavionLegendZzz Slime

    Messages:
    75
    GitHub:
    savionlegends
     
    jasonwynn10 likes this.
  11. 0x15f

    0x15f Baby Zombie

    Messages:
    145
    GitHub:
    0x15f
    Just import ALL classes and register like this:
    PHP:
    foreach (glob("MyNameSpace/Listeners*.php") as $file)
        {
            require_once 
    $file;
            if (
    class_existsbasename($file".php")))
            {
                
    $class = new $class;
                
    $this->getServer()->getPluginManager()->registerEvents($class$this);
            }
     
    jasonwynn10 and SavionLegendZzz like this.
  12. NotRedd

    NotRedd Silverfish

    Messages:
    17
    Will it register for all folders?
     
  13. 0x15f

    0x15f Baby Zombie

    Messages:
    145
    GitHub:
    0x15f
    Change "MyNamespace/Listeners" to the directory that your listeners are in.
     
  14. NotRedd

    NotRedd Silverfish

    Messages:
    17
    Ok what if I want to register them in the root directory of ur plugin example : UHCReload
     
  15. 0x15f

    0x15f Baby Zombie

    Messages:
    145
    GitHub:
    0x15f
    Use dirname(__FILE__) . "/WhateverSubDirMyListenersAreIn"
     
    jasonwynn10 likes this.
  16. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    What exactly do you want to do? If you want to register them all together, why don't you just put them all in the same file?
     
  17. 0x15f

    0x15f Baby Zombie

    Messages:
    145
    GitHub:
    0x15f
    It appears they are different Listeners for a reason, he/she doesn't want them all active at the same time.
     
  18. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    using globbing trick and checking if the config value is true to load it seems like not a bad way
     
  19. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    If you want to register all listener functions, putting them in the same listener class makes more sense. You would want to put listener functions in different classes only if you want to register them separately. But in that case, you shouldn't unconditionally register them all using a scandir.
     
    BEcraft likes this.
  20. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    by checking associated value in config is true

    /Somedir/
    ScenarioNoFall.php
    ScenarioBarebones.php
    ScenarioCorrupted.php

    it would scan and check if in config "ScenarioCorrupted" is true meaning is enabled on the config only then register that class as listener

    unless you are not replying to me
     
  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.