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: <?phpnamespace 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 P implements L { 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($task, 20); }} Task.php PHP: <?phpnamespace 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 $plugin, Player $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); }}
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().
I did have it at $this->detonate only to take it out. I will try out that function, thanks for the help.
For stop Task use this for me it's work on my other task PHP: <?phpnamespace 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 $plugin, Player $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..
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($task, 20);$task->setHandler($taskid);
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.