I'm trying to create a countdown that will broadcast every second (starting at 30) Game Will Start In x Seconds. It was working, but I had a suspicicion that after it had finished, it was running in the background, so I added a getLogger->info->("STILL RUNNING") and I found that no matter what I tried, the schedule continued to check every second. What is going on? (The value for the counter is stored in count.txt for the moment) PHP: class Countdown extends PluginTask{ public function onRun($currentTick) { $sec = file_get_contents($this->getOwner()->getDataFolder() . "count.txt"); if($sec != 0) { $this->getOwner()->getServer()->broadcastMessage("Game Starts In $sec Seconds"); $sec--; file_put_contents($this->getOwner()->getDataFolder() . "count.txt", $sec); } $this->getOwner()->getServer()->broadcastMessage("STILL RUNNING"); } public function cancel() { file_put_contents($this->getOwner()->getDataFolder() . "count.txt", 30); $this->getOwner()->getServer()->broadcastMessage("Game Will Now Start!"); $this->getHandler()->cancel(); }} PHP: public function onBlockBreak(BlockBreakEvent $BBE) { $task = new Countdown($this); $this->getServer()->getScheduler()->scheduleRepeatingTask($task, 20); }
Reference: https://github.com/PocketMine/PocketMine-MP/blob/master/src/pocketmine/plugin/PluginManager.php#L640 (shall be the uppermost call because this function is not specific for scheduling) https://github.com/PocketMine/Pocke...etmine/scheduler/ServerScheduler.php#L136-137 ( https://github.com/PocketMine/Pocke.../pocketmine/scheduler/ServerScheduler.php#L41 $task is a TaskHandler instance) https://github.com/PocketMine/Pocke...pocketmine/scheduler/TaskHandler.php#L130-140 Therefore, the correct sequence of code call to cancel an event is: ServerScheduler->cancelTask($taskId) (by comparing https://github.com/PocketMine/Pocke...etmine/scheduler/ServerScheduler.php#L124-125 and https://github.com/PocketMine/Pocke...etmine/scheduler/ServerScheduler.php#L136-137 , we can see that cancelTasks(Plugin) executes code similar to cancel($taskId) TaskHandler->cancel() Task->onCancel() You are calling 2., so the last part of cancelling a task (ServerScheduler->tasks[$taskId] unsetting) is skipped, so it doesn't work.