Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Solved Problem with RepeatingTask

Comments in 'Plugin Development' started by Timmpann, Aug 1, 2015.

Thread Status:
Not open for further replies.
  1. Timmpann
    Offline

    Timmpann New Member

    Joined:
    Feb 5, 2015
    Posts:
    24
    Minecraft User:
    Timmpann
    I'm trying to run a repeating task that adds a particle every second. But I get this error:
    Code:
    Fatal error: Call to undefined method MCPESE\Timer::getServer() in /somewhere/somewhere/devserver/plugins/MCPESE/src/MCPESE/Timer.php on line 19
    I have 2 classes, MainClass and Timer.

    In MainClass.php at onEnable I run this code:
    PHP:
    $this->getServer()->getScheduler()->scheduleRepeatingTask(new Timer($this), 20);
    In Timer.php I run this code:
    PHP:
    <?php

    namespace MCPESE;

    use 
    pocketmine\scheduler\PluginTask;
    use 
    pocketmine\level\particle\Particle;
    use 
    pocketmine\level\particle\DustParticle;
    use 
    pocketmine\math\Vector3;

    class 
    Timer extends PluginTask{
      public function 
    __construct($plugin){
        
    $this->plugin $plugin;
        
    parent::__construct($plugin);
      }

      public function 
    onRun($tick){
            
    $center = new Vector3(12873128);
            
    $level $this->getServer()->getDefaultLevel();
            
    $level->addParticle(new DustParticle($center40452081));
        }
      }
    If I run the following code in MainClass.php without a repeating task, it works. But when it runs in Timer.php I get the error i mentioned in the start of the post:
    PHP:
            $center = new Vector3(12873128);
            
    $level $this->getServer()->getDefaultLevel();
            
    $level->addParticle(new DustParticle($center40452081));
    It's probably a very easy fix, but I can't figure it out.
  2. Kvetinac97
    Offline

    Kvetinac97 Active Member Plugin Developer

    Joined:
    Nov 17, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    Kvetinac97
    There are two possibilities how fix it:
    1) add:
    PHP:
    use MCPESE\MainClass//in Timer
    use pocketmine\Server//in MainClass
    //And then use:
    $level $this->getOwner()->getServer()->getDefaultLevel();
    //or
    $level $this->plugin->getServer()->getDefaultLevel();
    2) #RECOMMENDED Add:
    PHP:
    use pocketmine\Server//in Timer
    //and use
     
    Server::getInstance()->getDefaultLevel();
    You have two problems: 1) You can't use $this->getServer() in PluginTask 2) You don't use pocketmine\Server;
    Last edited: Aug 2, 2015
  3. hoyinm14mc
    Offline

    hoyinm14mc Notable Member Plugin Developer

    Joined:
    Jun 4, 2014
    Posts:
    502
    Plugins:
    9
    Minecraft User:
    hoyinm14mc
    Remember ';' symbol
    iiDeathStrokeii likes this.
  4. Kvetinac97
    Offline

    Kvetinac97 Active Member Plugin Developer

    Joined:
    Nov 17, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    Kvetinac97
    Level of error: "STUPID"
    (BTW fixed)
    PianoPlayer38 and TheKofola12 like this.
  5. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    Use $this->getServer().
  6. Gamecrafter
    Offline

    Gamecrafter Notable Member Plugin Developer

    Joined:
    Nov 20, 2014
    Posts:
    981
    Plugins:
    9
    PluginTask doesn't have getServer(). You have to construct a path to Main.php. That's why it's $this->owner->getServer().
    Legoboy0215 likes this.
  7. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    I mean don't do Server::getInstance(). Sorry for the mistake.
    PEMapModder likes this.
  8. Gamecrafter
    Offline

    Gamecrafter Notable Member Plugin Developer

    Joined:
    Nov 20, 2014
    Posts:
    981
    Plugins:
    9
    Static functions ruin the whole purpose of OOP.
    Legoboy0215 likes this.
  9. Kvetinac97
    Offline

    Kvetinac97 Active Member Plugin Developer

    Joined:
    Nov 17, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    Kvetinac97
    It's more laggy, but it works too ;)
    TheKofola12 and Legoboy0215 like this.
  10. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    It doesn't. It is a static getter for an object.
    Server::getInstance() is discouraged by the PocketMine Team. Always try to use non-static methods.
    Timmpann and Legoboy0215 like this.
  11. Kvetinac97
    Offline

    Kvetinac97 Active Member Plugin Developer

    Joined:
    Nov 17, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    Kvetinac97
    I use $this->getOwner()->getServer() ;)
    TheKofola12 likes this.
  12. Timmpann
    Offline

    Timmpann New Member

    Joined:
    Feb 5, 2015
    Posts:
    24
    Minecraft User:
    Timmpann
    Thank you for the answers everyone! Now it works. :)
Thread Status:
Not open for further replies.

Share This Page

Advertisement