| 1 | package net.minecraft.nbt;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.util.ArrayList;
|
| 5 | import java.util.Collection;
|
| 6 | import java.util.Collections;
|
| 7 | import java.util.HashMap;
|
| 8 | import java.util.Iterator;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Map;
|
| 11 | import java.util.Objects;
|
| 12 | import java.util.regex.Pattern;
|
| 13 | import net.minecraft.ChatFormatting;
|
| 14 | import net.minecraft.network.chat.Component;
|
| 15 | import net.minecraft.network.chat.MutableComponent;
|
| 16 | import net.minecraft.network.chat.Style;
|
| 17 | import org.slf4j.Logger;
|
| 18 |
|
| 19 | public class TextComponentTagVisitor implements TagVisitor {
|
| 20 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 21 | private static final int INLINE_LIST_THRESHOLD = 8;
|
| 22 | private static final int MAX_DEPTH = 64;
|
| 23 | private static final int MAX_LENGTH = 128;
|
| 24 | private static final Pattern SIMPLE_VALUE = Pattern.compile("[A-Za-z0-9._+-]+");
|
| 25 | private static final Component NEWLINE = Component.literal("\n");
|
| 26 | private static final Component ELEMENT_SPACING = Component.literal(" ");
|
| 27 | private final String indentation;
|
| 28 | private final TextComponentTagVisitor.Styling styling;
|
| 29 | private final boolean sortKeys;
|
| 30 | private int indentDepth;
|
| 31 | private int depth;
|
| 32 | private final MutableComponent result = Component.empty();
|
| 33 |
|
| 34 | public TextComponentTagVisitor(final String indentation) {
|
| 35 | this(indentation, TextComponentTagVisitor.RichStyling.INSTANCE);
|
| 36 | }
|
| 37 |
|
| 38 | public TextComponentTagVisitor(final String indentation, final TextComponentTagVisitor.Styling styling) {
|
| 39 | this(indentation, styling, LOGGER.isDebugEnabled());
|
| 40 | }
|
| 41 |
|
| 42 | public TextComponentTagVisitor(final String indentation, final TextComponentTagVisitor.Styling styling, final boolean sortKeys) {
|
| 43 | this.indentation = indentation;
|
| 44 | this.styling = styling;
|
| 45 | this.sortKeys = sortKeys;
|
| 46 | }
|
| 47 |
|
| 48 | public Component visit(final Tag tag) {
|
| 49 | tag.accept(this);
|
| 50 | return this.result;
|
| 51 | }
|
| 52 |
|
| 53 | private TextComponentTagVisitor append(final String string, final Style style) {
|
| 54 | this.result.append(Component.literal(string).withStyle(style));
|
| 55 | return this;
|
| 56 | }
|
| 57 |
|
| 58 | private TextComponentTagVisitor append(final Component component) {
|
| 59 | this.result.append(component);
|
| 60 | return this;
|
| 61 | }
|
| 62 |
|
| 63 | private TextComponentTagVisitor append(final TextComponentTagVisitor.Token token) {
|
| 64 | this.result.append(this.styling.token(token));
|
| 65 | return this;
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public void visitString(final StringTag tag) {
|
| 70 | String quoted = StringTag.quoteAndEscape(tag.value());
|
| 71 | Component quote = Component.literal(quoted.substring(0, 1));
|
| 72 | this.append(quote).append(quoted.substring(1, quoted.length() - 1), this.styling.stringStyle()).append(quote);
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | public void visitByte(final ByteTag tag) {
|
| 77 | this.append(String.valueOf(tag.value()), this.styling.numberStyle()).append(TextComponentTagVisitor.Token.BYTE_SUFFIX);
|
| 78 | }
|
| 79 |
|
| 80 | @Override
|
| 81 | public void visitShort(final ShortTag tag) {
|
| 82 | this.append(String.valueOf(tag.value()), this.styling.numberStyle()).append(TextComponentTagVisitor.Token.SHORT_SUFFIX);
|
| 83 | }
|
| 84 |
|
| 85 | @Override
|
| 86 | public void visitInt(final IntTag tag) {
|
| 87 | this.append(String.valueOf(tag.value()), this.styling.numberStyle());
|
| 88 | }
|
| 89 |
|
| 90 | @Override
|
| 91 | public void visitLong(final LongTag tag) {
|
| 92 | this.append(String.valueOf(tag.value()), this.styling.numberStyle()).append(TextComponentTagVisitor.Token.LONG_SUFFIX);
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | public void visitFloat(final FloatTag tag) {
|
| 97 | this.append(String.valueOf(tag.value()), this.styling.numberStyle()).append(TextComponentTagVisitor.Token.FLOAT_SUFFIX);
|
| 98 | }
|
| 99 |
|
| 100 | @Override
|
| 101 | public void visitDouble(final DoubleTag tag) {
|
| 102 | this.append(String.valueOf(tag.value()), this.styling.numberStyle()).append(TextComponentTagVisitor.Token.DOUBLE_SUFFIX);
|
| 103 | }
|
| 104 |
|
| 105 | @Override
|
| 106 | public void visitByteArray(final ByteArrayTag tag) {
|
| 107 | this.append(TextComponentTagVisitor.Token.LIST_OPEN)
|
| 108 | .append(TextComponentTagVisitor.Token.BYTE_ARRAY_PREFIX)
|
| 109 | .append(TextComponentTagVisitor.Token.LIST_TYPE_SEPARATOR);
|
| 110 | byte[] data = tag.getAsByteArray();
|
| 111 |
|
| 112 | for (int i = 0; i < data.length && i < 128; i++) {
|
| 113 | this.append(ELEMENT_SPACING).append(String.valueOf(data[i]), this.styling.numberStyle()).append(TextComponentTagVisitor.Token.BYTE_SUFFIX);
|
| 114 | if (i != data.length - 1) {
|
| 115 | this.append(TextComponentTagVisitor.Token.ELEMENT_SEPARATOR);
|
| 116 | }
|
| 117 | }
|
| 118 |
|
| 119 | if (data.length > 128) {
|
| 120 | this.append(TextComponentTagVisitor.Token.FOLDED);
|
| 121 | }
|
| 122 |
|
| 123 | this.append(TextComponentTagVisitor.Token.LIST_CLOSE);
|
| 124 | }
|
| 125 |
|
| 126 | @Override
|
| 127 | public void visitIntArray(final IntArrayTag tag) {
|
| 128 | this.append(TextComponentTagVisitor.Token.LIST_OPEN)
|
| 129 | .append(TextComponentTagVisitor.Token.INT_ARRAY_PREFIX)
|
| 130 | .append(TextComponentTagVisitor.Token.LIST_TYPE_SEPARATOR);
|
| 131 | int[] data = tag.getAsIntArray();
|
| 132 |
|
| 133 | for (int i = 0; i < data.length && i < 128; i++) {
|
| 134 | this.append(ELEMENT_SPACING).append(String.valueOf(data[i]), this.styling.numberStyle());
|
| 135 | if (i != data.length - 1) {
|
| 136 | this.append(TextComponentTagVisitor.Token.ELEMENT_SEPARATOR);
|
| 137 | }
|
| 138 | }
|
| 139 |
|
| 140 | if (data.length > 128) {
|
| 141 | this.append(TextComponentTagVisitor.Token.FOLDED);
|
| 142 | }
|
| 143 |
|
| 144 | this.append(TextComponentTagVisitor.Token.LIST_CLOSE);
|
| 145 | }
|
| 146 |
|
| 147 | @Override
|
| 148 | public void visitLongArray(final LongArrayTag tag) {
|
| 149 | this.append(TextComponentTagVisitor.Token.LIST_OPEN)
|
| 150 | .append(TextComponentTagVisitor.Token.LONG_ARRAY_PREFIX)
|
| 151 | .append(TextComponentTagVisitor.Token.LIST_TYPE_SEPARATOR);
|
| 152 | long[] data = tag.getAsLongArray();
|
| 153 |
|
| 154 | for (int i = 0; i < data.length && i < 128; i++) {
|
| 155 | this.append(ELEMENT_SPACING).append(String.valueOf(data[i]), this.styling.numberStyle()).append(TextComponentTagVisitor.Token.LONG_SUFFIX);
|
| 156 | if (i != data.length - 1) {
|
| 157 | this.append(TextComponentTagVisitor.Token.ELEMENT_SEPARATOR);
|
| 158 | }
|
| 159 | }
|
| 160 |
|
| 161 | if (data.length > 128) {
|
| 162 | this.append(TextComponentTagVisitor.Token.FOLDED);
|
| 163 | }
|
| 164 |
|
| 165 | this.append(TextComponentTagVisitor.Token.LIST_CLOSE);
|
| 166 | }
|
| 167 |
|
| 168 | private static boolean shouldWrapListElements(final ListTag list) {
|
| 169 | if (list.size() >= 8) {
|
| 170 | return false;
|
| 171 | }
|
| 172 |
|
| 173 | for (Tag element : list) {
|
| 174 | if (!(element instanceof NumericTag)) {
|
| 175 | return true;
|
| 176 | }
|
| 177 | }
|
| 178 |
|
| 179 | return false;
|
| 180 | }
|
| 181 |
|
| 182 | @Override
|
| 183 | public void visitList(final ListTag tag) {
|
| 184 | if (tag.isEmpty()) {
|
| 185 | this.append(TextComponentTagVisitor.Token.LIST_OPEN).append(TextComponentTagVisitor.Token.LIST_CLOSE);
|
| 186 | } else if (this.depth >= 64) {
|
| 187 | this.append(TextComponentTagVisitor.Token.LIST_OPEN).append(TextComponentTagVisitor.Token.FOLDED).append(TextComponentTagVisitor.Token.LIST_CLOSE);
|
| 188 | } else if (!shouldWrapListElements(tag)) {
|
| 189 | this.append(TextComponentTagVisitor.Token.LIST_OPEN);
|
| 190 |
|
| 191 | for (int i = 0; i < tag.size(); i++) {
|
| 192 | if (i != 0) {
|
| 193 | this.append(TextComponentTagVisitor.Token.ELEMENT_SEPARATOR).append(ELEMENT_SPACING);
|
| 194 | }
|
| 195 |
|
| 196 | this.appendSubTag(tag.get(i), false);
|
| 197 | }
|
| 198 |
|
| 199 | this.append(TextComponentTagVisitor.Token.LIST_CLOSE);
|
| 200 | } else {
|
| 201 | this.append(TextComponentTagVisitor.Token.LIST_OPEN);
|
| 202 | if (!this.indentation.isEmpty()) {
|
| 203 | this.append(NEWLINE);
|
| 204 | }
|
| 205 |
|
| 206 | Component entryIndent = Component.literal(this.indentation.repeat(this.indentDepth + 1));
|
| 207 | Component elementSpacing = this.indentation.isEmpty() ? ELEMENT_SPACING : NEWLINE;
|
| 208 |
|
| 209 | for (int i = 0; i < tag.size() && i < 128; i++) {
|
| 210 | this.append(entryIndent);
|
| 211 | this.appendSubTag(tag.get(i), true);
|
| 212 | if (i != tag.size() - 1) {
|
| 213 | this.append(TextComponentTagVisitor.Token.ELEMENT_SEPARATOR).append(elementSpacing);
|
| 214 | }
|
| 215 | }
|
| 216 |
|
| 217 | if (tag.size() > 128) {
|
| 218 | this.append(entryIndent).append(TextComponentTagVisitor.Token.FOLDED);
|
| 219 | }
|
| 220 |
|
| 221 | if (!this.indentation.isEmpty()) {
|
| 222 | this.append(NEWLINE).append(Component.literal(this.indentation.repeat(this.indentDepth)));
|
| 223 | }
|
| 224 |
|
| 225 | this.append(TextComponentTagVisitor.Token.LIST_CLOSE);
|
| 226 | }
|
| 227 | }
|
| 228 |
|
| 229 | @Override
|
| 230 | public void visitCompound(final CompoundTag tag) {
|
| 231 | if (tag.isEmpty()) {
|
| 232 | this.append(TextComponentTagVisitor.Token.STRUCT_OPEN).append(TextComponentTagVisitor.Token.STRUCT_CLOSE);
|
| 233 | } else if (this.depth >= 64) {
|
| 234 | this.append(TextComponentTagVisitor.Token.STRUCT_OPEN).append(TextComponentTagVisitor.Token.FOLDED).append(TextComponentTagVisitor.Token.STRUCT_CLOSE);
|
| 235 | } else {
|
| 236 | this.append(TextComponentTagVisitor.Token.STRUCT_OPEN);
|
| 237 | Collection<String> keys;
|
| 238 | if (this.sortKeys) {
|
| 239 | List<String> keyCopy = new ArrayList<>(tag.keySet());
|
| 240 | Collections.sort(keyCopy);
|
| 241 | keys = keyCopy;
|
| 242 | } else {
|
| 243 | keys = tag.keySet();
|
| 244 | }
|
| 245 |
|
| 246 | if (!this.indentation.isEmpty()) {
|
| 247 | this.append(NEWLINE);
|
| 248 | }
|
| 249 |
|
| 250 | Component entryIndent = Component.literal(this.indentation.repeat(this.indentDepth + 1));
|
| 251 | Component elementSpacing = this.indentation.isEmpty() ? ELEMENT_SPACING : NEWLINE;
|
| 252 | Iterator<String> iterator = keys.iterator();
|
| 253 |
|
| 254 | while (iterator.hasNext()) {
|
| 255 | String key = iterator.next();
|
| 256 | this.append(entryIndent).append(this.handleEscapePretty(key)).append(TextComponentTagVisitor.Token.NAME_VALUE_SEPARATOR).append(ELEMENT_SPACING);
|
| 257 | this.appendSubTag(tag.get(key), true);
|
| 258 | if (iterator.hasNext()) {
|
| 259 | this.append(TextComponentTagVisitor.Token.ELEMENT_SEPARATOR).append(elementSpacing);
|
| 260 | }
|
| 261 | }
|
| 262 |
|
| 263 | if (!this.indentation.isEmpty()) {
|
| 264 | this.append(NEWLINE).append(Component.literal(this.indentation.repeat(this.indentDepth)));
|
| 265 | }
|
| 266 |
|
| 267 | this.append(TextComponentTagVisitor.Token.STRUCT_CLOSE);
|
| 268 | }
|
| 269 | }
|
| 270 |
|
| 271 | private void appendSubTag(final Tag tag, final boolean indent) {
|
| 272 | if (indent) {
|
| 273 | this.indentDepth++;
|
| 274 | }
|
| 275 |
|
| 276 | this.depth++;
|
| 277 |
|
| 278 | try {
|
| 279 | tag.accept(this);
|
| 280 | } finally {
|
| 281 | if (indent) {
|
| 282 | this.indentDepth--;
|
| 283 | }
|
| 284 |
|
| 285 | this.depth--;
|
| 286 | }
|
| 287 | }
|
| 288 |
|
| 289 | private Component handleEscapePretty(final String input) {
|
| 290 | if (SIMPLE_VALUE.matcher(input).matches()) {
|
| 291 | return Component.literal(input).withStyle(this.styling.keyStyle());
|
| 292 | }
|
| 293 |
|
| 294 | String quoted = StringTag.quoteAndEscape(input);
|
| 295 | String quote = quoted.substring(0, 1);
|
| 296 | Component inner = Component.literal(quoted.substring(1, quoted.length() - 1)).withStyle(this.styling.keyStyle());
|
| 297 | return Component.literal(quote).append(inner).append(quote);
|
| 298 | }
|
| 299 |
|
| 300 | @Override
|
| 301 | public void visitEnd(final EndTag tag) {
|
| 302 | }
|
| 303 |
|
| 304 | public static class PlainStyling implements TextComponentTagVisitor.Styling {
|
| 305 | public static final TextComponentTagVisitor.Styling INSTANCE = new TextComponentTagVisitor.PlainStyling();
|
| 306 | private final Map<TextComponentTagVisitor.Token, Component> tokens = new HashMap<>();
|
| 307 |
|
| 308 | private PlainStyling() {
|
| 309 | for (TextComponentTagVisitor.Token value : TextComponentTagVisitor.Token.values()) {
|
| 310 | this.tokens.put(value, Component.literal(value.text));
|
| 311 | }
|
| 312 | }
|
| 313 |
|
| 314 | @Override
|
| 315 | public Style keyStyle() {
|
| 316 | return Style.EMPTY;
|
| 317 | }
|
| 318 |
|
| 319 | @Override
|
| 320 | public Style stringStyle() {
|
| 321 | return Style.EMPTY;
|
| 322 | }
|
| 323 |
|
| 324 | @Override
|
| 325 | public Style numberStyle() {
|
| 326 | return Style.EMPTY;
|
| 327 | }
|
| 328 |
|
| 329 | @Override
|
| 330 | public Component token(final TextComponentTagVisitor.Token token) {
|
| 331 | return Objects.requireNonNull(this.tokens.get(token));
|
| 332 | }
|
| 333 | }
|
| 334 |
|
| 335 | public static class RichStyling implements TextComponentTagVisitor.Styling {
|
| 336 | private static final Style SYNTAX_HIGHLIGHTING_NUMBER_TYPE = Style.EMPTY.withColor(ChatFormatting.RED);
|
| 337 | public static final TextComponentTagVisitor.Styling INSTANCE = new TextComponentTagVisitor.RichStyling();
|
| 338 | private final Map<TextComponentTagVisitor.Token, Component> tokens = new HashMap<>();
|
| 339 | private static final Style SYNTAX_HIGHLIGHTING_KEY = Style.EMPTY.withColor(ChatFormatting.AQUA);
|
| 340 | private static final Style SYNTAX_HIGHLIGHTING_STRING = Style.EMPTY.withColor(ChatFormatting.GREEN);
|
| 341 | private static final Style SYNTAX_HIGHLIGHTING_NUMBER = Style.EMPTY.withColor(ChatFormatting.GOLD);
|
| 342 |
|
| 343 | private RichStyling() {
|
| 344 | this.overrideToken(TextComponentTagVisitor.Token.FOLDED, Style.EMPTY.withColor(ChatFormatting.GRAY));
|
| 345 | this.overrideToken(TextComponentTagVisitor.Token.BYTE_SUFFIX, SYNTAX_HIGHLIGHTING_NUMBER_TYPE);
|
| 346 | this.overrideToken(TextComponentTagVisitor.Token.BYTE_ARRAY_PREFIX, SYNTAX_HIGHLIGHTING_NUMBER_TYPE);
|
| 347 | this.overrideToken(TextComponentTagVisitor.Token.SHORT_SUFFIX, SYNTAX_HIGHLIGHTING_NUMBER_TYPE);
|
| 348 | this.overrideToken(TextComponentTagVisitor.Token.INT_ARRAY_PREFIX, SYNTAX_HIGHLIGHTING_NUMBER_TYPE);
|
| 349 | this.overrideToken(TextComponentTagVisitor.Token.LONG_SUFFIX, SYNTAX_HIGHLIGHTING_NUMBER_TYPE);
|
| 350 | this.overrideToken(TextComponentTagVisitor.Token.LONG_ARRAY_PREFIX, SYNTAX_HIGHLIGHTING_NUMBER_TYPE);
|
| 351 | this.overrideToken(TextComponentTagVisitor.Token.FLOAT_SUFFIX, SYNTAX_HIGHLIGHTING_NUMBER_TYPE);
|
| 352 | this.overrideToken(TextComponentTagVisitor.Token.DOUBLE_SUFFIX, SYNTAX_HIGHLIGHTING_NUMBER_TYPE);
|
| 353 |
|
| 354 | for (TextComponentTagVisitor.Token value : TextComponentTagVisitor.Token.values()) {
|
| 355 | this.tokens.putIfAbsent(value, Component.literal(value.text));
|
| 356 | }
|
| 357 | }
|
| 358 |
|
| 359 | private void overrideToken(final TextComponentTagVisitor.Token token, final Style style) {
|
| 360 | this.tokens.put(token, Component.literal(token.text).withStyle(style));
|
| 361 | }
|
| 362 |
|
| 363 | @Override
|
| 364 | public Style keyStyle() {
|
| 365 | return SYNTAX_HIGHLIGHTING_KEY;
|
| 366 | }
|
| 367 |
|
| 368 | @Override
|
| 369 | public Style stringStyle() {
|
| 370 | return SYNTAX_HIGHLIGHTING_STRING;
|
| 371 | }
|
| 372 |
|
| 373 | @Override
|
| 374 | public Style numberStyle() {
|
| 375 | return SYNTAX_HIGHLIGHTING_NUMBER;
|
| 376 | }
|
| 377 |
|
| 378 | @Override
|
| 379 | public Component token(final TextComponentTagVisitor.Token token) {
|
| 380 | return Objects.requireNonNull(this.tokens.get(token));
|
| 381 | }
|
| 382 | }
|
| 383 |
|
| 384 | public interface Styling {
|
| 385 | Style keyStyle();
|
| 386 |
|
| 387 | Style stringStyle();
|
| 388 |
|
| 389 | Style numberStyle();
|
| 390 |
|
| 391 | Component token(final TextComponentTagVisitor.Token token);
|
| 392 | }
|
| 393 |
|
| 394 | public enum Token {
|
| 395 | FOLDED("<...>"),
|
| 396 | ELEMENT_SEPARATOR(","),
|
| 397 | LIST_CLOSE("]"),
|
| 398 | LIST_OPEN("["),
|
| 399 | LIST_TYPE_SEPARATOR(";"),
|
| 400 | STRUCT_CLOSE("}"),
|
| 401 | STRUCT_OPEN("{"),
|
| 402 | NAME_VALUE_SEPARATOR(":"),
|
| 403 | BYTE_SUFFIX("b"),
|
| 404 | BYTE_ARRAY_PREFIX("B"),
|
| 405 | SHORT_SUFFIX("s"),
|
| 406 | INT_ARRAY_PREFIX("I"),
|
| 407 | LONG_SUFFIX("L"),
|
| 408 | LONG_ARRAY_PREFIX("L"),
|
| 409 | FLOAT_SUFFIX("f"),
|
| 410 | DOUBLE_SUFFIX("d");
|
| 411 |
|
| 412 | public final String text;
|
| 413 |
|
| 414 | Token(final String text) {
|
| 415 | this.text = text;
|
| 416 | }
|
| 417 | }
|
| 418 | }
|
| 419 |
|