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

Solved scheduleDelayedTask help

Comments in 'Plugin Development' started by Irish, Jun 27, 2016.

  1. Irish
    Offline

    Irish Active Member

    Joined:
    Apr 24, 2016
    Posts:
    96
    Minecraft User:
    _Irish_
    I have a delayed task that I call on a player death, but when I log what the time is, it logs as nothing and no time goes by at all? Help?

    Main.php
    PHP:
    <?php

    namespace Timebomb;

    use 
    pocketmine\event\player\PlayerDeathEvent as Death;
    use 
    pocketmine\plugin\PluginBase as P;
    use 
    pocketmine\event\Listener as L;
    use 
    pocketmine\utils\TextFormat as TF;

    class 
    Main extends implements {

        public 
    $time = [];

        public function 
    onEnable()
        {
            
    $this->getLogger()->info("TimebombPE has been enabled!");
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }

        public function 
    onDisable()
        {
            
    $this->getLogger()->info("TimebombPE has been disabled!");
        }

        public function 
    onDeath(Death $event){
            
    $player $event->getPlayer();
            if(!
    $player->getGamemode() == 0){
                return 
    false;
            }
            
    $task = new Task($this$player);
            
    $this->getServer()->getScheduler()->scheduleDelayedTask($task20);


        }
    }
    Task.php
    PHP:
    <?php

    namespace Timebomb;

    use 
    pocketmine\Player;
    use 
    pocketmine\plugin\Plugin;
    use 
    pocketmine\scheduler\PluginTask;

    class 
    Task extends PluginTask {

        public 
    $plugin;
        public 
    $player;
        public 
    $detonate 30;

        public function 
    __construct(Plugin $pluginPlayer $player){
            
    parent::__construct($plugin);
            
    $this->plugin $plugin;
            
    $this->player $player;
        }

        
    /**
         * Actions to execute when run
         *
         * @param $currentTick
         *
         * @return void
         */
        
    public function onRun($currentTick)
        {
            
    $detonate 30;
            
    $detonate--;
            
    $this->plugin->getServer()->broadcastMessage($detonate);
            if(
    $detonate == 25){
                
    $this->plugin->getLogger()->info("5 seconds have passed!");
            }

            if(
    $detonate == 20){
                
    $this->plugin->getLogger()->info("10 seconds have passed!");
            }

            if(
    $detonate == 15){
                
    $this->plugin->getLogger()->info("15 seconds have passed!");
            }

            if(
    $detonate == 10){
                
    $this->plugin->getLogger()->info("20 seconds have passed!");
            }

            if(
    $detonate == 5){
                
    $this->plugin->getLogger()->info("25 seconds have passed!");
            }

            if(
    $detonate == 0){
                
    $this->plugin->getLogger()->info("30 seconds have passed!");
                
    $detonate 30;
            }
        }
      
    //using this after i figure out why its not working.
        
    public function cancel(){
            
    $task $this->getTaskId();
            
    $this->plugin->getServer()->getScheduler()->cancelTask($task);
        }
    }
  2. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    In your onRun() method, $this->detonate instead of $detonate, because right now, it will always start at 30 and only go down to 29. And If you're doing a countdown, use scheduleDelayedRepeatingTask().
  3. Irish
    Offline

    Irish Active Member

    Joined:
    Apr 24, 2016
    Posts:
    96
    Minecraft User:
    _Irish_
    I did have it at $this->detonate only to take it out. I will try out that function, thanks for the help.
  4. Ritch
    Offline

    Ritch Active Member

    Joined:
    Oct 4, 2015
    Posts:
    184
    Minecraft User:
    anonymous
    For stop Task use this for me it's work on my other task
    PHP:
    <?php

    namespace Timebomb;

    use 
    pocketmine\Player;
    use 
    pocketmine\plugin\Plugin;
    use 
    pocketmine\scheduler\PluginTask;

    class 
    Task extends PluginTask {

        public 
    $plugin;
        public 
    $player;
        public 
    $detonate 30;

        public function 
    __construct(Plugin $pluginPlayer $player){
            
    parent::__construct($plugin);
            
    $this->plugin $plugin;
            
    $this->player $player;
        }

        
    /**
         * Actions to execute when run
         *
         * @param $currentTick
         *
         * @return void
         */
        
    public function onRun($currentTick)
        {
           
            
    $detonate--;
            
    $this->plugin->getServer()->broadcastMessage($detonate);
            if(
    $this->detonate == 25){
                
    $this->plugin->getLogger()->info("5 seconds have passed!");
            }

            if(
    $this->detonate == 20){
                
    $this->plugin->getLogger()->info("10 seconds have passed!");
            }

            if(
    $this->detonate == 15){
                
    $this->plugin->getLogger()->info("15 seconds have passed!");
            }

            if(
    $this->detonate == 10){
                
    $this->plugin->getLogger()->info("20 seconds have passed!");
            }

            if(
    $this->detonate == 5){
                
    $this->plugin->getLogger()->info("25 seconds have passed!");
            }

            if(
    $this->detonate 1){
                
    $this->plugin->getLogger()->info("30 seconds have passed!");
             
            }else{ 
    //for stop timer i use }else{
           
    }
        }
    ect..
  5. Jazzwhistle
    Offline

    Jazzwhistle Notable Member

    Joined:
    Dec 27, 2014
    Posts:
    364
    Minecraft User:
    Awzaw
    Also, your cancel function is correct, but to cancel the task you need to call setHandler() too when instantiating the new Task. Do not use "else" as in the above example, or you will keep creating new tasks without ever cancelling them :-(

    PHP:
    $task = new Task($this$player]);
    $taskid $this->getServer()->getScheduler()->scheduleRepeatingTask($task20);
    $task->setHandler($taskid);
    Last edited: Jul 1, 2016
  6. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    You realize that a delayed task only runs once after being scheduled right? By the looks of your code, you would want to use a repeating task for that.
    Jazzwhistle likes this.
  7. Irish
    Offline

    Irish Active Member

    Joined:
    Apr 24, 2016
    Posts:
    96
    Minecraft User:
    _Irish_
    Yes, I know realize that, I did use scheduleDelayedRepeatingTask and it worked very well.

Share This Page

Advertisement