From 4d683c63d8a7ced240a2f7a440c9df1f6cb20356 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 12 Aug 2021 13:43:58 +0100 Subject: [PATCH] TextFormat: remove toJSON() this is not used in the PM core, and is only used by 1 plugin in the whole public ecosystem: BigBrother. It makes more sense to make BigBrother maintainers shoulder the responsibility of maintaining this code, since they are the only ones that need it - besides, if this changed in any MCJE update in the last 5y, nobody has updated it, and nobody has maintained it either due to not having a fucking clue what the thing is for (the documentation is basically nonexistent). --- src/utils/TextFormat.php | 192 ----------------------------- src/utils/TextFormatJsonObject.php | 60 --------- 2 files changed, 252 deletions(-) delete mode 100644 src/utils/TextFormatJsonObject.php diff --git a/src/utils/TextFormat.php b/src/utils/TextFormat.php index 5594024a0b..cd25a21812 100644 --- a/src/utils/TextFormat.php +++ b/src/utils/TextFormat.php @@ -24,8 +24,6 @@ declare(strict_types=1); namespace pocketmine\utils; use function is_array; -use function json_encode; -use function json_last_error_msg; use function mb_scrub; use function preg_last_error; use function preg_quote; @@ -33,7 +31,6 @@ use function preg_replace; use function preg_split; use function str_repeat; use function str_replace; -use const JSON_UNESCAPED_SLASHES; use const PREG_BACKTRACK_LIMIT_ERROR; use const PREG_BAD_UTF8_ERROR; use const PREG_BAD_UTF8_OFFSET_ERROR; @@ -160,195 +157,6 @@ abstract class TextFormat{ return self::preg_replace('/' . preg_quote($placeholder, "/") . '([0-9a-fk-or])/u', TextFormat::ESCAPE . '$1', $string); } - /** - * Returns an JSON-formatted string with colors/markup - * - * @param string|string[] $string - */ - public static function toJSON($string) : string{ - if(!is_array($string)){ - $string = self::tokenize($string); - } - $newString = new TextFormatJsonObject(); - $pointer = $newString; - $color = "white"; - $bold = false; - $italic = false; - $underlined = false; - $strikethrough = false; - $obfuscated = false; - $index = 0; - - foreach($string as $token){ - if($pointer->text !== null){ - if($newString->extra === null){ - $newString->extra = []; - } - $newString->extra[$index] = $pointer = new TextFormatJsonObject(); - if($color !== "white"){ - $pointer->color = $color; - } - if($bold){ - $pointer->bold = true; - } - if($italic){ - $pointer->italic = true; - } - if($underlined){ - $pointer->underlined = true; - } - if($strikethrough){ - $pointer->strikethrough = true; - } - if($obfuscated){ - $pointer->obfuscated = true; - } - ++$index; - } - switch($token){ - case TextFormat::BOLD: - if(!$bold){ - $pointer->bold = true; - $bold = true; - } - break; - case TextFormat::OBFUSCATED: - if(!$obfuscated){ - $pointer->obfuscated = true; - $obfuscated = true; - } - break; - case TextFormat::ITALIC: - if(!$italic){ - $pointer->italic = true; - $italic = true; - } - break; - case TextFormat::UNDERLINE: - if(!$underlined){ - $pointer->underlined = true; - $underlined = true; - } - break; - case TextFormat::STRIKETHROUGH: - if(!$strikethrough){ - $pointer->strikethrough = true; - $strikethrough = true; - } - break; - case TextFormat::RESET: - if($color !== "white"){ - $pointer->color = "white"; - $color = "white"; - } - if($bold){ - $pointer->bold = false; - $bold = false; - } - if($italic){ - $pointer->italic = false; - $italic = false; - } - if($underlined){ - $pointer->underlined = false; - $underlined = false; - } - if($strikethrough){ - $pointer->strikethrough = false; - $strikethrough = false; - } - if($obfuscated){ - $pointer->obfuscated = false; - $obfuscated = false; - } - break; - - //Colors - case TextFormat::BLACK: - $pointer->color = "black"; - $color = "black"; - break; - case TextFormat::DARK_BLUE: - $pointer->color = "dark_blue"; - $color = "dark_blue"; - break; - case TextFormat::DARK_GREEN: - $pointer->color = "dark_green"; - $color = "dark_green"; - break; - case TextFormat::DARK_AQUA: - $pointer->color = "dark_aqua"; - $color = "dark_aqua"; - break; - case TextFormat::DARK_RED: - $pointer->color = "dark_red"; - $color = "dark_red"; - break; - case TextFormat::DARK_PURPLE: - $pointer->color = "dark_purple"; - $color = "dark_purple"; - break; - case TextFormat::GOLD: - $pointer->color = "gold"; - $color = "gold"; - break; - case TextFormat::GRAY: - $pointer->color = "gray"; - $color = "gray"; - break; - case TextFormat::DARK_GRAY: - $pointer->color = "dark_gray"; - $color = "dark_gray"; - break; - case TextFormat::BLUE: - $pointer->color = "blue"; - $color = "blue"; - break; - case TextFormat::GREEN: - $pointer->color = "green"; - $color = "green"; - break; - case TextFormat::AQUA: - $pointer->color = "aqua"; - $color = "aqua"; - break; - case TextFormat::RED: - $pointer->color = "red"; - $color = "red"; - break; - case TextFormat::LIGHT_PURPLE: - $pointer->color = "light_purple"; - $color = "light_purple"; - break; - case TextFormat::YELLOW: - $pointer->color = "yellow"; - $color = "yellow"; - break; - case TextFormat::WHITE: - $pointer->color = "white"; - $color = "white"; - break; - default: - $pointer->text = $token; - break; - } - } - - if($newString->extra !== null){ - foreach($newString->extra as $k => $d){ - if($d->text === null){ - unset($newString->extra[$k]); - } - } - } - - $result = json_encode($newString, JSON_UNESCAPED_SLASHES); - if($result === false){ - throw new \InvalidArgumentException("Failed to encode result JSON: " . json_last_error_msg()); - } - return $result; - } - /** * Returns an HTML-formatted string with colors/markup * diff --git a/src/utils/TextFormatJsonObject.php b/src/utils/TextFormatJsonObject.php deleted file mode 100644 index f6ebf02aa6..0000000000 --- a/src/utils/TextFormatJsonObject.php +++ /dev/null @@ -1,60 +0,0 @@ -|null - */ - public $extra = null; - - public function jsonSerialize(){ - $result = (array) $this; - foreach($result as $k => $v){ - if($v === null){ - unset($result[$k]); - } - } - return $result; - } -}