SnbtPrinterTagVisitor.java

net.minecraft.nbt.SnbtPrinterTagVisitor

信息

  • 全限定名:net.minecraft.nbt.SnbtPrinterTagVisitor
  • 类型:public class
  • 包:net.minecraft.nbt
  • 源码路径:src/main/java/net/minecraft/nbt/SnbtPrinterTagVisitor.java
  • 起始行号:L16
  • 实现:TagVisitor
  • 职责:

    TODO

字段/常量

  • KEY_ORDER

    • 类型: Map<String,List<String>>
    • 修饰符: private static final
    • 源码定位: L17
    • 说明:

      TODO

  • NO_INDENTATION

    • 类型: Set<String>
    • 修饰符: private static final
    • 源码定位: L22
    • 说明:

      TODO

  • SIMPLE_VALUE

    • 类型: Pattern
    • 修饰符: private static final
    • 源码定位: L23
    • 说明:

      TODO

  • NAME_VALUE_SEPARATOR

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

      TODO

  • ELEMENT_SEPARATOR

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

      TODO

  • LIST_OPEN

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

      TODO

  • LIST_CLOSE

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

      TODO

  • LIST_TYPE_SEPARATOR

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

      TODO

  • ELEMENT_SPACING

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

      TODO

  • STRUCT_OPEN

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

      TODO

  • STRUCT_CLOSE

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

      TODO

  • NEWLINE

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

      TODO

  • indentation

    • 类型: String
    • 修饰符: private final
    • 源码定位: L33
    • 说明:

      TODO

  • depth

    • 类型: int
    • 修饰符: private final
    • 源码定位: L34
    • 说明:

      TODO

  • path

    • 类型: List<String>
    • 修饰符: private final
    • 源码定位: L35
    • 说明:

      TODO

  • result

    • 类型: String
    • 修饰符: private
    • 源码定位: L36
    • 说明:

      TODO

内部类/嵌套类型

构造器

public SnbtPrinterTagVisitor() @ L38

  • 构造器名:SnbtPrinterTagVisitor
  • 源码定位:L38
  • 修饰符:public

参数:

说明:

TODO

public SnbtPrinterTagVisitor(String indentation, int depth, List<String> path) @ L42

  • 构造器名:SnbtPrinterTagVisitor
  • 源码定位:L42
  • 修饰符:public

参数:

  • indentation: String
  • depth: int
  • path: List

说明:

TODO

方法

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

public String visit(Tag tag) @ L48

  • 方法名:visit
  • 源码定位:L48
  • 返回类型:String
  • 修饰符:public

参数:

  • tag: Tag

说明:

TODO

public void visitString(StringTag tag) @ L53

  • 方法名:visitString
  • 源码定位:L53
  • 返回类型:void
  • 修饰符:public

参数:

  • tag: StringTag

说明:

TODO

public void visitByte(ByteTag tag) @ L58

  • 方法名:visitByte
  • 源码定位:L58
  • 返回类型:void
  • 修饰符:public

参数:

  • tag: ByteTag

说明:

TODO

public void visitShort(ShortTag tag) @ L63

  • 方法名:visitShort
  • 源码定位:L63
  • 返回类型:void
  • 修饰符:public

参数:

  • tag: ShortTag

说明:

TODO

public void visitInt(IntTag tag) @ L68

  • 方法名:visitInt
  • 源码定位:L68
  • 返回类型:void
  • 修饰符:public

参数:

  • tag: IntTag

说明:

TODO

public void visitLong(LongTag tag) @ L73

  • 方法名:visitLong
  • 源码定位:L73
  • 返回类型:void
  • 修饰符:public

参数:

  • tag: LongTag

说明:

TODO

public void visitFloat(FloatTag tag) @ L78

  • 方法名:visitFloat
  • 源码定位:L78
  • 返回类型:void
  • 修饰符:public

参数:

  • tag: FloatTag

说明:

TODO

public void visitDouble(DoubleTag tag) @ L83

  • 方法名:visitDouble
  • 源码定位:L83
  • 返回类型:void
  • 修饰符:public

参数:

  • tag: DoubleTag

说明:

TODO

public void visitByteArray(ByteArrayTag tag) @ L88

  • 方法名:visitByteArray
  • 源码定位:L88
  • 返回类型:void
  • 修饰符:public

参数:

  • tag: ByteArrayTag

说明:

TODO

public void visitIntArray(IntArrayTag tag) @ L104

  • 方法名:visitIntArray
  • 源码定位:L104
  • 返回类型:void
  • 修饰符:public

参数:

  • tag: IntArrayTag

说明:

TODO

public void visitLongArray(LongArrayTag tag) @ L120

  • 方法名:visitLongArray
  • 源码定位:L120
  • 返回类型:void
  • 修饰符:public

参数:

  • tag: LongArrayTag

说明:

TODO

public void visitList(ListTag tag) @ L137

  • 方法名:visitList
  • 源码定位:L137
  • 返回类型:void
  • 修饰符:public

参数:

  • tag: ListTag

说明:

TODO

public void visitCompound(CompoundTag tag) @ L167

  • 方法名:visitCompound
  • 源码定位:L167
  • 返回类型:void
  • 修饰符:public

参数:

  • tag: CompoundTag

说明:

TODO

private void popPath() @ L207

  • 方法名:popPath
  • 源码定位:L207
  • 返回类型:void
  • 修饰符:private

参数:

说明:

TODO

private void pushPath(String e) @ L211

  • 方法名:pushPath
  • 源码定位:L211
  • 返回类型:void
  • 修饰符:private

参数:

  • e: String

说明:

TODO

protected List<String> getKeys(CompoundTag tag) @ L215

  • 方法名:getKeys
  • 源码定位:L215
  • 返回类型:List
  • 修饰符:protected

参数:

  • tag: CompoundTag

说明:

TODO

public String pathString() @ L237

  • 方法名:pathString
  • 源码定位:L237
  • 返回类型:String
  • 修饰符:public

参数:

说明:

TODO

protected static String handleEscapePretty(String input) @ L241

  • 方法名:handleEscapePretty
  • 源码定位:L241
  • 返回类型:String
  • 修饰符:protected static

参数:

  • input: String

说明:

TODO

public void visitEnd(EndTag tag) @ L245

  • 方法名:visitEnd
  • 源码定位:L245
  • 返回类型:void
  • 修饰符:public

参数:

  • tag: EndTag

说明:

TODO

代码

public class SnbtPrinterTagVisitor implements TagVisitor {
    private static final Map<String, List<String>> KEY_ORDER = Util.make(Maps.newHashMap(), map -> {
        map.put("{}", Lists.newArrayList("DataVersion", "author", "size", "data", "entities", "palette", "palettes"));
        map.put("{}.data.[].{}", Lists.newArrayList("pos", "state", "nbt"));
        map.put("{}.entities.[].{}", Lists.newArrayList("blockPos", "pos"));
    });
    private static final Set<String> NO_INDENTATION = Sets.newHashSet("{}.size.[]", "{}.data.[].{}", "{}.palette.[].{}", "{}.entities.[].{}");
    private static final Pattern SIMPLE_VALUE = Pattern.compile("[A-Za-z0-9._+-]+");
    private static final String NAME_VALUE_SEPARATOR = String.valueOf(':');
    private static final String ELEMENT_SEPARATOR = String.valueOf(',');
    private static final String LIST_OPEN = "[";
    private static final String LIST_CLOSE = "]";
    private static final String LIST_TYPE_SEPARATOR = ";";
    private static final String ELEMENT_SPACING = " ";
    private static final String STRUCT_OPEN = "{";
    private static final String STRUCT_CLOSE = "}";
    private static final String NEWLINE = "\n";
    private final String indentation;
    private final int depth;
    private final List<String> path;
    private String result = "";
 
    public SnbtPrinterTagVisitor() {
        this("    ", 0, Lists.newArrayList());
    }
 
    public SnbtPrinterTagVisitor(String indentation, int depth, List<String> path) {
        this.indentation = indentation;
        this.depth = depth;
        this.path = path;
    }
 
    public String visit(Tag tag) {
        tag.accept(this);
        return this.result;
    }
 
    @Override
    public void visitString(StringTag tag) {
        this.result = StringTag.quoteAndEscape(tag.value());
    }
 
    @Override
    public void visitByte(ByteTag tag) {
        this.result = tag.value() + "b";
    }
 
    @Override
    public void visitShort(ShortTag tag) {
        this.result = tag.value() + "s";
    }
 
    @Override
    public void visitInt(IntTag tag) {
        this.result = String.valueOf(tag.value());
    }
 
    @Override
    public void visitLong(LongTag tag) {
        this.result = tag.value() + "L";
    }
 
    @Override
    public void visitFloat(FloatTag tag) {
        this.result = tag.value() + "f";
    }
 
    @Override
    public void visitDouble(DoubleTag tag) {
        this.result = tag.value() + "d";
    }
 
    @Override
    public void visitByteArray(ByteArrayTag tag) {
        StringBuilder builder = new StringBuilder("[").append("B").append(";");
        byte[] data = tag.getAsByteArray();
 
        for (int i = 0; i < data.length; i++) {
            builder.append(" ").append(data[i]).append("B");
            if (i != data.length - 1) {
                builder.append(ELEMENT_SEPARATOR);
            }
        }
 
        builder.append("]");
        this.result = builder.toString();
    }
 
    @Override
    public void visitIntArray(IntArrayTag tag) {
        StringBuilder builder = new StringBuilder("[").append("I").append(";");
        int[] data = tag.getAsIntArray();
 
        for (int i = 0; i < data.length; i++) {
            builder.append(" ").append(data[i]);
            if (i != data.length - 1) {
                builder.append(ELEMENT_SEPARATOR);
            }
        }
 
        builder.append("]");
        this.result = builder.toString();
    }
 
    @Override
    public void visitLongArray(LongArrayTag tag) {
        String type = "L";
        StringBuilder builder = new StringBuilder("[").append("L").append(";");
        long[] data = tag.getAsLongArray();
 
        for (int i = 0; i < data.length; i++) {
            builder.append(" ").append(data[i]).append("L");
            if (i != data.length - 1) {
                builder.append(ELEMENT_SEPARATOR);
            }
        }
 
        builder.append("]");
        this.result = builder.toString();
    }
 
    @Override
    public void visitList(ListTag tag) {
        if (tag.isEmpty()) {
            this.result = "[]";
        } else {
            StringBuilder builder = new StringBuilder("[");
            this.pushPath("[]");
            String indentation = NO_INDENTATION.contains(this.pathString()) ? "" : this.indentation;
            if (!indentation.isEmpty()) {
                builder.append("\n");
            }
 
            for (int i = 0; i < tag.size(); i++) {
                builder.append(Strings.repeat(indentation, this.depth + 1));
                builder.append(new SnbtPrinterTagVisitor(indentation, this.depth + 1, this.path).visit(tag.get(i)));
                if (i != tag.size() - 1) {
                    builder.append(ELEMENT_SEPARATOR).append(indentation.isEmpty() ? " " : "\n");
                }
            }
 
            if (!indentation.isEmpty()) {
                builder.append("\n").append(Strings.repeat(indentation, this.depth));
            }
 
            builder.append("]");
            this.result = builder.toString();
            this.popPath();
        }
    }
 
    @Override
    public void visitCompound(CompoundTag tag) {
        if (tag.isEmpty()) {
            this.result = "{}";
        } else {
            StringBuilder builder = new StringBuilder("{");
            this.pushPath("{}");
            String indentation = NO_INDENTATION.contains(this.pathString()) ? "" : this.indentation;
            if (!indentation.isEmpty()) {
                builder.append("\n");
            }
 
            Collection<String> keys = this.getKeys(tag);
            Iterator<String> iterator = keys.iterator();
 
            while (iterator.hasNext()) {
                String key = iterator.next();
                Tag value = tag.get(key);
                this.pushPath(key);
                builder.append(Strings.repeat(indentation, this.depth + 1))
                    .append(handleEscapePretty(key))
                    .append(NAME_VALUE_SEPARATOR)
                    .append(" ")
                    .append(new SnbtPrinterTagVisitor(indentation, this.depth + 1, this.path).visit(value));
                this.popPath();
                if (iterator.hasNext()) {
                    builder.append(ELEMENT_SEPARATOR).append(indentation.isEmpty() ? " " : "\n");
                }
            }
 
            if (!indentation.isEmpty()) {
                builder.append("\n").append(Strings.repeat(indentation, this.depth));
            }
 
            builder.append("}");
            this.result = builder.toString();
            this.popPath();
        }
    }
 
    private void popPath() {
        this.path.remove(this.path.size() - 1);
    }
 
    private void pushPath(String e) {
        this.path.add(e);
    }
 
    protected List<String> getKeys(CompoundTag tag) {
        Set<String> keys = Sets.newHashSet(tag.keySet());
        List<String> strings = Lists.newArrayList();
        List<String> order = KEY_ORDER.get(this.pathString());
        if (order != null) {
            for (String key : order) {
                if (keys.remove(key)) {
                    strings.add(key);
                }
            }
 
            if (!keys.isEmpty()) {
                keys.stream().sorted().forEach(strings::add);
            }
        } else {
            strings.addAll(keys);
            Collections.sort(strings);
        }
 
        return strings;
    }
 
    public String pathString() {
        return String.join(".", this.path);
    }
 
    protected static String handleEscapePretty(String input) {
        return SIMPLE_VALUE.matcher(input).matches() ? input : StringTag.quoteAndEscape(input);
    }
 
    @Override
    public void visitEnd(EndTag tag) {
    }
}

引用的其他类