mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-06 11:57:10 +00:00
Add generic types for TaskHandler (#6030)
This commit is contained in:
parent
082119cfd2
commit
8338ebaffd
@ -26,8 +26,12 @@ namespace pocketmine\scheduler;
|
|||||||
use pocketmine\utils\Utils;
|
use pocketmine\utils\Utils;
|
||||||
|
|
||||||
abstract class Task{
|
abstract class Task{
|
||||||
|
/** @phpstan-var TaskHandler<static>|null */
|
||||||
private ?TaskHandler $taskHandler = null;
|
private ?TaskHandler $taskHandler = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @phpstan-return TaskHandler<static>|null
|
||||||
|
*/
|
||||||
final public function getHandler() : ?TaskHandler{
|
final public function getHandler() : ?TaskHandler{
|
||||||
return $this->taskHandler;
|
return $this->taskHandler;
|
||||||
}
|
}
|
||||||
@ -36,6 +40,9 @@ abstract class Task{
|
|||||||
return Utils::getNiceClassName($this);
|
return Utils::getNiceClassName($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @phpstan-param TaskHandler<static>|null $taskHandler
|
||||||
|
*/
|
||||||
final public function setHandler(?TaskHandler $taskHandler) : void{
|
final public function setHandler(?TaskHandler $taskHandler) : void{
|
||||||
if($this->taskHandler === null || $taskHandler === null){
|
if($this->taskHandler === null || $taskHandler === null){
|
||||||
$this->taskHandler = $taskHandler;
|
$this->taskHandler = $taskHandler;
|
||||||
|
@ -26,6 +26,9 @@ namespace pocketmine\scheduler;
|
|||||||
use pocketmine\timings\Timings;
|
use pocketmine\timings\Timings;
|
||||||
use pocketmine\timings\TimingsHandler;
|
use pocketmine\timings\TimingsHandler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @template TTask of Task
|
||||||
|
*/
|
||||||
class TaskHandler{
|
class TaskHandler{
|
||||||
protected int $nextRun;
|
protected int $nextRun;
|
||||||
|
|
||||||
@ -36,6 +39,9 @@ class TaskHandler{
|
|||||||
private string $taskName;
|
private string $taskName;
|
||||||
private string $ownerName;
|
private string $ownerName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @phpstan-param TTask $task
|
||||||
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
protected Task $task,
|
protected Task $task,
|
||||||
protected int $delay = -1,
|
protected int $delay = -1,
|
||||||
@ -66,6 +72,9 @@ class TaskHandler{
|
|||||||
$this->nextRun = $ticks;
|
$this->nextRun = $ticks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @phpstan-return TTask
|
||||||
|
*/
|
||||||
public function getTask() : Task{
|
public function getTask() : Task{
|
||||||
return $this->task;
|
return $this->task;
|
||||||
}
|
}
|
||||||
|
@ -33,12 +33,12 @@ use pocketmine\utils\ReversePriorityQueue;
|
|||||||
class TaskScheduler{
|
class TaskScheduler{
|
||||||
private bool $enabled = true;
|
private bool $enabled = true;
|
||||||
|
|
||||||
/** @phpstan-var ReversePriorityQueue<int, TaskHandler> */
|
/** @phpstan-var ReversePriorityQueue<int, TaskHandler<covariant Task>> */
|
||||||
protected ReversePriorityQueue $queue;
|
protected ReversePriorityQueue $queue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var ObjectSet|TaskHandler[]
|
* @var ObjectSet|TaskHandler[]
|
||||||
* @phpstan-var ObjectSet<TaskHandler>
|
* @phpstan-var ObjectSet<TaskHandler<covariant Task>>
|
||||||
*/
|
*/
|
||||||
protected ObjectSet $tasks;
|
protected ObjectSet $tasks;
|
||||||
|
|
||||||
@ -51,18 +51,42 @@ class TaskScheduler{
|
|||||||
$this->tasks = new ObjectSet();
|
$this->tasks = new ObjectSet();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @phpstan-template TTask of Task
|
||||||
|
* @phpstan-param TTask $task
|
||||||
|
*
|
||||||
|
* @phpstan-return TaskHandler<TTask>
|
||||||
|
*/
|
||||||
public function scheduleTask(Task $task) : TaskHandler{
|
public function scheduleTask(Task $task) : TaskHandler{
|
||||||
return $this->addTask($task, -1, -1);
|
return $this->addTask($task, -1, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @phpstan-template TTask of Task
|
||||||
|
* @phpstan-param TTask $task
|
||||||
|
*
|
||||||
|
* @phpstan-return TaskHandler<TTask>
|
||||||
|
*/
|
||||||
public function scheduleDelayedTask(Task $task, int $delay) : TaskHandler{
|
public function scheduleDelayedTask(Task $task, int $delay) : TaskHandler{
|
||||||
return $this->addTask($task, $delay, -1);
|
return $this->addTask($task, $delay, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @phpstan-template TTask of Task
|
||||||
|
* @phpstan-param TTask $task
|
||||||
|
*
|
||||||
|
* @phpstan-return TaskHandler<TTask>
|
||||||
|
*/
|
||||||
public function scheduleRepeatingTask(Task $task, int $period) : TaskHandler{
|
public function scheduleRepeatingTask(Task $task, int $period) : TaskHandler{
|
||||||
return $this->addTask($task, -1, $period);
|
return $this->addTask($task, -1, $period);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @phpstan-template TTask of Task
|
||||||
|
* @phpstan-param TTask $task
|
||||||
|
*
|
||||||
|
* @phpstan-return TaskHandler<TTask>
|
||||||
|
*/
|
||||||
public function scheduleDelayedRepeatingTask(Task $task, int $delay, int $period) : TaskHandler{
|
public function scheduleDelayedRepeatingTask(Task $task, int $delay, int $period) : TaskHandler{
|
||||||
return $this->addTask($task, $delay, $period);
|
return $this->addTask($task, $delay, $period);
|
||||||
}
|
}
|
||||||
@ -77,10 +101,19 @@ class TaskScheduler{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @phpstan-param TaskHandler<covariant Task> $task
|
||||||
|
*/
|
||||||
public function isQueued(TaskHandler $task) : bool{
|
public function isQueued(TaskHandler $task) : bool{
|
||||||
return $this->tasks->contains($task);
|
return $this->tasks->contains($task);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @phpstan-template TTask of Task
|
||||||
|
* @phpstan-param TTask $task
|
||||||
|
*
|
||||||
|
* @phpstan-return TaskHandler<TTask>
|
||||||
|
*/
|
||||||
private function addTask(Task $task, int $delay, int $period) : TaskHandler{
|
private function addTask(Task $task, int $delay, int $period) : TaskHandler{
|
||||||
if(!$this->enabled){
|
if(!$this->enabled){
|
||||||
throw new \LogicException("Tried to schedule task to disabled scheduler");
|
throw new \LogicException("Tried to schedule task to disabled scheduler");
|
||||||
@ -99,6 +132,11 @@ class TaskScheduler{
|
|||||||
return $this->handle(new TaskHandler($task, $delay, $period, $this->owner));
|
return $this->handle(new TaskHandler($task, $delay, $period, $this->owner));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @phpstan-template TTask of Task
|
||||||
|
* @phpstan-param TaskHandler<TTask> $handler
|
||||||
|
* @phpstan-return TaskHandler<TTask>
|
||||||
|
*/
|
||||||
private function handle(TaskHandler $handler) : TaskHandler{
|
private function handle(TaskHandler $handler) : TaskHandler{
|
||||||
if($handler->isDelayed()){
|
if($handler->isDelayed()){
|
||||||
$nextRun = $this->currentTick + $handler->getDelay();
|
$nextRun = $this->currentTick + $handler->getDelay();
|
||||||
@ -128,7 +166,7 @@ class TaskScheduler{
|
|||||||
}
|
}
|
||||||
$this->currentTick = $currentTick;
|
$this->currentTick = $currentTick;
|
||||||
while($this->isReady($this->currentTick)){
|
while($this->isReady($this->currentTick)){
|
||||||
/** @var TaskHandler $task */
|
/** @phpstan-var TaskHandler<covariant Task> $task */
|
||||||
$task = $this->queue->extract();
|
$task = $this->queue->extract();
|
||||||
if($task->isCancelled()){
|
if($task->isCancelled()){
|
||||||
$this->tasks->remove($task);
|
$this->tasks->remove($task);
|
||||||
|
@ -30,6 +30,7 @@ use pocketmine\network\mcpe\protocol\ClientboundPacket;
|
|||||||
use pocketmine\network\mcpe\protocol\ServerboundPacket;
|
use pocketmine\network\mcpe\protocol\ServerboundPacket;
|
||||||
use pocketmine\player\Player;
|
use pocketmine\player\Player;
|
||||||
use pocketmine\scheduler\AsyncTask;
|
use pocketmine\scheduler\AsyncTask;
|
||||||
|
use pocketmine\scheduler\Task;
|
||||||
use pocketmine\scheduler\TaskHandler;
|
use pocketmine\scheduler\TaskHandler;
|
||||||
use function get_class;
|
use function get_class;
|
||||||
use function str_starts_with;
|
use function str_starts_with;
|
||||||
@ -192,6 +193,10 @@ abstract class Timings{
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @template TTask of Task
|
||||||
|
* @phpstan-param TaskHandler<TTask> $task
|
||||||
|
*/
|
||||||
public static function getScheduledTaskTimings(TaskHandler $task, int $period) : TimingsHandler{
|
public static function getScheduledTaskTimings(TaskHandler $task, int $period) : TimingsHandler{
|
||||||
self::init();
|
self::init();
|
||||||
$name = "Task: " . $task->getTaskName();
|
$name = "Task: " . $task->getTaskName();
|
||||||
|
@ -801,7 +801,7 @@ parameters:
|
|||||||
path: ../../../src/scheduler/BulkCurlTask.php
|
path: ../../../src/scheduler/BulkCurlTask.php
|
||||||
|
|
||||||
-
|
-
|
||||||
message: "#^Cannot call method getNextRun\\(\\) on array\\<string, int\\|pocketmine\\\\scheduler\\\\TaskHandler\\>\\|int\\|pocketmine\\\\scheduler\\\\TaskHandler\\.$#"
|
message: "#^Cannot call method getNextRun\\(\\) on array\\<string, int\\|pocketmine\\\\scheduler\\\\TaskHandler\\<covariant pocketmine\\\\scheduler\\\\Task\\>\\>\\|int\\|pocketmine\\\\scheduler\\\\TaskHandler\\<covariant pocketmine\\\\scheduler\\\\Task\\>\\.$#"
|
||||||
count: 1
|
count: 1
|
||||||
path: ../../../src/scheduler/TaskScheduler.php
|
path: ../../../src/scheduler/TaskScheduler.php
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user