TextFormat: Added new material colours for armor trims (#5838)

Unfortunately, these new formatting codes conflict with the Java strikethrough and underline, so we can't support these anymore.

A TextFormat::javaToBedrock() is provided to strip these codes, or (if these formats become supported via different codes) to convert them to Bedrock variants.

Co-authored-by: Dylan T. <dktapps@pmmp.io>
This commit is contained in:
Maxence 2024-12-10 14:40:03 +01:00 committed by GitHub
parent 67b9d6222d
commit 6817215683
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 130 additions and 26 deletions

View File

@ -59,6 +59,16 @@ abstract class Terminal{
public static string $COLOR_YELLOW = "";
public static string $COLOR_WHITE = "";
public static string $COLOR_MINECOIN_GOLD = "";
public static string $COLOR_MATERIAL_QUARTZ = "";
public static string $COLOR_MATERIAL_IRON = "";
public static string $COLOR_MATERIAL_NETHERITE = "";
public static string $COLOR_MATERIAL_REDSTONE = "";
public static string $COLOR_MATERIAL_COPPER = "";
public static string $COLOR_MATERIAL_GOLD = "";
public static string $COLOR_MATERIAL_EMERALD = "";
public static string $COLOR_MATERIAL_DIAMOND = "";
public static string $COLOR_MATERIAL_LAPIS = "";
public static string $COLOR_MATERIAL_AMETHYST = "";
private static ?bool $formattingCodes = null;
@ -111,6 +121,16 @@ abstract class Terminal{
self::$COLOR_YELLOW = $color(227);
self::$COLOR_WHITE = $color(231);
self::$COLOR_MINECOIN_GOLD = $color(184);
self::$COLOR_MATERIAL_QUARTZ = $color(188);
self::$COLOR_MATERIAL_IRON = $color(251);
self::$COLOR_MATERIAL_NETHERITE = $color(237);
self::$COLOR_MATERIAL_REDSTONE = $color(88);
self::$COLOR_MATERIAL_COPPER = $color(131);
self::$COLOR_MATERIAL_GOLD = $color(178);
self::$COLOR_MATERIAL_EMERALD = $color(35);
self::$COLOR_MATERIAL_DIAMOND = $color(37);
self::$COLOR_MATERIAL_LAPIS = $color(24);
self::$COLOR_MATERIAL_AMETHYST = $color(98);
}
protected static function getEscapeCodes() : void{
@ -144,15 +164,25 @@ abstract class Terminal{
self::$COLOR_YELLOW = $colors >= 256 ? $setaf(227) : $setaf(11);
self::$COLOR_WHITE = $colors >= 256 ? $setaf(231) : $setaf(15);
self::$COLOR_MINECOIN_GOLD = $colors >= 256 ? $setaf(184) : $setaf(11);
self::$COLOR_MATERIAL_QUARTZ = $colors >= 256 ? $setaf(188) : $setaf(7);
self::$COLOR_MATERIAL_IRON = $colors >= 256 ? $setaf(251) : $setaf(7);
self::$COLOR_MATERIAL_NETHERITE = $colors >= 256 ? $setaf(237) : $setaf(1);
self::$COLOR_MATERIAL_REDSTONE = $colors >= 256 ? $setaf(88) : $setaf(9);
self::$COLOR_MATERIAL_COPPER = $colors >= 256 ? $setaf(131) : $setaf(3);
self::$COLOR_MATERIAL_GOLD = $colors >= 256 ? $setaf(178) : $setaf(11);
self::$COLOR_MATERIAL_EMERALD = $colors >= 256 ? $setaf(35) : $setaf(2);
self::$COLOR_MATERIAL_DIAMOND = $colors >= 256 ? $setaf(37) : $setaf(14);
self::$COLOR_MATERIAL_LAPIS = $colors >= 256 ? $setaf(24) : $setaf(12);
self::$COLOR_MATERIAL_AMETHYST = $colors >= 256 ? $setaf(98) : $setaf(13);
}else{
self::$COLOR_BLACK = self::$COLOR_DARK_GRAY = $setaf(0);
self::$COLOR_RED = self::$COLOR_DARK_RED = $setaf(1);
self::$COLOR_GREEN = self::$COLOR_DARK_GREEN = $setaf(2);
self::$COLOR_YELLOW = self::$COLOR_GOLD = self::$COLOR_MINECOIN_GOLD = $setaf(3);
self::$COLOR_BLUE = self::$COLOR_DARK_BLUE = $setaf(4);
self::$COLOR_LIGHT_PURPLE = self::$COLOR_PURPLE = $setaf(5);
self::$COLOR_AQUA = self::$COLOR_DARK_AQUA = $setaf(6);
self::$COLOR_GRAY = self::$COLOR_WHITE = $setaf(7);
self::$COLOR_BLACK = self::$COLOR_DARK_GRAY = self::$COLOR_MATERIAL_NETHERITE = $setaf(0);
self::$COLOR_RED = self::$COLOR_DARK_RED = self::$COLOR_MATERIAL_REDSTONE = self::$COLOR_MATERIAL_COPPER = $setaf(1);
self::$COLOR_GREEN = self::$COLOR_DARK_GREEN = self::$COLOR_MATERIAL_EMERALD = $setaf(2);
self::$COLOR_YELLOW = self::$COLOR_GOLD = self::$COLOR_MINECOIN_GOLD = self::$COLOR_MATERIAL_GOLD = $setaf(3);
self::$COLOR_BLUE = self::$COLOR_DARK_BLUE = self::$COLOR_MATERIAL_LAPIS = $setaf(4);
self::$COLOR_LIGHT_PURPLE = self::$COLOR_PURPLE = self::$COLOR_MATERIAL_AMETHYST = $setaf(5);
self::$COLOR_AQUA = self::$COLOR_DARK_AQUA = self::$COLOR_MATERIAL_DIAMOND = $setaf(6);
self::$COLOR_GRAY = self::$COLOR_WHITE = self::$COLOR_MATERIAL_QUARTZ = self::$COLOR_MATERIAL_IRON = $setaf(7);
}
}
@ -191,12 +221,10 @@ abstract class Terminal{
public static function toANSI(string $string) : string{
$newString = "";
foreach(TextFormat::tokenize($string) as $token){
$newString .= match($token){
$newString .= match ($token) {
TextFormat::BOLD => Terminal::$FORMAT_BOLD,
TextFormat::OBFUSCATED => Terminal::$FORMAT_OBFUSCATED,
TextFormat::ITALIC => Terminal::$FORMAT_ITALIC,
TextFormat::UNDERLINE => Terminal::$FORMAT_UNDERLINE,
TextFormat::STRIKETHROUGH => Terminal::$FORMAT_STRIKETHROUGH,
TextFormat::RESET => Terminal::$FORMAT_RESET,
TextFormat::BLACK => Terminal::$COLOR_BLACK,
TextFormat::DARK_BLUE => Terminal::$COLOR_DARK_BLUE,
@ -215,6 +243,16 @@ abstract class Terminal{
TextFormat::YELLOW => Terminal::$COLOR_YELLOW,
TextFormat::WHITE => Terminal::$COLOR_WHITE,
TextFormat::MINECOIN_GOLD => Terminal::$COLOR_MINECOIN_GOLD,
TextFormat::MATERIAL_QUARTZ => Terminal::$COLOR_MATERIAL_QUARTZ,
TextFormat::MATERIAL_IRON => Terminal::$COLOR_MATERIAL_IRON,
TextFormat::MATERIAL_NETHERITE => Terminal::$COLOR_MATERIAL_NETHERITE,
TextFormat::MATERIAL_REDSTONE => Terminal::$COLOR_MATERIAL_REDSTONE,
TextFormat::MATERIAL_COPPER => Terminal::$COLOR_MATERIAL_COPPER,
TextFormat::MATERIAL_GOLD => Terminal::$COLOR_MATERIAL_GOLD,
TextFormat::MATERIAL_EMERALD => Terminal::$COLOR_MATERIAL_EMERALD,
TextFormat::MATERIAL_DIAMOND => Terminal::$COLOR_MATERIAL_DIAMOND,
TextFormat::MATERIAL_LAPIS => Terminal::$COLOR_MATERIAL_LAPIS,
TextFormat::MATERIAL_AMETHYST => Terminal::$COLOR_MATERIAL_AMETHYST,
default => $token,
};
}

View File

@ -63,6 +63,16 @@ abstract class TextFormat{
public const YELLOW = TextFormat::ESCAPE . "e";
public const WHITE = TextFormat::ESCAPE . "f";
public const MINECOIN_GOLD = TextFormat::ESCAPE . "g";
public const MATERIAL_QUARTZ = TextFormat::ESCAPE . "h";
public const MATERIAL_IRON = TextFormat::ESCAPE . "i";
public const MATERIAL_NETHERITE = TextFormat::ESCAPE . "j";
public const MATERIAL_REDSTONE = TextFormat::ESCAPE . "m";
public const MATERIAL_COPPER = TextFormat::ESCAPE . "n";
public const MATERIAL_GOLD = TextFormat::ESCAPE . "p";
public const MATERIAL_EMERALD = TextFormat::ESCAPE . "q";
public const MATERIAL_DIAMOND = TextFormat::ESCAPE . "s";
public const MATERIAL_LAPIS = TextFormat::ESCAPE . "t";
public const MATERIAL_AMETHYST = TextFormat::ESCAPE . "u";
public const COLORS = [
self::BLACK => self::BLACK,
@ -82,19 +92,29 @@ abstract class TextFormat{
self::YELLOW => self::YELLOW,
self::WHITE => self::WHITE,
self::MINECOIN_GOLD => self::MINECOIN_GOLD,
self::MATERIAL_QUARTZ => self::MATERIAL_QUARTZ,
self::MATERIAL_IRON => self::MATERIAL_IRON,
self::MATERIAL_NETHERITE => self::MATERIAL_NETHERITE,
self::MATERIAL_REDSTONE => self::MATERIAL_REDSTONE,
self::MATERIAL_COPPER => self::MATERIAL_COPPER,
self::MATERIAL_GOLD => self::MATERIAL_GOLD,
self::MATERIAL_EMERALD => self::MATERIAL_EMERALD,
self::MATERIAL_DIAMOND => self::MATERIAL_DIAMOND,
self::MATERIAL_LAPIS => self::MATERIAL_LAPIS,
self::MATERIAL_AMETHYST => self::MATERIAL_AMETHYST,
];
public const OBFUSCATED = TextFormat::ESCAPE . "k";
public const BOLD = TextFormat::ESCAPE . "l";
public const STRIKETHROUGH = TextFormat::ESCAPE . "m";
public const UNDERLINE = TextFormat::ESCAPE . "n";
/** @deprecated */
public const STRIKETHROUGH = "";
/** @deprecated */
public const UNDERLINE = "";
public const ITALIC = TextFormat::ESCAPE . "o";
public const FORMATS = [
self::OBFUSCATED => self::OBFUSCATED,
self::BOLD => self::BOLD,
self::STRIKETHROUGH => self::STRIKETHROUGH,
self::UNDERLINE => self::UNDERLINE,
self::ITALIC => self::ITALIC,
];
@ -130,7 +150,7 @@ abstract class TextFormat{
* @return string[]
*/
public static function tokenize(string $string) : array{
$result = preg_split("/(" . TextFormat::ESCAPE . "[0-9a-gk-or])/u", $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
$result = preg_split("/(" . TextFormat::ESCAPE . "[0-9a-u])/u", $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
if($result === false) throw self::makePcreError();
return $result;
}
@ -144,7 +164,7 @@ abstract class TextFormat{
$string = mb_scrub($string, 'UTF-8');
$string = self::preg_replace("/[\x{E000}-\x{F8FF}]/u", "", $string); //remove unicode private-use-area characters (they might break the console)
if($removeFormat){
$string = str_replace(TextFormat::ESCAPE, "", self::preg_replace("/" . TextFormat::ESCAPE . "[0-9a-gk-or]/u", "", $string));
$string = str_replace(TextFormat::ESCAPE, "", self::preg_replace("/" . TextFormat::ESCAPE . "[0-9a-u]/u", "", $string));
}
return str_replace("\x1b", "", self::preg_replace("/\x1b[\\(\\][[0-9;\\[\\(]+[Bm]/u", "", $string));
}
@ -155,7 +175,7 @@ abstract class TextFormat{
* @param string $placeholder default "&"
*/
public static function colorize(string $string, string $placeholder = "&") : string{
return self::preg_replace('/' . preg_quote($placeholder, "/") . '([0-9a-gk-or])/u', TextFormat::ESCAPE . '$1', $string);
return self::preg_replace('/' . preg_quote($placeholder, "/") . '([0-9a-u])/u', TextFormat::ESCAPE . '$1', $string);
}
/**
@ -183,6 +203,20 @@ abstract class TextFormat{
return $baseFormat . str_replace(TextFormat::RESET, $baseFormat, $string);
}
/**
* Converts any Java formatting codes in the given string to Bedrock.
*
* As of 1.21.50, strikethrough (§m) and underline (§n) are not supported by Bedrock, and these symbols are instead
* used to represent additional colours in Bedrock. To avoid unintended formatting, this function currently strips
* those formatting codes to prevent unintended colour display in formatted text.
*
* If Bedrock starts to support these formats in the future, this function will be updated to translate them rather
* than removing them.
*/
public static function javaToBedrock(string $string) : string{
return str_replace([TextFormat::ESCAPE . "m", TextFormat::ESCAPE . "n"], "", $string);
}
/**
* Returns an HTML-formatted string with colors/markup
*/
@ -203,14 +237,6 @@ abstract class TextFormat{
$newString .= "<span style=font-style:italic>";
++$tokens;
break;
case TextFormat::UNDERLINE:
$newString .= "<span style=text-decoration:underline>";
++$tokens;
break;
case TextFormat::STRIKETHROUGH:
$newString .= "<span style=text-decoration:line-through>";
++$tokens;
break;
case TextFormat::RESET:
$newString .= str_repeat("</span>", $tokens);
$tokens = 0;
@ -285,6 +311,46 @@ abstract class TextFormat{
$newString .= "<span style=color:#dd0>";
++$tokens;
break;
case TextFormat::MATERIAL_QUARTZ:
$newString .= "<span style=color:#e2d3d1>";
++$tokens;
break;
case TextFormat::MATERIAL_IRON:
$newString .= "<span style=color:#cec9c9>";
++$tokens;
break;
case TextFormat::MATERIAL_NETHERITE:
$newString .= "<span style=color:#44393a>";
++$tokens;
break;
case TextFormat::MATERIAL_REDSTONE:
$newString .= "<span style=color:#961506>";
++$tokens;
break;
case TextFormat::MATERIAL_COPPER:
$newString .= "<span style=color:#b4684d>";
++$tokens;
break;
case TextFormat::MATERIAL_GOLD:
$newString .= "<span style=color:#deb02c>";
++$tokens;
break;
case TextFormat::MATERIAL_EMERALD:
$newString .= "<span style=color:#119f36>";
++$tokens;
break;
case TextFormat::MATERIAL_DIAMOND:
$newString .= "<span style=color:#2cb9a8>";
++$tokens;
break;
case TextFormat::MATERIAL_LAPIS:
$newString .= "<span style=color:#20487a>";
++$tokens;
break;
case TextFormat::MATERIAL_AMETHYST:
$newString .= "<span style=color:#9a5cc5>";
++$tokens;
break;
default:
$newString .= $token;
break;