diff --git a/src/pocketmine/Thread.php b/src/pocketmine/Thread.php index ad2074881..350dae2b9 100644 --- a/src/pocketmine/Thread.php +++ b/src/pocketmine/Thread.php @@ -23,6 +23,8 @@ declare(strict_types=1); namespace pocketmine; +use function error_reporting; + /** * This class must be extended by all custom threading classes */ @@ -73,6 +75,19 @@ abstract class Thread extends \Thread{ return parent::start($options); } + final public function run() : void{ + error_reporting(-1); + $this->registerClassLoader(); + //set this after the autoloader is registered + \ErrorUtils::setErrorExceptionHandler(); + $this->onRun(); + } + + /** + * Runs code on the thread. + */ + abstract protected function onRun() : void; + /** * Stops the thread using the best way possible. Try to stop it yourself before calling this. */ diff --git a/src/pocketmine/Worker.php b/src/pocketmine/Worker.php index e6859f41b..0d67bd9ee 100644 --- a/src/pocketmine/Worker.php +++ b/src/pocketmine/Worker.php @@ -23,6 +23,8 @@ declare(strict_types=1); namespace pocketmine; +use function error_reporting; + /** * This class must be extended by all custom threading classes */ @@ -73,6 +75,17 @@ abstract class Worker extends \Worker{ return parent::start($options); } + final public function run() : void{ + error_reporting(-1); + $this->registerClassLoader(); + $this->onRun(); + } + + /** + * Runs code on the thread. + */ + abstract protected function onRun() : void; + /** * Stops the thread using the best way possible. Try to stop it yourself before calling this. */ diff --git a/src/pocketmine/command/CommandReader.php b/src/pocketmine/command/CommandReader.php index 1f18dd0e9..bed67d6ec 100644 --- a/src/pocketmine/command/CommandReader.php +++ b/src/pocketmine/command/CommandReader.php @@ -184,9 +184,7 @@ class CommandReader extends Thread{ return null; } - public function run() : void{ - $this->registerClassLoader(); - + protected function onRun() : void{ if($this->type !== self::TYPE_READLINE){ $this->initStdin(); } diff --git a/src/pocketmine/scheduler/AsyncWorker.php b/src/pocketmine/scheduler/AsyncWorker.php index f4d98de4e..c751a2529 100644 --- a/src/pocketmine/scheduler/AsyncWorker.php +++ b/src/pocketmine/scheduler/AsyncWorker.php @@ -25,7 +25,6 @@ namespace pocketmine\scheduler; use pocketmine\utils\MainLogger; use pocketmine\Worker; -use function error_reporting; use function gc_enable; use function ini_set; @@ -45,14 +44,7 @@ class AsyncWorker extends Worker{ $this->memoryLimit = $memoryLimit; } - public function run() : void{ - error_reporting(-1); - - $this->registerClassLoader(); - - //set this after the autoloader is registered - \ErrorUtils::setErrorExceptionHandler(); - + protected function onRun() : void{ \GlobalLogger::set($this->logger); if($this->logger instanceof MainLogger){ $this->logger->registerStatic(); diff --git a/src/pocketmine/utils/ServerKiller.php b/src/pocketmine/utils/ServerKiller.php index d8abd40d3..d49582397 100644 --- a/src/pocketmine/utils/ServerKiller.php +++ b/src/pocketmine/utils/ServerKiller.php @@ -38,8 +38,7 @@ class ServerKiller extends Thread{ $this->time = $time; } - public function run() : void{ - $this->registerClassLoader(); + protected function onRun() : void{ $start = time(); $this->synchronized(function(){ if(!$this->stopped){