From 67666db827e2c85886b3edb880308d3d223ef451 Mon Sep 17 00:00:00 2001 From: Dylan T Date: Sat, 16 May 2020 15:28:45 +0100 Subject: [PATCH] Task: Remove currentTick parameter (#3498) This parameter is not used for the vast majority of task use cases and just serves as extra useless boilerplate code, especially for closure-based tasks. This use case can be replaced using Server->getTick() in the cases where it matters. --- src/scheduler/CancellableClosureTask.php | 16 ++++++++-------- src/scheduler/ClosureTask.php | 16 ++++++++-------- src/scheduler/Task.php | 2 +- src/scheduler/TaskHandler.php | 4 ++-- src/scheduler/TaskScheduler.php | 2 +- .../TesterPlugin/CheckTestCompletionTask.php | 2 +- 6 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/scheduler/CancellableClosureTask.php b/src/scheduler/CancellableClosureTask.php index 934edb9dd..68d2fa946 100644 --- a/src/scheduler/CancellableClosureTask.php +++ b/src/scheduler/CancellableClosureTask.php @@ -32,8 +32,8 @@ use pocketmine\utils\Utils; * Example usage: * * ``` - * TaskScheduler->scheduleTask(new CancellableClosureTask(function(int $currentTick) : bool{ - * echo "HI on $currentTick\n"; + * TaskScheduler->scheduleTask(new CancellableClosureTask(function() : bool{ + * echo "HI\n"; * $continue = false; * return $continue; //stop repeating * }); @@ -47,20 +47,20 @@ class CancellableClosureTask extends Task{ /** * @var \Closure - * @phpstan-var \Closure(int $currentTick) : bool + * @phpstan-var \Closure() : bool */ private $closure; /** * CancellableClosureTask constructor. * - * The closure should follow the signature callback(int $currentTick) : bool. The return value will be used to + * The closure should follow the signature callback() : bool. The return value will be used to * decide whether to continue repeating. * - * @phpstan-param \Closure(int $currentTick) : bool $closure + * @phpstan-param \Closure() : bool $closure */ public function __construct(\Closure $closure){ - Utils::validateCallableSignature(function(int $currentTick) : bool{ return false; }, $closure); + Utils::validateCallableSignature(function() : bool{ return false; }, $closure); $this->closure = $closure; } @@ -68,8 +68,8 @@ class CancellableClosureTask extends Task{ return Utils::getNiceClosureName($this->closure); } - public function onRun(int $currentTick) : void{ - if(!($this->closure)($currentTick)){ + public function onRun() : void{ + if(!($this->closure)()){ $this->getHandler()->cancel(); } } diff --git a/src/scheduler/ClosureTask.php b/src/scheduler/ClosureTask.php index 2a9996de1..45e30212e 100644 --- a/src/scheduler/ClosureTask.php +++ b/src/scheduler/ClosureTask.php @@ -31,8 +31,8 @@ use pocketmine\utils\Utils; * Example usage: * * ``` - * TaskScheduler->scheduleTask(new ClosureTask(function(int $currentTick) : void{ - * echo "HI on $currentTick\n"; + * TaskScheduler->scheduleTask(new ClosureTask(function() : void{ + * echo "HI\n"; * }); * ``` */ @@ -40,16 +40,16 @@ class ClosureTask extends Task{ /** * @var \Closure - * @phpstan-var \Closure(int) : void + * @phpstan-var \Closure() : void */ private $closure; /** - * @param \Closure $closure Must accept only ONE parameter, $currentTick - * @phpstan-param \Closure(int) : void $closure + * @param \Closure $closure Must accept zero parameters + * @phpstan-param \Closure() : void $closure */ public function __construct(\Closure $closure){ - Utils::validateCallableSignature(function(int $currentTick) : void{}, $closure); + Utils::validateCallableSignature(function() : void{}, $closure); $this->closure = $closure; } @@ -57,7 +57,7 @@ class ClosureTask extends Task{ return Utils::getNiceClosureName($this->closure); } - public function onRun(int $currentTick) : void{ - ($this->closure)($currentTick); + public function onRun() : void{ + ($this->closure)(); } } diff --git a/src/scheduler/Task.php b/src/scheduler/Task.php index 72a0b237c..9d3e5a6fe 100644 --- a/src/scheduler/Task.php +++ b/src/scheduler/Task.php @@ -60,7 +60,7 @@ abstract class Task{ * * @return void */ - abstract public function onRun(int $currentTick); + abstract public function onRun(); /** * Actions to execute if the Task is cancelled diff --git a/src/scheduler/TaskHandler.php b/src/scheduler/TaskHandler.php index 69159b371..986b8005e 100644 --- a/src/scheduler/TaskHandler.php +++ b/src/scheduler/TaskHandler.php @@ -116,10 +116,10 @@ class TaskHandler{ $this->task->setHandler(null); } - public function run(int $currentTick) : void{ + public function run() : void{ $this->timings->startTiming(); try{ - $this->task->onRun($currentTick); + $this->task->onRun(); }finally{ $this->timings->stopTiming(); } diff --git a/src/scheduler/TaskScheduler.php b/src/scheduler/TaskScheduler.php index 55b70de0b..2786bc417 100644 --- a/src/scheduler/TaskScheduler.php +++ b/src/scheduler/TaskScheduler.php @@ -147,7 +147,7 @@ class TaskScheduler{ unset($this->tasks[$task->getTaskId()]); continue; } - $task->run($this->currentTick); + $task->run(); if($task->isRepeating()){ $task->setNextRun($this->currentTick + $task->getPeriod()); $this->queue->insert($task, $this->currentTick + $task->getPeriod()); diff --git a/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/CheckTestCompletionTask.php b/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/CheckTestCompletionTask.php index dc65a9a72..49ee205b0 100644 --- a/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/CheckTestCompletionTask.php +++ b/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/CheckTestCompletionTask.php @@ -34,7 +34,7 @@ class CheckTestCompletionTask extends Task{ $this->plugin = $plugin; } - public function onRun(int $currentTick){ + public function onRun(){ $test = $this->plugin->getCurrentTest(); if($test === null){ if(!$this->plugin->startNextTest()){