| 1 | package net.minecraft.nbt.visitors;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableSet;
|
| 4 | import com.google.common.collect.ImmutableSet.Builder;
|
| 5 | import java.util.ArrayDeque;
|
| 6 | import java.util.Deque;
|
| 7 | import java.util.Set;
|
| 8 | import net.minecraft.nbt.CompoundTag;
|
| 9 | import net.minecraft.nbt.StreamTagVisitor;
|
| 10 | import net.minecraft.nbt.TagType;
|
| 11 |
|
| 12 | public class CollectFields extends CollectToTag {
|
| 13 | private int fieldsToGetCount;
|
| 14 | private final Set<TagType<?>> wantedTypes;
|
| 15 | private final Deque<FieldTree> stack = new ArrayDeque<>();
|
| 16 |
|
| 17 | public CollectFields(final FieldSelector... wantedFields) {
|
| 18 | this.fieldsToGetCount = wantedFields.length;
|
| 19 | Builder<TagType<?>> wantedTypes = ImmutableSet.builder();
|
| 20 | FieldTree rootFrame = FieldTree.createRoot();
|
| 21 |
|
| 22 | for (FieldSelector wantedField : wantedFields) {
|
| 23 | rootFrame.addEntry(wantedField);
|
| 24 | wantedTypes.add(wantedField.type());
|
| 25 | }
|
| 26 |
|
| 27 | this.stack.push(rootFrame);
|
| 28 | wantedTypes.add(CompoundTag.TYPE);
|
| 29 | this.wantedTypes = wantedTypes.build();
|
| 30 | }
|
| 31 |
|
| 32 | @Override
|
| 33 | public StreamTagVisitor.ValueResult visitRootEntry(final TagType<?> type) {
|
| 34 | return type != CompoundTag.TYPE ? StreamTagVisitor.ValueResult.HALT : super.visitRootEntry(type);
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | public StreamTagVisitor.EntryResult visitEntry(final TagType<?> type) {
|
| 39 | FieldTree currentFrame = this.stack.element();
|
| 40 | if (this.depth() > currentFrame.depth()) {
|
| 41 | return super.visitEntry(type);
|
| 42 | } else if (this.fieldsToGetCount <= 0) {
|
| 43 | return StreamTagVisitor.EntryResult.BREAK;
|
| 44 | } else {
|
| 45 | return !this.wantedTypes.contains(type) ? StreamTagVisitor.EntryResult.SKIP : super.visitEntry(type);
|
| 46 | }
|
| 47 | }
|
| 48 |
|
| 49 | @Override
|
| 50 | public StreamTagVisitor.EntryResult visitEntry(final TagType<?> type, final String id) {
|
| 51 | FieldTree currentFrame = this.stack.element();
|
| 52 | if (this.depth() > currentFrame.depth()) {
|
| 53 | return super.visitEntry(type, id);
|
| 54 | }
|
| 55 |
|
| 56 | if (currentFrame.selectedFields().remove(id, type)) {
|
| 57 | this.fieldsToGetCount--;
|
| 58 | return super.visitEntry(type, id);
|
| 59 | }
|
| 60 |
|
| 61 | if (type == CompoundTag.TYPE) {
|
| 62 | FieldTree newFrame = currentFrame.fieldsToRecurse().get(id);
|
| 63 | if (newFrame != null) {
|
| 64 | this.stack.push(newFrame);
|
| 65 | return super.visitEntry(type, id);
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | return StreamTagVisitor.EntryResult.SKIP;
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | public StreamTagVisitor.ValueResult visitContainerEnd() {
|
| 74 | if (this.depth() == this.stack.element().depth()) {
|
| 75 | this.stack.pop();
|
| 76 | }
|
| 77 |
|
| 78 | return super.visitContainerEnd();
|
| 79 | }
|
| 80 |
|
| 81 | public int getMissingFieldCount() {
|
| 82 | return this.fieldsToGetCount;
|
| 83 | }
|
| 84 | }
|
| 85 |
|