| 1 | package net.minecraft.nbt;
|
| 2 |
|
| 3 | import com.google.common.base.Strings;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import com.google.common.collect.Maps;
|
| 6 | import com.google.common.collect.Sets;
|
| 7 | import java.util.Collection;
|
| 8 | import java.util.Collections;
|
| 9 | import java.util.Iterator;
|
| 10 | import java.util.List;
|
| 11 | import java.util.Map;
|
| 12 | import java.util.Set;
|
| 13 | import java.util.regex.Pattern;
|
| 14 | import net.minecraft.util.Util;
|
| 15 |
|
| 16 | public class SnbtPrinterTagVisitor implements TagVisitor {
|
| 17 | private static final Map<String, List<String>> KEY_ORDER = Util.make(Maps.newHashMap(), map -> {
|
| 18 | map.put("{}", Lists.newArrayList("DataVersion", "author", "size", "data", "entities", "palette", "palettes"));
|
| 19 | map.put("{}.data.[].{}", Lists.newArrayList("pos", "state", "nbt"));
|
| 20 | map.put("{}.entities.[].{}", Lists.newArrayList("blockPos", "pos"));
|
| 21 | });
|
| 22 | private static final Set<String> NO_INDENTATION = Sets.newHashSet("{}.size.[]", "{}.data.[].{}", "{}.palette.[].{}", "{}.entities.[].{}");
|
| 23 | private static final Pattern SIMPLE_VALUE = Pattern.compile("[A-Za-z0-9._+-]+");
|
| 24 | private static final String NAME_VALUE_SEPARATOR = String.valueOf(':');
|
| 25 | private static final String ELEMENT_SEPARATOR = String.valueOf(',');
|
| 26 | private static final String LIST_OPEN = "[";
|
| 27 | private static final String LIST_CLOSE = "]";
|
| 28 | private static final String LIST_TYPE_SEPARATOR = ";";
|
| 29 | private static final String ELEMENT_SPACING = " ";
|
| 30 | private static final String STRUCT_OPEN = "{";
|
| 31 | private static final String STRUCT_CLOSE = "}";
|
| 32 | private static final String NEWLINE = "\n";
|
| 33 | private final String indentation;
|
| 34 | private final int depth;
|
| 35 | private final List<String> path;
|
| 36 | private String result = "";
|
| 37 |
|
| 38 | public SnbtPrinterTagVisitor() {
|
| 39 | this(" ", 0, Lists.newArrayList());
|
| 40 | }
|
| 41 |
|
| 42 | public SnbtPrinterTagVisitor(final String indentation, final int depth, final List<String> path) {
|
| 43 | this.indentation = indentation;
|
| 44 | this.depth = depth;
|
| 45 | this.path = path;
|
| 46 | }
|
| 47 |
|
| 48 | public String visit(final Tag tag) {
|
| 49 | tag.accept(this);
|
| 50 | return this.result;
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public void visitString(final StringTag tag) {
|
| 55 | this.result = StringTag.quoteAndEscape(tag.value());
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | public void visitByte(final ByteTag tag) {
|
| 60 | this.result = tag.value() + "b";
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | public void visitShort(final ShortTag tag) {
|
| 65 | this.result = tag.value() + "s";
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public void visitInt(final IntTag tag) {
|
| 70 | this.result = String.valueOf(tag.value());
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | public void visitLong(final LongTag tag) {
|
| 75 | this.result = tag.value() + "L";
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public void visitFloat(final FloatTag tag) {
|
| 80 | this.result = tag.value() + "f";
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public void visitDouble(final DoubleTag tag) {
|
| 85 | this.result = tag.value() + "d";
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | public void visitByteArray(final ByteArrayTag tag) {
|
| 90 | StringBuilder builder = new StringBuilder("[").append("B").append(";");
|
| 91 | byte[] data = tag.getAsByteArray();
|
| 92 |
|
| 93 | for (int i = 0; i < data.length; i++) {
|
| 94 | builder.append(" ").append(data[i]).append("B");
|
| 95 | if (i != data.length - 1) {
|
| 96 | builder.append(ELEMENT_SEPARATOR);
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | builder.append("]");
|
| 101 | this.result = builder.toString();
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | public void visitIntArray(final IntArrayTag tag) {
|
| 106 | StringBuilder builder = new StringBuilder("[").append("I").append(";");
|
| 107 | int[] data = tag.getAsIntArray();
|
| 108 |
|
| 109 | for (int i = 0; i < data.length; i++) {
|
| 110 | builder.append(" ").append(data[i]);
|
| 111 | if (i != data.length - 1) {
|
| 112 | builder.append(ELEMENT_SEPARATOR);
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | builder.append("]");
|
| 117 | this.result = builder.toString();
|
| 118 | }
|
| 119 |
|
| 120 | @Override
|
| 121 | public void visitLongArray(final LongArrayTag tag) {
|
| 122 | String type = "L";
|
| 123 | StringBuilder builder = new StringBuilder("[").append("L").append(";");
|
| 124 | long[] data = tag.getAsLongArray();
|
| 125 |
|
| 126 | for (int i = 0; i < data.length; i++) {
|
| 127 | builder.append(" ").append(data[i]).append("L");
|
| 128 | if (i != data.length - 1) {
|
| 129 | builder.append(ELEMENT_SEPARATOR);
|
| 130 | }
|
| 131 | }
|
| 132 |
|
| 133 | builder.append("]");
|
| 134 | this.result = builder.toString();
|
| 135 | }
|
| 136 |
|
| 137 | @Override
|
| 138 | public void visitList(final ListTag tag) {
|
| 139 | if (tag.isEmpty()) {
|
| 140 | this.result = "[]";
|
| 141 | } else {
|
| 142 | StringBuilder builder = new StringBuilder("[");
|
| 143 | this.pushPath("[]");
|
| 144 | String indentation = NO_INDENTATION.contains(this.pathString()) ? "" : this.indentation;
|
| 145 | if (!indentation.isEmpty()) {
|
| 146 | builder.append("\n");
|
| 147 | }
|
| 148 |
|
| 149 | for (int i = 0; i < tag.size(); i++) {
|
| 150 | builder.append(Strings.repeat(indentation, this.depth + 1));
|
| 151 | builder.append(new SnbtPrinterTagVisitor(indentation, this.depth + 1, this.path).visit(tag.get(i)));
|
| 152 | if (i != tag.size() - 1) {
|
| 153 | builder.append(ELEMENT_SEPARATOR).append(indentation.isEmpty() ? " " : "\n");
|
| 154 | }
|
| 155 | }
|
| 156 |
|
| 157 | if (!indentation.isEmpty()) {
|
| 158 | builder.append("\n").append(Strings.repeat(indentation, this.depth));
|
| 159 | }
|
| 160 |
|
| 161 | builder.append("]");
|
| 162 | this.result = builder.toString();
|
| 163 | this.popPath();
|
| 164 | }
|
| 165 | }
|
| 166 |
|
| 167 | @Override
|
| 168 | public void visitCompound(final CompoundTag tag) {
|
| 169 | if (tag.isEmpty()) {
|
| 170 | this.result = "{}";
|
| 171 | } else {
|
| 172 | StringBuilder builder = new StringBuilder("{");
|
| 173 | this.pushPath("{}");
|
| 174 | String indentation = NO_INDENTATION.contains(this.pathString()) ? "" : this.indentation;
|
| 175 | if (!indentation.isEmpty()) {
|
| 176 | builder.append("\n");
|
| 177 | }
|
| 178 |
|
| 179 | Collection<String> keys = this.getKeys(tag);
|
| 180 | Iterator<String> iterator = keys.iterator();
|
| 181 |
|
| 182 | while (iterator.hasNext()) {
|
| 183 | String key = iterator.next();
|
| 184 | Tag value = tag.get(key);
|
| 185 | this.pushPath(key);
|
| 186 | builder.append(Strings.repeat(indentation, this.depth + 1))
|
| 187 | .append(handleEscapePretty(key))
|
| 188 | .append(NAME_VALUE_SEPARATOR)
|
| 189 | .append(" ")
|
| 190 | .append(new SnbtPrinterTagVisitor(indentation, this.depth + 1, this.path).visit(value));
|
| 191 | this.popPath();
|
| 192 | if (iterator.hasNext()) {
|
| 193 | builder.append(ELEMENT_SEPARATOR).append(indentation.isEmpty() ? " " : "\n");
|
| 194 | }
|
| 195 | }
|
| 196 |
|
| 197 | if (!indentation.isEmpty()) {
|
| 198 | builder.append("\n").append(Strings.repeat(indentation, this.depth));
|
| 199 | }
|
| 200 |
|
| 201 | builder.append("}");
|
| 202 | this.result = builder.toString();
|
| 203 | this.popPath();
|
| 204 | }
|
| 205 | }
|
| 206 |
|
| 207 | private void popPath() {
|
| 208 | this.path.remove(this.path.size() - 1);
|
| 209 | }
|
| 210 |
|
| 211 | private void pushPath(final String e) {
|
| 212 | this.path.add(e);
|
| 213 | }
|
| 214 |
|
| 215 | protected List<String> getKeys(final CompoundTag tag) {
|
| 216 | Set<String> keys = Sets.newHashSet(tag.keySet());
|
| 217 | List<String> strings = Lists.newArrayList();
|
| 218 | List<String> order = KEY_ORDER.get(this.pathString());
|
| 219 | if (order != null) {
|
| 220 | for (String key : order) {
|
| 221 | if (keys.remove(key)) {
|
| 222 | strings.add(key);
|
| 223 | }
|
| 224 | }
|
| 225 |
|
| 226 | if (!keys.isEmpty()) {
|
| 227 | keys.stream().sorted().forEach(strings::add);
|
| 228 | }
|
| 229 | } else {
|
| 230 | strings.addAll(keys);
|
| 231 | Collections.sort(strings);
|
| 232 | }
|
| 233 |
|
| 234 | return strings;
|
| 235 | }
|
| 236 |
|
| 237 | public String pathString() {
|
| 238 | return String.join(".", this.path);
|
| 239 | }
|
| 240 |
|
| 241 | protected static String handleEscapePretty(final String input) {
|
| 242 | return SIMPLE_VALUE.matcher(input).matches() ? input : StringTag.quoteAndEscape(input);
|
| 243 | }
|
| 244 |
|
| 245 | @Override
|
| 246 | public void visitEnd(final EndTag tag) {
|
| 247 | }
|
| 248 | }
|
| 249 |
|