| 1 | package net.minecraft.network.chat;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.brigadier.Message;
|
| 5 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 6 | import com.mojang.datafixers.DataFixUtils;
|
| 7 | import java.util.Collection;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Optional;
|
| 10 | import java.util.function.Function;
|
| 11 | import javax.annotation.CheckReturnValue;
|
| 12 | import net.minecraft.ChatFormatting;
|
| 13 | import net.minecraft.locale.Language;
|
| 14 | import net.minecraft.network.chat.contents.TranslatableContents;
|
| 15 | import org.jspecify.annotations.Nullable;
|
| 16 |
|
| 17 | public class ComponentUtils {
|
| 18 | public static final String DEFAULT_SEPARATOR_TEXT = ", ";
|
| 19 | public static final Component DEFAULT_SEPARATOR = Component.literal(", ").withStyle(ChatFormatting.GRAY);
|
| 20 | public static final Component DEFAULT_NO_STYLE_SEPARATOR = Component.literal(", ");
|
| 21 |
|
| 22 | @CheckReturnValue
|
| 23 | public static MutableComponent mergeStyles(final MutableComponent component, final Style style) {
|
| 24 | if (style.isEmpty()) {
|
| 25 | return component;
|
| 26 | } else {
|
| 27 | Style inner = component.getStyle();
|
| 28 | if (inner.isEmpty()) {
|
| 29 | return component.setStyle(style);
|
| 30 | } else {
|
| 31 | return inner.equals(style) ? component : component.setStyle(inner.applyTo(style));
|
| 32 | }
|
| 33 | }
|
| 34 | }
|
| 35 |
|
| 36 | @CheckReturnValue
|
| 37 | public static Component mergeStyles(final Component component, final Style style) {
|
| 38 | if (style.isEmpty()) {
|
| 39 | return component;
|
| 40 | } else {
|
| 41 | Style inner = component.getStyle();
|
| 42 | if (inner.isEmpty()) {
|
| 43 | return component.copy().setStyle(style);
|
| 44 | } else {
|
| 45 | return inner.equals(style) ? component : component.copy().setStyle(inner.applyTo(style));
|
| 46 | }
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | public static Optional<MutableComponent> resolve(final ResolutionContext context, final Optional<Component> component, final int recursionDepth) throws CommandSyntaxException {
|
| 51 | return component.isPresent() ? Optional.of(resolve(context, component.get(), recursionDepth)) : Optional.empty();
|
| 52 | }
|
| 53 |
|
| 54 | public static MutableComponent resolve(final ResolutionContext context, final Component component) throws CommandSyntaxException {
|
| 55 | return resolve(context, component, 0);
|
| 56 | }
|
| 57 |
|
| 58 | public static MutableComponent resolve(final ResolutionContext context, final Component component, final int recursionDepth) throws CommandSyntaxException {
|
| 59 | if (recursionDepth > context.depthLimit()) {
|
| 60 | return switch (context.depthLimitBehavior()) {
|
| 61 | case DISCARD_REMAINING -> CommonComponents.ELLIPSIS.copy();
|
| 62 | case STOP_PROCESSING_AND_COPY_REMAINING -> component.copy();
|
| 63 | };
|
| 64 | } else {
|
| 65 | MutableComponent result = component.getContents().resolve(context, recursionDepth + 1);
|
| 66 |
|
| 67 | for (Component sibling : component.getSiblings()) {
|
| 68 | result.append(resolve(context, sibling, recursionDepth + 1));
|
| 69 | }
|
| 70 |
|
| 71 | return result.withStyle(resolveStyle(context, component.getStyle(), recursionDepth));
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | private static Style resolveStyle(final ResolutionContext context, final Style style, final int recursionDepth) throws CommandSyntaxException {
|
| 76 | if (style.getHoverEvent() instanceof HoverEvent.ShowText(Component text)) {
|
| 77 | HoverEvent resolved = new HoverEvent.ShowText(resolve(context, text, recursionDepth + 1));
|
| 78 | return style.withHoverEvent(resolved);
|
| 79 | } else {
|
| 80 | return style;
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | public static Component formatList(final Collection<String> values) {
|
| 85 | return formatAndSortList(values, v -> Component.literal(v).withStyle(ChatFormatting.GREEN));
|
| 86 | }
|
| 87 |
|
| 88 | public static <T extends Comparable<T>> Component formatAndSortList(final Collection<T> values, final Function<T, Component> formatter) {
|
| 89 | if (values.isEmpty()) {
|
| 90 | return CommonComponents.EMPTY;
|
| 91 | }
|
| 92 |
|
| 93 | if (values.size() == 1) {
|
| 94 | return formatter.apply(values.iterator().next());
|
| 95 | }
|
| 96 |
|
| 97 | List<T> sorted = Lists.newArrayList(values);
|
| 98 | sorted.sort(Comparable::compareTo);
|
| 99 | return formatList(sorted, formatter);
|
| 100 | }
|
| 101 |
|
| 102 | public static <T> Component formatList(final Collection<? extends T> values, final Function<T, Component> formatter) {
|
| 103 | return formatList(values, DEFAULT_SEPARATOR, formatter);
|
| 104 | }
|
| 105 |
|
| 106 | public static <T> MutableComponent formatList(
|
| 107 | final Collection<? extends T> values, final Optional<? extends Component> separator, final Function<T, Component> formatter
|
| 108 | ) {
|
| 109 | return formatList(values, DataFixUtils.orElse(separator, DEFAULT_SEPARATOR), formatter);
|
| 110 | }
|
| 111 |
|
| 112 | public static Component formatList(final Collection<? extends Component> values, final Component separator) {
|
| 113 | return formatList(values, separator, Function.identity());
|
| 114 | }
|
| 115 |
|
| 116 | public static <T> MutableComponent formatList(final Collection<? extends T> values, final Component separator, final Function<T, Component> formatter) {
|
| 117 | if (values.isEmpty()) {
|
| 118 | return Component.empty();
|
| 119 | }
|
| 120 |
|
| 121 | if (values.size() == 1) {
|
| 122 | return formatter.apply((T)values.iterator().next()).copy();
|
| 123 | }
|
| 124 |
|
| 125 | MutableComponent result = Component.empty();
|
| 126 | boolean first = true;
|
| 127 |
|
| 128 | for (T value : values) {
|
| 129 | if (!first) {
|
| 130 | result.append(separator);
|
| 131 | }
|
| 132 |
|
| 133 | result.append(formatter.apply(value));
|
| 134 | first = false;
|
| 135 | }
|
| 136 |
|
| 137 | return result;
|
| 138 | }
|
| 139 |
|
| 140 | public static MutableComponent wrapInSquareBrackets(final Component inner) {
|
| 141 | return Component.translatable("chat.square_brackets", inner);
|
| 142 | }
|
| 143 |
|
| 144 | public static Component fromMessage(final Message message) {
|
| 145 | return message instanceof Component component ? component : Component.literal(message.getString());
|
| 146 | }
|
| 147 |
|
| 148 | public static boolean isTranslationResolvable(final @Nullable Component component) {
|
| 149 | if (component != null && component.getContents() instanceof TranslatableContents translatable) {
|
| 150 | String key = translatable.getKey();
|
| 151 | String fallback = translatable.getFallback();
|
| 152 | return fallback != null || Language.getInstance().has(key);
|
| 153 | } else {
|
| 154 | return true;
|
| 155 | }
|
| 156 | }
|
| 157 |
|
| 158 | public static MutableComponent copyOnClickText(final String text) {
|
| 159 | return wrapInSquareBrackets(
|
| 160 | Component.literal(text)
|
| 161 | .withStyle(
|
| 162 | s -> s.withColor(ChatFormatting.GREEN)
|
| 163 | .withClickEvent(new ClickEvent.CopyToClipboard(text))
|
| 164 | .withHoverEvent(new HoverEvent.ShowText(Component.translatable("chat.copy.click")))
|
| 165 | .withInsertion(text)
|
| 166 | )
|
| 167 | );
|
| 168 | }
|
| 169 | }
|
| 170 |
|