mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-06 01:46:04 +00:00
Fixed wrong paths
This commit is contained in:
52
src/pocketmine/scheduler/CallbackTask.php
Normal file
52
src/pocketmine/scheduler/CallbackTask.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?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
|
||||
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace pocketmine\scheduler;
|
||||
|
||||
/**
|
||||
* Allows the creation if simple callbacks with extra data
|
||||
* The last parameter in the callback will be this object
|
||||
*
|
||||
* If you want to do a task in a Plugin, consider extending PluginTask to your needs
|
||||
*/
|
||||
class CallbackTask extends Task{
|
||||
|
||||
/** @var callable */
|
||||
protected $callable;
|
||||
|
||||
/** @var array */
|
||||
protected $args;
|
||||
|
||||
/**
|
||||
* @param callable $callable
|
||||
* @param array $args
|
||||
*/
|
||||
public function __construct(callable $callable, array $args = array()){
|
||||
$this->callable = $callable;
|
||||
$this->args = $args;
|
||||
$this->args[] = $this;
|
||||
}
|
||||
|
||||
public function onRun($currentTicks){
|
||||
call_user_func_array($this->callable, $this->args);
|
||||
}
|
||||
|
||||
}
|
48
src/pocketmine/scheduler/PluginTask.php
Normal file
48
src/pocketmine/scheduler/PluginTask.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?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
|
||||
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace pocketmine\scheduler;
|
||||
|
||||
use pocketmine\plugin\Plugin;
|
||||
|
||||
/**
|
||||
* Base class for plugin tasks. Allows the Server to delete them easily when needed
|
||||
*/
|
||||
abstract class PluginTask extends Task{
|
||||
|
||||
/** @var Plugin */
|
||||
protected $owner;
|
||||
|
||||
/**
|
||||
* @param Plugin $owner
|
||||
*/
|
||||
public function __construct(Plugin $owner){
|
||||
$this->owner = $owner;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Plugin
|
||||
*/
|
||||
public final function getOwner(){
|
||||
return $this->owner;
|
||||
}
|
||||
|
||||
}
|
197
src/pocketmine/scheduler/ServerScheduler.php
Normal file
197
src/pocketmine/scheduler/ServerScheduler.php
Normal file
@ -0,0 +1,197 @@
|
||||
<?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
|
||||
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* Task scheduling related classes
|
||||
*/
|
||||
namespace pocketmine\scheduler;
|
||||
|
||||
use pocketmine\plugin\Plugin;
|
||||
|
||||
class ServerScheduler{
|
||||
|
||||
/**
|
||||
* @var \SplPriorityQueue<Task>
|
||||
*/
|
||||
protected $queue;
|
||||
|
||||
/**
|
||||
* @var TaskHandler[]
|
||||
*/
|
||||
protected $tasks = array();
|
||||
|
||||
/** @var int */
|
||||
private $ids = 1;
|
||||
|
||||
/** @var int */
|
||||
protected $currentTick = 0;
|
||||
|
||||
public function __construct(){
|
||||
$this->queue = new TaskQueue();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Task $task
|
||||
*
|
||||
* @return null|TaskHandler
|
||||
*/
|
||||
public function scheduleTask(Task $task){
|
||||
return $this->addTask($task, -1, -1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Task $task
|
||||
* @param int $delay
|
||||
*
|
||||
* @return null|TaskHandler
|
||||
*/
|
||||
public function scheduleDelayedTask(Task $task, $delay){
|
||||
return $this->addTask($task, (int) $delay, -1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Task $task
|
||||
* @param int $period
|
||||
*
|
||||
* @return null|TaskHandler
|
||||
*/
|
||||
public function scheduleRepeatingTask(Task $task, $period){
|
||||
return $this->addTask($task, -1, (int) $period);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Task $task
|
||||
* @param int $delay
|
||||
* @param int $period
|
||||
*
|
||||
* @return null|TaskHandler
|
||||
*/
|
||||
public function scheduleDelayedRepeatingTask(Task $task, $delay, $period){
|
||||
return $this->addTask($task, (int) $delay, (int) $period);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $taskId
|
||||
*/
|
||||
public function cancelTask($taskId){
|
||||
if(isset($this->tasks[$taskId])){
|
||||
$this->tasks[$taskId]->cancel();
|
||||
unset($this->tasks[$taskId]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Plugin $plugin
|
||||
*/
|
||||
public function cancelTasks(Plugin $plugin){
|
||||
foreach($this->tasks as $taskId => $task){
|
||||
if($task->getTask() instanceof PluginTask){
|
||||
$task->cancel();
|
||||
unset($this->tasks[$taskId]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function cancelAllTasks(){
|
||||
foreach($this->tasks as $task){
|
||||
$task->cancel();
|
||||
}
|
||||
$this->tasks = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $taskId
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isQueued($taskId){
|
||||
return isset($this->tasks[$taskId]);
|
||||
}
|
||||
|
||||
private function addTask(Task $task, $delay, $period){
|
||||
if($task instanceof PluginTask and !$task->getOwner()->isEnabled()){
|
||||
trigger_error("Plugin attempted to register a task while disabled", E_USER_WARNING);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if($delay <= 0){
|
||||
$delay = -1;
|
||||
}
|
||||
|
||||
if($period === 1){
|
||||
$period = 1;
|
||||
}elseif($period < -1){
|
||||
$period = -1;
|
||||
}
|
||||
|
||||
return $this->handle(new TaskHandler($task, $this->nextId(), $delay, $period));
|
||||
}
|
||||
|
||||
private function handle(TaskHandler $handler){
|
||||
if($handler->isDelayed()){
|
||||
$nextRun = $this->currentTick + $handler->getDelay();
|
||||
}else{
|
||||
$nextRun = $this->currentTick;
|
||||
}
|
||||
|
||||
$handler->setNextRun($nextRun);
|
||||
$this->tasks[$handler->getTaskId()] = $handler;
|
||||
$this->queue->insert($handler, $nextRun);
|
||||
|
||||
return $handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $currentTick
|
||||
*/
|
||||
public function mainThreadHeartbeat($currentTick){
|
||||
$this->currentTick = $currentTick;
|
||||
while($this->isReady($this->currentTick)){
|
||||
$task = $this->queue->extract();
|
||||
if($task->isCancelled()){
|
||||
unset($this->tasks[$task->getTaskId()]);
|
||||
continue;
|
||||
}else{
|
||||
$task->run($this->currentTick);
|
||||
}
|
||||
if($task->isRepeating()){
|
||||
$task->setNextRun($this->currentTick + $task->getPeriod());
|
||||
$this->queue->insert($task, $this->currentTick + $task->getPeriod());
|
||||
}else{
|
||||
$task->remove();
|
||||
unset($this->tasks[$task->getTaskId()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function isReady($currentTicks){
|
||||
return count($this->tasks) > 0 and $this->queue->current()->getNextRun() <= $currentTicks;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
private function nextId(){
|
||||
return $this->ids++;
|
||||
}
|
||||
|
||||
}
|
72
src/pocketmine/scheduler/Task.php
Normal file
72
src/pocketmine/scheduler/Task.php
Normal file
@ -0,0 +1,72 @@
|
||||
<?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
|
||||
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace pocketmine\scheduler;
|
||||
|
||||
abstract class Task{
|
||||
|
||||
/** @var TaskHandler */
|
||||
private $taskHandler = null;
|
||||
|
||||
/**
|
||||
* @return TaskHandler
|
||||
*/
|
||||
public final function getHandler(){
|
||||
return $this->taskHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public final function getTaskId(){
|
||||
if($this->taskHandler !== null){
|
||||
return $this->taskHandler->getTaskId();
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TaskHandler $taskHandler
|
||||
*/
|
||||
public final function setHandler($taskHandler){
|
||||
if($this->taskHandler === null or $taskHandler === null){
|
||||
$this->taskHandler = $taskHandler;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Actions to execute when run
|
||||
*
|
||||
* @param $currentTick
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public abstract function onRun($currentTick);
|
||||
|
||||
/**
|
||||
* Actions to execute if the Task is cancelled
|
||||
*/
|
||||
public function onCancel(){
|
||||
|
||||
}
|
||||
|
||||
}
|
138
src/pocketmine/scheduler/TaskHandler.php
Normal file
138
src/pocketmine/scheduler/TaskHandler.php
Normal file
@ -0,0 +1,138 @@
|
||||
<?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
|
||||
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace pocketmine\scheduler;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* @param Task $task
|
||||
* @param int $taskId
|
||||
* @param int $delay
|
||||
* @param int $period
|
||||
*/
|
||||
public function __construct(Task $task, $taskId, $delay = -1, $period = -1){
|
||||
$this->task = $task;
|
||||
$this->taskId = $taskId;
|
||||
$this->delay = $delay;
|
||||
$this->period = $period;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isCancelled(){
|
||||
return $this->cancelled === true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getNextRun(){
|
||||
return $this->nextRun;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $ticks
|
||||
*/
|
||||
public function setNextRun($ticks){
|
||||
$this->nextRun = $ticks;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getTaskId(){
|
||||
return $this->taskId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Task
|
||||
*/
|
||||
public function getTask(){
|
||||
return $this->task;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getDelay(){
|
||||
return $this->delay;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isDelayed(){
|
||||
return $this->delay > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isRepeating(){
|
||||
return $this->period > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getPeriod(){
|
||||
return $this->period;
|
||||
}
|
||||
|
||||
public function cancel(){
|
||||
if(!$this->isCancelled()){
|
||||
$this->task->onCancel();
|
||||
}
|
||||
$this->remove();
|
||||
}
|
||||
|
||||
public function remove(){
|
||||
$this->cancelled = true;
|
||||
$this->task->setHandler(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $currentTick
|
||||
*/
|
||||
public function run($currentTick){
|
||||
$this->task->onRun($currentTick);
|
||||
}
|
||||
}
|
29
src/pocketmine/scheduler/TaskQueue.php
Normal file
29
src/pocketmine/scheduler/TaskQueue.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?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
|
||||
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace pocketmine\scheduler;
|
||||
|
||||
class TaskQueue extends \SplPriorityQueue{
|
||||
|
||||
public function compare($priority1, $priority2){
|
||||
return (int) -($priority1 - $priority2);
|
||||
}
|
||||
}
|
79
src/pocketmine/scheduler/TickScheduler.php
Normal file
79
src/pocketmine/scheduler/TickScheduler.php
Normal file
@ -0,0 +1,79 @@
|
||||
<?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/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace pocketmine\scheduler;
|
||||
|
||||
class TickScheduler extends \Thread{
|
||||
protected $sleepTime;
|
||||
protected $ticksPerSecond;
|
||||
protected $tickMeasure;
|
||||
public $hasTick;
|
||||
|
||||
public function __construct($ticksPerSecond = 20){
|
||||
$this->ticksPerSecond = (int) $ticksPerSecond;
|
||||
$this->sleepTime = (int) (1000000 / $this->ticksPerSecond);
|
||||
$this->tickMeasure = $this->sleepTime;
|
||||
$this->start(PTHREADS_INHERIT_ALL & ~PTHREADS_INHERIT_CLASSES);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if clear to run tick
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasTick(){
|
||||
return $this->synchronized(function (){
|
||||
$hasTick = $this->hasTick;
|
||||
$this->hasTick = false;
|
||||
|
||||
return $hasTick === true;
|
||||
});
|
||||
}
|
||||
|
||||
public function doTick(){
|
||||
$this->notify();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return float
|
||||
*/
|
||||
public function getTPS(){
|
||||
return $this->synchronized(function (){
|
||||
return round(($this->sleepTime / $this->tickMeasure) * $this->ticksPerSecond, 2);
|
||||
});
|
||||
}
|
||||
|
||||
public function run(){
|
||||
$tickTime = microtime(true);
|
||||
$this->hasTick = true;
|
||||
while(true){
|
||||
$this->synchronized(function (){
|
||||
$this->hasTick = true;
|
||||
$this->wait();
|
||||
$this->hasTick = false;
|
||||
});
|
||||
|
||||
$this->tickMeasure = (int) ((($time = microtime(true)) - $tickTime) * 1000000);
|
||||
$tickTime = $time;
|
||||
usleep($this->sleepTime - 100); //Remove a few ms for processing
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user