ComponentUtils.java

net.minecraft.network.chat.ComponentUtils

信息

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

    TODO

字段/常量

  • DEFAULT_SEPARATOR_TEXT

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

      TODO

  • DEFAULT_SEPARATOR

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

      TODO

  • DEFAULT_NO_STYLE_SEPARATOR

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

      TODO

内部类/嵌套类型

构造器

方法

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

public static MutableComponent mergeStyles(MutableComponent component, Style style) @ L22

  • 方法名:mergeStyles
  • 源码定位:L22
  • 返回类型:MutableComponent
  • 修饰符:public static

参数:

  • component: MutableComponent
  • style: Style

说明:

TODO

public static Component mergeStyles(Component component, Style style) @ L36

  • 方法名:mergeStyles
  • 源码定位:L36
  • 返回类型:Component
  • 修饰符:public static

参数:

  • component: Component
  • style: Style

说明:

TODO

public static Optional<MutableComponent> resolve(ResolutionContext context, Optional<Component> component, int recursionDepth) @ L50

  • 方法名:resolve
  • 源码定位:L50
  • 返回类型:Optional
  • 修饰符:public static

参数:

  • context: ResolutionContext
  • component: Optional
  • recursionDepth: int

说明:

TODO

public static MutableComponent resolve(ResolutionContext context, Component component) @ L54

  • 方法名:resolve
  • 源码定位:L54
  • 返回类型:MutableComponent
  • 修饰符:public static

参数:

  • context: ResolutionContext
  • component: Component

说明:

TODO

public static MutableComponent resolve(ResolutionContext context, Component component, int recursionDepth) @ L58

  • 方法名:resolve
  • 源码定位:L58
  • 返回类型:MutableComponent
  • 修饰符:public static

参数:

  • context: ResolutionContext
  • component: Component
  • recursionDepth: int

说明:

TODO

private static Style resolveStyle(ResolutionContext context, Style style, int recursionDepth) @ L75

  • 方法名:resolveStyle
  • 源码定位:L75
  • 返回类型:Style
  • 修饰符:private static

参数:

  • context: ResolutionContext
  • style: Style
  • recursionDepth: int

说明:

TODO

public static Component formatList(Collection<String> values) @ L84

  • 方法名:formatList
  • 源码定位:L84
  • 返回类型:Component
  • 修饰符:public static

参数:

  • values: Collection

说明:

TODO

public static <T extends Comparable<T>> Component formatAndSortList(Collection<T> values, Function<T,Component> formatter) @ L88

  • 方法名:formatAndSortList
  • 源码定位:L88
  • 返回类型:<T extends Comparable> Component
  • 修饰符:public static

参数:

  • values: Collection
  • formatter: Function<T,Component>

说明:

TODO

public static <T> Component formatList(Collection<?extends T> values, Function<T,Component> formatter) @ L100

  • 方法名:formatList
  • 源码定位:L100
  • 返回类型: Component
  • 修饰符:public static

参数:

  • values: Collection<?extends T>
  • formatter: Function<T,Component>

说明:

TODO

public static <T> MutableComponent formatList(Collection<?extends T> values, Optional<?extends Component> separator, Function<T,Component> formatter) @ L104

  • 方法名:formatList
  • 源码定位:L104
  • 返回类型: MutableComponent
  • 修饰符:public static

参数:

  • values: Collection<?extends T>
  • separator: Optional<?extends Component>
  • formatter: Function<T,Component>

说明:

TODO

public static Component formatList(Collection<?extends Component> values, Component separator) @ L108

  • 方法名:formatList
  • 源码定位:L108
  • 返回类型:Component
  • 修饰符:public static

参数:

  • values: Collection<?extends Component>
  • separator: Component

说明:

TODO

public static <T> MutableComponent formatList(Collection<?extends T> values, Component separator, Function<T,Component> formatter) @ L112

  • 方法名:formatList
  • 源码定位:L112
  • 返回类型: MutableComponent
  • 修饰符:public static

参数:

  • values: Collection<?extends T>
  • separator: Component
  • formatter: Function<T,Component>

说明:

TODO

public static MutableComponent wrapInSquareBrackets(Component inner) @ L134

  • 方法名:wrapInSquareBrackets
  • 源码定位:L134
  • 返回类型:MutableComponent
  • 修饰符:public static

参数:

  • inner: Component

说明:

TODO

public static Component fromMessage(Message message) @ L138

  • 方法名:fromMessage
  • 源码定位:L138
  • 返回类型:Component
  • 修饰符:public static

参数:

  • message: Message

说明:

TODO

public static boolean isTranslationResolvable(Component component) @ L142

  • 方法名:isTranslationResolvable
  • 源码定位:L142
  • 返回类型:boolean
  • 修饰符:public static

参数:

  • component: Component

说明:

TODO

public static MutableComponent copyOnClickText(String text) @ L152

  • 方法名:copyOnClickText
  • 源码定位:L152
  • 返回类型:MutableComponent
  • 修饰符:public static

参数:

  • text: String

说明:

TODO

代码

public class ComponentUtils {
    public static final String DEFAULT_SEPARATOR_TEXT = ", ";
    public static final Component DEFAULT_SEPARATOR = Component.literal(", ").withStyle(ChatFormatting.GRAY);
    public static final Component DEFAULT_NO_STYLE_SEPARATOR = Component.literal(", ");
 
    @CheckReturnValue
    public static MutableComponent mergeStyles(MutableComponent component, Style style) {
        if (style.isEmpty()) {
            return component;
        } else {
            Style inner = component.getStyle();
            if (inner.isEmpty()) {
                return component.setStyle(style);
            } else {
                return inner.equals(style) ? component : component.setStyle(inner.applyTo(style));
            }
        }
    }
 
    @CheckReturnValue
    public static Component mergeStyles(Component component, Style style) {
        if (style.isEmpty()) {
            return component;
        } else {
            Style inner = component.getStyle();
            if (inner.isEmpty()) {
                return component.copy().setStyle(style);
            } else {
                return (Component)(inner.equals(style) ? component : component.copy().setStyle(inner.applyTo(style)));
            }
        }
    }
 
    public static Optional<MutableComponent> resolve(ResolutionContext context, Optional<Component> component, int recursionDepth) throws CommandSyntaxException {
        return component.isPresent() ? Optional.of(resolve(context, component.get(), recursionDepth)) : Optional.empty();
    }
 
    public static MutableComponent resolve(ResolutionContext context, Component component) throws CommandSyntaxException {
        return resolve(context, component, 0);
    }
 
    public static MutableComponent resolve(ResolutionContext context, Component component, int recursionDepth) throws CommandSyntaxException {
        if (recursionDepth > context.depthLimit()) {
            return switch (context.depthLimitBehavior()) {
                case DISCARD_REMAINING -> CommonComponents.ELLIPSIS.copy();
                case STOP_PROCESSING_AND_COPY_REMAINING -> component.copy();
            };
        } else {
            MutableComponent result = component.getContents().resolve(context, recursionDepth + 1);
 
            for (Component sibling : component.getSiblings()) {
                result.append(resolve(context, sibling, recursionDepth + 1));
            }
 
            return result.withStyle(resolveStyle(context, component.getStyle(), recursionDepth));
        }
    }
 
    private static Style resolveStyle(ResolutionContext context, Style style, int recursionDepth) throws CommandSyntaxException {
        if (style.getHoverEvent() instanceof HoverEvent.ShowText(Component resolved)) {
            HoverEvent resolvedx = new HoverEvent.ShowText(resolve(context, resolved, recursionDepth + 1));
            return style.withHoverEvent(resolvedx);
        } else {
            return style;
        }
    }
 
    public static Component formatList(Collection<String> values) {
        return formatAndSortList(values, v -> Component.literal(v).withStyle(ChatFormatting.GREEN));
    }
 
    public static <T extends Comparable<T>> Component formatAndSortList(Collection<T> values, Function<T, Component> formatter) {
        if (values.isEmpty()) {
            return CommonComponents.EMPTY;
        } else if (values.size() == 1) {
            return formatter.apply(values.iterator().next());
        } else {
            List<T> sorted = Lists.newArrayList(values);
            sorted.sort(Comparable::compareTo);
            return formatList(sorted, formatter);
        }
    }
 
    public static <T> Component formatList(Collection<? extends T> values, Function<T, Component> formatter) {
        return formatList(values, DEFAULT_SEPARATOR, formatter);
    }
 
    public static <T> MutableComponent formatList(Collection<? extends T> values, Optional<? extends Component> separator, Function<T, Component> formatter) {
        return formatList(values, DataFixUtils.orElse(separator, DEFAULT_SEPARATOR), formatter);
    }
 
    public static Component formatList(Collection<? extends Component> values, Component separator) {
        return formatList(values, separator, Function.identity());
    }
 
    public static <T> MutableComponent formatList(Collection<? extends T> values, Component separator, Function<T, Component> formatter) {
        if (values.isEmpty()) {
            return Component.empty();
        } else if (values.size() == 1) {
            return formatter.apply((T)values.iterator().next()).copy();
        } else {
            MutableComponent result = Component.empty();
            boolean first = true;
 
            for (T value : values) {
                if (!first) {
                    result.append(separator);
                }
 
                result.append(formatter.apply(value));
                first = false;
            }
 
            return result;
        }
    }
 
    public static MutableComponent wrapInSquareBrackets(Component inner) {
        return Component.translatable("chat.square_brackets", inner);
    }
 
    public static Component fromMessage(Message message) {
        return (Component)(message instanceof Component component ? component : Component.literal(message.getString()));
    }
 
    public static boolean isTranslationResolvable(@Nullable Component component) {
        if (component != null && component.getContents() instanceof TranslatableContents translatable) {
            String key = translatable.getKey();
            String fallback = translatable.getFallback();
            return fallback != null || Language.getInstance().has(key);
        } else {
            return true;
        }
    }
 
    public static MutableComponent copyOnClickText(String text) {
        return wrapInSquareBrackets(
            Component.literal(text)
                .withStyle(
                    s -> s.withColor(ChatFormatting.GREEN)
                        .withClickEvent(new ClickEvent.CopyToClipboard(text))
                        .withHoverEvent(new HoverEvent.ShowText(Component.translatable("chat.copy.click")))
                        .withInsertion(text)
                )
        );
    }
}

引用的其他类

  • Language

    • 引用位置: 方法调用
    • 关联成员: Language.getInstance()
  • ClickEvent

    • 引用位置: 方法调用/构造调用
    • 关联成员: ClickEvent.CopyToClipboard(), CopyToClipboard()
  • Component

    • 引用位置: 参数/字段/方法调用/返回值
    • 关联成员: Component.empty(), Component.literal(), Component.translatable()
  • HoverEvent

    • 引用位置: 方法调用/构造调用
    • 关联成员: HoverEvent.ShowText(), ShowText()
  • MutableComponent

    • 引用位置: 参数/返回值
  • ResolutionContext

    • 引用位置: 参数
  • Style

    • 引用位置: 参数/返回值