diff --git a/src/Server.php b/src/Server.php index e930c0d1a..541e1ae9f 100644 --- a/src/Server.php +++ b/src/Server.php @@ -1712,7 +1712,7 @@ class Server{ $session = $player->getNetworkSession(); $position = $player->getPosition(); $this->logger->info($this->language->translate(KnownTranslationFactory::pocketmine_player_logIn( - TextFormat::AQUA . $player->getName() . TextFormat::WHITE, + TextFormat::AQUA . $player->getName() . TextFormat::RESET, $session->getIp(), (string) $session->getPort(), (string) $player->getId(), diff --git a/src/command/defaults/GarbageCollectorCommand.php b/src/command/defaults/GarbageCollectorCommand.php index c77fd794f..2aebf3b19 100644 --- a/src/command/defaults/GarbageCollectorCommand.php +++ b/src/command/defaults/GarbageCollectorCommand.php @@ -59,7 +59,7 @@ class GarbageCollectorCommand extends VanillaCommand{ $cyclesCollected = $sender->getServer()->getMemoryManager()->triggerGarbageCollector(); - $sender->sendMessage(KnownTranslationFactory::pocketmine_command_gc_header()->format(TextFormat::GREEN . "---- " . TextFormat::WHITE, TextFormat::GREEN . " ----" . TextFormat::WHITE)); + $sender->sendMessage(KnownTranslationFactory::pocketmine_command_gc_header()->format(TextFormat::GREEN . "---- " . TextFormat::RESET, TextFormat::GREEN . " ----" . TextFormat::RESET)); $sender->sendMessage(KnownTranslationFactory::pocketmine_command_gc_chunks(TextFormat::RED . number_format($chunksCollected))->prefix(TextFormat::GOLD)); $sender->sendMessage(KnownTranslationFactory::pocketmine_command_gc_entities(TextFormat::RED . number_format($entitiesCollected))->prefix(TextFormat::GOLD)); diff --git a/src/command/defaults/HelpCommand.php b/src/command/defaults/HelpCommand.php index d5a530f56..3f440be29 100644 --- a/src/command/defaults/HelpCommand.php +++ b/src/command/defaults/HelpCommand.php @@ -91,7 +91,7 @@ class HelpCommand extends VanillaCommand{ foreach($commands[$pageNumber - 1] as $command){ $description = $command->getDescription(); $descriptionString = $description instanceof Translatable ? $lang->translate($description) : $description; - $sender->sendMessage(TextFormat::DARK_GREEN . "/" . $command->getName() . ": " . TextFormat::WHITE . $descriptionString); + $sender->sendMessage(TextFormat::DARK_GREEN . "/" . $command->getName() . ": " . TextFormat::RESET . $descriptionString); } } @@ -103,18 +103,18 @@ class HelpCommand extends VanillaCommand{ $description = $cmd->getDescription(); $descriptionString = $description instanceof Translatable ? $lang->translate($description) : $description; $sender->sendMessage(KnownTranslationFactory::pocketmine_command_help_specificCommand_header($cmd->getName()) - ->format(TextFormat::YELLOW . "--------- " . TextFormat::WHITE, TextFormat::YELLOW . " ---------")); - $sender->sendMessage(KnownTranslationFactory::pocketmine_command_help_specificCommand_description(TextFormat::WHITE . $descriptionString) + ->format(TextFormat::YELLOW . "--------- " . TextFormat::RESET, TextFormat::YELLOW . " ---------")); + $sender->sendMessage(KnownTranslationFactory::pocketmine_command_help_specificCommand_description(TextFormat::RESET . $descriptionString) ->prefix(TextFormat::GOLD)); $usage = $cmd->getUsage(); $usageString = $usage instanceof Translatable ? $lang->translate($usage) : $usage; - $sender->sendMessage(KnownTranslationFactory::pocketmine_command_help_specificCommand_usage(TextFormat::WHITE . implode("\n" . TextFormat::WHITE, explode("\n", $usageString))) + $sender->sendMessage(KnownTranslationFactory::pocketmine_command_help_specificCommand_usage(TextFormat::RESET . implode("\n" . TextFormat::RESET, explode("\n", $usageString))) ->prefix(TextFormat::GOLD)); $aliases = $cmd->getAliases(); sort($aliases, SORT_NATURAL); - $sender->sendMessage(KnownTranslationFactory::pocketmine_command_help_specificCommand_aliases(TextFormat::WHITE . implode(", ", $aliases)) + $sender->sendMessage(KnownTranslationFactory::pocketmine_command_help_specificCommand_aliases(TextFormat::RESET . implode(", ", $aliases)) ->prefix(TextFormat::GOLD)); return true; diff --git a/src/command/defaults/MeCommand.php b/src/command/defaults/MeCommand.php index 33564e9cc..f7d3978a5 100644 --- a/src/command/defaults/MeCommand.php +++ b/src/command/defaults/MeCommand.php @@ -48,7 +48,7 @@ class MeCommand extends VanillaCommand{ throw new InvalidCommandSyntaxException(); } - $sender->getServer()->broadcastMessage(KnownTranslationFactory::chat_type_emote($sender instanceof Player ? $sender->getDisplayName() : $sender->getName(), TextFormat::WHITE . implode(" ", $args))); + $sender->getServer()->broadcastMessage(KnownTranslationFactory::chat_type_emote($sender instanceof Player ? $sender->getDisplayName() : $sender->getName(), TextFormat::RESET . implode(" ", $args))); return true; } diff --git a/src/command/defaults/PluginsCommand.php b/src/command/defaults/PluginsCommand.php index 39f86e637..ba7f602a4 100644 --- a/src/command/defaults/PluginsCommand.php +++ b/src/command/defaults/PluginsCommand.php @@ -52,7 +52,7 @@ class PluginsCommand extends VanillaCommand{ }, $sender->getServer()->getPluginManager()->getPlugins()); sort($list, SORT_STRING); - $sender->sendMessage(KnownTranslationFactory::pocketmine_command_plugins_success((string) count($list), implode(TextFormat::WHITE . ", ", $list))); + $sender->sendMessage(KnownTranslationFactory::pocketmine_command_plugins_success((string) count($list), implode(TextFormat::RESET . ", ", $list))); return true; } } diff --git a/src/command/defaults/StatusCommand.php b/src/command/defaults/StatusCommand.php index 67088be9c..7f5b0766b 100644 --- a/src/command/defaults/StatusCommand.php +++ b/src/command/defaults/StatusCommand.php @@ -48,7 +48,7 @@ class StatusCommand extends VanillaCommand{ $mUsage = Process::getAdvancedMemoryUsage(); $server = $sender->getServer(); - $sender->sendMessage(TextFormat::GREEN . "---- " . TextFormat::WHITE . "Server status" . TextFormat::GREEN . " ----"); + $sender->sendMessage(TextFormat::GREEN . "---- " . TextFormat::RESET . "Server status" . TextFormat::GREEN . " ----"); $time = (int) (microtime(true) - $server->getStartTime()); diff --git a/src/command/defaults/VersionCommand.php b/src/command/defaults/VersionCommand.php index 0200c4ff3..20aa9b0e9 100644 --- a/src/command/defaults/VersionCommand.php +++ b/src/command/defaults/VersionCommand.php @@ -53,17 +53,18 @@ class VersionCommand extends VanillaCommand{ public function execute(CommandSender $sender, string $commandLabel, array $args){ if(count($args) === 0){ $sender->sendMessage(KnownTranslationFactory::pocketmine_command_version_serverSoftwareName( - VersionInfo::NAME + TextFormat::GREEN . VersionInfo::NAME . TextFormat::RESET )); + $versionColor = VersionInfo::IS_DEVELOPMENT_BUILD ? TextFormat::YELLOW : TextFormat::GREEN; $sender->sendMessage(KnownTranslationFactory::pocketmine_command_version_serverSoftwareVersion( - VersionInfo::VERSION()->getFullVersion(), - VersionInfo::GIT_HASH() + $versionColor . VersionInfo::VERSION()->getFullVersion() . TextFormat::RESET, + TextFormat::GREEN . VersionInfo::GIT_HASH() . TextFormat::RESET )); $sender->sendMessage(KnownTranslationFactory::pocketmine_command_version_minecraftVersion( - ProtocolInfo::MINECRAFT_VERSION_NETWORK, - (string) ProtocolInfo::CURRENT_PROTOCOL + TextFormat::GREEN . ProtocolInfo::MINECRAFT_VERSION_NETWORK . TextFormat::RESET, + TextFormat::GREEN . ProtocolInfo::CURRENT_PROTOCOL . TextFormat::RESET )); - $sender->sendMessage(KnownTranslationFactory::pocketmine_command_version_phpVersion(PHP_VERSION)); + $sender->sendMessage(KnownTranslationFactory::pocketmine_command_version_phpVersion(TextFormat::GREEN . PHP_VERSION . TextFormat::RESET)); $jitMode = Utils::getOpcacheJitMode(); if($jitMode !== null){ @@ -75,8 +76,8 @@ class VersionCommand extends VanillaCommand{ }else{ $jitStatus = KnownTranslationFactory::pocketmine_command_version_phpJitNotSupported(); } - $sender->sendMessage(KnownTranslationFactory::pocketmine_command_version_phpJitStatus($jitStatus)); - $sender->sendMessage(KnownTranslationFactory::pocketmine_command_version_operatingSystem(Utils::getOS())); + $sender->sendMessage(KnownTranslationFactory::pocketmine_command_version_phpJitStatus($jitStatus->format(TextFormat::GREEN, TextFormat::RESET))); + $sender->sendMessage(KnownTranslationFactory::pocketmine_command_version_operatingSystem(TextFormat::GREEN . Utils::getOS() . TextFormat::RESET)); }else{ $pluginName = implode(" ", $args); $exactPlugin = $sender->getServer()->getPluginManager()->getPlugin($pluginName); @@ -106,7 +107,7 @@ class VersionCommand extends VanillaCommand{ private function describeToSender(Plugin $plugin, CommandSender $sender) : void{ $desc = $plugin->getDescription(); - $sender->sendMessage(TextFormat::DARK_GREEN . $desc->getName() . TextFormat::WHITE . " version " . TextFormat::DARK_GREEN . $desc->getVersion()); + $sender->sendMessage(TextFormat::DARK_GREEN . $desc->getName() . TextFormat::RESET . " version " . TextFormat::DARK_GREEN . $desc->getVersion()); if($desc->getDescription() !== ""){ $sender->sendMessage($desc->getDescription()); diff --git a/src/console/ConsoleCommandSender.php b/src/console/ConsoleCommandSender.php index 2a121a69a..aa7ea6e69 100644 --- a/src/console/ConsoleCommandSender.php +++ b/src/console/ConsoleCommandSender.php @@ -63,7 +63,7 @@ class ConsoleCommandSender implements CommandSender{ } foreach(explode("\n", trim($message)) as $line){ - Terminal::writeLine(TextFormat::GREEN . "Command output | " . TextFormat::WHITE . $line); + Terminal::writeLine(TextFormat::GREEN . "Command output | " . TextFormat::addBase(TextFormat::WHITE, $line)); } } diff --git a/src/utils/MainLogger.php b/src/utils/MainLogger.php index ee7bb6d6c..641233a26 100644 --- a/src/utils/MainLogger.php +++ b/src/utils/MainLogger.php @@ -190,7 +190,7 @@ class MainLogger extends \AttachableThreadedLogger implements \BufferedLogger{ $threadName = (new \ReflectionClass($thread))->getShortName() . " thread"; } - $message = sprintf($this->format, $time->format("H:i:s.v"), $color, $threadName, $prefix, TextFormat::clean($message, false)); + $message = sprintf($this->format, $time->format("H:i:s.v"), $color, $threadName, $prefix, TextFormat::addBase($color, TextFormat::clean($message, false))); if(!Terminal::isInit()){ Terminal::init($this->useFormattingCodes); //lazy-init colour codes because we don't know if they've been registered on this thread diff --git a/src/utils/TextFormat.php b/src/utils/TextFormat.php index d2250057f..dfd6a359a 100644 --- a/src/utils/TextFormat.php +++ b/src/utils/TextFormat.php @@ -158,6 +158,31 @@ abstract class TextFormat{ return self::preg_replace('/' . preg_quote($placeholder, "/") . '([0-9a-gk-or])/u', TextFormat::ESCAPE . '$1', $string); } + /** + * Adds base formatting to the string. The given format codes will be inserted directly after any RESET (§r) codes. + * + * This is useful for log messages, where a RESET code should return to the log message's original colour (e.g. + * blue for NOTICE), rather than whatever the terminal's base text colour is (usually some off-white colour). + * + * Example behaviour: + * - Base format "§c" (red) + "Hello" (no format) = "§r§cHello" + * - Base format "§c" + "Hello §rWorld" = "§r§cHello §r§cWorld" + * + * Note: Adding base formatting to the output string a second time will result in a combination of formats from both + * calls. This is not by design, but simply a consequence of the way the function is implemented. + */ + public static function addBase(string $baseFormat, string $string) : string{ + $baseFormatParts = self::tokenize($baseFormat); + foreach($baseFormatParts as $part){ + if(!isset(self::FORMATS[$part]) && !isset(self::COLORS[$part])){ + throw new \InvalidArgumentException("Unexpected base format token \"$part\", expected only color and format tokens"); + } + } + $baseFormat = self::RESET . $baseFormat; + + return $baseFormat . str_replace(TextFormat::RESET, $baseFormat, $string); + } + /** * Returns an HTML-formatted string with colors/markup */