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

How to stop a task

Discussion in 'Development' started by Besher, Sep 29, 2020.

  1. Besher

    Besher Witch

    Messages:
    51
    How do i stop a task i have looked in this forum but havent found anything that works with my code
    PHP:
    <?php

    namespace besher\timer;

    use 
    besher\timer\Main;

    use 
    pocketmine\scheduler\Task;
    use 
    pocketmine\level\sound\ClickSound;
    use 
    pocketmine\level\sound\BlazeShootSound;

    class 
    SecondTask extends Task{
       
        public 
    $time 10// Count down set to 10
       
        
    public function __construct(Main $main){
            
    $this->main $main;
        }
       
        public function 
    onRun(int $currentTick){
            foreach(
    $this->main->getServer()->getOnlinePlayers() as $player){
            
    $this->time--;
            
    $player->sendTip("Time: ".$this->time);
            
    $player->getLevel()->addSound(new ClickSound($player));
            if(
    $this->time == 1$player->getLevel()->addSound(new BlazeShootSound($player));
            if(
    $this->time == 0$this->plugin->getScheduler()->cancelTask($this->getTaskId());
            }
        }
    }
    That just makes the server crash
     
  2. Akmal Fairuz

    Akmal Fairuz Creeper

    Messages:
    4
    GitHub:
    akmalfairuz
    Send crash and backtrace.
     
  3. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    $this->plugin is not defined
     
  4. Besher

    Besher Witch

    Messages:
    51
    How do i do that could you post the code?
     
  5. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    PHP:
    <?php

    namespace besher\timer;

    use 
    besher\timer\Main;

    use 
    pocketmine\scheduler\Task;
    use 
    pocketmine\level\sound\ClickSound;
    use 
    pocketmine\level\sound\BlazeShootSound;

    class 
    SecondTask extends Task{
     
        public 
    $time 10// Count down set to 10
     
        
    public function __construct(Main $main){
            
    $this->main $main;
        }
     
        public function 
    onRun(int $currentTick){
            foreach(
    $this->main->getServer()->getOnlinePlayers() as $player){
            
    $this->time--;
            
    $player->sendTip("Time: ".$this->time);
            
    $player->getLevel()->addSound(new ClickSound($player));
            if(
    $this->time == 1$player->getLevel()->addSound(new BlazeShootSound($player));
            if(
    $this->time == 0$this->main->getScheduler()->cancelTask($this->getTaskId());
            }
        }
    }
     
    ItsInspire likes this.
  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.