Fields & Constants (4)
PREFIX_CODE
STRIP_FORMATTING_PATTERN
code
toString
Full Source
ChatFormatting.java63 lines · Minecraft 26.3 · verbatim
| 1 | package net.minecraft;
|
| 2 |
|
| 3 | import java.util.regex.Pattern;
|
| 4 | import org.jetbrains.annotations.Contract;
|
| 5 | import org.jspecify.annotations.Nullable;
|
| 6 |
|
| 7 | public enum ChatFormatting {
|
| 8 | BLACK('0'),
|
| 9 | DARK_BLUE('1'),
|
| 10 | DARK_GREEN('2'),
|
| 11 | DARK_AQUA('3'),
|
| 12 | DARK_RED('4'),
|
| 13 | DARK_PURPLE('5'),
|
| 14 | GOLD('6'),
|
| 15 | GRAY('7'),
|
| 16 | DARK_GRAY('8'),
|
| 17 | BLUE('9'),
|
| 18 | GREEN('a'),
|
| 19 | AQUA('b'),
|
| 20 | RED('c'),
|
| 21 | LIGHT_PURPLE('d'),
|
| 22 | YELLOW('e'),
|
| 23 | WHITE('f'),
|
| 24 | OBFUSCATED('k'),
|
| 25 | BOLD('l'),
|
| 26 | STRIKETHROUGH('m'),
|
| 27 | UNDERLINE('n'),
|
| 28 | ITALIC('o'),
|
| 29 | RESET('r');
|
| 30 |
|
| 31 | public static final char PREFIX_CODE = '§';
|
| 32 | private static final Pattern STRIP_FORMATTING_PATTERN = Pattern.compile("(?i)§[0-9A-FK-OR]");
|
| 33 | private final char code;
|
| 34 | private final String toString;
|
| 35 |
|
| 36 | ChatFormatting(final char code) {
|
| 37 | this.code = code;
|
| 38 | this.toString = "§" + code;
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | public String toString() {
|
| 43 | return this.toString;
|
| 44 | }
|
| 45 |
|
| 46 | @Contract("!null->!null;_->_")
|
| 47 | public static @Nullable String stripFormatting(final @Nullable String input) {
|
| 48 | return input == null ? null : STRIP_FORMATTING_PATTERN.matcher(input).replaceAll("");
|
| 49 | }
|
| 50 |
|
| 51 | public static @Nullable ChatFormatting getByCode(final char code) {
|
| 52 | char sanitized = Character.toLowerCase(code);
|
| 53 |
|
| 54 | for (ChatFormatting format : values()) {
|
| 55 | if (format.code == sanitized) {
|
| 56 | return format;
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | return null;
|
| 61 | }
|
| 62 | }
|
| 63 |
|
Decompiled Minecraft 26.3 source of ChatFormatting (net/minecraft/ChatFormatting.java) with Mojang official mappings, stored verbatim on the site.