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

[SOLVED] PluginTask error

Discussion in 'Development' started by InspectorGadget, Jan 26, 2017.

  1. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Hi, i've been getting this error a lot. I'm not sure why.. Any help would be appreciated..

    My Error:
    PHP:
    [06:27:39] [Server thread/CRITICAL]: pocketmine\utils\PluginException"Invalid owner of PluginTask RTG\HUD\Task" (EXCEPTIONin "/src/pocketmine/scheduler/ServerScheduler" at line 276
    My Main:
    PHP:
    <?php

    namespace RTG\HUD;

    /* Essentials */
    use pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\Server;
    use 
    pocketmine\scheduler\PluginTask;
    use 
    pocketmine\command\CommandExecutor;
    use 
    pocketmine\Player;

    use 
    RTG\HUD\Task;
    use 
    RTG\HUD\command\HUDCommand;

    class 
    Loader extends PluginBase implements Listener {
      
        public 
    $enable;
      
        public function 
    onEnable() {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    $this->getLogger()->warning("Starting...");
            
    $this->getServer()->getScheduler()->scheduleRepeatingTask(new Task($this), 20);
            
    $this->enable = array();
          
            
    $this->getCommand("hud")->setExecutor(new HUDCommand($this));
        }
      
        public function 
    onDisable() {
        }
      
    }
    My Task;
    PHP:
    <?php

    namespace RTG\HUD;

    use 
    RTG\HUD\Loader;

    use 
    pocketmine\Server;
    use 
    pocketmine\Player;
    use 
    pocketmine\scheduler\PluginTask;
    use 
    pocketmine\utils\Config;

    class 
    Task extends PluginTask {
      
        public function 
    __construct(Loader $plugin) {
            
    $this->plugin $plugin;
        }
      
        public function 
    onRun($tick) {
          
            
    $online $this->getServer()->getOnlinePlayers();
            
    $c $this->plugin->getConfig();
          
                foreach(
    $online as $p) {
                  
                    if(isset(
    $this->plugin->enable[strtolower($p->getName())])) {
                        
    $p->sendPopup("->".$c->get("hud"));
                    }
                  
                }
              
        }
      
    }
    NOTE: This isn't the full plugin, it contains more files like for commands. For the full SRC. I've added it here :) -> https://github.com/RTGDaCoder/CustomHUD
     
    Last edited: Jan 26, 2017
  2. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    Just add parents::__construct in your task constructor.
     
  3. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Can u please show Example?
     
  4. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    You must call the parent constructor when you overwrite the constructor method of a plugin task:
    PHP:
    class YourTask extends pocketmine\scheduler\PluginTask{

        protected 
    $myPlugin;

        public function 
    __construct(pocketmine\plugin\Plugin $yourPluginInstance){
            
    parent::__construct($yourPluginInstance);
            
    $this->myPlugin $myPluginInstance;
        }

        public function 
    onRun($tick){
            
    // do stuff
        
    }

    }
     
    eDroid likes this.
  5. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    So my plugin instance is like
    PHP:
    __construct(Loader ($this));
     
  6. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    If the name of your plugins main class is Loader and your are passing it to your tasks constructor, yes that would be your plugins instance/object.
     
  7. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    This issue has been solved :)
     
  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.