TextFormat
Class used to handle Minecraft chat format, and convert it to other formats like HTML
Constants
AQUA
public const AQUA = \pocketmine\utils\TextFormat::ESCAPE . "b"BLACK
public const BLACK = \pocketmine\utils\TextFormat::ESCAPE . "0"BLUE
public const BLUE = \pocketmine\utils\TextFormat::ESCAPE . "9"BOLD
public const BOLD = \pocketmine\utils\TextFormat::ESCAPE . "l"COLORS
public const COLORS = [self::BLACK => self::BLACK, self::DARK_BLUE => self::DARK_BLUE, self::DARK_GREEN => self::DARK_GREEN, self::DARK_AQUA => self::DARK_AQUA, self::DARK_RED => self::DARK_RED, self::DARK_PURPLE => self::DARK_PURPLE, self::GOLD => self::GOLD, self::GRAY => self::GRAY, self::DARK_GRAY => self::DARK_GRAY, self::BLUE => self::BLUE, self::GREEN => self::GREEN, self::AQUA => self::AQU …DARK_AQUA
public const DARK_AQUA = \pocketmine\utils\TextFormat::ESCAPE . "3"DARK_BLUE
public const DARK_BLUE = \pocketmine\utils\TextFormat::ESCAPE . "1"DARK_GRAY
public const DARK_GRAY = \pocketmine\utils\TextFormat::ESCAPE . "8"DARK_GREEN
public const DARK_GREEN = \pocketmine\utils\TextFormat::ESCAPE . "2"DARK_PURPLE
public const DARK_PURPLE = \pocketmine\utils\TextFormat::ESCAPE . "5"DARK_RED
public const DARK_RED = \pocketmine\utils\TextFormat::ESCAPE . "4"EOL
public const EOL = "\n"ESCAPE
public const ESCAPE = "§"FORMATS
public const FORMATS = [self::OBFUSCATED => self::OBFUSCATED, self::BOLD => self::BOLD, self::ITALIC => self::ITALIC]GOLD
public const GOLD = \pocketmine\utils\TextFormat::ESCAPE . "6"GRAY
public const GRAY = \pocketmine\utils\TextFormat::ESCAPE . "7"GREEN
public const GREEN = \pocketmine\utils\TextFormat::ESCAPE . "a"ITALIC
public const ITALIC = \pocketmine\utils\TextFormat::ESCAPE . "o"LIGHT_PURPLE
public const LIGHT_PURPLE = \pocketmine\utils\TextFormat::ESCAPE . "d"MATERIAL_AMETHYST
public const MATERIAL_AMETHYST = \pocketmine\utils\TextFormat::ESCAPE . "u"MATERIAL_COPPER
public const MATERIAL_COPPER = \pocketmine\utils\TextFormat::ESCAPE . "n"MATERIAL_DIAMOND
public const MATERIAL_DIAMOND = \pocketmine\utils\TextFormat::ESCAPE . "s"MATERIAL_EMERALD
public const MATERIAL_EMERALD = \pocketmine\utils\TextFormat::ESCAPE . "q"MATERIAL_GOLD
public const MATERIAL_GOLD = \pocketmine\utils\TextFormat::ESCAPE . "p"MATERIAL_IRON
public const MATERIAL_IRON = \pocketmine\utils\TextFormat::ESCAPE . "i"MATERIAL_LAPIS
public const MATERIAL_LAPIS = \pocketmine\utils\TextFormat::ESCAPE . "t"MATERIAL_NETHERITE
public const MATERIAL_NETHERITE = \pocketmine\utils\TextFormat::ESCAPE . "j"MATERIAL_QUARTZ
public const MATERIAL_QUARTZ = \pocketmine\utils\TextFormat::ESCAPE . "h"MATERIAL_REDSTONE
public const MATERIAL_REDSTONE = \pocketmine\utils\TextFormat::ESCAPE . "m"MATERIAL_RESIN
public const MATERIAL_RESIN = \pocketmine\utils\TextFormat::ESCAPE . "v"MINECOIN_GOLD
public const MINECOIN_GOLD = \pocketmine\utils\TextFormat::ESCAPE . "g"OBFUSCATED
public const OBFUSCATED = \pocketmine\utils\TextFormat::ESCAPE . "k"RED
public const RED = \pocketmine\utils\TextFormat::ESCAPE . "c"RESET
public const RESET = \pocketmine\utils\TextFormat::ESCAPE . "r"STRIKETHROUGH
public const STRIKETHROUGH = ""Deprecated
Deprecated.
UNDERLINE
public const UNDERLINE = ""Deprecated
Deprecated.
WHITE
public const WHITE = \pocketmine\utils\TextFormat::ESCAPE . "f"YELLOW
public const YELLOW = \pocketmine\utils\TextFormat::ESCAPE . "e"Methods
addBase()
public static function addBase(string $baseFormat, string $string) : stringAdds 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.
clean()
public static function clean(string $string, bool $removeFormat = true) : stringCleans the string from Minecraft codes, ANSI Escape Codes and invalid UTF-8 characters
colorize()
public static function colorize(string $string, string $placeholder = "&") : stringReplaces placeholders of § with the correct character. Only valid codes (as in the constants of the TextFormat class) will be converted.
javaToBedrock()
public static function javaToBedrock(string $string) : stringConverts 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.
toHTML()
public static function toHTML(string $string) : stringReturns an HTML-formatted string with colors/markup
tokenize()
public static function tokenize(string $string) : arraySplits the string by Format tokens
