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

How to stop repeat task after 5 seconds?

Discussion in 'Development' started by devntnghia, Jan 16, 2020.

  1. devntnghia

    devntnghia Spider

    Messages:
    9
    GitHub:
    devntnghia
    As the title, I coded a repeat task but don't know how to stop it after 5 seconds. Here is my code:

    PHP:
    <?php
    namespace Test;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Server;
    use 
    pocketmine\Player;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\scheduler\Task;
    use 
    pocketmine\level\Level;

    class 
    dropTask extends Task {
        private 
    $moneyItems;
        private 
    $level;
        private 
    $player;

        function 
    __construct(Array $moneyItemsLevel $levelPlayer $player) {
            
    $this->moneyItems $moneyItems;
            
    $this->level $level;
            
    $this->player $player;
        }

        public function 
    onRun(int $currentTick) {
            
    $x $this->player->getX();
            
    $y $this->player->getY() + 2;
            
    $z $this->player->getZ();
            
    $pos = new Vector3($x$y$z);
            
    $this->level->dropItem($posItem::get($this->moneyItems[mt_rand(0count($this->moneyItems) - 1)])->setCustomName("CANT_PICKUP"));
        }
    }

    class 
    Main extends PluginBase implements Listener {

        public 
    $tasks = [];

        public function 
    onEnable(): void {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }

        public function 
    onCommand(CommandSender $playerCommand $commandstring $label, array $args): bool {
            if (
    $command->getName() === "drop") {
                if (
    $player instanceof Player) {
                    if (!isset(
    $args[0])) {
                        
    $player->sendMessage("Usage: /drop <on/off>");
                        return 
    false;
                    }
                    else {
                        if (
    $args[0] === "on") {
                            if (isset(
    $this->tasks[$player->getId()])) {
                                
    $player->sendMessage("/drop is already on");
                                return 
    false;
                            }
                            
    $level $player->getLevel();
                            
    $moneyItems = [Item::EMERALDItem::GOLD_NUGGETItem::DIAMOND];
                            
    $task = new dropTask($moneyItems$level$player);
                            
    $this->tasks[$player->getId()] = $task;
                            
    $task2 = new stopTask($player$this->tasks);
                            
    $this->getScheduler()->scheduleRepeatingTask($task10);
                            
    /* Turn off the task after 5 seconds */
                        
    }
                        else if (
    $args[0] === "off") {
                            if (!isset(
    $this->tasks[$player->getId()])) {
                                
    $player->sendMessage("/drop is already off");
                                return 
    false;
                            }
                            
    $task $this->tasks[$player->getId()];
                            unset(
    $this->tasks[$player->getId()]);
                            
    $task->getHandler()->cancel();
                        }
                    }
                }
            }
            return 
    true;
        }
    }
     
  2. Mr174

    Mr174 Baby Zombie

    Messages:
    187
    GitHub:
    mr174
    $this->getScheduler()->cancelTask($this->getTaskId());
     
  3. devntnghia

    devntnghia Spider

    Messages:
    9
    GitHub:
    devntnghia
    I did this in /drop off. What I want is the task stop after 5 seconds in /drop on
     
  4. Mr174

    Mr174 Baby Zombie

    Messages:
    187
    GitHub:
    mr174
    Also you need to have $time = 5; and a $time--;
     
  5. devntnghia

    devntnghia Spider

    Messages:
    9
    GitHub:
    devntnghia
    Can you write some codes to explain it please?
     
  6. Mr174

    Mr174 Baby Zombie

    Messages:
    187
    GitHub:
    mr174
    PHP:
    <?php
    namespace Test;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Server;
    use 
    pocketmine\Player;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\scheduler\Task;
    use 
    pocketmine\level\Level;

    class 
    dropTask extends Task {
        private 
    $moneyItems;
        private 
    $level;
        private 
    $player;
        private 
    $time 5//Time to the Task

        
    function __construct(Array $moneyItemsLevel $levelPlayer $player) {
            
    $this->moneyItems $moneyItems;
            
    $this->level $level;
            
    $this->player $player;
        }

        public function 
    onRun(int $currentTick) {
            
    $this->time--; //Subtracting 1 every tick
            
    if ($this->time 1){
            
    $this->getScheduler()->cancelTask($this->getTaskId()); //Canceled Task here
            
    }

            
    $x $this->player->getX();
            
    $y $this->player->getY() + 2;
            
    $z $this->player->getZ();
            
    $pos = new Vector3($x$y$z);
            
    $this->level->dropItem($posItem::get($this->moneyItems[mt_rand(0count($this->moneyItems) - 1)])->setCustomName("CANT_PICKUP"));
        }
    }

    class 
    Main extends PluginBase implements Listener {

        public 
    $tasks = [];

        public function 
    onEnable(): void {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }

        public function 
    onCommand(CommandSender $playerCommand $commandstring $label, array $args): bool {
            if (
    $command->getName() === "drop") {
                if (
    $player instanceof Player) {
                    if (!isset(
    $args[0])) {
                        
    $player->sendMessage("Usage: /drop <on/off>");
                        return 
    false;
                    }
                    else {
                        if (
    $args[0] === "on") {
                            if (isset(
    $this->tasks[$player->getId()])) {
                                
    $player->sendMessage("/drop is already on");
                                return 
    false;
                            }
                            
    $level $player->getLevel();
                            
    $moneyItems = [Item::EMERALDItem::GOLD_NUGGETItem::DIAMOND];
                            
    $task = new dropTask($moneyItems$level$player);
                            
    $this->tasks[$player->getId()] = $task;
                            
    $task2 = new stopTask($player$this->tasks); //I dont think you need this
                            
    $this->getScheduler()->scheduleRepeatingTask($task20); //Changed to 20 = 1 tick
                        
    }
                        else if (
    $args[0] === "off") {
                            if (!isset(
    $this->tasks[$player->getId()])) {
                                
    $player->sendMessage("/drop is already off");
                                return 
    false;
                            }
                            
    $task $this->tasks[$player->getId()];
                            unset(
    $this->tasks[$player->getId()]);
                            
    $task->getHandler()->cancel();
                        }
                    }
                }
            }
            return 
    true;
        }
    }
    There ya go!
     
  7. devntnghia

    devntnghia Spider

    Messages:
    9
    GitHub:
    devntnghia
    Thank you so much!
     
  8. Mr174

    Mr174 Baby Zombie

    Messages:
    187
    GitHub:
    mr174
    Let us know if it doesn't work! Ill help fix!
     
  9. devntnghia

    devntnghia Spider

    Messages:
    9
    GitHub:
    devntnghia
    I have this error:
    "Call to undefined method Test\dropTask::getScheduler()" (EXCEPTION) in "plugins/drop/src/Test/Main" at line 32"
     
  10. Mr174

    Mr174 Baby Zombie

    Messages:
    187
    GitHub:
    mr174
    Register the Task to your plugin
     
  11. devntnghia

    devntnghia Spider

    Messages:
    9
    GitHub:
    devntnghia
    Umm, how to do that? Sorry if I bother you T_T
     
  12. Mr174

    Mr174 Baby Zombie

    Messages:
    187
    GitHub:
    mr174
    All good 1 second
     
  13. Mr174

    Mr174 Baby Zombie

    Messages:
    187
    GitHub:
    mr174
    PHP:
    <?php
    namespace Test;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Server;
    use 
    pocketmine\Player;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\scheduler\Task;
    use 
    pocketmine\level\Level;

    class 
    dropTask extends Task {
        private 
    $moneyItems;
        private 
    $level;
        private 
    $player;
        private 
    $plugin//Added
        
    private $time 5//Time to the Task

        //Added Main $main

        
    public function __construct(Main $main,Array $moneyItemsLevel $levelPlayer $player) {
            
    $this->moneyItems $moneyItems;
            
    $this->level $level;
            
    $this->player $player;
            
    $this->plugin $main//Register to the plugin
        
    }

        public function 
    onRun(int $currentTick) {
            
    $this->time--; //Subtracting 1 every tick
            
    if ($this->time 1){
            
    $this->getScheduler()->cancelTask($this->getTaskId()); //Canceled Task here
            
    }

            
    $x $this->player->getX();
            
    $y $this->player->getY() + 2;
            
    $z $this->player->getZ();
            
    $pos = new Vector3($x$y$z);
            
    $this->level->dropItem($posItem::get($this->moneyItems[mt_rand(0count($this->moneyItems) - 1)])->setCustomName("CANT_PICKUP"));
        }
    }

    class 
    Main extends PluginBase implements Listener {

        public 
    $tasks = [];

        public function 
    onEnable(): void {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }

        public function 
    onCommand(CommandSender $playerCommand $commandstring $label, array $args): bool {
            if (
    $command->getName() === "drop") {
                if (
    $player instanceof Player) {
                    if (!isset(
    $args[0])) {
                        
    $player->sendMessage("Usage: /drop <on/off>");
                        return 
    false;
                    }
                    else {
                        if (
    $args[0] === "on") {
                            if (isset(
    $this->tasks[$player->getId()])) {
                                
    $player->sendMessage("/drop is already on");
                                return 
    false;
                            }
                            
    $level $player->getLevel();
                            
    $moneyItems = [Item::EMERALDItem::GOLD_NUGGETItem::DIAMOND];
                            
    $task = new dropTask($this,$moneyItems$level$player); //Added $this, I think this is the plugin in on my phone
                            
    $this->tasks[$player->getId()] = $task;
                            
    $this->getScheduler()->scheduleRepeatingTask($task20); Changed to 20 1 tick
                        
    }
                        else if (
    $args[0] === "off") {
                            if (!isset(
    $this->tasks[$player->getId()])) {
                                
    $player->sendMessage("/drop is already off");
                                return 
    false;
                            }
                            
    $task $this->tasks[$player->getId()];
                            unset(
    $this->tasks[$player->getId()]);
                            
    $task->getHandler()->cancel();
                        }
                    }
                }
            }
            return 
    true;
        }
    }
     
  14. devntnghia

    devntnghia Spider

    Messages:
    9
    GitHub:
    devntnghia
    I changed $this->getScheduler() to $this->plugin->getScheduler() in your code and it works!
    Thanks for helping me
     
    Mr174 likes this.
  15. devntnghia

    devntnghia Spider

    Messages:
    9
    GitHub:
    devntnghia
    Umm... Can I ask you 1 more question?
    The task only drop 5 times then stop, not 5 seconds
     
  16. Mr174

    Mr174 Baby Zombie

    Messages:
    187
    GitHub:
    mr174
    Because it runs every tick that's how tasks work
     
  17. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    Are you looking for it to run once after 5 seconds instead of every second, 5 times? Use a delayed task instead
     
    Mr174 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.