| 1 | package net.minecraft.nbt.visitors;
|
| 2 |
|
| 3 | import java.util.ArrayDeque;
|
| 4 | import java.util.Deque;
|
| 5 | import net.minecraft.nbt.ByteArrayTag;
|
| 6 | import net.minecraft.nbt.ByteTag;
|
| 7 | import net.minecraft.nbt.CompoundTag;
|
| 8 | import net.minecraft.nbt.DoubleTag;
|
| 9 | import net.minecraft.nbt.EndTag;
|
| 10 | import net.minecraft.nbt.FloatTag;
|
| 11 | import net.minecraft.nbt.IntArrayTag;
|
| 12 | import net.minecraft.nbt.IntTag;
|
| 13 | import net.minecraft.nbt.ListTag;
|
| 14 | import net.minecraft.nbt.LongArrayTag;
|
| 15 | import net.minecraft.nbt.LongTag;
|
| 16 | import net.minecraft.nbt.ShortTag;
|
| 17 | import net.minecraft.nbt.StreamTagVisitor;
|
| 18 | import net.minecraft.nbt.StringTag;
|
| 19 | import net.minecraft.nbt.Tag;
|
| 20 | import net.minecraft.nbt.TagType;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 |
|
| 23 | public class CollectToTag implements StreamTagVisitor {
|
| 24 | private final Deque<CollectToTag.ContainerBuilder> containerStack = new ArrayDeque<>();
|
| 25 |
|
| 26 | public CollectToTag() {
|
| 27 | this.containerStack.addLast(new CollectToTag.RootBuilder());
|
| 28 | }
|
| 29 |
|
| 30 | public @Nullable Tag getResult() {
|
| 31 | return this.containerStack.getFirst().build();
|
| 32 | }
|
| 33 |
|
| 34 | protected int depth() {
|
| 35 | return this.containerStack.size() - 1;
|
| 36 | }
|
| 37 |
|
| 38 | private void appendEntry(final Tag instance) {
|
| 39 | this.containerStack.getLast().acceptValue(instance);
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | public StreamTagVisitor.ValueResult visitEnd() {
|
| 44 | this.appendEntry(EndTag.INSTANCE);
|
| 45 | return StreamTagVisitor.ValueResult.CONTINUE;
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | public StreamTagVisitor.ValueResult visit(final String value) {
|
| 50 | this.appendEntry(StringTag.valueOf(value));
|
| 51 | return StreamTagVisitor.ValueResult.CONTINUE;
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | public StreamTagVisitor.ValueResult visit(final byte value) {
|
| 56 | this.appendEntry(ByteTag.valueOf(value));
|
| 57 | return StreamTagVisitor.ValueResult.CONTINUE;
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | public StreamTagVisitor.ValueResult visit(final short value) {
|
| 62 | this.appendEntry(ShortTag.valueOf(value));
|
| 63 | return StreamTagVisitor.ValueResult.CONTINUE;
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | public StreamTagVisitor.ValueResult visit(final int value) {
|
| 68 | this.appendEntry(IntTag.valueOf(value));
|
| 69 | return StreamTagVisitor.ValueResult.CONTINUE;
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | public StreamTagVisitor.ValueResult visit(final long value) {
|
| 74 | this.appendEntry(LongTag.valueOf(value));
|
| 75 | return StreamTagVisitor.ValueResult.CONTINUE;
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public StreamTagVisitor.ValueResult visit(final float value) {
|
| 80 | this.appendEntry(FloatTag.valueOf(value));
|
| 81 | return StreamTagVisitor.ValueResult.CONTINUE;
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public StreamTagVisitor.ValueResult visit(final double value) {
|
| 86 | this.appendEntry(DoubleTag.valueOf(value));
|
| 87 | return StreamTagVisitor.ValueResult.CONTINUE;
|
| 88 | }
|
| 89 |
|
| 90 | @Override
|
| 91 | public StreamTagVisitor.ValueResult visit(final byte[] value) {
|
| 92 | this.appendEntry(new ByteArrayTag(value));
|
| 93 | return StreamTagVisitor.ValueResult.CONTINUE;
|
| 94 | }
|
| 95 |
|
| 96 | @Override
|
| 97 | public StreamTagVisitor.ValueResult visit(final int[] value) {
|
| 98 | this.appendEntry(new IntArrayTag(value));
|
| 99 | return StreamTagVisitor.ValueResult.CONTINUE;
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | public StreamTagVisitor.ValueResult visit(final long[] value) {
|
| 104 | this.appendEntry(new LongArrayTag(value));
|
| 105 | return StreamTagVisitor.ValueResult.CONTINUE;
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | public StreamTagVisitor.ValueResult visitList(final TagType<?> elementType, final int size) {
|
| 110 | return StreamTagVisitor.ValueResult.CONTINUE;
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | public StreamTagVisitor.EntryResult visitElement(final TagType<?> type, final int index) {
|
| 115 | this.enterContainerIfNeeded(type);
|
| 116 | return StreamTagVisitor.EntryResult.ENTER;
|
| 117 | }
|
| 118 |
|
| 119 | @Override
|
| 120 | public StreamTagVisitor.EntryResult visitEntry(final TagType<?> type) {
|
| 121 | return StreamTagVisitor.EntryResult.ENTER;
|
| 122 | }
|
| 123 |
|
| 124 | @Override
|
| 125 | public StreamTagVisitor.EntryResult visitEntry(final TagType<?> type, final String id) {
|
| 126 | this.containerStack.getLast().acceptKey(id);
|
| 127 | this.enterContainerIfNeeded(type);
|
| 128 | return StreamTagVisitor.EntryResult.ENTER;
|
| 129 | }
|
| 130 |
|
| 131 | private void enterContainerIfNeeded(final TagType<?> type) {
|
| 132 | if (type == ListTag.TYPE) {
|
| 133 | this.containerStack.addLast(new CollectToTag.ListBuilder());
|
| 134 | } else if (type == CompoundTag.TYPE) {
|
| 135 | this.containerStack.addLast(new CollectToTag.CompoundBuilder());
|
| 136 | }
|
| 137 | }
|
| 138 |
|
| 139 | @Override
|
| 140 | public StreamTagVisitor.ValueResult visitContainerEnd() {
|
| 141 | CollectToTag.ContainerBuilder container = this.containerStack.removeLast();
|
| 142 | Tag tag = container.build();
|
| 143 | if (tag != null) {
|
| 144 | this.containerStack.getLast().acceptValue(tag);
|
| 145 | }
|
| 146 |
|
| 147 | return StreamTagVisitor.ValueResult.CONTINUE;
|
| 148 | }
|
| 149 |
|
| 150 | @Override
|
| 151 | public StreamTagVisitor.ValueResult visitRootEntry(final TagType<?> type) {
|
| 152 | this.enterContainerIfNeeded(type);
|
| 153 | return StreamTagVisitor.ValueResult.CONTINUE;
|
| 154 | }
|
| 155 |
|
| 156 | private static class CompoundBuilder implements CollectToTag.ContainerBuilder {
|
| 157 | private final CompoundTag compound = new CompoundTag();
|
| 158 | private String lastId = "";
|
| 159 |
|
| 160 | @Override
|
| 161 | public void acceptKey(final String id) {
|
| 162 | this.lastId = id;
|
| 163 | }
|
| 164 |
|
| 165 | @Override
|
| 166 | public void acceptValue(final Tag tag) {
|
| 167 | this.compound.put(this.lastId, tag);
|
| 168 | }
|
| 169 |
|
| 170 | @Override
|
| 171 | public Tag build() {
|
| 172 | return this.compound;
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | private interface ContainerBuilder {
|
| 177 | default void acceptKey(final String id) {
|
| 178 | }
|
| 179 |
|
| 180 | void acceptValue(Tag tag);
|
| 181 |
|
| 182 | @Nullable Tag build();
|
| 183 | }
|
| 184 |
|
| 185 | private static class ListBuilder implements CollectToTag.ContainerBuilder {
|
| 186 | private final ListTag list = new ListTag();
|
| 187 |
|
| 188 | @Override
|
| 189 | public void acceptValue(final Tag tag) {
|
| 190 | this.list.addAndUnwrap(tag);
|
| 191 | }
|
| 192 |
|
| 193 | @Override
|
| 194 | public Tag build() {
|
| 195 | return this.list;
|
| 196 | }
|
| 197 | }
|
| 198 |
|
| 199 | private static class RootBuilder implements CollectToTag.ContainerBuilder {
|
| 200 | private @Nullable Tag result;
|
| 201 |
|
| 202 | @Override
|
| 203 | public void acceptValue(final Tag tag) {
|
| 204 | this.result = tag;
|
| 205 | }
|
| 206 |
|
| 207 | @Override
|
| 208 | public @Nullable Tag build() {
|
| 209 | return this.result;
|
| 210 | }
|
| 211 | }
|
| 212 | }
|
| 213 |
|