| 1 | package net.minecraft.network.chat;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.brigadier.Message;
|
| 5 | import com.mojang.datafixers.util.Either;
|
| 6 | import java.net.URI;
|
| 7 | import java.util.ArrayList;
|
| 8 | import java.util.Collections;
|
| 9 | import java.util.Date;
|
| 10 | import java.util.List;
|
| 11 | import java.util.Optional;
|
| 12 | import java.util.UUID;
|
| 13 | import net.minecraft.commands.arguments.NbtPathArgument;
|
| 14 | import net.minecraft.commands.arguments.selector.EntitySelector;
|
| 15 | import net.minecraft.network.chat.contents.KeybindContents;
|
| 16 | import net.minecraft.network.chat.contents.NbtContents;
|
| 17 | import net.minecraft.network.chat.contents.ObjectContents;
|
| 18 | import net.minecraft.network.chat.contents.PlainTextContents;
|
| 19 | import net.minecraft.network.chat.contents.ScoreContents;
|
| 20 | import net.minecraft.network.chat.contents.SelectorContents;
|
| 21 | import net.minecraft.network.chat.contents.TranslatableContents;
|
| 22 | import net.minecraft.network.chat.contents.data.DataSource;
|
| 23 | import net.minecraft.network.chat.contents.objects.ObjectInfo;
|
| 24 | import net.minecraft.resources.Identifier;
|
| 25 | import net.minecraft.util.CompilableString;
|
| 26 | import net.minecraft.util.FormattedCharSequence;
|
| 27 | import net.minecraft.world.level.ChunkPos;
|
| 28 | import org.jspecify.annotations.Nullable;
|
| 29 |
|
| 30 | public interface Component extends Message, FormattedText {
|
| 31 | Style getStyle();
|
| 32 |
|
| 33 | ComponentContents getContents();
|
| 34 |
|
| 35 | @Override
|
| 36 | default String getString() {
|
| 37 | return FormattedText.super.getString();
|
| 38 | }
|
| 39 |
|
| 40 | default String getString(final int limit) {
|
| 41 | StringBuilder builder = new StringBuilder();
|
| 42 | this.visit(contents -> {
|
| 43 | int remaining = limit - builder.length();
|
| 44 | if (remaining <= 0) {
|
| 45 | return STOP_ITERATION;
|
| 46 | }
|
| 47 |
|
| 48 | builder.append(contents.length() <= remaining ? contents : contents.substring(0, remaining));
|
| 49 | return Optional.empty();
|
| 50 | });
|
| 51 | return builder.toString();
|
| 52 | }
|
| 53 |
|
| 54 | List<Component> getSiblings();
|
| 55 |
|
| 56 | default @Nullable String tryCollapseToString() {
|
| 57 | return this.getContents() instanceof PlainTextContents text && this.getSiblings().isEmpty() && this.getStyle().isEmpty() ? text.text() : null;
|
| 58 | }
|
| 59 |
|
| 60 | default MutableComponent plainCopy() {
|
| 61 | return MutableComponent.create(this.getContents());
|
| 62 | }
|
| 63 |
|
| 64 | default MutableComponent copy() {
|
| 65 | return new MutableComponent(this.getContents(), new ArrayList<>(this.getSiblings()), this.getStyle());
|
| 66 | }
|
| 67 |
|
| 68 | FormattedCharSequence getVisualOrderText();
|
| 69 |
|
| 70 | @Override
|
| 71 | default <T> Optional<T> visit(final FormattedText.StyledContentConsumer<T> output, final Style parentStyle) {
|
| 72 | Style selfStyle = this.getStyle().applyTo(parentStyle);
|
| 73 | Optional<T> selfResult = this.getContents().visit(output, selfStyle);
|
| 74 | if (selfResult.isPresent()) {
|
| 75 | return selfResult;
|
| 76 | }
|
| 77 |
|
| 78 | for (Component sibling : this.getSiblings()) {
|
| 79 | Optional<T> result = sibling.visit(output, selfStyle);
|
| 80 | if (result.isPresent()) {
|
| 81 | return result;
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | return Optional.empty();
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | default <T> Optional<T> visit(final FormattedText.ContentConsumer<T> output) {
|
| 90 | Optional<T> selfResult = this.getContents().visit(output);
|
| 91 | if (selfResult.isPresent()) {
|
| 92 | return selfResult;
|
| 93 | }
|
| 94 |
|
| 95 | for (Component sibling : this.getSiblings()) {
|
| 96 | Optional<T> result = sibling.visit(output);
|
| 97 | if (result.isPresent()) {
|
| 98 | return result;
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | return Optional.empty();
|
| 103 | }
|
| 104 |
|
| 105 | default List<Component> toFlatList() {
|
| 106 | return this.toFlatList(Style.EMPTY);
|
| 107 | }
|
| 108 |
|
| 109 | default List<Component> toFlatList(final Style rootStyle) {
|
| 110 | List<Component> result = Lists.newArrayList();
|
| 111 | this.visit((style, contents) -> {
|
| 112 | if (!contents.isEmpty()) {
|
| 113 | result.add(literal(contents).withStyle(style));
|
| 114 | }
|
| 115 |
|
| 116 | return Optional.empty();
|
| 117 | }, rootStyle);
|
| 118 | return result;
|
| 119 | }
|
| 120 |
|
| 121 | default boolean contains(final Component other) {
|
| 122 | if (this.equals(other)) {
|
| 123 | return true;
|
| 124 | }
|
| 125 |
|
| 126 | List<Component> flat = this.toFlatList();
|
| 127 | List<Component> otherFlat = other.toFlatList(this.getStyle());
|
| 128 | return Collections.indexOfSubList(flat, otherFlat) != -1;
|
| 129 | }
|
| 130 |
|
| 131 | static Component nullToEmpty(final @Nullable String text) {
|
| 132 | return text != null ? literal(text) : CommonComponents.EMPTY;
|
| 133 | }
|
| 134 |
|
| 135 | static MutableComponent literal(final String text) {
|
| 136 | return MutableComponent.create(PlainTextContents.create(text));
|
| 137 | }
|
| 138 |
|
| 139 | static MutableComponent translatable(final String key) {
|
| 140 | return MutableComponent.create(new TranslatableContents(key, null, TranslatableContents.NO_ARGS));
|
| 141 | }
|
| 142 |
|
| 143 | static MutableComponent translatable(final String key, final Object... args) {
|
| 144 | return MutableComponent.create(new TranslatableContents(key, null, args));
|
| 145 | }
|
| 146 |
|
| 147 | static MutableComponent translatableEscape(final String key, final Object... args) {
|
| 148 | for (int i = 0; i < args.length; i++) {
|
| 149 | Object arg = args[i];
|
| 150 | if (!TranslatableContents.isAllowedPrimitiveArgument(arg) && !(arg instanceof Component)) {
|
| 151 | args[i] = String.valueOf(arg);
|
| 152 | }
|
| 153 | }
|
| 154 |
|
| 155 | return translatable(key, args);
|
| 156 | }
|
| 157 |
|
| 158 | static MutableComponent translatableWithFallback(final String key, final @Nullable String fallback) {
|
| 159 | return MutableComponent.create(new TranslatableContents(key, fallback, TranslatableContents.NO_ARGS));
|
| 160 | }
|
| 161 |
|
| 162 | static MutableComponent translatableWithFallback(final String key, final @Nullable String fallback, final Object... args) {
|
| 163 | return MutableComponent.create(new TranslatableContents(key, fallback, args));
|
| 164 | }
|
| 165 |
|
| 166 | static MutableComponent empty() {
|
| 167 | return MutableComponent.create(PlainTextContents.EMPTY);
|
| 168 | }
|
| 169 |
|
| 170 | static MutableComponent keybind(final String name) {
|
| 171 | return MutableComponent.create(new KeybindContents(name));
|
| 172 | }
|
| 173 |
|
| 174 | static MutableComponent nbt(
|
| 175 | final CompilableString<NbtPathArgument.NbtPath> nbtPath,
|
| 176 | final boolean interpreting,
|
| 177 | final boolean plain,
|
| 178 | final Optional<Component> separator,
|
| 179 | final DataSource dataSource
|
| 180 | ) {
|
| 181 | return MutableComponent.create(new NbtContents(nbtPath, interpreting, plain, separator, dataSource));
|
| 182 | }
|
| 183 |
|
| 184 | static MutableComponent score(final CompilableString<EntitySelector> pattern, final String objective) {
|
| 185 | return MutableComponent.create(new ScoreContents(Either.left(pattern), objective));
|
| 186 | }
|
| 187 |
|
| 188 | static MutableComponent score(final String name, final String objective) {
|
| 189 | return MutableComponent.create(new ScoreContents(Either.right(name), objective));
|
| 190 | }
|
| 191 |
|
| 192 | static MutableComponent selector(final CompilableString<EntitySelector> pattern, final Optional<Component> separator) {
|
| 193 | return MutableComponent.create(new SelectorContents(pattern, separator));
|
| 194 | }
|
| 195 |
|
| 196 | static MutableComponent object(final ObjectInfo info) {
|
| 197 | return MutableComponent.create(new ObjectContents(info, Optional.empty()));
|
| 198 | }
|
| 199 |
|
| 200 | static MutableComponent object(final ObjectInfo info, final Component fallback) {
|
| 201 | return MutableComponent.create(new ObjectContents(info, Optional.of(fallback)));
|
| 202 | }
|
| 203 |
|
| 204 | static Component translationArg(final Date date) {
|
| 205 | return literal(date.toString());
|
| 206 | }
|
| 207 |
|
| 208 | static Component translationArg(final Message message) {
|
| 209 | return message instanceof Component component ? component : literal(message.getString());
|
| 210 | }
|
| 211 |
|
| 212 | static Component translationArg(final UUID uuid) {
|
| 213 | return literal(uuid.toString());
|
| 214 | }
|
| 215 |
|
| 216 | static Component translationArg(final Identifier id) {
|
| 217 | return literal(id.toString());
|
| 218 | }
|
| 219 |
|
| 220 | static Component translationArg(final ChunkPos chunkPos) {
|
| 221 | return literal(chunkPos.toString());
|
| 222 | }
|
| 223 |
|
| 224 | static Component translationArg(final URI uri) {
|
| 225 | return literal(uri.toString());
|
| 226 | }
|
| 227 | }
|
| 228 |
|