mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-08 02:51:49 +00:00
This fixes undefined behaviour when scheduling the same task twice. This is usually accidental and almost never desirable. Note that this still allows a task to be scheduled again after it has been cancelled; it only disallows scheduling a task multiple times concurrently. This commit will probably break MyPlot and other plugins that have self-scheduling tasks, but as far as I can tell those use-cases should be replaced with self-cancelling repeating tasks anyway.
151 lines
2.9 KiB
PHP
151 lines
2.9 KiB
PHP
<?php
|
|
|
|
/*
|
|
*
|
|
* ____ _ _ __ __ _ __ __ ____
|
|
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
|
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
|
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* @author PocketMine Team
|
|
* @link http://www.pocketmine.net/
|
|
*
|
|
*
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace pocketmine\scheduler;
|
|
|
|
use pocketmine\timings\Timings;
|
|
use pocketmine\timings\TimingsHandler;
|
|
|
|
class TaskHandler{
|
|
|
|
/** @var Task */
|
|
protected $task;
|
|
|
|
/** @var int */
|
|
protected $taskId;
|
|
|
|
/** @var int */
|
|
protected $delay;
|
|
|
|
/** @var int */
|
|
protected $period;
|
|
|
|
/** @var int */
|
|
protected $nextRun;
|
|
|
|
/** @var bool */
|
|
protected $cancelled = false;
|
|
|
|
/** @var TimingsHandler */
|
|
private $timings;
|
|
|
|
/** @var string */
|
|
private $taskName;
|
|
/** @var string */
|
|
private $ownerName;
|
|
|
|
public function __construct(Task $task, int $taskId, int $delay = -1, int $period = -1, ?string $ownerName = null){
|
|
if($task->getHandler() !== null){
|
|
throw new \InvalidArgumentException("Cannot assign multiple handlers to the same task");
|
|
}
|
|
$this->task = $task;
|
|
$this->taskId = $taskId;
|
|
$this->delay = $delay;
|
|
$this->period = $period;
|
|
$this->taskName = $task->getName();
|
|
$this->ownerName = $ownerName ?? "Unknown";
|
|
$this->timings = Timings::getScheduledTaskTimings($this, $period);
|
|
$this->task->setHandler($this);
|
|
}
|
|
|
|
public function isCancelled() : bool{
|
|
return $this->cancelled;
|
|
}
|
|
|
|
public function getNextRun() : int{
|
|
return $this->nextRun;
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function setNextRun(int $ticks){
|
|
$this->nextRun = $ticks;
|
|
}
|
|
|
|
public function getTaskId() : int{
|
|
return $this->taskId;
|
|
}
|
|
|
|
public function getTask() : Task{
|
|
return $this->task;
|
|
}
|
|
|
|
public function getDelay() : int{
|
|
return $this->delay;
|
|
}
|
|
|
|
public function isDelayed() : bool{
|
|
return $this->delay > 0;
|
|
}
|
|
|
|
public function isRepeating() : bool{
|
|
return $this->period > 0;
|
|
}
|
|
|
|
public function getPeriod() : int{
|
|
return $this->period;
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function cancel(){
|
|
try{
|
|
if(!$this->isCancelled()){
|
|
$this->task->onCancel();
|
|
}
|
|
}finally{
|
|
$this->remove();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function remove(){
|
|
$this->cancelled = true;
|
|
$this->task->setHandler(null);
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function run(int $currentTick){
|
|
$this->timings->startTiming();
|
|
try{
|
|
$this->task->onRun($currentTick);
|
|
}finally{
|
|
$this->timings->stopTiming();
|
|
}
|
|
}
|
|
|
|
public function getTaskName() : string{
|
|
return $this->taskName;
|
|
}
|
|
|
|
public function getOwnerName() : string{
|
|
return $this->ownerName;
|
|
}
|
|
}
|