Hi, I thought I'd use ClosureTask but when I try to stop RepeatingTask I still can't get the TaskHandler inside Closure. What should I do with this?
PHP: $task = new ClosureTask(...);$this->getScheduler()->scheduleRepeatingTask($task, 20);$handler = $task->getHandler();// Do whatever you want to do with the handler, f.e: Cancelling the event where $this is an instance of PluginBase
PHP: $task = new ClosureTask(function (int $currentTick): void { if (mt_rand(0, 100) === 0) { //end the task... //what TaskHandler... $this->handler->cancel(); return; } $this->getServer()->getLogger()->info("Hello!"); }); $this->handler = $this->getScheduler()->scheduleRepeatingTask($task, 20); Is there a smart way to cancel a task from within a closure like this? Otherwise, you have to store Task in the property of the main class. I don't know if this is smart.
You can simply do this PHP: $handler = $this->getScheduler()->scheduleRepeatingTask(new ClosureTask(function (int $currentTick)use(&$handler): void { if (mt_rand(0, 100) === 0) { //end the task... $handler->cancel(); return; } $this->getServer()->getLogger()->info("Hello!"); }), 20);