Merge 'minor-next' into 'major-next'

Automatic merge performed by: https://github.com/pmmp/RestrictedActions/actions/runs/12268005529
This commit is contained in:
pmmp-admin-bot[bot] 2024-12-11 01:40:03 +00:00
commit 851bbd7384
2 changed files with 130 additions and 116 deletions

View File

@ -59,6 +59,16 @@ abstract class Terminal{
public static string $COLOR_YELLOW = ""; public static string $COLOR_YELLOW = "";
public static string $COLOR_WHITE = ""; public static string $COLOR_WHITE = "";
public static string $COLOR_MINECOIN_GOLD = ""; 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; private static ?bool $formattingCodes = null;
@ -111,6 +121,16 @@ abstract class Terminal{
self::$COLOR_YELLOW = $color(227); self::$COLOR_YELLOW = $color(227);
self::$COLOR_WHITE = $color(231); self::$COLOR_WHITE = $color(231);
self::$COLOR_MINECOIN_GOLD = $color(184); 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{ protected static function getEscapeCodes() : void{
@ -144,15 +164,25 @@ abstract class Terminal{
self::$COLOR_YELLOW = $colors >= 256 ? $setaf(227) : $setaf(11); self::$COLOR_YELLOW = $colors >= 256 ? $setaf(227) : $setaf(11);
self::$COLOR_WHITE = $colors >= 256 ? $setaf(231) : $setaf(15); self::$COLOR_WHITE = $colors >= 256 ? $setaf(231) : $setaf(15);
self::$COLOR_MINECOIN_GOLD = $colors >= 256 ? $setaf(184) : $setaf(11); 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{ }else{
self::$COLOR_BLACK = self::$COLOR_DARK_GRAY = $setaf(0); self::$COLOR_BLACK = self::$COLOR_DARK_GRAY = self::$COLOR_MATERIAL_NETHERITE = $setaf(0);
self::$COLOR_RED = self::$COLOR_DARK_RED = $setaf(1); self::$COLOR_RED = self::$COLOR_DARK_RED = self::$COLOR_MATERIAL_REDSTONE = self::$COLOR_MATERIAL_COPPER = $setaf(1);
self::$COLOR_GREEN = self::$COLOR_DARK_GREEN = $setaf(2); self::$COLOR_GREEN = self::$COLOR_DARK_GREEN = self::$COLOR_MATERIAL_EMERALD = $setaf(2);
self::$COLOR_YELLOW = self::$COLOR_GOLD = self::$COLOR_MINECOIN_GOLD = $setaf(3); self::$COLOR_YELLOW = self::$COLOR_GOLD = self::$COLOR_MINECOIN_GOLD = self::$COLOR_MATERIAL_GOLD = $setaf(3);
self::$COLOR_BLUE = self::$COLOR_DARK_BLUE = $setaf(4); self::$COLOR_BLUE = self::$COLOR_DARK_BLUE = self::$COLOR_MATERIAL_LAPIS = $setaf(4);
self::$COLOR_LIGHT_PURPLE = self::$COLOR_PURPLE = $setaf(5); self::$COLOR_LIGHT_PURPLE = self::$COLOR_PURPLE = self::$COLOR_MATERIAL_AMETHYST = $setaf(5);
self::$COLOR_AQUA = self::$COLOR_DARK_AQUA = $setaf(6); self::$COLOR_AQUA = self::$COLOR_DARK_AQUA = self::$COLOR_MATERIAL_DIAMOND = $setaf(6);
self::$COLOR_GRAY = self::$COLOR_WHITE = $setaf(7); 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{ public static function toANSI(string $string) : string{
$newString = ""; $newString = "";
foreach(TextFormat::tokenize($string) as $token){ foreach(TextFormat::tokenize($string) as $token){
$newString .= match($token){ $newString .= match ($token) {
TextFormat::BOLD => Terminal::$FORMAT_BOLD, TextFormat::BOLD => Terminal::$FORMAT_BOLD,
TextFormat::OBFUSCATED => Terminal::$FORMAT_OBFUSCATED, TextFormat::OBFUSCATED => Terminal::$FORMAT_OBFUSCATED,
TextFormat::ITALIC => Terminal::$FORMAT_ITALIC, TextFormat::ITALIC => Terminal::$FORMAT_ITALIC,
TextFormat::UNDERLINE => Terminal::$FORMAT_UNDERLINE,
TextFormat::STRIKETHROUGH => Terminal::$FORMAT_STRIKETHROUGH,
TextFormat::RESET => Terminal::$FORMAT_RESET, TextFormat::RESET => Terminal::$FORMAT_RESET,
TextFormat::BLACK => Terminal::$COLOR_BLACK, TextFormat::BLACK => Terminal::$COLOR_BLACK,
TextFormat::DARK_BLUE => Terminal::$COLOR_DARK_BLUE, TextFormat::DARK_BLUE => Terminal::$COLOR_DARK_BLUE,
@ -215,6 +243,16 @@ abstract class Terminal{
TextFormat::YELLOW => Terminal::$COLOR_YELLOW, TextFormat::YELLOW => Terminal::$COLOR_YELLOW,
TextFormat::WHITE => Terminal::$COLOR_WHITE, TextFormat::WHITE => Terminal::$COLOR_WHITE,
TextFormat::MINECOIN_GOLD => Terminal::$COLOR_MINECOIN_GOLD, 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, default => $token,
}; };
} }

View File

@ -63,6 +63,16 @@ abstract class TextFormat{
public const YELLOW = TextFormat::ESCAPE . "e"; public const YELLOW = TextFormat::ESCAPE . "e";
public const WHITE = TextFormat::ESCAPE . "f"; public const WHITE = TextFormat::ESCAPE . "f";
public const MINECOIN_GOLD = TextFormat::ESCAPE . "g"; 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 = [ public const COLORS = [
self::BLACK => self::BLACK, self::BLACK => self::BLACK,
@ -82,19 +92,29 @@ abstract class TextFormat{
self::YELLOW => self::YELLOW, self::YELLOW => self::YELLOW,
self::WHITE => self::WHITE, self::WHITE => self::WHITE,
self::MINECOIN_GOLD => self::MINECOIN_GOLD, 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 OBFUSCATED = TextFormat::ESCAPE . "k";
public const BOLD = TextFormat::ESCAPE . "l"; public const BOLD = TextFormat::ESCAPE . "l";
public const STRIKETHROUGH = TextFormat::ESCAPE . "m"; /** @deprecated */
public const UNDERLINE = TextFormat::ESCAPE . "n"; public const STRIKETHROUGH = "";
/** @deprecated */
public const UNDERLINE = "";
public const ITALIC = TextFormat::ESCAPE . "o"; public const ITALIC = TextFormat::ESCAPE . "o";
public const FORMATS = [ public const FORMATS = [
self::OBFUSCATED => self::OBFUSCATED, self::OBFUSCATED => self::OBFUSCATED,
self::BOLD => self::BOLD, self::BOLD => self::BOLD,
self::STRIKETHROUGH => self::STRIKETHROUGH,
self::UNDERLINE => self::UNDERLINE,
self::ITALIC => self::ITALIC, self::ITALIC => self::ITALIC,
]; ];
@ -130,7 +150,7 @@ abstract class TextFormat{
* @return string[] * @return string[]
*/ */
public static function tokenize(string $string) : array{ 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(); if($result === false) throw self::makePcreError();
return $result; return $result;
} }
@ -144,7 +164,7 @@ abstract class TextFormat{
$string = mb_scrub($string, 'UTF-8'); $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) $string = self::preg_replace("/[\x{E000}-\x{F8FF}]/u", "", $string); //remove unicode private-use-area characters (they might break the console)
if($removeFormat){ 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)); return str_replace("\x1b", "", self::preg_replace("/\x1b[\\(\\][[0-9;\\[\\(]+[Bm]/u", "", $string));
} }
@ -155,7 +175,7 @@ abstract class TextFormat{
* @param string $placeholder default "&" * @param string $placeholder default "&"
*/ */
public static function colorize(string $string, string $placeholder = "&") : string{ 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); 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 * Returns an HTML-formatted string with colors/markup
*/ */
@ -190,104 +224,46 @@ abstract class TextFormat{
$newString = ""; $newString = "";
$tokens = 0; $tokens = 0;
foreach(self::tokenize($string) as $token){ foreach(self::tokenize($string) as $token){
switch($token){ $formatString = match($token){
case TextFormat::BOLD: TextFormat::BLACK => "color:#000",
$newString .= "<span style=font-weight:bold>"; TextFormat::DARK_BLUE => "color:#00A",
++$tokens; TextFormat::DARK_GREEN => "color:#0A0",
break; TextFormat::DARK_AQUA => "color:#0AA",
case TextFormat::OBFUSCATED: TextFormat::DARK_RED => "color:#A00",
//$newString .= "<span style=text-decoration:line-through>"; TextFormat::DARK_PURPLE => "color:#A0A",
//++$tokens; TextFormat::GOLD => "color:#FA0",
break; TextFormat::GRAY => "color:#AAA",
case TextFormat::ITALIC: TextFormat::DARK_GRAY => "color:#555",
$newString .= "<span style=font-style:italic>"; TextFormat::BLUE => "color:#55F",
++$tokens; TextFormat::GREEN => "color:#5F5",
break; TextFormat::AQUA => "color:#5FF",
case TextFormat::UNDERLINE: TextFormat::RED => "color:#F55",
$newString .= "<span style=text-decoration:underline>"; TextFormat::LIGHT_PURPLE => "color:#F5F",
++$tokens; TextFormat::YELLOW => "color:#FF5",
break; TextFormat::WHITE => "color:#FFF",
case TextFormat::STRIKETHROUGH: TextFormat::MINECOIN_GOLD => "color:#dd0",
$newString .= "<span style=text-decoration:line-through>"; TextFormat::MATERIAL_QUARTZ => "color:#e2d3d1",
++$tokens; TextFormat::MATERIAL_IRON => "color:#cec9c9",
break; TextFormat::MATERIAL_NETHERITE => "color:#44393a",
case TextFormat::RESET: TextFormat::MATERIAL_REDSTONE => "color:#961506",
$newString .= str_repeat("</span>", $tokens); TextFormat::MATERIAL_COPPER => "color:#b4684d",
$tokens = 0; TextFormat::MATERIAL_GOLD => "color:#deb02c",
break; TextFormat::MATERIAL_EMERALD => "color:#119f36",
TextFormat::MATERIAL_DIAMOND => "color:#2cb9a8",
//Colors TextFormat::MATERIAL_LAPIS => "color:#20487a",
case TextFormat::BLACK: TextFormat::MATERIAL_AMETHYST => "color:#9a5cc5",
$newString .= "<span style=color:#000>"; TextFormat::BOLD => "font-weight:bold",
++$tokens; TextFormat::ITALIC => "font-style:italic",
break; default => null
case TextFormat::DARK_BLUE: };
$newString .= "<span style=color:#00A>"; if($formatString !== null){
++$tokens; $newString .= "<span style=\"$formatString\">";
break; ++$tokens;
case TextFormat::DARK_GREEN: }elseif($token === TextFormat::RESET){
$newString .= "<span style=color:#0A0>"; $newString .= str_repeat("</span>", $tokens);
++$tokens; $tokens = 0;
break; }else{
case TextFormat::DARK_AQUA: $newString .= $token;
$newString .= "<span style=color:#0AA>";
++$tokens;
break;
case TextFormat::DARK_RED:
$newString .= "<span style=color:#A00>";
++$tokens;
break;
case TextFormat::DARK_PURPLE:
$newString .= "<span style=color:#A0A>";
++$tokens;
break;
case TextFormat::GOLD:
$newString .= "<span style=color:#FA0>";
++$tokens;
break;
case TextFormat::GRAY:
$newString .= "<span style=color:#AAA>";
++$tokens;
break;
case TextFormat::DARK_GRAY:
$newString .= "<span style=color:#555>";
++$tokens;
break;
case TextFormat::BLUE:
$newString .= "<span style=color:#55F>";
++$tokens;
break;
case TextFormat::GREEN:
$newString .= "<span style=color:#5F5>";
++$tokens;
break;
case TextFormat::AQUA:
$newString .= "<span style=color:#5FF>";
++$tokens;
break;
case TextFormat::RED:
$newString .= "<span style=color:#F55>";
++$tokens;
break;
case TextFormat::LIGHT_PURPLE:
$newString .= "<span style=color:#F5F>";
++$tokens;
break;
case TextFormat::YELLOW:
$newString .= "<span style=color:#FF5>";
++$tokens;
break;
case TextFormat::WHITE:
$newString .= "<span style=color:#FFF>";
++$tokens;
break;
case TextFormat::MINECOIN_GOLD:
$newString .= "<span style=color:#dd0>";
++$tokens;
break;
default:
$newString .= $token;
break;
} }
} }