Deprecated CallbackTask, moved sleep checking to level

This commit is contained in:
Shoghi Cervantes 2015-04-25 17:28:30 +02:00
parent 1d8c29add7
commit 7ad98d4659
6 changed files with 84 additions and 71 deletions

View File

@ -117,7 +117,6 @@ use pocketmine\network\SourceInterface;
use pocketmine\permission\PermissibleBase;
use pocketmine\permission\PermissionAttachment;
use pocketmine\plugin\Plugin;
use pocketmine\scheduler\CallbackTask;
use pocketmine\tile\Sign;
use pocketmine\tile\Spawnable;
use pocketmine\tile\Tile;
@ -219,11 +218,6 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
private $batchedPackets = [];
/**
* @var \pocketmine\scheduler\TaskHandler[]
*/
protected $tasks = [];
/** @var PermissibleBase */
private $perm = null;
@ -871,7 +865,8 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$this->setDataFlag(self::DATA_PLAYER_FLAGS, self::DATA_PLAYER_FLAG_SLEEP, true);
$this->setSpawn($pos);
$this->tasks[] = $this->server->getScheduler()->scheduleDelayedTask(new CallbackTask([$this, "checkSleep"]), 60);
$this->level->sleepTicks = 60;
return true;
@ -903,36 +898,13 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$this->sleeping = null;
$this->setDataProperty(self::DATA_PLAYER_BED_POSITION, self::DATA_TYPE_POS, [0, 0, 0]);
$this->setDataFlag(self::DATA_PLAYER_FLAGS, self::DATA_PLAYER_FLAG_SLEEP, false);
$this->level->sleepTicks = 0;
}
}
/**
* WARNING: Do not use this, it's only for internal use.
* Changes to this function won't be recorded on the version.
*/
public function checkSleep(){
if($this->sleeping instanceof Vector3){
//TODO: Move to Level
$time = $this->level->getTime() % Level::TIME_FULL;
if($time >= Level::TIME_NIGHT and $time < Level::TIME_SUNRISE){
foreach($this->level->getPlayers() as $p){
if($p->sleeping === null){
return;
}
}
$this->level->setTime($this->level->getTime() + Level::TIME_FULL - $time);
foreach($this->level->getPlayers() as $p){
$p->stopSleep();
}
}
}
}
/**
* @param string $achievementId
*
@ -2609,11 +2581,6 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
*/
public function close($message = "", $reason = "generic reason", $notify = true){
foreach($this->tasks as $task){
$task->cancel();
}
$this->tasks = [];
if($this->connected and !$this->closed){
if($notify and $reason != ""){
$pk = new DisconnectPacket;

View File

@ -91,7 +91,6 @@ use pocketmine\plugin\PharPluginLoader;
use pocketmine\plugin\Plugin;
use pocketmine\plugin\PluginLoadOrder;
use pocketmine\plugin\PluginManager;
use pocketmine\scheduler\CallbackTask;
use pocketmine\scheduler\SendUsageTask;
use pocketmine\scheduler\ServerScheduler;
use pocketmine\tile\Chest;
@ -153,11 +152,15 @@ class Server{
*/
private $tickCounter;
private $nextTick = 0;
private $tickAverage = [20, 20, 20, 20, 20];
private $useAverage = [20, 20, 20, 20, 20];
private $tickAverage = [20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20];
private $useAverage = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
private $maxTick = 20;
private $maxUse = 0;
private $sendUsageTicker = 0;
private $dispatchSignals = false;
/** @var \AttachableThreadedLogger */
private $logger;
@ -206,6 +209,9 @@ class Server{
private $alwaysTickPlayers = false;
private $baseTickRate = 1;
private $autoSaveTicker = 0;
private $autoSaveTicks = 0;
/** @var BaseLang */
private $baseLang;
@ -1734,7 +1740,7 @@ class Server{
}
if($this->getAutoSave() and $this->getProperty("ticks-per.autosave", 6000) > 0){
$this->scheduler->scheduleDelayedRepeatingTask(new CallbackTask([$this, "doAutoSave"]), $this->getProperty("ticks-per.autosave", 6000), $this->getProperty("ticks-per.autosave", 6000));
$this->autoSaveTicks = (int) $this->getProperty("ticks-per.autosave", 6000);
}
$this->enablePlugins(PluginLoadOrder::POSTWORLD);
@ -2031,7 +2037,7 @@ class Server{
}
if($this->getProperty("settings.send-usage", true) !== false){
$this->scheduler->scheduleDelayedRepeatingTask(new CallbackTask([$this, "sendUsage"]), 6000, 6000);
$this->sendUsageTicker = 6000;
$this->sendUsage();
}
@ -2047,12 +2053,9 @@ class Server{
pcntl_signal(SIGTERM, [$this, "handleSignal"]);
pcntl_signal(SIGINT, [$this, "handleSignal"]);
pcntl_signal(SIGHUP, [$this, "handleSignal"]);
$this->getScheduler()->scheduleRepeatingTask(new CallbackTask("pcntl_signal_dispatch"), 5);
$this->dispatchSignals = true;
}
$this->getScheduler()->scheduleRepeatingTask(new CallbackTask([$this, "checkTicks"]), 20 * 5);
$this->logger->info($this->getLanguage()->translateString("pocketmine.server.defaultGameMode", [self::getGamemodeString($this->getGamemode())]));
$this->logger->info($this->getLanguage()->translateString("pocketmine.server.startFinished", [round(microtime(true) - \pocketmine\START_TIME, 3)]));
@ -2069,12 +2072,6 @@ class Server{
}
}
public function checkTicks(){
if($this->getTicksPerSecond() < 12){
$this->logger->warning($this->getLanguage()->translateString("pocketmine.server.tickOverload"));
}
}
public function exceptionHandler(\Exception $e, $trace = null){
if($e === null){
return;
@ -2377,6 +2374,11 @@ class Server{
Timings::$connectionTimer->startTiming();
$this->network->processInterfaces();
if($this->rcon !== null){
$this->rcon->check();
}
Timings::$connectionTimer->stopTiming();
Timings::$schedulerTimer->startTiming();
@ -2401,10 +2403,28 @@ class Server{
}
}
if($this->autoSave and ++$this->autoSaveTicker >= $this->autoSaveTicks){
$this->autoSaveTicker = 0;
$this->doAutoSave();
}
if($this->sendUsageTicker > 0 and --$this->sendUsageTicker === 0){
$this->sendUsageTicker = 6000;
$this->sendUsage();
}
if(($this->tickCounter % 100) === 0){
foreach($this->levels as $level){
$level->clearCache();
}
if($this->getTicksPerSecondAverage() < 12){
$this->logger->warning($this->getLanguage()->translateString("pocketmine.server.tickOverload"));
}
}
if($this->dispatchSignals and $this->tickCounter % 5 === 0){
pcntl_signal_dispatch();
}
$this->getMemoryManager()->check();

View File

@ -203,6 +203,8 @@ class Level implements ChunkManager, Metadatable{
/** @var \SplFixedArray */
private $blockStates;
public $sleepTicks = 0;
private $chunkTickRadius;
private $chunkTickList = [];
private $chunksPerTick;
@ -644,9 +646,35 @@ class Level implements ChunkManager, Metadatable{
$this->processChunkRequest();
if($this->sleepTicks > 0 and --$this->sleepTicks <= 0){
$this->checkSleep();
}
$this->timings->doTick->stopTiming();
}
public function checkSleep(){
$resetTime = true;
foreach($this->getPlayers() as $p){
if(!$p->isSleeping()){
$resetTime = false;
break;
}
}
if($resetTime){
$time = $this->getTime() % Level::TIME_FULL;
if($time >= Level::TIME_NIGHT and $time < Level::TIME_SUNRISE){
$this->setTime($this->getTime() + Level::TIME_FULL - $time);
foreach($this->getPlayers() as $p){
$p->stopSleep();
}
}
}
}
/**
* @param Player[] $target
* @param Block[] $blocks

View File

@ -27,7 +27,6 @@ namespace pocketmine\network\rcon;
use pocketmine\command\RemoteConsoleCommandSender;
use pocketmine\event\server\RemoteServerCommandEvent;
use pocketmine\scheduler\CallbackTask;
use pocketmine\Server;
use pocketmine\utils\TextFormat;
@ -66,7 +65,6 @@ class RCON{
}
socket_getsockname($this->socket, $addr, $port);
$this->server->getLogger()->info("RCON running on $addr:$port");
$this->server->getScheduler()->scheduleRepeatingTask(new CallbackTask([$this, "check"]), 3);
}
public function stop(){

View File

@ -26,6 +26,9 @@ namespace pocketmine\scheduler;
* 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
*
* @deprecated
*
*/
class CallbackTask extends Task{

View File

@ -189,6 +189,18 @@ class ServerScheduler{
}elseif(!$task->getOwner()->isEnabled()){
throw new PluginException("Plugin '" . $task->getOwner()->getName() . "' attempted to register a task while disabled");
}
}elseif($task instanceof CallbackTask and Server::getInstance()->getProperty("settings.deprecated-verbose", true)){
$callable = $task->getCallable();
if(is_array($callable)){
if(is_object($callable[0])){
$taskName = "Callback#" . get_class($callable[0]) . "::" . $callable[1];
}else{
$taskName = "Callback#" . $callable[0] . "::" . $callable[1];
}
}else{
$taskName = "Callback#" . $callable;
}
Server::getInstance()->getLogger()->warning("A plugin attempted to register a deprecated CallbackTask ($taskName)");
}
if($delay <= 0){
@ -201,22 +213,7 @@ class ServerScheduler{
$period = 1;
}
if($task instanceof CallbackTask){
$callable = $task->getCallable();
if(is_array($callable)){
if(is_object($callable[0])){
$taskName = "Callback#" . get_class($callable[0]) . "::" . $callable[1];
}else{
$taskName = "Callback#" . $callable[0] . "::" . $callable[1];
}
}else{
$taskName = "Callback#" . $callable;
}
}else{
$taskName = get_class($task);
}
return $this->handle(new TaskHandler($taskName, $task, $this->nextId(), $delay, $period));
return $this->handle(new TaskHandler(get_class($task), $task, $this->nextId(), $delay, $period));
}
private function handle(TaskHandler $handler){