ChatFormatting.java

net.minecraft.ChatFormatting

信息

  • 全限定名:net.minecraft.ChatFormatting
  • 类型:public enum
  • 包:net.minecraft
  • 源码路径:src/main/java/net/minecraft/ChatFormatting.java
  • 起始行号:L17
  • 实现:StringRepresentable
  • 职责:

    TODO

字段/常量

  • BLACK, DARK_BLUE, DARK_GREEN, DARK_AQUA, DARK_RED, DARK_PURPLE, GOLD, GRAY, DARK_GRAY, BLUE, GREEN, AQUA, RED, LIGHT_PURPLE, YELLOW, WHITE, true, true, true, true, true, null

    • 类型: ChatFormatting
    • 修饰符: package-private
    • 源码定位: L18
    • 说明:

      TODO

  • CODEC

    • 类型: Codec<ChatFormatting>
    • 修饰符: public static final
    • 源码定位: L41
    • 说明:

      TODO

  • COLOR_CODEC

    • 类型: Codec<ChatFormatting>
    • 修饰符: public static final
    • 源码定位: L42
    • 说明:

      TODO

  • PREFIX_CODE

    • 类型: char
    • 修饰符: public static final
    • 源码定位: L45
    • 说明:

      TODO

  • FORMATTING_BY_NAME

    • 类型: Map<String,ChatFormatting>
    • 修饰符: private static final
    • 源码定位: L46
    • 说明:

      TODO

  • STRIP_FORMATTING_PATTERN

    • 类型: Pattern
    • 修饰符: private static final
    • 源码定位: L48
    • 说明:

      TODO

  • name

    • 类型: String
    • 修饰符: private final
    • 源码定位: L49
    • 说明:

      TODO

  • code

    • 类型: char
    • 修饰符: private final
    • 源码定位: L50
    • 说明:

      TODO

  • isFormat

    • 类型: boolean
    • 修饰符: private final
    • 源码定位: L51
    • 说明:

      TODO

  • toString

    • 类型: String
    • 修饰符: private final
    • 源码定位: L52
    • 说明:

      TODO

  • id

    • 类型: int
    • 修饰符: private final
    • 源码定位: L53
    • 说明:

      TODO

  • color

    • 类型: Integer
    • 修饰符: private final
    • 源码定位: L54
    • 说明:

      TODO

内部类/嵌套类型

构造器

private ChatFormatting(String name, char code, int id, Integer color) @ L60

  • 构造器名:ChatFormatting
  • 源码定位:L60
  • 修饰符:private

参数:

  • name: String
  • code: char
  • id: int
  • color: Integer

说明:

TODO

private ChatFormatting(String name, char code, boolean isFormat) @ L64

  • 构造器名:ChatFormatting
  • 源码定位:L64
  • 修饰符:private

参数:

  • name: String
  • code: char
  • isFormat: boolean

说明:

TODO

private ChatFormatting(String name, char code, boolean isFormat, int id, Integer color) @ L68

  • 构造器名:ChatFormatting
  • 源码定位:L68
  • 修饰符:private

参数:

  • name: String
  • code: char
  • isFormat: boolean
  • id: int
  • color: Integer

说明:

TODO

方法

下面的方法块按源码顺序生成。

private static String cleanName(String name) @ L56

  • 方法名:cleanName
  • 源码定位:L56
  • 返回类型:String
  • 修饰符:private static

参数:

  • name: String

说明:

TODO

public char getChar() @ L77

  • 方法名:getChar
  • 源码定位:L77
  • 返回类型:char
  • 修饰符:public

参数:

说明:

TODO

public int getId() @ L81

  • 方法名:getId
  • 源码定位:L81
  • 返回类型:int
  • 修饰符:public

参数:

说明:

TODO

public boolean isFormat() @ L85

  • 方法名:isFormat
  • 源码定位:L85
  • 返回类型:boolean
  • 修饰符:public

参数:

说明:

TODO

public boolean isColor() @ L89

  • 方法名:isColor
  • 源码定位:L89
  • 返回类型:boolean
  • 修饰符:public

参数:

说明:

TODO

public Integer getColor() @ L93

  • 方法名:getColor
  • 源码定位:L93
  • 返回类型:Integer
  • 修饰符:public

参数:

说明:

TODO

public String getName() @ L97

  • 方法名:getName
  • 源码定位:L97
  • 返回类型:String
  • 修饰符:public

参数:

说明:

TODO

public String toString() @ L101

  • 方法名:toString
  • 源码定位:L101
  • 返回类型:String
  • 修饰符:public

参数:

说明:

TODO

public static String stripFormatting(String input) @ L106

  • 方法名:stripFormatting
  • 源码定位:L106
  • 返回类型:String
  • 修饰符:public static

参数:

  • input: String

说明:

TODO

public static ChatFormatting getByName(String name) @ L111

  • 方法名:getByName
  • 源码定位:L111
  • 返回类型:ChatFormatting
  • 修饰符:public static

参数:

  • name: String

说明:

TODO

public static ChatFormatting getById(int id) @ L115

  • 方法名:getById
  • 源码定位:L115
  • 返回类型:ChatFormatting
  • 修饰符:public static

参数:

  • id: int

说明:

TODO

public static ChatFormatting getByCode(char code) @ L129

  • 方法名:getByCode
  • 源码定位:L129
  • 返回类型:ChatFormatting
  • 修饰符:public static

参数:

  • code: char

说明:

TODO

public static Collection<String> getNames(boolean getColors, boolean getFormats) @ L141

  • 方法名:getNames
  • 源码定位:L141
  • 返回类型:Collection
  • 修饰符:public static

参数:

  • getColors: boolean
  • getFormats: boolean

说明:

TODO

public String getSerializedName() @ L153

  • 方法名:getSerializedName
  • 源码定位:L153
  • 返回类型:String
  • 修饰符:public

参数:

说明:

TODO

代码

public enum ChatFormatting implements StringRepresentable {
    BLACK("BLACK", '0', 0, 0),
    DARK_BLUE("DARK_BLUE", '1', 1, 170),
    DARK_GREEN("DARK_GREEN", '2', 2, 43520),
    DARK_AQUA("DARK_AQUA", '3', 3, 43690),
    DARK_RED("DARK_RED", '4', 4, 11141120),
    DARK_PURPLE("DARK_PURPLE", '5', 5, 11141290),
    GOLD("GOLD", '6', 6, 16755200),
    GRAY("GRAY", '7', 7, 11184810),
    DARK_GRAY("DARK_GRAY", '8', 8, 5592405),
    BLUE("BLUE", '9', 9, 5592575),
    GREEN("GREEN", 'a', 10, 5635925),
    AQUA("AQUA", 'b', 11, 5636095),
    RED("RED", 'c', 12, 16733525),
    LIGHT_PURPLE("LIGHT_PURPLE", 'd', 13, 16733695),
    YELLOW("YELLOW", 'e', 14, 16777045),
    WHITE("WHITE", 'f', 15, 16777215),
    OBFUSCATED("OBFUSCATED", 'k', true),
    BOLD("BOLD", 'l', true),
    STRIKETHROUGH("STRIKETHROUGH", 'm', true),
    UNDERLINE("UNDERLINE", 'n', true),
    ITALIC("ITALIC", 'o', true),
    RESET("RESET", 'r', -1, null);
 
    public static final Codec<ChatFormatting> CODEC = StringRepresentable.fromEnum(ChatFormatting::values);
    public static final Codec<ChatFormatting> COLOR_CODEC = CODEC.validate(
        color -> color.isFormat() ? DataResult.error(() -> "Formatting was not a valid color: " + color) : DataResult.success(color)
    );
    public static final char PREFIX_CODE = '\u00a7';
    private static final Map<String, ChatFormatting> FORMATTING_BY_NAME = Arrays.stream(values())
        .collect(Collectors.toMap(format -> cleanName(format.name), f -> (ChatFormatting)f));
    private static final Pattern STRIP_FORMATTING_PATTERN = Pattern.compile("(?i)\u00a7[0-9A-FK-OR]");
    private final String name;
    private final char code;
    private final boolean isFormat;
    private final String toString;
    private final int id;
    private final @Nullable Integer color;
 
    private static String cleanName(String name) {
        return name.toLowerCase(Locale.ROOT).replaceAll("[^a-z]", "");
    }
 
    private ChatFormatting(String name, char code, int id, @Nullable Integer color) {
        this(name, code, false, id, color);
    }
 
    private ChatFormatting(String name, char code, boolean isFormat) {
        this(name, code, isFormat, -1, null);
    }
 
    private ChatFormatting(String name, char code, boolean isFormat, int id, @Nullable Integer color) {
        this.name = name;
        this.code = code;
        this.isFormat = isFormat;
        this.id = id;
        this.color = color;
        this.toString = "\u00a7" + code;
    }
 
    public char getChar() {
        return this.code;
    }
 
    public int getId() {
        return this.id;
    }
 
    public boolean isFormat() {
        return this.isFormat;
    }
 
    public boolean isColor() {
        return !this.isFormat && this != RESET;
    }
 
    public @Nullable Integer getColor() {
        return this.color;
    }
 
    public String getName() {
        return this.name().toLowerCase(Locale.ROOT);
    }
 
    @Override
    public String toString() {
        return this.toString;
    }
 
    @Contract("!null->!null;_->_")
    public static @Nullable String stripFormatting(@Nullable String input) {
        return input == null ? null : STRIP_FORMATTING_PATTERN.matcher(input).replaceAll("");
    }
 
    public static @Nullable ChatFormatting getByName(@Nullable String name) {
        return name == null ? null : FORMATTING_BY_NAME.get(cleanName(name));
    }
 
    public static @Nullable ChatFormatting getById(int id) {
        if (id < 0) {
            return RESET;
        } else {
            for (ChatFormatting format : values()) {
                if (format.getId() == id) {
                    return format;
                }
            }
 
            return null;
        }
    }
 
    public static @Nullable ChatFormatting getByCode(char code) {
        char sanitized = Character.toLowerCase(code);
 
        for (ChatFormatting format : values()) {
            if (format.code == sanitized) {
                return format;
            }
        }
 
        return null;
    }
 
    public static Collection<String> getNames(boolean getColors, boolean getFormats) {
        List<String> result = Lists.newArrayList();
 
        for (ChatFormatting format : values()) {
            if ((!format.isColor() || getColors) && (!format.isFormat() || getFormats)) {
                result.add(format.getName());
            }
        }
 
        return result;
    }
 
    @Override
    public String getSerializedName() {
        return this.getName();
    }
}

引用的其他类

  • StringRepresentable

    • 引用位置: 实现/方法调用
    • 关联成员: StringRepresentable.fromEnum()
  • TropicalFish

    • 引用位置: 字段/方法调用
    • 关联成员: Pattern.compile()