From 14d3e6c7d5f98b13ddbc39fab55a0512f9c9c262 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 1 Sep 2022 23:43:13 +0100 Subject: [PATCH] Allow disabling the console reader via pocketmine.yml Useful to save resources on headless servers where the console is never used (e.g. hosted server, Docker, etc.) --- resources/pocketmine.yml | 3 +++ src/Server.php | 28 +++++++++++++++------------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/resources/pocketmine.yml b/resources/pocketmine.yml index f922cb225..4ad8ea9c0 100644 --- a/resources/pocketmine.yml +++ b/resources/pocketmine.yml @@ -168,6 +168,9 @@ timings: host: timings.pmmp.io console: + #Whether to accept commands via the console. If disabled, anything typed on the console will be ignored. + #Useful to save resources on headless servers where the console is never used (e.g. hosted server, Docker, etc.) + enable-input: true #Choose whether to enable server stats reporting on the console title. #NOTE: The title ticker will be disabled regardless if console colours are not enabled. title-tick: true diff --git a/src/Server.php b/src/Server.php index 3128a7c39..25b689093 100644 --- a/src/Server.php +++ b/src/Server.php @@ -225,7 +225,7 @@ class Server{ private MemoryManager $memoryManager; - private ConsoleReaderThread $console; + private ?ConsoleReaderThread $console = null; private SimpleCommandMap $commandMap; @@ -1048,17 +1048,19 @@ class Server{ $this->subscribeToBroadcastChannel(self::BROADCAST_CHANNEL_ADMINISTRATIVE, $consoleSender); $this->subscribeToBroadcastChannel(self::BROADCAST_CHANNEL_USERS, $consoleSender); - $consoleNotifier = new SleeperNotifier(); - $commandBuffer = new \Threaded(); - $this->console = new ConsoleReaderThread($commandBuffer, $consoleNotifier); - $this->tickSleeper->addNotifier($consoleNotifier, function() use ($commandBuffer, $consoleSender) : void{ - Timings::$serverCommand->startTiming(); - while(($line = $commandBuffer->shift()) !== null){ - $this->dispatchCommand($consoleSender, (string) $line); - } - Timings::$serverCommand->stopTiming(); - }); - $this->console->start(PTHREADS_INHERIT_NONE); + if($this->configGroup->getPropertyBool("console.enable-input", true)){ + $consoleNotifier = new SleeperNotifier(); + $commandBuffer = new \Threaded(); + $this->console = new ConsoleReaderThread($commandBuffer, $consoleNotifier); + $this->tickSleeper->addNotifier($consoleNotifier, function() use ($commandBuffer, $consoleSender) : void{ + Timings::$serverCommand->startTiming(); + while(($line = $commandBuffer->shift()) !== null){ + $this->dispatchCommand($consoleSender, (string) $line); + } + Timings::$serverCommand->stopTiming(); + }); + $this->console->start(PTHREADS_INHERIT_NONE); + } $this->tickProcessor(); $this->forceShutdown(); @@ -1511,7 +1513,7 @@ class Server{ $this->configGroup->save(); } - if(isset($this->console)){ + if($this->console !== null){ $this->getLogger()->debug("Closing console"); $this->console->quit(); }