mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-06 01:46:04 +00:00
Moar typehints
This commit is contained in:
@ -43,9 +43,9 @@ class AsyncPool{
|
||||
/** @var int[] */
|
||||
private $workerUsage = [];
|
||||
|
||||
public function __construct(Server $server, $size){
|
||||
public function __construct(Server $server, int $size){
|
||||
$this->server = $server;
|
||||
$this->size = (int) $size;
|
||||
$this->size = $size;
|
||||
|
||||
for($i = 0; $i < $this->size; ++$i){
|
||||
$this->workerUsage[$i] = 0;
|
||||
@ -55,12 +55,11 @@ class AsyncPool{
|
||||
}
|
||||
}
|
||||
|
||||
public function getSize(){
|
||||
public function getSize() : int{
|
||||
return $this->size;
|
||||
}
|
||||
|
||||
public function increaseSize($newSize){
|
||||
$newSize = (int) $newSize;
|
||||
public function increaseSize(int $newSize){
|
||||
if($newSize > $this->size){
|
||||
for($i = $this->size; $i < $newSize; ++$i){
|
||||
$this->workerUsage[$i] = 0;
|
||||
@ -72,12 +71,11 @@ class AsyncPool{
|
||||
}
|
||||
}
|
||||
|
||||
public function submitTaskToWorker(AsyncTask $task, $worker){
|
||||
public function submitTaskToWorker(AsyncTask $task, int $worker){
|
||||
if(isset($this->tasks[$task->getTaskId()]) or $task->isGarbage()){
|
||||
return;
|
||||
}
|
||||
|
||||
$worker = (int) $worker;
|
||||
if($worker < 0 or $worker >= $this->size){
|
||||
throw new \InvalidArgumentException("Invalid worker $worker");
|
||||
}
|
||||
@ -106,7 +104,7 @@ class AsyncPool{
|
||||
$this->submitTaskToWorker($task, $selectedWorker);
|
||||
}
|
||||
|
||||
private function removeTask(AsyncTask $task, $force = false){
|
||||
private function removeTask(AsyncTask $task, bool $force = false){
|
||||
if(isset($this->taskWorkers[$task->getTaskId()])){
|
||||
if(!$force and ($task->isRunning() or !$task->isGarbage())){
|
||||
return;
|
||||
|
@ -25,7 +25,6 @@ namespace pocketmine\scheduler;
|
||||
|
||||
use pocketmine\Collectable;
|
||||
use pocketmine\Server;
|
||||
use pocketmine\utils\MainLogger;
|
||||
|
||||
/**
|
||||
* Class used to run async tasks in other threads.
|
||||
@ -47,7 +46,7 @@ abstract class AsyncTask extends Collectable{
|
||||
private $result = null;
|
||||
private $serialized = false;
|
||||
private $cancelRun = false;
|
||||
/** @var int */
|
||||
/** @var int|null */
|
||||
private $taskId = null;
|
||||
|
||||
private $crashed = false;
|
||||
@ -90,7 +89,7 @@ abstract class AsyncTask extends Collectable{
|
||||
$this->setGarbage();
|
||||
}
|
||||
|
||||
public function isCrashed(){
|
||||
public function isCrashed() : bool{
|
||||
return $this->crashed;
|
||||
}
|
||||
|
||||
@ -105,14 +104,14 @@ abstract class AsyncTask extends Collectable{
|
||||
$this->cancelRun = true;
|
||||
}
|
||||
|
||||
public function hasCancelledRun(){
|
||||
public function hasCancelledRun() : bool{
|
||||
return $this->cancelRun === true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasResult(){
|
||||
public function hasResult() : bool{
|
||||
return $this->result !== null;
|
||||
}
|
||||
|
||||
@ -120,15 +119,18 @@ abstract class AsyncTask extends Collectable{
|
||||
* @param mixed $result
|
||||
* @param bool $serialize
|
||||
*/
|
||||
public function setResult($result, $serialize = true){
|
||||
public function setResult($result, bool $serialize = true){
|
||||
$this->result = $serialize ? serialize($result) : $result;
|
||||
$this->serialized = $serialize;
|
||||
}
|
||||
|
||||
public function setTaskId($taskId){
|
||||
public function setTaskId(int $taskId){
|
||||
$this->taskId = $taskId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|null
|
||||
*/
|
||||
public function getTaskId(){
|
||||
return $this->taskId;
|
||||
}
|
||||
@ -140,7 +142,7 @@ abstract class AsyncTask extends Collectable{
|
||||
* @param string $identifier
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFromThreadStore($identifier){
|
||||
public function getFromThreadStore(string $identifier){
|
||||
global $store;
|
||||
return ($this->isGarbage() or !isset($store[$identifier])) ? null : $store[$identifier];
|
||||
}
|
||||
@ -152,7 +154,7 @@ abstract class AsyncTask extends Collectable{
|
||||
* @param string $identifier
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function saveToThreadStore($identifier, $value){
|
||||
public function saveToThreadStore(string $identifier, $value){
|
||||
global $store;
|
||||
if(!$this->isGarbage()){
|
||||
$store[$identifier] = $value;
|
||||
|
@ -31,7 +31,7 @@ class AsyncWorker extends Worker{
|
||||
private $logger;
|
||||
private $id;
|
||||
|
||||
public function __construct(MainLogger $logger, $id){
|
||||
public function __construct(MainLogger $logger, int $id){
|
||||
$this->logger = $logger;
|
||||
$this->id = $id;
|
||||
}
|
||||
@ -53,7 +53,7 @@ class AsyncWorker extends Worker{
|
||||
$this->logger->logException($e);
|
||||
}
|
||||
|
||||
public function getThreadName(){
|
||||
public function getThreadName() : string{
|
||||
return "Asynchronous Worker #" . $this->id;
|
||||
}
|
||||
}
|
||||
|
@ -25,19 +25,27 @@ namespace pocketmine\scheduler;
|
||||
|
||||
class FileWriteTask extends AsyncTask{
|
||||
|
||||
/** @var string */
|
||||
private $path;
|
||||
/** @var mixed */
|
||||
private $contents;
|
||||
/** @var int */
|
||||
private $flags;
|
||||
|
||||
public function __construct($path, $contents, $flags = 0){
|
||||
/**
|
||||
* @param string $path
|
||||
* @param mixed $contents
|
||||
* @param int $flags
|
||||
*/
|
||||
public function __construct(string $path, $contents, int $flags = 0){
|
||||
$this->path = $path;
|
||||
$this->contents = $contents;
|
||||
$this->flags = (int) $flags;
|
||||
$this->flags = $flags;
|
||||
}
|
||||
|
||||
public function onRun(){
|
||||
try{
|
||||
file_put_contents($this->path, $this->contents, (int) $this->flags);
|
||||
file_put_contents($this->path, $this->contents, $this->flags);
|
||||
}catch(\Throwable $e){
|
||||
|
||||
}
|
||||
|
@ -43,7 +43,7 @@ abstract class PluginTask extends Task{
|
||||
/**
|
||||
* @return Plugin
|
||||
*/
|
||||
final public function getOwner(){
|
||||
final public function getOwner() : Plugin{
|
||||
return $this->owner;
|
||||
}
|
||||
|
||||
|
@ -38,7 +38,12 @@ class SendUsageTask extends AsyncTask{
|
||||
public $endpoint;
|
||||
public $data;
|
||||
|
||||
public function __construct(Server $server, $type, $playerList = []){
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $type
|
||||
* @param array $playerList
|
||||
*/
|
||||
public function __construct(Server $server, int $type, array $playerList = []){
|
||||
$endpoint = "http://" . $server->getProperty("anonymous-statistics.host", "stats.pocketmine.net") . "/";
|
||||
|
||||
$data = [];
|
||||
|
@ -180,11 +180,11 @@ class ServerScheduler{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getAsyncTaskPoolSize(){
|
||||
public function getAsyncTaskPoolSize() : int{
|
||||
return $this->asyncPool->getSize();
|
||||
}
|
||||
|
||||
public function increaseAsyncTaskPoolSize($newSize){
|
||||
public function increaseAsyncTaskPoolSize(int $newSize){
|
||||
$this->asyncPool->increaseSize($newSize);
|
||||
}
|
||||
|
||||
@ -194,8 +194,8 @@ class ServerScheduler{
|
||||
*
|
||||
* @return null|TaskHandler
|
||||
*/
|
||||
public function scheduleDelayedTask(Task $task, $delay){
|
||||
return $this->addTask($task, (int) $delay, -1);
|
||||
public function scheduleDelayedTask(Task $task, int $delay){
|
||||
return $this->addTask($task, $delay, -1);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -204,8 +204,8 @@ class ServerScheduler{
|
||||
*
|
||||
* @return null|TaskHandler
|
||||
*/
|
||||
public function scheduleRepeatingTask(Task $task, $period){
|
||||
return $this->addTask($task, -1, (int) $period);
|
||||
public function scheduleRepeatingTask(Task $task, int $period){
|
||||
return $this->addTask($task, -1, $period);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -215,14 +215,14 @@ class ServerScheduler{
|
||||
*
|
||||
* @return null|TaskHandler
|
||||
*/
|
||||
public function scheduleDelayedRepeatingTask(Task $task, $delay, $period){
|
||||
return $this->addTask($task, (int) $delay, (int) $period);
|
||||
public function scheduleDelayedRepeatingTask(Task $task, int $delay, int $period){
|
||||
return $this->addTask($task, $delay, $period);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $taskId
|
||||
*/
|
||||
public function cancelTask($taskId){
|
||||
public function cancelTask(int $taskId){
|
||||
if($taskId !== null and isset($this->tasks[$taskId])){
|
||||
$this->tasks[$taskId]->cancel();
|
||||
unset($this->tasks[$taskId]);
|
||||
@ -259,20 +259,20 @@ class ServerScheduler{
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isQueued($taskId){
|
||||
public function isQueued(int $taskId) : bool{
|
||||
return isset($this->tasks[$taskId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Task $task
|
||||
* @param $delay
|
||||
* @param $period
|
||||
* @param int $delay
|
||||
* @param int $period
|
||||
*
|
||||
* @return null|TaskHandler
|
||||
*
|
||||
* @throws PluginException
|
||||
*/
|
||||
private function addTask(Task $task, $delay, $period){
|
||||
private function addTask(Task $task, int $delay, int $period){
|
||||
if($task instanceof PluginTask){
|
||||
if(!($task->getOwner() instanceof Plugin)){
|
||||
throw new PluginException("Invalid owner of PluginTask " . get_class($task));
|
||||
@ -311,7 +311,7 @@ class ServerScheduler{
|
||||
/**
|
||||
* @param int $currentTick
|
||||
*/
|
||||
public function mainThreadHeartbeat($currentTick){
|
||||
public function mainThreadHeartbeat(int $currentTick){
|
||||
$this->currentTick = $currentTick;
|
||||
while($this->isReady($this->currentTick)){
|
||||
/** @var TaskHandler $task */
|
||||
@ -341,14 +341,14 @@ class ServerScheduler{
|
||||
$this->asyncPool->collectTasks();
|
||||
}
|
||||
|
||||
private function isReady($currentTicks){
|
||||
private function isReady(int $currentTicks) : bool{
|
||||
return count($this->tasks) > 0 and $this->queue->current()->getNextRun() <= $currentTicks;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
private function nextId(){
|
||||
private function nextId() : int{
|
||||
return $this->ids++;
|
||||
}
|
||||
|
||||
|
@ -32,7 +32,7 @@ abstract class Task{
|
||||
private $taskHandler = null;
|
||||
|
||||
/**
|
||||
* @return TaskHandler
|
||||
* @return TaskHandler|null
|
||||
*/
|
||||
final public function getHandler(){
|
||||
return $this->taskHandler;
|
||||
@ -41,7 +41,7 @@ abstract class Task{
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
final public function getTaskId(){
|
||||
final public function getTaskId() : int{
|
||||
if($this->taskHandler !== null){
|
||||
return $this->taskHandler->getTaskId();
|
||||
}
|
||||
@ -50,9 +50,9 @@ abstract class Task{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TaskHandler $taskHandler
|
||||
* @param TaskHandler|null $taskHandler
|
||||
*/
|
||||
final public function setHandler($taskHandler){
|
||||
final public function setHandler(TaskHandler $taskHandler = null){
|
||||
if($this->taskHandler === null or $taskHandler === null){
|
||||
$this->taskHandler = $taskHandler;
|
||||
}
|
||||
|
@ -58,7 +58,7 @@ class TaskHandler{
|
||||
* @param int $delay
|
||||
* @param int $period
|
||||
*/
|
||||
public function __construct($timingName, Task $task, $taskId, $delay = -1, $period = -1){
|
||||
public function __construct(string $timingName, Task $task, int $taskId, int $delay = -1, int $period = -1){
|
||||
$this->task = $task;
|
||||
$this->taskId = $taskId;
|
||||
$this->delay = $delay;
|
||||
@ -71,63 +71,63 @@ class TaskHandler{
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isCancelled(){
|
||||
public function isCancelled() : bool{
|
||||
return $this->cancelled === true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getNextRun(){
|
||||
public function getNextRun() : int{
|
||||
return $this->nextRun;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $ticks
|
||||
*/
|
||||
public function setNextRun($ticks){
|
||||
public function setNextRun(int $ticks){
|
||||
$this->nextRun = $ticks;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getTaskId(){
|
||||
public function getTaskId() : int{
|
||||
return $this->taskId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Task
|
||||
*/
|
||||
public function getTask(){
|
||||
public function getTask() : Task{
|
||||
return $this->task;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getDelay(){
|
||||
public function getDelay() : int{
|
||||
return $this->delay;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isDelayed(){
|
||||
public function isDelayed() : bool{
|
||||
return $this->delay > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isRepeating(){
|
||||
public function isRepeating() : bool{
|
||||
return $this->period > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getPeriod(){
|
||||
public function getPeriod() : int{
|
||||
return $this->period;
|
||||
}
|
||||
|
||||
@ -150,14 +150,14 @@ class TaskHandler{
|
||||
/**
|
||||
* @param int $currentTick
|
||||
*/
|
||||
public function run($currentTick){
|
||||
public function run(int $currentTick){
|
||||
$this->task->onRun($currentTick);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTaskName(){
|
||||
public function getTaskName() : string{
|
||||
if($this->timingName !== null){
|
||||
return $this->timingName;
|
||||
}
|
||||
|
Reference in New Issue
Block a user