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).
This commit is contained in:
Dylan K. Taylor 2021-08-12 13:43:58 +01:00
parent eb24e040f3
commit 4d683c63d8
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 0 additions and 252 deletions

View File

@ -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
*

View File

@ -1,60 +0,0 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\utils;
/**
* @internal
* @see TextFormat::toJSON()
*/
final class TextFormatJsonObject implements \JsonSerializable{
/** @var string|null */
public $text = null;
/** @var string|null */
public $color = null;
/** @var bool|null */
public $bold = null;
/** @var bool|null */
public $italic = null;
/** @var bool|null */
public $underlined = null;
/** @var bool|null */
public $strikethrough = null;
/** @var bool|null */
public $obfuscated = null;
/**
* @var TextFormatJsonObject[]|null
* @phpstan-var array<int, TextFormatJsonObject>|null
*/
public $extra = null;
public function jsonSerialize(){
$result = (array) $this;
foreach($result as $k => $v){
if($v === null){
unset($result[$k]);
}
}
return $result;
}
}