TextColor.java

net.minecraft.network.chat.TextColor

信息

  • 全限定名:net.minecraft.network.chat.TextColor
  • 类型:public final class
  • 包:net.minecraft.network.chat
  • 源码路径:src/main/java/net/minecraft/network/chat/TextColor.java
  • 起始行号:L15
  • 职责:

    TODO

字段/常量

  • CUSTOM_COLOR_PREFIX

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

      TODO

  • CODEC

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

      TODO

  • LEGACY_FORMAT_TO_COLOR

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

      TODO

  • NAMED_COLORS

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

      TODO

  • value

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

      TODO

  • name

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

      TODO

内部类/嵌套类型

构造器

private TextColor(int value, String name) @ L27

  • 构造器名:TextColor
  • 源码定位:L27
  • 修饰符:private

参数:

  • value: int
  • name: String

说明:

TODO

private TextColor(int value) @ L32

  • 构造器名:TextColor
  • 源码定位:L32
  • 修饰符:private

参数:

  • value: int

说明:

TODO

方法

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

public int getValue() @ L37

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

参数:

说明:

TODO

public String serialize() @ L41

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

参数:

说明:

TODO

private String formatValue() @ L45

  • 方法名:formatValue
  • 源码定位:L45
  • 返回类型:String
  • 修饰符:private

参数:

说明:

TODO

public boolean equals(Object o) @ L49

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

参数:

  • o: Object

说明:

TODO

public int hashCode() @ L61

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

参数:

说明:

TODO

public String toString() @ L66

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

参数:

说明:

TODO

public static TextColor fromLegacyFormat(ChatFormatting format) @ L71

  • 方法名:fromLegacyFormat
  • 源码定位:L71
  • 返回类型:TextColor
  • 修饰符:public static

参数:

  • format: ChatFormatting

说明:

TODO

public static TextColor fromRgb(int rgb) @ L75

  • 方法名:fromRgb
  • 源码定位:L75
  • 返回类型:TextColor
  • 修饰符:public static

参数:

  • rgb: int

说明:

TODO

public static DataResult<TextColor> parseColor(String color) @ L79

  • 方法名:parseColor
  • 源码定位:L79
  • 返回类型:DataResult
  • 修饰符:public static

参数:

  • color: String

说明:

TODO

代码

public final class TextColor {
    private static final String CUSTOM_COLOR_PREFIX = "#";
    public static final Codec<TextColor> CODEC = Codec.STRING.comapFlatMap(TextColor::parseColor, TextColor::serialize);
    private static final Map<ChatFormatting, TextColor> LEGACY_FORMAT_TO_COLOR = Stream.of(ChatFormatting.values())
        .filter(ChatFormatting::isColor)
        .collect(ImmutableMap.toImmutableMap(Function.identity(), f -> new TextColor(f.getColor(), f.getName())));
    private static final Map<String, TextColor> NAMED_COLORS = LEGACY_FORMAT_TO_COLOR.values()
        .stream()
        .collect(ImmutableMap.toImmutableMap(e -> e.name, Function.identity()));
    private final int value;
    private final @Nullable String name;
 
    private TextColor(int value, String name) {
        this.value = value & 16777215;
        this.name = name;
    }
 
    private TextColor(int value) {
        this.value = value & 16777215;
        this.name = null;
    }
 
    public int getValue() {
        return this.value;
    }
 
    public String serialize() {
        return this.name != null ? this.name : this.formatValue();
    }
 
    private String formatValue() {
        return String.format(Locale.ROOT, "#%06X", this.value);
    }
 
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        } else if (o != null && this.getClass() == o.getClass()) {
            TextColor other = (TextColor)o;
            return this.value == other.value;
        } else {
            return false;
        }
    }
 
    @Override
    public int hashCode() {
        return Objects.hash(this.value, this.name);
    }
 
    @Override
    public String toString() {
        return this.serialize();
    }
 
    public static @Nullable TextColor fromLegacyFormat(ChatFormatting format) {
        return LEGACY_FORMAT_TO_COLOR.get(format);
    }
 
    public static TextColor fromRgb(int rgb) {
        return new TextColor(rgb);
    }
 
    public static DataResult<TextColor> parseColor(String color) {
        if (color.startsWith("#")) {
            try {
                int value = Integer.parseInt(color.substring(1), 16);
                return value >= 0 && value <= 16777215
                    ? DataResult.success(fromRgb(value), Lifecycle.stable())
                    : DataResult.error(() -> "Color value out of range: " + color);
            } catch (NumberFormatException var2) {
                return DataResult.error(() -> "Invalid color value: " + color);
            }
        } else {
            TextColor predefinedColor = NAMED_COLORS.get(color);
            return predefinedColor == null ? DataResult.error(() -> "Invalid color name: " + color) : DataResult.success(predefinedColor, Lifecycle.stable());
        }
    }
}

引用的其他类

  • ChatFormatting
    • 引用位置: 参数/字段/方法调用
    • 关联成员: ChatFormatting.values()