trurwsuieghfdskg

This commit is contained in:
2025-06-08 00:02:04 +09:00
commit 56c8ad5bc2
14172 changed files with 1139672 additions and 0 deletions

View File

@@ -0,0 +1,162 @@
package net.minecraft.nbt;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
public class ByteArrayTag extends CollectionTag<ByteTag> {
private static final int SELF_SIZE_IN_BYTES = 24;
public static final TagType<ByteArrayTag> TYPE = new TagType.VariableSize<ByteArrayTag>() {
public ByteArrayTag load(DataInput p_128247_, int p_128248_, NbtAccounter p_128249_) throws IOException {
p_128249_.accountBytes(24L);
int i = p_128247_.readInt();
p_128249_.accountBytes(1L * (long)i);
byte[] abyte = new byte[i];
p_128247_.readFully(abyte);
return new ByteArrayTag(abyte);
}
public StreamTagVisitor.ValueResult parse(DataInput p_197433_, StreamTagVisitor p_197434_) throws IOException {
int i = p_197433_.readInt();
byte[] abyte = new byte[i];
p_197433_.readFully(abyte);
return p_197434_.visit(abyte);
}
public void skip(DataInput p_197431_) throws IOException {
p_197431_.skipBytes(p_197431_.readInt() * 1);
}
public String getName() {
return "BYTE[]";
}
public String getPrettyName() {
return "TAG_Byte_Array";
}
};
private byte[] data;
public ByteArrayTag(byte[] p_128191_) {
this.data = p_128191_;
}
public ByteArrayTag(List<Byte> p_128189_) {
this(toArray(p_128189_));
}
private static byte[] toArray(List<Byte> p_128207_) {
byte[] abyte = new byte[p_128207_.size()];
for(int i = 0; i < p_128207_.size(); ++i) {
Byte obyte = p_128207_.get(i);
abyte[i] = obyte == null ? 0 : obyte;
}
return abyte;
}
public void write(DataOutput p_128202_) throws IOException {
p_128202_.writeInt(this.data.length);
p_128202_.write(this.data);
}
public int sizeInBytes() {
return 24 + 1 * this.data.length;
}
public byte getId() {
return 7;
}
public TagType<ByteArrayTag> getType() {
return TYPE;
}
public String toString() {
return this.getAsString();
}
public Tag copy() {
byte[] abyte = new byte[this.data.length];
System.arraycopy(this.data, 0, abyte, 0, this.data.length);
return new ByteArrayTag(abyte);
}
public boolean equals(Object p_128233_) {
if (this == p_128233_) {
return true;
} else {
return p_128233_ instanceof ByteArrayTag && Arrays.equals(this.data, ((ByteArrayTag)p_128233_).data);
}
}
public int hashCode() {
return Arrays.hashCode(this.data);
}
public void accept(TagVisitor p_177839_) {
p_177839_.visitByteArray(this);
}
public byte[] getAsByteArray() {
return this.data;
}
public int size() {
return this.data.length;
}
public ByteTag get(int p_128194_) {
return ByteTag.valueOf(this.data[p_128194_]);
}
public ByteTag set(int p_128196_, ByteTag p_128197_) {
byte b0 = this.data[p_128196_];
this.data[p_128196_] = p_128197_.getAsByte();
return ByteTag.valueOf(b0);
}
public void add(int p_128215_, ByteTag p_128216_) {
this.data = ArrayUtils.add(this.data, p_128215_, p_128216_.getAsByte());
}
public boolean setTag(int p_128199_, Tag p_128200_) {
if (p_128200_ instanceof NumericTag) {
this.data[p_128199_] = ((NumericTag)p_128200_).getAsByte();
return true;
} else {
return false;
}
}
public boolean addTag(int p_128218_, Tag p_128219_) {
if (p_128219_ instanceof NumericTag) {
this.data = ArrayUtils.add(this.data, p_128218_, ((NumericTag)p_128219_).getAsByte());
return true;
} else {
return false;
}
}
public ByteTag remove(int p_128213_) {
byte b0 = this.data[p_128213_];
this.data = ArrayUtils.remove(this.data, p_128213_);
return ByteTag.valueOf(b0);
}
public byte getElementType() {
return 1;
}
public void clear() {
this.data = new byte[0];
}
public StreamTagVisitor.ValueResult accept(StreamTagVisitor p_197429_) {
return p_197429_.visit(this.data);
}
}

View File

@@ -0,0 +1,132 @@
package net.minecraft.nbt;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
public class ByteTag extends NumericTag {
private static final int SELF_SIZE_IN_BYTES = 9;
public static final TagType<ByteTag> TYPE = new TagType.StaticSize<ByteTag>() {
public ByteTag load(DataInput p_128292_, int p_128293_, NbtAccounter p_128294_) throws IOException {
p_128294_.accountBytes(9L);
return ByteTag.valueOf(p_128292_.readByte());
}
public StreamTagVisitor.ValueResult parse(DataInput p_197438_, StreamTagVisitor p_197439_) throws IOException {
return p_197439_.visit(p_197438_.readByte());
}
public int size() {
return 1;
}
public String getName() {
return "BYTE";
}
public String getPrettyName() {
return "TAG_Byte";
}
public boolean isValue() {
return true;
}
};
public static final ByteTag ZERO = valueOf((byte)0);
public static final ByteTag ONE = valueOf((byte)1);
private final byte data;
ByteTag(byte p_128261_) {
this.data = p_128261_;
}
public static ByteTag valueOf(byte p_128267_) {
return ByteTag.Cache.cache[128 + p_128267_];
}
public static ByteTag valueOf(boolean p_128274_) {
return p_128274_ ? ONE : ZERO;
}
public void write(DataOutput p_128269_) throws IOException {
p_128269_.writeByte(this.data);
}
public int sizeInBytes() {
return 9;
}
public byte getId() {
return 1;
}
public TagType<ByteTag> getType() {
return TYPE;
}
public ByteTag copy() {
return this;
}
public boolean equals(Object p_128280_) {
if (this == p_128280_) {
return true;
} else {
return p_128280_ instanceof ByteTag && this.data == ((ByteTag)p_128280_).data;
}
}
public int hashCode() {
return this.data;
}
public void accept(TagVisitor p_177842_) {
p_177842_.visitByte(this);
}
public long getAsLong() {
return (long)this.data;
}
public int getAsInt() {
return this.data;
}
public short getAsShort() {
return (short)this.data;
}
public byte getAsByte() {
return this.data;
}
public double getAsDouble() {
return (double)this.data;
}
public float getAsFloat() {
return (float)this.data;
}
public Number getAsNumber() {
return this.data;
}
public StreamTagVisitor.ValueResult accept(StreamTagVisitor p_197436_) {
return p_197436_.visit(this.data);
}
static class Cache {
static final ByteTag[] cache = new ByteTag[256];
private Cache() {
}
static {
for(int i = 0; i < cache.length; ++i) {
cache[i] = new ByteTag((byte)(i - 128));
}
}
}
}

View File

@@ -0,0 +1,17 @@
package net.minecraft.nbt;
import java.util.AbstractList;
public abstract class CollectionTag<T extends Tag> extends AbstractList<T> implements Tag {
public abstract T set(int p_128318_, T p_128319_);
public abstract void add(int p_128315_, T p_128316_);
public abstract T remove(int p_128313_);
public abstract boolean setTag(int p_128305_, Tag p_128306_);
public abstract boolean addTag(int p_128310_, Tag p_128311_);
public abstract byte getElementType();
}

View File

@@ -0,0 +1,545 @@
package net.minecraft.nbt;
import com.google.common.collect.Maps;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.Dynamic;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import javax.annotation.Nullable;
import net.minecraft.CrashReport;
import net.minecraft.CrashReportCategory;
import net.minecraft.ReportedException;
public class CompoundTag implements Tag {
public static final Codec<CompoundTag> CODEC = Codec.PASSTHROUGH.comapFlatMap((p_274781_) -> {
Tag tag = p_274781_.convert(NbtOps.INSTANCE).getValue();
return tag instanceof CompoundTag ? DataResult.success((CompoundTag)tag) : DataResult.error(() -> {
return "Not a compound tag: " + tag;
});
}, (p_128412_) -> {
return new Dynamic<>(NbtOps.INSTANCE, p_128412_);
});
private static final int SELF_SIZE_IN_BYTES = 48;
private static final int MAP_ENTRY_SIZE_IN_BYTES = 32;
public static final TagType<CompoundTag> TYPE = new TagType.VariableSize<CompoundTag>() {
public CompoundTag load(DataInput p_128485_, int p_128486_, NbtAccounter p_128487_) throws IOException {
p_128487_.accountBytes(48L);
if (p_128486_ > 512) {
throw new RuntimeException("Tried to read NBT tag with too high complexity, depth > 512");
} else {
Map<String, Tag> map = Maps.newHashMap();
byte b0;
while((b0 = CompoundTag.readNamedTagType(p_128485_, p_128487_)) != 0) {
String s = CompoundTag.readNamedTagName(p_128485_, p_128487_);
p_128487_.accountBytes((long)(28 + 2 * s.length()));
p_128487_.accountBytes(4); //Forge: 4 extra bytes for the object allocation.
Tag tag = CompoundTag.readNamedTagData(TagTypes.getType(b0), s, p_128485_, p_128486_ + 1, p_128487_);
if (map.put(s, tag) == null) {
p_128487_.accountBytes(36L);
}
}
return new CompoundTag(map);
}
}
public StreamTagVisitor.ValueResult parse(DataInput p_197446_, StreamTagVisitor p_197447_) throws IOException {
while(true) {
byte b0;
if ((b0 = p_197446_.readByte()) != 0) {
TagType<?> tagtype = TagTypes.getType(b0);
switch (p_197447_.visitEntry(tagtype)) {
case HALT:
return StreamTagVisitor.ValueResult.HALT;
case BREAK:
StringTag.skipString(p_197446_);
tagtype.skip(p_197446_);
break;
case SKIP:
StringTag.skipString(p_197446_);
tagtype.skip(p_197446_);
continue;
default:
String s = p_197446_.readUTF();
switch (p_197447_.visitEntry(tagtype, s)) {
case HALT:
return StreamTagVisitor.ValueResult.HALT;
case BREAK:
tagtype.skip(p_197446_);
break;
case SKIP:
tagtype.skip(p_197446_);
continue;
default:
switch (tagtype.parse(p_197446_, p_197447_)) {
case HALT:
return StreamTagVisitor.ValueResult.HALT;
case BREAK:
default:
continue;
}
}
}
}
if (b0 != 0) {
while((b0 = p_197446_.readByte()) != 0) {
StringTag.skipString(p_197446_);
TagTypes.getType(b0).skip(p_197446_);
}
}
return p_197447_.visitContainerEnd();
}
}
public void skip(DataInput p_197444_) throws IOException {
byte b0;
while((b0 = p_197444_.readByte()) != 0) {
StringTag.skipString(p_197444_);
TagTypes.getType(b0).skip(p_197444_);
}
}
public String getName() {
return "COMPOUND";
}
public String getPrettyName() {
return "TAG_Compound";
}
};
private final Map<String, Tag> tags;
protected CompoundTag(Map<String, Tag> p_128333_) {
this.tags = p_128333_;
}
public CompoundTag() {
this(Maps.newHashMap());
}
public void write(DataOutput p_128341_) throws IOException {
for(String s : this.tags.keySet()) {
Tag tag = this.tags.get(s);
writeNamedTag(s, tag, p_128341_);
}
p_128341_.writeByte(0);
}
public int sizeInBytes() {
int i = 48;
for(Map.Entry<String, Tag> entry : this.tags.entrySet()) {
i += 28 + 2 * entry.getKey().length();
i += 36;
i += entry.getValue().sizeInBytes();
}
return i;
}
public Set<String> getAllKeys() {
return this.tags.keySet();
}
public byte getId() {
return 10;
}
public TagType<CompoundTag> getType() {
return TYPE;
}
public int size() {
return this.tags.size();
}
@Nullable
public Tag put(String p_128366_, Tag p_128367_) {
if (p_128367_ == null) throw new IllegalArgumentException("Invalid null NBT value with key " + p_128366_);
return this.tags.put(p_128366_, p_128367_);
}
public void putByte(String p_128345_, byte p_128346_) {
this.tags.put(p_128345_, ByteTag.valueOf(p_128346_));
}
public void putShort(String p_128377_, short p_128378_) {
this.tags.put(p_128377_, ShortTag.valueOf(p_128378_));
}
public void putInt(String p_128406_, int p_128407_) {
this.tags.put(p_128406_, IntTag.valueOf(p_128407_));
}
public void putLong(String p_128357_, long p_128358_) {
this.tags.put(p_128357_, LongTag.valueOf(p_128358_));
}
public void putUUID(String p_128363_, UUID p_128364_) {
this.tags.put(p_128363_, NbtUtils.createUUID(p_128364_));
}
public UUID getUUID(String p_128343_) {
return NbtUtils.loadUUID(this.get(p_128343_));
}
public boolean hasUUID(String p_128404_) {
Tag tag = this.get(p_128404_);
return tag != null && tag.getType() == IntArrayTag.TYPE && ((IntArrayTag)tag).getAsIntArray().length == 4;
}
public void putFloat(String p_128351_, float p_128352_) {
this.tags.put(p_128351_, FloatTag.valueOf(p_128352_));
}
public void putDouble(String p_128348_, double p_128349_) {
this.tags.put(p_128348_, DoubleTag.valueOf(p_128349_));
}
public void putString(String p_128360_, String p_128361_) {
this.tags.put(p_128360_, StringTag.valueOf(p_128361_));
}
public void putByteArray(String p_128383_, byte[] p_128384_) {
this.tags.put(p_128383_, new ByteArrayTag(p_128384_));
}
public void putByteArray(String p_177854_, List<Byte> p_177855_) {
this.tags.put(p_177854_, new ByteArrayTag(p_177855_));
}
public void putIntArray(String p_128386_, int[] p_128387_) {
this.tags.put(p_128386_, new IntArrayTag(p_128387_));
}
public void putIntArray(String p_128409_, List<Integer> p_128410_) {
this.tags.put(p_128409_, new IntArrayTag(p_128410_));
}
public void putLongArray(String p_128389_, long[] p_128390_) {
this.tags.put(p_128389_, new LongArrayTag(p_128390_));
}
public void putLongArray(String p_128429_, List<Long> p_128430_) {
this.tags.put(p_128429_, new LongArrayTag(p_128430_));
}
public void putBoolean(String p_128380_, boolean p_128381_) {
this.tags.put(p_128380_, ByteTag.valueOf(p_128381_));
}
@Nullable
public Tag get(String p_128424_) {
return this.tags.get(p_128424_);
}
public byte getTagType(String p_128436_) {
Tag tag = this.tags.get(p_128436_);
return tag == null ? 0 : tag.getId();
}
public boolean contains(String p_128442_) {
return this.tags.containsKey(p_128442_);
}
public boolean contains(String p_128426_, int p_128427_) {
int i = this.getTagType(p_128426_);
if (i == p_128427_) {
return true;
} else if (p_128427_ != 99) {
return false;
} else {
return i == 1 || i == 2 || i == 3 || i == 4 || i == 5 || i == 6;
}
}
public byte getByte(String p_128446_) {
try {
if (this.contains(p_128446_, 99)) {
return ((NumericTag)this.tags.get(p_128446_)).getAsByte();
}
} catch (ClassCastException classcastexception) {
}
return 0;
}
public short getShort(String p_128449_) {
try {
if (this.contains(p_128449_, 99)) {
return ((NumericTag)this.tags.get(p_128449_)).getAsShort();
}
} catch (ClassCastException classcastexception) {
}
return 0;
}
public int getInt(String p_128452_) {
try {
if (this.contains(p_128452_, 99)) {
return ((NumericTag)this.tags.get(p_128452_)).getAsInt();
}
} catch (ClassCastException classcastexception) {
}
return 0;
}
public long getLong(String p_128455_) {
try {
if (this.contains(p_128455_, 99)) {
return ((NumericTag)this.tags.get(p_128455_)).getAsLong();
}
} catch (ClassCastException classcastexception) {
}
return 0L;
}
public float getFloat(String p_128458_) {
try {
if (this.contains(p_128458_, 99)) {
return ((NumericTag)this.tags.get(p_128458_)).getAsFloat();
}
} catch (ClassCastException classcastexception) {
}
return 0.0F;
}
public double getDouble(String p_128460_) {
try {
if (this.contains(p_128460_, 99)) {
return ((NumericTag)this.tags.get(p_128460_)).getAsDouble();
}
} catch (ClassCastException classcastexception) {
}
return 0.0D;
}
public String getString(String p_128462_) {
try {
if (this.contains(p_128462_, 8)) {
return this.tags.get(p_128462_).getAsString();
}
} catch (ClassCastException classcastexception) {
}
return "";
}
public byte[] getByteArray(String p_128464_) {
try {
if (this.contains(p_128464_, 7)) {
return ((ByteArrayTag)this.tags.get(p_128464_)).getAsByteArray();
}
} catch (ClassCastException classcastexception) {
throw new ReportedException(this.createReport(p_128464_, ByteArrayTag.TYPE, classcastexception));
}
return new byte[0];
}
public int[] getIntArray(String p_128466_) {
try {
if (this.contains(p_128466_, 11)) {
return ((IntArrayTag)this.tags.get(p_128466_)).getAsIntArray();
}
} catch (ClassCastException classcastexception) {
throw new ReportedException(this.createReport(p_128466_, IntArrayTag.TYPE, classcastexception));
}
return new int[0];
}
public long[] getLongArray(String p_128468_) {
try {
if (this.contains(p_128468_, 12)) {
return ((LongArrayTag)this.tags.get(p_128468_)).getAsLongArray();
}
} catch (ClassCastException classcastexception) {
throw new ReportedException(this.createReport(p_128468_, LongArrayTag.TYPE, classcastexception));
}
return new long[0];
}
public CompoundTag getCompound(String p_128470_) {
try {
if (this.contains(p_128470_, 10)) {
return (CompoundTag)this.tags.get(p_128470_);
}
} catch (ClassCastException classcastexception) {
throw new ReportedException(this.createReport(p_128470_, TYPE, classcastexception));
}
return new CompoundTag();
}
public ListTag getList(String p_128438_, int p_128439_) {
try {
if (this.getTagType(p_128438_) == 9) {
ListTag listtag = (ListTag)this.tags.get(p_128438_);
if (!listtag.isEmpty() && listtag.getElementType() != p_128439_) {
return new ListTag();
}
return listtag;
}
} catch (ClassCastException classcastexception) {
throw new ReportedException(this.createReport(p_128438_, ListTag.TYPE, classcastexception));
}
return new ListTag();
}
public boolean getBoolean(String p_128472_) {
return this.getByte(p_128472_) != 0;
}
public void remove(String p_128474_) {
this.tags.remove(p_128474_);
}
public String toString() {
return this.getAsString();
}
public boolean isEmpty() {
return this.tags.isEmpty();
}
private CrashReport createReport(String p_128373_, TagType<?> p_128374_, ClassCastException p_128375_) {
CrashReport crashreport = CrashReport.forThrowable(p_128375_, "Reading NBT data");
CrashReportCategory crashreportcategory = crashreport.addCategory("Corrupt NBT tag", 1);
crashreportcategory.setDetail("Tag type found", () -> {
return this.tags.get(p_128373_).getType().getName();
});
crashreportcategory.setDetail("Tag type expected", p_128374_::getName);
crashreportcategory.setDetail("Tag name", p_128373_);
return crashreport;
}
public CompoundTag copy() {
Map<String, Tag> map = Maps.newHashMap(Maps.transformValues(this.tags, Tag::copy));
return new CompoundTag(map);
}
public boolean equals(Object p_128444_) {
if (this == p_128444_) {
return true;
} else {
return p_128444_ instanceof CompoundTag && Objects.equals(this.tags, ((CompoundTag)p_128444_).tags);
}
}
public int hashCode() {
return this.tags.hashCode();
}
private static void writeNamedTag(String p_128369_, Tag p_128370_, DataOutput p_128371_) throws IOException {
p_128371_.writeByte(p_128370_.getId());
if (p_128370_.getId() != 0) {
p_128371_.writeUTF(p_128369_);
p_128370_.write(p_128371_);
}
}
static byte readNamedTagType(DataInput p_128421_, NbtAccounter p_128422_) throws IOException {
p_128422_.accountBytes(2);
return p_128421_.readByte();
}
static String readNamedTagName(DataInput p_128433_, NbtAccounter p_128434_) throws IOException {
return p_128434_.readUTF(p_128433_.readUTF());
}
static Tag readNamedTagData(TagType<?> p_128414_, String p_128415_, DataInput p_128416_, int p_128417_, NbtAccounter p_128418_) {
try {
return p_128414_.load(p_128416_, p_128417_, p_128418_);
} catch (IOException ioexception) {
CrashReport crashreport = CrashReport.forThrowable(ioexception, "Loading NBT data");
CrashReportCategory crashreportcategory = crashreport.addCategory("NBT Tag");
crashreportcategory.setDetail("Tag name", p_128415_);
crashreportcategory.setDetail("Tag type", p_128414_.getName());
throw new ReportedException(crashreport);
}
}
public CompoundTag merge(CompoundTag p_128392_) {
for(String s : p_128392_.tags.keySet()) {
Tag tag = p_128392_.tags.get(s);
if (tag.getId() == 10) {
if (this.contains(s, 10)) {
CompoundTag compoundtag = this.getCompound(s);
compoundtag.merge((CompoundTag)tag);
} else {
this.put(s, tag.copy());
}
} else {
this.put(s, tag.copy());
}
}
return this;
}
public void accept(TagVisitor p_177857_) {
p_177857_.visitCompound(this);
}
protected Map<String, Tag> entries() {
return Collections.unmodifiableMap(this.tags);
}
public StreamTagVisitor.ValueResult accept(StreamTagVisitor p_197442_) {
for(Map.Entry<String, Tag> entry : this.tags.entrySet()) {
Tag tag = entry.getValue();
TagType<?> tagtype = tag.getType();
StreamTagVisitor.EntryResult streamtagvisitor$entryresult = p_197442_.visitEntry(tagtype);
switch (streamtagvisitor$entryresult) {
case HALT:
return StreamTagVisitor.ValueResult.HALT;
case BREAK:
return p_197442_.visitContainerEnd();
case SKIP:
break;
default:
streamtagvisitor$entryresult = p_197442_.visitEntry(tagtype, entry.getKey());
switch (streamtagvisitor$entryresult) {
case HALT:
return StreamTagVisitor.ValueResult.HALT;
case BREAK:
return p_197442_.visitContainerEnd();
case SKIP:
break;
default:
StreamTagVisitor.ValueResult streamtagvisitor$valueresult = tag.accept(p_197442_);
switch (streamtagvisitor$valueresult) {
case HALT:
return StreamTagVisitor.ValueResult.HALT;
case BREAK:
return p_197442_.visitContainerEnd();
}
}
}
}
return p_197442_.visitContainerEnd();
}
}

View File

@@ -0,0 +1,115 @@
package net.minecraft.nbt;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import net.minecraft.util.Mth;
public class DoubleTag extends NumericTag {
private static final int SELF_SIZE_IN_BYTES = 16;
public static final DoubleTag ZERO = new DoubleTag(0.0D);
public static final TagType<DoubleTag> TYPE = new TagType.StaticSize<DoubleTag>() {
public DoubleTag load(DataInput p_128524_, int p_128525_, NbtAccounter p_128526_) throws IOException {
p_128526_.accountBytes(16L);
return DoubleTag.valueOf(p_128524_.readDouble());
}
public StreamTagVisitor.ValueResult parse(DataInput p_197454_, StreamTagVisitor p_197455_) throws IOException {
return p_197455_.visit(p_197454_.readDouble());
}
public int size() {
return 8;
}
public String getName() {
return "DOUBLE";
}
public String getPrettyName() {
return "TAG_Double";
}
public boolean isValue() {
return true;
}
};
private final double data;
private DoubleTag(double p_128498_) {
this.data = p_128498_;
}
public static DoubleTag valueOf(double p_128501_) {
return p_128501_ == 0.0D ? ZERO : new DoubleTag(p_128501_);
}
public void write(DataOutput p_128503_) throws IOException {
p_128503_.writeDouble(this.data);
}
public int sizeInBytes() {
return 16;
}
public byte getId() {
return 6;
}
public TagType<DoubleTag> getType() {
return TYPE;
}
public DoubleTag copy() {
return this;
}
public boolean equals(Object p_128512_) {
if (this == p_128512_) {
return true;
} else {
return p_128512_ instanceof DoubleTag && this.data == ((DoubleTag)p_128512_).data;
}
}
public int hashCode() {
long i = Double.doubleToLongBits(this.data);
return (int)(i ^ i >>> 32);
}
public void accept(TagVisitor p_177860_) {
p_177860_.visitDouble(this);
}
public long getAsLong() {
return (long)Math.floor(this.data);
}
public int getAsInt() {
return Mth.floor(this.data);
}
public short getAsShort() {
return (short)(Mth.floor(this.data) & '\uffff');
}
public byte getAsByte() {
return (byte)(Mth.floor(this.data) & 255);
}
public double getAsDouble() {
return this.data;
}
public float getAsFloat() {
return (float)this.data;
}
public Number getAsNumber() {
return this.data;
}
public StreamTagVisitor.ValueResult accept(StreamTagVisitor p_197452_) {
return p_197452_.visit(this.data);
}
}

View File

@@ -0,0 +1,72 @@
package net.minecraft.nbt;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
public class EndTag implements Tag {
private static final int SELF_SIZE_IN_BYTES = 8;
public static final TagType<EndTag> TYPE = new TagType<EndTag>() {
public EndTag load(DataInput p_128550_, int p_128551_, NbtAccounter p_128552_) {
p_128552_.accountBytes(8L);
return EndTag.INSTANCE;
}
public StreamTagVisitor.ValueResult parse(DataInput p_197465_, StreamTagVisitor p_197466_) {
return p_197466_.visitEnd();
}
public void skip(DataInput p_197462_, int p_197463_) {
}
public void skip(DataInput p_197460_) {
}
public String getName() {
return "END";
}
public String getPrettyName() {
return "TAG_End";
}
public boolean isValue() {
return true;
}
};
public static final EndTag INSTANCE = new EndTag();
private EndTag() {
}
public void write(DataOutput p_128539_) throws IOException {
}
public int sizeInBytes() {
return 8;
}
public byte getId() {
return 0;
}
public TagType<EndTag> getType() {
return TYPE;
}
public String toString() {
return this.getAsString();
}
public EndTag copy() {
return this;
}
public void accept(TagVisitor p_177863_) {
p_177863_.visitEnd(this);
}
public StreamTagVisitor.ValueResult accept(StreamTagVisitor p_197458_) {
return p_197458_.visitEnd();
}
}

View File

@@ -0,0 +1,114 @@
package net.minecraft.nbt;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import net.minecraft.util.Mth;
public class FloatTag extends NumericTag {
private static final int SELF_SIZE_IN_BYTES = 12;
public static final FloatTag ZERO = new FloatTag(0.0F);
public static final TagType<FloatTag> TYPE = new TagType.StaticSize<FloatTag>() {
public FloatTag load(DataInput p_128590_, int p_128591_, NbtAccounter p_128592_) throws IOException {
p_128592_.accountBytes(12L);
return FloatTag.valueOf(p_128590_.readFloat());
}
public StreamTagVisitor.ValueResult parse(DataInput p_197470_, StreamTagVisitor p_197471_) throws IOException {
return p_197471_.visit(p_197470_.readFloat());
}
public int size() {
return 4;
}
public String getName() {
return "FLOAT";
}
public String getPrettyName() {
return "TAG_Float";
}
public boolean isValue() {
return true;
}
};
private final float data;
private FloatTag(float p_128564_) {
this.data = p_128564_;
}
public static FloatTag valueOf(float p_128567_) {
return p_128567_ == 0.0F ? ZERO : new FloatTag(p_128567_);
}
public void write(DataOutput p_128569_) throws IOException {
p_128569_.writeFloat(this.data);
}
public int sizeInBytes() {
return 12;
}
public byte getId() {
return 5;
}
public TagType<FloatTag> getType() {
return TYPE;
}
public FloatTag copy() {
return this;
}
public boolean equals(Object p_128578_) {
if (this == p_128578_) {
return true;
} else {
return p_128578_ instanceof FloatTag && this.data == ((FloatTag)p_128578_).data;
}
}
public int hashCode() {
return Float.floatToIntBits(this.data);
}
public void accept(TagVisitor p_177866_) {
p_177866_.visitFloat(this);
}
public long getAsLong() {
return (long)this.data;
}
public int getAsInt() {
return Mth.floor(this.data);
}
public short getAsShort() {
return (short)(Mth.floor(this.data) & '\uffff');
}
public byte getAsByte() {
return (byte)(Mth.floor(this.data) & 255);
}
public double getAsDouble() {
return (double)this.data;
}
public float getAsFloat() {
return this.data;
}
public Number getAsNumber() {
return this.data;
}
public StreamTagVisitor.ValueResult accept(StreamTagVisitor p_197468_) {
return p_197468_.visit(this.data);
}
}

View File

@@ -0,0 +1,174 @@
package net.minecraft.nbt;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
public class IntArrayTag extends CollectionTag<IntTag> {
private static final int SELF_SIZE_IN_BYTES = 24;
public static final TagType<IntArrayTag> TYPE = new TagType.VariableSize<IntArrayTag>() {
public IntArrayTag load(DataInput p_128662_, int p_128663_, NbtAccounter p_128664_) throws IOException {
p_128664_.accountBytes(24L);
int i = p_128662_.readInt();
p_128664_.accountBytes(4L * (long)i);
int[] aint = new int[i];
for(int j = 0; j < i; ++j) {
aint[j] = p_128662_.readInt();
}
return new IntArrayTag(aint);
}
public StreamTagVisitor.ValueResult parse(DataInput p_197478_, StreamTagVisitor p_197479_) throws IOException {
int i = p_197478_.readInt();
int[] aint = new int[i];
for(int j = 0; j < i; ++j) {
aint[j] = p_197478_.readInt();
}
return p_197479_.visit(aint);
}
public void skip(DataInput p_197476_) throws IOException {
p_197476_.skipBytes(p_197476_.readInt() * 4);
}
public String getName() {
return "INT[]";
}
public String getPrettyName() {
return "TAG_Int_Array";
}
};
private int[] data;
public IntArrayTag(int[] p_128605_) {
this.data = p_128605_;
}
public IntArrayTag(List<Integer> p_128603_) {
this(toArray(p_128603_));
}
private static int[] toArray(List<Integer> p_128621_) {
int[] aint = new int[p_128621_.size()];
for(int i = 0; i < p_128621_.size(); ++i) {
Integer integer = p_128621_.get(i);
aint[i] = integer == null ? 0 : integer;
}
return aint;
}
public void write(DataOutput p_128616_) throws IOException {
p_128616_.writeInt(this.data.length);
for(int i : this.data) {
p_128616_.writeInt(i);
}
}
public int sizeInBytes() {
return 24 + 4 * this.data.length;
}
public byte getId() {
return 11;
}
public TagType<IntArrayTag> getType() {
return TYPE;
}
public String toString() {
return this.getAsString();
}
public IntArrayTag copy() {
int[] aint = new int[this.data.length];
System.arraycopy(this.data, 0, aint, 0, this.data.length);
return new IntArrayTag(aint);
}
public boolean equals(Object p_128647_) {
if (this == p_128647_) {
return true;
} else {
return p_128647_ instanceof IntArrayTag && Arrays.equals(this.data, ((IntArrayTag)p_128647_).data);
}
}
public int hashCode() {
return Arrays.hashCode(this.data);
}
public int[] getAsIntArray() {
return this.data;
}
public void accept(TagVisitor p_177869_) {
p_177869_.visitIntArray(this);
}
public int size() {
return this.data.length;
}
public IntTag get(int p_128608_) {
return IntTag.valueOf(this.data[p_128608_]);
}
public IntTag set(int p_128610_, IntTag p_128611_) {
int i = this.data[p_128610_];
this.data[p_128610_] = p_128611_.getAsInt();
return IntTag.valueOf(i);
}
public void add(int p_128629_, IntTag p_128630_) {
this.data = ArrayUtils.add(this.data, p_128629_, p_128630_.getAsInt());
}
public boolean setTag(int p_128613_, Tag p_128614_) {
if (p_128614_ instanceof NumericTag) {
this.data[p_128613_] = ((NumericTag)p_128614_).getAsInt();
return true;
} else {
return false;
}
}
public boolean addTag(int p_128632_, Tag p_128633_) {
if (p_128633_ instanceof NumericTag) {
this.data = ArrayUtils.add(this.data, p_128632_, ((NumericTag)p_128633_).getAsInt());
return true;
} else {
return false;
}
}
public IntTag remove(int p_128627_) {
int i = this.data[p_128627_];
this.data = ArrayUtils.remove(this.data, p_128627_);
return IntTag.valueOf(i);
}
public byte getElementType() {
return 3;
}
public void clear() {
this.data = new int[0];
}
public StreamTagVisitor.ValueResult accept(StreamTagVisitor p_197474_) {
return p_197474_.visit(this.data);
}
}

View File

@@ -0,0 +1,128 @@
package net.minecraft.nbt;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
public class IntTag extends NumericTag {
private static final int SELF_SIZE_IN_BYTES = 12;
public static final TagType<IntTag> TYPE = new TagType.StaticSize<IntTag>() {
public IntTag load(DataInput p_128703_, int p_128704_, NbtAccounter p_128705_) throws IOException {
p_128705_.accountBytes(12L);
return IntTag.valueOf(p_128703_.readInt());
}
public StreamTagVisitor.ValueResult parse(DataInput p_197483_, StreamTagVisitor p_197484_) throws IOException {
return p_197484_.visit(p_197483_.readInt());
}
public int size() {
return 4;
}
public String getName() {
return "INT";
}
public String getPrettyName() {
return "TAG_Int";
}
public boolean isValue() {
return true;
}
};
private final int data;
IntTag(int p_128674_) {
this.data = p_128674_;
}
public static IntTag valueOf(int p_128680_) {
return p_128680_ >= -128 && p_128680_ <= 1024 ? IntTag.Cache.cache[p_128680_ - -128] : new IntTag(p_128680_);
}
public void write(DataOutput p_128682_) throws IOException {
p_128682_.writeInt(this.data);
}
public int sizeInBytes() {
return 12;
}
public byte getId() {
return 3;
}
public TagType<IntTag> getType() {
return TYPE;
}
public IntTag copy() {
return this;
}
public boolean equals(Object p_128691_) {
if (this == p_128691_) {
return true;
} else {
return p_128691_ instanceof IntTag && this.data == ((IntTag)p_128691_).data;
}
}
public int hashCode() {
return this.data;
}
public void accept(TagVisitor p_177984_) {
p_177984_.visitInt(this);
}
public long getAsLong() {
return (long)this.data;
}
public int getAsInt() {
return this.data;
}
public short getAsShort() {
return (short)(this.data & '\uffff');
}
public byte getAsByte() {
return (byte)(this.data & 255);
}
public double getAsDouble() {
return (double)this.data;
}
public float getAsFloat() {
return (float)this.data;
}
public Number getAsNumber() {
return this.data;
}
public StreamTagVisitor.ValueResult accept(StreamTagVisitor p_197481_) {
return p_197481_.visit(this.data);
}
static class Cache {
private static final int HIGH = 1024;
private static final int LOW = -128;
static final IntTag[] cache = new IntTag[1153];
private Cache() {
}
static {
for(int i = 0; i < cache.length; ++i) {
cache[i] = new IntTag(-128 + i);
}
}
}
}

View File

@@ -0,0 +1,380 @@
package net.minecraft.nbt;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
public class ListTag extends CollectionTag<Tag> {
private static final int SELF_SIZE_IN_BYTES = 37;
public static final TagType<ListTag> TYPE = new TagType.VariableSize<ListTag>() {
public ListTag load(DataInput p_128792_, int p_128793_, NbtAccounter p_128794_) throws IOException {
p_128794_.accountBytes(37L);
if (p_128793_ > 512) {
throw new RuntimeException("Tried to read NBT tag with too high complexity, depth > 512");
} else {
byte b0 = p_128792_.readByte();
int i = p_128792_.readInt();
if (b0 == 0 && i > 0) {
throw new RuntimeException("Missing type on ListTag");
} else {
p_128794_.accountBytes(4L * (long)i);
TagType<?> tagtype = TagTypes.getType(b0);
List<Tag> list = Lists.newArrayListWithCapacity(i);
for(int j = 0; j < i; ++j) {
list.add(tagtype.load(p_128792_, p_128793_ + 1, p_128794_));
}
return new ListTag(list, b0);
}
}
}
public StreamTagVisitor.ValueResult parse(DataInput p_197491_, StreamTagVisitor p_197492_) throws IOException {
TagType<?> tagtype = TagTypes.getType(p_197491_.readByte());
int i = p_197491_.readInt();
switch (p_197492_.visitList(tagtype, i)) {
case HALT:
return StreamTagVisitor.ValueResult.HALT;
case BREAK:
tagtype.skip(p_197491_, i);
return p_197492_.visitContainerEnd();
default:
int j = 0;
while(true) {
label45: {
if (j < i) {
switch (p_197492_.visitElement(tagtype, j)) {
case HALT:
return StreamTagVisitor.ValueResult.HALT;
case BREAK:
tagtype.skip(p_197491_);
break;
case SKIP:
tagtype.skip(p_197491_);
break label45;
default:
switch (tagtype.parse(p_197491_, p_197492_)) {
case HALT:
return StreamTagVisitor.ValueResult.HALT;
case BREAK:
break;
default:
break label45;
}
}
}
int k = i - 1 - j;
if (k > 0) {
tagtype.skip(p_197491_, k);
}
return p_197492_.visitContainerEnd();
}
++j;
}
}
}
public void skip(DataInput p_197489_) throws IOException {
TagType<?> tagtype = TagTypes.getType(p_197489_.readByte());
int i = p_197489_.readInt();
tagtype.skip(p_197489_, i);
}
public String getName() {
return "LIST";
}
public String getPrettyName() {
return "TAG_List";
}
};
private final List<Tag> list;
private byte type;
ListTag(List<Tag> p_128721_, byte p_128722_) {
this.list = p_128721_;
this.type = p_128722_;
}
public ListTag() {
this(Lists.newArrayList(), (byte)0);
}
public void write(DataOutput p_128734_) throws IOException {
if (this.list.isEmpty()) {
this.type = 0;
} else {
this.type = this.list.get(0).getId();
}
p_128734_.writeByte(this.type);
p_128734_.writeInt(this.list.size());
for(Tag tag : this.list) {
tag.write(p_128734_);
}
}
public int sizeInBytes() {
int i = 37;
i += 4 * this.list.size();
for(Tag tag : this.list) {
i += tag.sizeInBytes();
}
return i;
}
public byte getId() {
return 9;
}
public TagType<ListTag> getType() {
return TYPE;
}
public String toString() {
return this.getAsString();
}
private void updateTypeAfterRemove() {
if (this.list.isEmpty()) {
this.type = 0;
}
}
public Tag remove(int p_128751_) {
Tag tag = this.list.remove(p_128751_);
this.updateTypeAfterRemove();
return tag;
}
public boolean isEmpty() {
return this.list.isEmpty();
}
public CompoundTag getCompound(int p_128729_) {
if (p_128729_ >= 0 && p_128729_ < this.list.size()) {
Tag tag = this.list.get(p_128729_);
if (tag.getId() == 10) {
return (CompoundTag)tag;
}
}
return new CompoundTag();
}
public ListTag getList(int p_128745_) {
if (p_128745_ >= 0 && p_128745_ < this.list.size()) {
Tag tag = this.list.get(p_128745_);
if (tag.getId() == 9) {
return (ListTag)tag;
}
}
return new ListTag();
}
public short getShort(int p_128758_) {
if (p_128758_ >= 0 && p_128758_ < this.list.size()) {
Tag tag = this.list.get(p_128758_);
if (tag.getId() == 2) {
return ((ShortTag)tag).getAsShort();
}
}
return 0;
}
public int getInt(int p_128764_) {
if (p_128764_ >= 0 && p_128764_ < this.list.size()) {
Tag tag = this.list.get(p_128764_);
if (tag.getId() == 3) {
return ((IntTag)tag).getAsInt();
}
}
return 0;
}
public int[] getIntArray(int p_128768_) {
if (p_128768_ >= 0 && p_128768_ < this.list.size()) {
Tag tag = this.list.get(p_128768_);
if (tag.getId() == 11) {
return ((IntArrayTag)tag).getAsIntArray();
}
}
return new int[0];
}
public long[] getLongArray(int p_177992_) {
if (p_177992_ >= 0 && p_177992_ < this.list.size()) {
Tag tag = this.list.get(p_177992_);
if (tag.getId() == 12) {
return ((LongArrayTag)tag).getAsLongArray();
}
}
return new long[0];
}
public double getDouble(int p_128773_) {
if (p_128773_ >= 0 && p_128773_ < this.list.size()) {
Tag tag = this.list.get(p_128773_);
if (tag.getId() == 6) {
return ((DoubleTag)tag).getAsDouble();
}
}
return 0.0D;
}
public float getFloat(int p_128776_) {
if (p_128776_ >= 0 && p_128776_ < this.list.size()) {
Tag tag = this.list.get(p_128776_);
if (tag.getId() == 5) {
return ((FloatTag)tag).getAsFloat();
}
}
return 0.0F;
}
public String getString(int p_128779_) {
if (p_128779_ >= 0 && p_128779_ < this.list.size()) {
Tag tag = this.list.get(p_128779_);
return tag.getId() == 8 ? tag.getAsString() : tag.toString();
} else {
return "";
}
}
public int size() {
return this.list.size();
}
public Tag get(int p_128781_) {
return this.list.get(p_128781_);
}
public Tag set(int p_128760_, Tag p_128761_) {
Tag tag = this.get(p_128760_);
if (!this.setTag(p_128760_, p_128761_)) {
throw new UnsupportedOperationException(String.format(Locale.ROOT, "Trying to add tag of type %d to list of %d", p_128761_.getId(), this.type));
} else {
return tag;
}
}
public void add(int p_128753_, Tag p_128754_) {
if (!this.addTag(p_128753_, p_128754_)) {
throw new UnsupportedOperationException(String.format(Locale.ROOT, "Trying to add tag of type %d to list of %d", p_128754_.getId(), this.type));
}
}
public boolean setTag(int p_128731_, Tag p_128732_) {
if (this.updateType(p_128732_)) {
this.list.set(p_128731_, p_128732_);
return true;
} else {
return false;
}
}
public boolean addTag(int p_128747_, Tag p_128748_) {
if (this.updateType(p_128748_)) {
this.list.add(p_128747_, p_128748_);
return true;
} else {
return false;
}
}
private boolean updateType(Tag p_128739_) {
if (p_128739_.getId() == 0) {
return false;
} else if (this.type == 0) {
this.type = p_128739_.getId();
return true;
} else {
return this.type == p_128739_.getId();
}
}
public ListTag copy() {
Iterable<Tag> iterable = (Iterable<Tag>)(TagTypes.getType(this.type).isValue() ? this.list : Iterables.transform(this.list, Tag::copy));
List<Tag> list = Lists.newArrayList(iterable);
return new ListTag(list, this.type);
}
public boolean equals(Object p_128766_) {
if (this == p_128766_) {
return true;
} else {
return p_128766_ instanceof ListTag && Objects.equals(this.list, ((ListTag)p_128766_).list);
}
}
public int hashCode() {
return this.list.hashCode();
}
public void accept(TagVisitor p_177990_) {
p_177990_.visitList(this);
}
public byte getElementType() {
return this.type;
}
public void clear() {
this.list.clear();
this.type = 0;
}
public StreamTagVisitor.ValueResult accept(StreamTagVisitor p_197487_) {
switch (p_197487_.visitList(TagTypes.getType(this.type), this.list.size())) {
case HALT:
return StreamTagVisitor.ValueResult.HALT;
case BREAK:
return p_197487_.visitContainerEnd();
default:
int i = 0;
while(i < this.list.size()) {
Tag tag = this.list.get(i);
switch (p_197487_.visitElement(tag.getType(), i)) {
case HALT:
return StreamTagVisitor.ValueResult.HALT;
case BREAK:
return p_197487_.visitContainerEnd();
default:
switch (tag.accept(p_197487_)) {
case HALT:
return StreamTagVisitor.ValueResult.HALT;
case BREAK:
return p_197487_.visitContainerEnd();
}
case SKIP:
++i;
}
}
return p_197487_.visitContainerEnd();
}
}
}

View File

@@ -0,0 +1,179 @@
package net.minecraft.nbt;
import it.unimi.dsi.fastutil.longs.LongSet;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
public class LongArrayTag extends CollectionTag<LongTag> {
private static final int SELF_SIZE_IN_BYTES = 24;
public static final TagType<LongArrayTag> TYPE = new TagType.VariableSize<LongArrayTag>() {
public LongArrayTag load(DataInput p_128865_, int p_128866_, NbtAccounter p_128867_) throws IOException {
p_128867_.accountBytes(24L);
int i = p_128865_.readInt();
p_128867_.accountBytes(8L * (long)i);
long[] along = new long[i];
for(int j = 0; j < i; ++j) {
along[j] = p_128865_.readLong();
}
return new LongArrayTag(along);
}
public StreamTagVisitor.ValueResult parse(DataInput p_197501_, StreamTagVisitor p_197502_) throws IOException {
int i = p_197501_.readInt();
long[] along = new long[i];
for(int j = 0; j < i; ++j) {
along[j] = p_197501_.readLong();
}
return p_197502_.visit(along);
}
public void skip(DataInput p_197499_) throws IOException {
p_197499_.skipBytes(p_197499_.readInt() * 8);
}
public String getName() {
return "LONG[]";
}
public String getPrettyName() {
return "TAG_Long_Array";
}
};
private long[] data;
public LongArrayTag(long[] p_128808_) {
this.data = p_128808_;
}
public LongArrayTag(LongSet p_128804_) {
this.data = p_128804_.toLongArray();
}
public LongArrayTag(List<Long> p_128806_) {
this(toArray(p_128806_));
}
private static long[] toArray(List<Long> p_128824_) {
long[] along = new long[p_128824_.size()];
for(int i = 0; i < p_128824_.size(); ++i) {
Long olong = p_128824_.get(i);
along[i] = olong == null ? 0L : olong;
}
return along;
}
public void write(DataOutput p_128819_) throws IOException {
p_128819_.writeInt(this.data.length);
for(long i : this.data) {
p_128819_.writeLong(i);
}
}
public int sizeInBytes() {
return 24 + 8 * this.data.length;
}
public byte getId() {
return 12;
}
public TagType<LongArrayTag> getType() {
return TYPE;
}
public String toString() {
return this.getAsString();
}
public LongArrayTag copy() {
long[] along = new long[this.data.length];
System.arraycopy(this.data, 0, along, 0, this.data.length);
return new LongArrayTag(along);
}
public boolean equals(Object p_128850_) {
if (this == p_128850_) {
return true;
} else {
return p_128850_ instanceof LongArrayTag && Arrays.equals(this.data, ((LongArrayTag)p_128850_).data);
}
}
public int hashCode() {
return Arrays.hashCode(this.data);
}
public void accept(TagVisitor p_177995_) {
p_177995_.visitLongArray(this);
}
public long[] getAsLongArray() {
return this.data;
}
public int size() {
return this.data.length;
}
public LongTag get(int p_128811_) {
return LongTag.valueOf(this.data[p_128811_]);
}
public LongTag set(int p_128813_, LongTag p_128814_) {
long i = this.data[p_128813_];
this.data[p_128813_] = p_128814_.getAsLong();
return LongTag.valueOf(i);
}
public void add(int p_128832_, LongTag p_128833_) {
this.data = ArrayUtils.add(this.data, p_128832_, p_128833_.getAsLong());
}
public boolean setTag(int p_128816_, Tag p_128817_) {
if (p_128817_ instanceof NumericTag) {
this.data[p_128816_] = ((NumericTag)p_128817_).getAsLong();
return true;
} else {
return false;
}
}
public boolean addTag(int p_128835_, Tag p_128836_) {
if (p_128836_ instanceof NumericTag) {
this.data = ArrayUtils.add(this.data, p_128835_, ((NumericTag)p_128836_).getAsLong());
return true;
} else {
return false;
}
}
public LongTag remove(int p_128830_) {
long i = this.data[p_128830_];
this.data = ArrayUtils.remove(this.data, p_128830_);
return LongTag.valueOf(i);
}
public byte getElementType() {
return 4;
}
public void clear() {
this.data = new long[0];
}
public StreamTagVisitor.ValueResult accept(StreamTagVisitor p_197497_) {
return p_197497_.visit(this.data);
}
}

View File

@@ -0,0 +1,128 @@
package net.minecraft.nbt;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
public class LongTag extends NumericTag {
private static final int SELF_SIZE_IN_BYTES = 16;
public static final TagType<LongTag> TYPE = new TagType.StaticSize<LongTag>() {
public LongTag load(DataInput p_128906_, int p_128907_, NbtAccounter p_128908_) throws IOException {
p_128908_.accountBytes(16L);
return LongTag.valueOf(p_128906_.readLong());
}
public StreamTagVisitor.ValueResult parse(DataInput p_197506_, StreamTagVisitor p_197507_) throws IOException {
return p_197507_.visit(p_197506_.readLong());
}
public int size() {
return 8;
}
public String getName() {
return "LONG";
}
public String getPrettyName() {
return "TAG_Long";
}
public boolean isValue() {
return true;
}
};
private final long data;
LongTag(long p_128877_) {
this.data = p_128877_;
}
public static LongTag valueOf(long p_128883_) {
return p_128883_ >= -128L && p_128883_ <= 1024L ? LongTag.Cache.cache[(int)p_128883_ - -128] : new LongTag(p_128883_);
}
public void write(DataOutput p_128885_) throws IOException {
p_128885_.writeLong(this.data);
}
public int sizeInBytes() {
return 16;
}
public byte getId() {
return 4;
}
public TagType<LongTag> getType() {
return TYPE;
}
public LongTag copy() {
return this;
}
public boolean equals(Object p_128894_) {
if (this == p_128894_) {
return true;
} else {
return p_128894_ instanceof LongTag && this.data == ((LongTag)p_128894_).data;
}
}
public int hashCode() {
return (int)(this.data ^ this.data >>> 32);
}
public void accept(TagVisitor p_177998_) {
p_177998_.visitLong(this);
}
public long getAsLong() {
return this.data;
}
public int getAsInt() {
return (int)(this.data & -1L);
}
public short getAsShort() {
return (short)((int)(this.data & 65535L));
}
public byte getAsByte() {
return (byte)((int)(this.data & 255L));
}
public double getAsDouble() {
return (double)this.data;
}
public float getAsFloat() {
return (float)this.data;
}
public Number getAsNumber() {
return this.data;
}
public StreamTagVisitor.ValueResult accept(StreamTagVisitor p_197504_) {
return p_197504_.visit(this.data);
}
static class Cache {
private static final int HIGH = 1024;
private static final int LOW = -128;
static final LongTag[] cache = new LongTag[1153];
private Cache() {
}
static {
for(int i = 0; i < cache.length; ++i) {
cache[i] = new LongTag((long)(-128 + i));
}
}
}
}

View File

@@ -0,0 +1,58 @@
package net.minecraft.nbt;
import com.google.common.annotations.VisibleForTesting;
public class NbtAccounter {
public static final NbtAccounter UNLIMITED = new NbtAccounter(0L) {
public void accountBytes(long p_128927_) {
}
};
private final long quota;
private long usage;
public NbtAccounter(long p_128922_) {
this.quota = p_128922_;
}
public void accountBytes(long p_263515_) {
this.usage += p_263515_;
if (this.usage > this.quota) {
throw new RuntimeException("Tried to read NBT tag that was too big; tried to allocate: " + this.usage + "bytes where max allowed: " + this.quota);
}
}
/*
* UTF8 is not a simple encoding system, each character can be either
* 1, 2, or 3 bytes. Depending on where it's numerical value falls.
* We have to count up each character individually to see the true
* length of the data.
*
* Basic concept is that it uses the MSB of each byte as a 'read more' signal.
* So it has to shift each 7-bit segment.
*
* This will accurately count the correct byte length to encode this string, plus the 2 bytes for it's length prefix.
*/
public String readUTF(String data) {
accountBytes(2); //Header length
if (data == null)
return data;
int len = data.length();
int utflen = 0;
for (int i = 0; i < len; i++) {
int c = data.charAt(i);
if ((c >= 0x0001) && (c <= 0x007F)) utflen += 1;
else if (c > 0x07FF) utflen += 3;
else utflen += 2;
}
accountBytes(utflen);
return data;
}
@VisibleForTesting
public long getUsage() {
return this.usage;
}
}

View File

@@ -0,0 +1,162 @@
package net.minecraft.nbt;
import java.io.BufferedOutputStream;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import javax.annotation.Nullable;
import net.minecraft.CrashReport;
import net.minecraft.CrashReportCategory;
import net.minecraft.ReportedException;
import net.minecraft.util.FastBufferedInputStream;
public class NbtIo {
public static CompoundTag readCompressed(File p_128938_) throws IOException {
try (InputStream inputstream = new FileInputStream(p_128938_)) {
return readCompressed(inputstream);
}
}
private static DataInputStream createDecompressorStream(InputStream p_202494_) throws IOException {
return new DataInputStream(new FastBufferedInputStream(new GZIPInputStream(p_202494_)));
}
public static CompoundTag readCompressed(InputStream p_128940_) throws IOException {
try (DataInputStream datainputstream = createDecompressorStream(p_128940_)) {
return read(datainputstream, NbtAccounter.UNLIMITED);
}
}
public static void parseCompressed(File p_202488_, StreamTagVisitor p_202489_) throws IOException {
try (InputStream inputstream = new FileInputStream(p_202488_)) {
parseCompressed(inputstream, p_202489_);
}
}
public static void parseCompressed(InputStream p_202491_, StreamTagVisitor p_202492_) throws IOException {
try (DataInputStream datainputstream = createDecompressorStream(p_202491_)) {
parse(datainputstream, p_202492_);
}
}
public static void writeCompressed(CompoundTag p_128945_, File p_128946_) throws IOException {
try (OutputStream outputstream = new FileOutputStream(p_128946_)) {
writeCompressed(p_128945_, outputstream);
}
}
public static void writeCompressed(CompoundTag p_128948_, OutputStream p_128949_) throws IOException {
try (DataOutputStream dataoutputstream = new DataOutputStream(new BufferedOutputStream(new GZIPOutputStream(p_128949_)))) {
write(p_128948_, dataoutputstream);
}
}
public static void write(CompoundTag p_128956_, File p_128957_) throws IOException {
try (
FileOutputStream fileoutputstream = new FileOutputStream(p_128957_);
DataOutputStream dataoutputstream = new DataOutputStream(fileoutputstream);
) {
write(p_128956_, dataoutputstream);
}
}
@Nullable
public static CompoundTag read(File p_128954_) throws IOException {
if (!p_128954_.exists()) {
return null;
} else {
CompoundTag compoundtag;
try (
FileInputStream fileinputstream = new FileInputStream(p_128954_);
DataInputStream datainputstream = new DataInputStream(fileinputstream);
) {
compoundtag = read(datainputstream, NbtAccounter.UNLIMITED);
}
return compoundtag;
}
}
public static CompoundTag read(DataInput p_128929_) throws IOException {
return read(p_128929_, NbtAccounter.UNLIMITED);
}
public static CompoundTag read(DataInput p_128935_, NbtAccounter p_128936_) throws IOException {
Tag tag = readUnnamedTag(p_128935_, 0, p_128936_);
if (tag instanceof CompoundTag) {
return (CompoundTag)tag;
} else {
throw new IOException("Root tag must be a named compound tag");
}
}
public static void write(CompoundTag p_128942_, DataOutput p_128943_) throws IOException {
writeUnnamedTag(p_128942_, p_128943_);
}
public static void parse(DataInput p_197510_, StreamTagVisitor p_197511_) throws IOException {
TagType<?> tagtype = TagTypes.getType(p_197510_.readByte());
if (tagtype == EndTag.TYPE) {
if (p_197511_.visitRootEntry(EndTag.TYPE) == StreamTagVisitor.ValueResult.CONTINUE) {
p_197511_.visitEnd();
}
} else {
switch (p_197511_.visitRootEntry(tagtype)) {
case HALT:
default:
break;
case BREAK:
StringTag.skipString(p_197510_);
tagtype.skip(p_197510_);
break;
case CONTINUE:
StringTag.skipString(p_197510_);
tagtype.parse(p_197510_, p_197511_);
}
}
}
public static void writeUnnamedTag(Tag p_128951_, DataOutput p_128952_) throws IOException {
p_128952_.writeByte(p_128951_.getId());
if (p_128951_.getId() != 0) {
p_128952_.writeUTF("");
p_128951_.write(p_128952_);
}
}
private static Tag readUnnamedTag(DataInput p_128931_, int p_128932_, NbtAccounter p_128933_) throws IOException {
byte b0 = p_128931_.readByte();
p_128933_.accountBytes(1); // Forge: Count everything!
if (b0 == 0) {
return EndTag.INSTANCE;
} else {
p_128933_.readUTF(p_128931_.readUTF()); //Forge: Count this string.
p_128933_.accountBytes(4); //Forge: 4 extra bytes for the object allocation.
try {
return TagTypes.getType(b0).load(p_128931_, p_128932_, p_128933_);
} catch (IOException ioexception) {
CrashReport crashreport = CrashReport.forThrowable(ioexception, "Loading NBT data");
CrashReportCategory crashreportcategory = crashreport.addCategory("NBT Tag");
crashreportcategory.setDetail("Tag type", b0);
throw new ReportedException(crashreport);
}
}
}
}

View File

@@ -0,0 +1,654 @@
package net.minecraft.nbt;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mojang.datafixers.DataFixUtils;
import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.DynamicOps;
import com.mojang.serialization.MapLike;
import com.mojang.serialization.RecordBuilder;
import it.unimi.dsi.fastutil.bytes.ByteArrayList;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.longs.LongArrayList;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;
import javax.annotation.Nullable;
public class NbtOps implements DynamicOps<Tag> {
public static final NbtOps INSTANCE = new NbtOps();
private static final String WRAPPER_MARKER = "";
protected NbtOps() {
}
public Tag empty() {
return EndTag.INSTANCE;
}
public <U> U convertTo(DynamicOps<U> p_128980_, Tag p_128981_) {
switch (p_128981_.getId()) {
case 0:
return p_128980_.empty();
case 1:
return p_128980_.createByte(((NumericTag)p_128981_).getAsByte());
case 2:
return p_128980_.createShort(((NumericTag)p_128981_).getAsShort());
case 3:
return p_128980_.createInt(((NumericTag)p_128981_).getAsInt());
case 4:
return p_128980_.createLong(((NumericTag)p_128981_).getAsLong());
case 5:
return p_128980_.createFloat(((NumericTag)p_128981_).getAsFloat());
case 6:
return p_128980_.createDouble(((NumericTag)p_128981_).getAsDouble());
case 7:
return p_128980_.createByteList(ByteBuffer.wrap(((ByteArrayTag)p_128981_).getAsByteArray()));
case 8:
return p_128980_.createString(p_128981_.getAsString());
case 9:
return this.convertList(p_128980_, p_128981_);
case 10:
return this.convertMap(p_128980_, p_128981_);
case 11:
return p_128980_.createIntList(Arrays.stream(((IntArrayTag)p_128981_).getAsIntArray()));
case 12:
return p_128980_.createLongList(Arrays.stream(((LongArrayTag)p_128981_).getAsLongArray()));
default:
throw new IllegalStateException("Unknown tag type: " + p_128981_);
}
}
public DataResult<Number> getNumberValue(Tag p_129030_) {
if (p_129030_ instanceof NumericTag numerictag) {
return DataResult.success(numerictag.getAsNumber());
} else {
return DataResult.error(() -> {
return "Not a number";
});
}
}
public Tag createNumeric(Number p_128983_) {
return DoubleTag.valueOf(p_128983_.doubleValue());
}
public Tag createByte(byte p_128963_) {
return ByteTag.valueOf(p_128963_);
}
public Tag createShort(short p_129048_) {
return ShortTag.valueOf(p_129048_);
}
public Tag createInt(int p_128976_) {
return IntTag.valueOf(p_128976_);
}
public Tag createLong(long p_128978_) {
return LongTag.valueOf(p_128978_);
}
public Tag createFloat(float p_128974_) {
return FloatTag.valueOf(p_128974_);
}
public Tag createDouble(double p_128972_) {
return DoubleTag.valueOf(p_128972_);
}
public Tag createBoolean(boolean p_129050_) {
return ByteTag.valueOf(p_129050_);
}
public DataResult<String> getStringValue(Tag p_129061_) {
if (p_129061_ instanceof StringTag stringtag) {
return DataResult.success(stringtag.getAsString());
} else {
return DataResult.error(() -> {
return "Not a string";
});
}
}
public Tag createString(String p_128985_) {
return StringTag.valueOf(p_128985_);
}
public DataResult<Tag> mergeToList(Tag p_129041_, Tag p_129042_) {
return createCollector(p_129041_).map((p_248053_) -> {
return DataResult.success(p_248053_.accept(p_129042_).result());
}).orElseGet(() -> {
return DataResult.error(() -> {
return "mergeToList called with not a list: " + p_129041_;
}, p_129041_);
});
}
public DataResult<Tag> mergeToList(Tag p_129038_, List<Tag> p_129039_) {
return createCollector(p_129038_).map((p_248048_) -> {
return DataResult.success(p_248048_.acceptAll(p_129039_).result());
}).orElseGet(() -> {
return DataResult.error(() -> {
return "mergeToList called with not a list: " + p_129038_;
}, p_129038_);
});
}
public DataResult<Tag> mergeToMap(Tag p_129044_, Tag p_129045_, Tag p_129046_) {
if (!(p_129044_ instanceof CompoundTag) && !(p_129044_ instanceof EndTag)) {
return DataResult.error(() -> {
return "mergeToMap called with not a map: " + p_129044_;
}, p_129044_);
} else if (!(p_129045_ instanceof StringTag)) {
return DataResult.error(() -> {
return "key is not a string: " + p_129045_;
}, p_129044_);
} else {
CompoundTag compoundtag = new CompoundTag();
if (p_129044_ instanceof CompoundTag) {
CompoundTag compoundtag1 = (CompoundTag)p_129044_;
compoundtag1.getAllKeys().forEach((p_129068_) -> {
compoundtag.put(p_129068_, compoundtag1.get(p_129068_));
});
}
compoundtag.put(p_129045_.getAsString(), p_129046_);
return DataResult.success(compoundtag);
}
}
public DataResult<Tag> mergeToMap(Tag p_129032_, MapLike<Tag> p_129033_) {
if (!(p_129032_ instanceof CompoundTag) && !(p_129032_ instanceof EndTag)) {
return DataResult.error(() -> {
return "mergeToMap called with not a map: " + p_129032_;
}, p_129032_);
} else {
CompoundTag compoundtag = new CompoundTag();
if (p_129032_ instanceof CompoundTag) {
CompoundTag compoundtag1 = (CompoundTag)p_129032_;
compoundtag1.getAllKeys().forEach((p_129059_) -> {
compoundtag.put(p_129059_, compoundtag1.get(p_129059_));
});
}
List<Tag> list = Lists.newArrayList();
p_129033_.entries().forEach((p_128994_) -> {
Tag tag = p_128994_.getFirst();
if (!(tag instanceof StringTag)) {
list.add(tag);
} else {
compoundtag.put(tag.getAsString(), p_128994_.getSecond());
}
});
return !list.isEmpty() ? DataResult.error(() -> {
return "some keys are not strings: " + list;
}, compoundtag) : DataResult.success(compoundtag);
}
}
public DataResult<Stream<Pair<Tag, Tag>>> getMapValues(Tag p_129070_) {
if (p_129070_ instanceof CompoundTag compoundtag) {
return DataResult.success(compoundtag.getAllKeys().stream().map((p_129021_) -> {
return Pair.of(this.createString(p_129021_), compoundtag.get(p_129021_));
}));
} else {
return DataResult.error(() -> {
return "Not a map: " + p_129070_;
});
}
}
public DataResult<Consumer<BiConsumer<Tag, Tag>>> getMapEntries(Tag p_129103_) {
if (p_129103_ instanceof CompoundTag compoundtag) {
return DataResult.success((p_129024_) -> {
compoundtag.getAllKeys().forEach((p_178006_) -> {
p_129024_.accept(this.createString(p_178006_), compoundtag.get(p_178006_));
});
});
} else {
return DataResult.error(() -> {
return "Not a map: " + p_129103_;
});
}
}
public DataResult<MapLike<Tag>> getMap(Tag p_129105_) {
if (p_129105_ instanceof final CompoundTag compoundtag) {
return DataResult.success(new MapLike<Tag>() {
@Nullable
public Tag get(Tag p_129174_) {
return compoundtag.get(p_129174_.getAsString());
}
@Nullable
public Tag get(String p_129169_) {
return compoundtag.get(p_129169_);
}
public Stream<Pair<Tag, Tag>> entries() {
return compoundtag.getAllKeys().stream().map((p_129172_) -> {
return Pair.of(NbtOps.this.createString(p_129172_), compoundtag.get(p_129172_));
});
}
public String toString() {
return "MapLike[" + compoundtag + "]";
}
});
} else {
return DataResult.error(() -> {
return "Not a map: " + p_129105_;
});
}
}
public Tag createMap(Stream<Pair<Tag, Tag>> p_129004_) {
CompoundTag compoundtag = new CompoundTag();
p_129004_.forEach((p_129018_) -> {
compoundtag.put(p_129018_.getFirst().getAsString(), p_129018_.getSecond());
});
return compoundtag;
}
private static Tag tryUnwrap(CompoundTag p_251041_) {
if (p_251041_.size() == 1) {
Tag tag = p_251041_.get("");
if (tag != null) {
return tag;
}
}
return p_251041_;
}
public DataResult<Stream<Tag>> getStream(Tag p_129108_) {
if (p_129108_ instanceof ListTag listtag) {
return listtag.getElementType() == 10 ? DataResult.success(listtag.stream().map((p_248049_) -> {
return tryUnwrap((CompoundTag)p_248049_);
})) : DataResult.success(listtag.stream());
} else if (p_129108_ instanceof CollectionTag<?> collectiontag) {
return DataResult.success(collectiontag.stream().map((p_129158_) -> {
return p_129158_;
}));
} else {
return DataResult.error(() -> {
return "Not a list";
});
}
}
public DataResult<Consumer<Consumer<Tag>>> getList(Tag p_129110_) {
if (p_129110_ instanceof ListTag listtag) {
return listtag.getElementType() == 10 ? DataResult.success((p_248055_) -> {
listtag.forEach((p_248051_) -> {
p_248055_.accept(tryUnwrap((CompoundTag)p_248051_));
});
}) : DataResult.success(listtag::forEach);
} else if (p_129110_ instanceof CollectionTag<?> collectiontag) {
return DataResult.success(collectiontag::forEach);
} else {
return DataResult.error(() -> {
return "Not a list: " + p_129110_;
});
}
}
public DataResult<ByteBuffer> getByteBuffer(Tag p_129132_) {
if (p_129132_ instanceof ByteArrayTag bytearraytag) {
return DataResult.success(ByteBuffer.wrap(bytearraytag.getAsByteArray()));
} else {
return DynamicOps.super.getByteBuffer(p_129132_);
}
}
public Tag createByteList(ByteBuffer p_128990_) {
return new ByteArrayTag(DataFixUtils.toArray(p_128990_));
}
public DataResult<IntStream> getIntStream(Tag p_129134_) {
if (p_129134_ instanceof IntArrayTag intarraytag) {
return DataResult.success(Arrays.stream(intarraytag.getAsIntArray()));
} else {
return DynamicOps.super.getIntStream(p_129134_);
}
}
public Tag createIntList(IntStream p_129000_) {
return new IntArrayTag(p_129000_.toArray());
}
public DataResult<LongStream> getLongStream(Tag p_129136_) {
if (p_129136_ instanceof LongArrayTag longarraytag) {
return DataResult.success(Arrays.stream(longarraytag.getAsLongArray()));
} else {
return DynamicOps.super.getLongStream(p_129136_);
}
}
public Tag createLongList(LongStream p_129002_) {
return new LongArrayTag(p_129002_.toArray());
}
public Tag createList(Stream<Tag> p_129052_) {
return NbtOps.InitialListCollector.INSTANCE.acceptAll(p_129052_).result();
}
public Tag remove(Tag p_129035_, String p_129036_) {
if (p_129035_ instanceof CompoundTag compoundtag) {
CompoundTag compoundtag1 = new CompoundTag();
compoundtag.getAllKeys().stream().filter((p_128988_) -> {
return !Objects.equals(p_128988_, p_129036_);
}).forEach((p_129028_) -> {
compoundtag1.put(p_129028_, compoundtag.get(p_129028_));
});
return compoundtag1;
} else {
return p_129035_;
}
}
public String toString() {
return "NBT";
}
public RecordBuilder<Tag> mapBuilder() {
return new NbtOps.NbtRecordBuilder();
}
private static Optional<NbtOps.ListCollector> createCollector(Tag p_249503_) {
if (p_249503_ instanceof EndTag) {
return Optional.of(NbtOps.InitialListCollector.INSTANCE);
} else {
if (p_249503_ instanceof CollectionTag) {
CollectionTag<?> collectiontag = (CollectionTag)p_249503_;
if (collectiontag.isEmpty()) {
return Optional.of(NbtOps.InitialListCollector.INSTANCE);
}
if (collectiontag instanceof ListTag) {
ListTag listtag = (ListTag)collectiontag;
Optional optional;
switch (listtag.getElementType()) {
case 0:
optional = Optional.of(NbtOps.InitialListCollector.INSTANCE);
break;
case 10:
optional = Optional.of(new NbtOps.HeterogenousListCollector(listtag));
break;
default:
optional = Optional.of(new NbtOps.HomogenousListCollector(listtag));
}
return optional;
}
if (collectiontag instanceof ByteArrayTag) {
ByteArrayTag bytearraytag = (ByteArrayTag)collectiontag;
return Optional.of(new NbtOps.ByteListCollector(bytearraytag.getAsByteArray()));
}
if (collectiontag instanceof IntArrayTag) {
IntArrayTag intarraytag = (IntArrayTag)collectiontag;
return Optional.of(new NbtOps.IntListCollector(intarraytag.getAsIntArray()));
}
if (collectiontag instanceof LongArrayTag) {
LongArrayTag longarraytag = (LongArrayTag)collectiontag;
return Optional.of(new NbtOps.LongListCollector(longarraytag.getAsLongArray()));
}
}
return Optional.empty();
}
}
static class ByteListCollector implements NbtOps.ListCollector {
private final ByteArrayList values = new ByteArrayList();
public ByteListCollector(byte p_249905_) {
this.values.add(p_249905_);
}
public ByteListCollector(byte[] p_250457_) {
this.values.addElements(0, p_250457_);
}
public NbtOps.ListCollector accept(Tag p_250723_) {
if (p_250723_ instanceof ByteTag bytetag) {
this.values.add(bytetag.getAsByte());
return this;
} else {
return (new NbtOps.HeterogenousListCollector(this.values)).accept(p_250723_);
}
}
public Tag result() {
return new ByteArrayTag(this.values.toByteArray());
}
}
static class HeterogenousListCollector implements NbtOps.ListCollector {
private final ListTag result = new ListTag();
public HeterogenousListCollector() {
}
public HeterogenousListCollector(Collection<Tag> p_249606_) {
this.result.addAll(p_249606_);
}
public HeterogenousListCollector(IntArrayList p_250270_) {
p_250270_.forEach((p_249166_) -> {
this.result.add(wrapElement(IntTag.valueOf(p_249166_)));
});
}
public HeterogenousListCollector(ByteArrayList p_248575_) {
p_248575_.forEach((p_249160_) -> {
this.result.add(wrapElement(ByteTag.valueOf(p_249160_)));
});
}
public HeterogenousListCollector(LongArrayList p_249410_) {
p_249410_.forEach((p_249754_) -> {
this.result.add(wrapElement(LongTag.valueOf(p_249754_)));
});
}
private static boolean isWrapper(CompoundTag p_252073_) {
return p_252073_.size() == 1 && p_252073_.contains("");
}
private static Tag wrapIfNeeded(Tag p_252042_) {
if (p_252042_ instanceof CompoundTag compoundtag) {
if (!isWrapper(compoundtag)) {
return compoundtag;
}
}
return wrapElement(p_252042_);
}
private static CompoundTag wrapElement(Tag p_251263_) {
CompoundTag compoundtag = new CompoundTag();
compoundtag.put("", p_251263_);
return compoundtag;
}
public NbtOps.ListCollector accept(Tag p_249045_) {
this.result.add(wrapIfNeeded(p_249045_));
return this;
}
public Tag result() {
return this.result;
}
}
static class HomogenousListCollector implements NbtOps.ListCollector {
private final ListTag result = new ListTag();
HomogenousListCollector(Tag p_249247_) {
this.result.add(p_249247_);
}
HomogenousListCollector(ListTag p_249889_) {
this.result.addAll(p_249889_);
}
public NbtOps.ListCollector accept(Tag p_248727_) {
if (p_248727_.getId() != this.result.getElementType()) {
return (new NbtOps.HeterogenousListCollector()).acceptAll(this.result).accept(p_248727_);
} else {
this.result.add(p_248727_);
return this;
}
}
public Tag result() {
return this.result;
}
}
static class InitialListCollector implements NbtOps.ListCollector {
public static final NbtOps.InitialListCollector INSTANCE = new NbtOps.InitialListCollector();
private InitialListCollector() {
}
public NbtOps.ListCollector accept(Tag p_251635_) {
if (p_251635_ instanceof CompoundTag compoundtag) {
return (new NbtOps.HeterogenousListCollector()).accept(compoundtag);
} else if (p_251635_ instanceof ByteTag bytetag) {
return new NbtOps.ByteListCollector(bytetag.getAsByte());
} else if (p_251635_ instanceof IntTag inttag) {
return new NbtOps.IntListCollector(inttag.getAsInt());
} else if (p_251635_ instanceof LongTag longtag) {
return new NbtOps.LongListCollector(longtag.getAsLong());
} else {
return new NbtOps.HomogenousListCollector(p_251635_);
}
}
public Tag result() {
return new ListTag();
}
}
static class IntListCollector implements NbtOps.ListCollector {
private final IntArrayList values = new IntArrayList();
public IntListCollector(int p_250274_) {
this.values.add(p_250274_);
}
public IntListCollector(int[] p_249489_) {
this.values.addElements(0, p_249489_);
}
public NbtOps.ListCollector accept(Tag p_251372_) {
if (p_251372_ instanceof IntTag inttag) {
this.values.add(inttag.getAsInt());
return this;
} else {
return (new NbtOps.HeterogenousListCollector(this.values)).accept(p_251372_);
}
}
public Tag result() {
return new IntArrayTag(this.values.toIntArray());
}
}
interface ListCollector {
NbtOps.ListCollector accept(Tag p_249030_);
default NbtOps.ListCollector acceptAll(Iterable<Tag> p_249781_) {
NbtOps.ListCollector nbtops$listcollector = this;
for(Tag tag : p_249781_) {
nbtops$listcollector = nbtops$listcollector.accept(tag);
}
return nbtops$listcollector;
}
default NbtOps.ListCollector acceptAll(Stream<Tag> p_249876_) {
return this.acceptAll(p_249876_::iterator);
}
Tag result();
}
static class LongListCollector implements NbtOps.ListCollector {
private final LongArrayList values = new LongArrayList();
public LongListCollector(long p_249842_) {
this.values.add(p_249842_);
}
public LongListCollector(long[] p_251409_) {
this.values.addElements(0, p_251409_);
}
public NbtOps.ListCollector accept(Tag p_252167_) {
if (p_252167_ instanceof LongTag longtag) {
this.values.add(longtag.getAsLong());
return this;
} else {
return (new NbtOps.HeterogenousListCollector(this.values)).accept(p_252167_);
}
}
public Tag result() {
return new LongArrayTag(this.values.toLongArray());
}
}
class NbtRecordBuilder extends RecordBuilder.AbstractStringBuilder<Tag, CompoundTag> {
protected NbtRecordBuilder() {
super(NbtOps.this);
}
protected CompoundTag initBuilder() {
return new CompoundTag();
}
protected CompoundTag append(String p_129186_, Tag p_129187_, CompoundTag p_129188_) {
p_129188_.put(p_129186_, p_129187_);
return p_129188_;
}
protected DataResult<Tag> build(CompoundTag p_129190_, Tag p_129191_) {
if (p_129191_ != null && p_129191_ != EndTag.INSTANCE) {
if (!(p_129191_ instanceof CompoundTag)) {
return DataResult.error(() -> {
return "mergeToMap called with not a map: " + p_129191_;
}, p_129191_);
} else {
CompoundTag compoundtag = (CompoundTag)p_129191_;
CompoundTag compoundtag1 = new CompoundTag(Maps.newHashMap(compoundtag.entries()));
for(Map.Entry<String, Tag> entry : p_129190_.entries().entrySet()) {
compoundtag1.put(entry.getKey(), entry.getValue());
}
return DataResult.success(compoundtag1);
}
} else {
return DataResult.success(p_129190_);
}
}
}
}

View File

@@ -0,0 +1,654 @@
package net.minecraft.nbt;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.logging.LogUtils;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import net.minecraft.SharedConstants;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderGetter;
import net.minecraft.core.UUIDUtil;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.StringUtil;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.StateHolder;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.material.FluidState;
import org.slf4j.Logger;
public final class NbtUtils {
private static final Comparator<ListTag> YXZ_LISTTAG_INT_COMPARATOR = Comparator.<ListTag>comparingInt((p_178074_) -> {
return p_178074_.getInt(1);
}).thenComparingInt((p_178070_) -> {
return p_178070_.getInt(0);
}).thenComparingInt((p_178066_) -> {
return p_178066_.getInt(2);
});
private static final Comparator<ListTag> YXZ_LISTTAG_DOUBLE_COMPARATOR = Comparator.<ListTag>comparingDouble((p_178060_) -> {
return p_178060_.getDouble(1);
}).thenComparingDouble((p_178056_) -> {
return p_178056_.getDouble(0);
}).thenComparingDouble((p_178042_) -> {
return p_178042_.getDouble(2);
});
public static final String SNBT_DATA_TAG = "data";
private static final char PROPERTIES_START = '{';
private static final char PROPERTIES_END = '}';
private static final String ELEMENT_SEPARATOR = ",";
private static final char KEY_VALUE_SEPARATOR = ':';
private static final Splitter COMMA_SPLITTER = Splitter.on(",");
private static final Splitter COLON_SPLITTER = Splitter.on(':').limit(2);
private static final Logger LOGGER = LogUtils.getLogger();
private static final int INDENT = 2;
private static final int NOT_FOUND = -1;
private NbtUtils() {
}
@Nullable
public static GameProfile readGameProfile(CompoundTag p_129229_) {
String s = null;
UUID uuid = null;
if (p_129229_.contains("Name", 8)) {
s = p_129229_.getString("Name");
}
if (p_129229_.hasUUID("Id")) {
uuid = p_129229_.getUUID("Id");
}
try {
GameProfile gameprofile = new GameProfile(uuid, s);
if (p_129229_.contains("Properties", 10)) {
CompoundTag compoundtag = p_129229_.getCompound("Properties");
for(String s1 : compoundtag.getAllKeys()) {
ListTag listtag = compoundtag.getList(s1, 10);
for(int i = 0; i < listtag.size(); ++i) {
CompoundTag compoundtag1 = listtag.getCompound(i);
String s2 = compoundtag1.getString("Value");
if (compoundtag1.contains("Signature", 8)) {
gameprofile.getProperties().put(s1, new com.mojang.authlib.properties.Property(s1, s2, compoundtag1.getString("Signature")));
} else {
gameprofile.getProperties().put(s1, new com.mojang.authlib.properties.Property(s1, s2));
}
}
}
}
return gameprofile;
} catch (Throwable throwable) {
return null;
}
}
public static CompoundTag writeGameProfile(CompoundTag p_129231_, GameProfile p_129232_) {
if (!StringUtil.isNullOrEmpty(p_129232_.getName())) {
p_129231_.putString("Name", p_129232_.getName());
}
if (p_129232_.getId() != null) {
p_129231_.putUUID("Id", p_129232_.getId());
}
if (!p_129232_.getProperties().isEmpty()) {
CompoundTag compoundtag = new CompoundTag();
for(String s : p_129232_.getProperties().keySet()) {
ListTag listtag = new ListTag();
for(com.mojang.authlib.properties.Property property : p_129232_.getProperties().get(s)) {
CompoundTag compoundtag1 = new CompoundTag();
compoundtag1.putString("Value", property.getValue());
if (property.hasSignature()) {
compoundtag1.putString("Signature", property.getSignature());
}
listtag.add(compoundtag1);
}
compoundtag.put(s, listtag);
}
p_129231_.put("Properties", compoundtag);
}
return p_129231_;
}
@VisibleForTesting
public static boolean compareNbt(@Nullable Tag p_129236_, @Nullable Tag p_129237_, boolean p_129238_) {
if (p_129236_ == p_129237_) {
return true;
} else if (p_129236_ == null) {
return true;
} else if (p_129237_ == null) {
return false;
} else if (!p_129236_.getClass().equals(p_129237_.getClass())) {
return false;
} else if (p_129236_ instanceof CompoundTag) {
CompoundTag compoundtag = (CompoundTag)p_129236_;
CompoundTag compoundtag1 = (CompoundTag)p_129237_;
for(String s : compoundtag.getAllKeys()) {
Tag tag1 = compoundtag.get(s);
if (!compareNbt(tag1, compoundtag1.get(s), p_129238_)) {
return false;
}
}
return true;
} else if (p_129236_ instanceof ListTag && p_129238_) {
ListTag listtag = (ListTag)p_129236_;
ListTag listtag1 = (ListTag)p_129237_;
if (listtag.isEmpty()) {
return listtag1.isEmpty();
} else {
for(int i = 0; i < listtag.size(); ++i) {
Tag tag = listtag.get(i);
boolean flag = false;
for(int j = 0; j < listtag1.size(); ++j) {
if (compareNbt(tag, listtag1.get(j), p_129238_)) {
flag = true;
break;
}
}
if (!flag) {
return false;
}
}
return true;
}
} else {
return p_129236_.equals(p_129237_);
}
}
public static IntArrayTag createUUID(UUID p_129227_) {
return new IntArrayTag(UUIDUtil.uuidToIntArray(p_129227_));
}
public static UUID loadUUID(Tag p_129234_) {
if (p_129234_.getType() != IntArrayTag.TYPE) {
throw new IllegalArgumentException("Expected UUID-Tag to be of type " + IntArrayTag.TYPE.getName() + ", but found " + p_129234_.getType().getName() + ".");
} else {
int[] aint = ((IntArrayTag)p_129234_).getAsIntArray();
if (aint.length != 4) {
throw new IllegalArgumentException("Expected UUID-Array to be of length 4, but found " + aint.length + ".");
} else {
return UUIDUtil.uuidFromIntArray(aint);
}
}
}
public static BlockPos readBlockPos(CompoundTag p_129240_) {
return new BlockPos(p_129240_.getInt("X"), p_129240_.getInt("Y"), p_129240_.getInt("Z"));
}
public static CompoundTag writeBlockPos(BlockPos p_129225_) {
CompoundTag compoundtag = new CompoundTag();
compoundtag.putInt("X", p_129225_.getX());
compoundtag.putInt("Y", p_129225_.getY());
compoundtag.putInt("Z", p_129225_.getZ());
return compoundtag;
}
public static BlockState readBlockState(HolderGetter<Block> p_256363_, CompoundTag p_250775_) {
if (!p_250775_.contains("Name", 8)) {
return Blocks.AIR.defaultBlockState();
} else {
ResourceLocation resourcelocation = new ResourceLocation(p_250775_.getString("Name"));
Optional<? extends Holder<Block>> optional = p_256363_.get(ResourceKey.create(Registries.BLOCK, resourcelocation));
if (optional.isEmpty()) {
return Blocks.AIR.defaultBlockState();
} else {
Block block = optional.get().value();
BlockState blockstate = block.defaultBlockState();
if (p_250775_.contains("Properties", 10)) {
CompoundTag compoundtag = p_250775_.getCompound("Properties");
StateDefinition<Block, BlockState> statedefinition = block.getStateDefinition();
for(String s : compoundtag.getAllKeys()) {
Property<?> property = statedefinition.getProperty(s);
if (property != null) {
blockstate = setValueHelper(blockstate, property, s, compoundtag, p_250775_);
}
}
}
return blockstate;
}
}
}
private static <S extends StateHolder<?, S>, T extends Comparable<T>> S setValueHelper(S p_129205_, Property<T> p_129206_, String p_129207_, CompoundTag p_129208_, CompoundTag p_129209_) {
Optional<T> optional = p_129206_.getValue(p_129208_.getString(p_129207_));
if (optional.isPresent()) {
return p_129205_.setValue(p_129206_, optional.get());
} else {
LOGGER.warn("Unable to read property: {} with value: {} for blockstate: {}", p_129207_, p_129208_.getString(p_129207_), p_129209_.toString());
return p_129205_;
}
}
public static CompoundTag writeBlockState(BlockState p_129203_) {
CompoundTag compoundtag = new CompoundTag();
compoundtag.putString("Name", BuiltInRegistries.BLOCK.getKey(p_129203_.getBlock()).toString());
ImmutableMap<Property<?>, Comparable<?>> immutablemap = p_129203_.getValues();
if (!immutablemap.isEmpty()) {
CompoundTag compoundtag1 = new CompoundTag();
for(Map.Entry<Property<?>, Comparable<?>> entry : immutablemap.entrySet()) {
Property<?> property = entry.getKey();
compoundtag1.putString(property.getName(), getName(property, entry.getValue()));
}
compoundtag.put("Properties", compoundtag1);
}
return compoundtag;
}
public static CompoundTag writeFluidState(FluidState p_178023_) {
CompoundTag compoundtag = new CompoundTag();
compoundtag.putString("Name", BuiltInRegistries.FLUID.getKey(p_178023_.getType()).toString());
ImmutableMap<Property<?>, Comparable<?>> immutablemap = p_178023_.getValues();
if (!immutablemap.isEmpty()) {
CompoundTag compoundtag1 = new CompoundTag();
for(Map.Entry<Property<?>, Comparable<?>> entry : immutablemap.entrySet()) {
Property<?> property = entry.getKey();
compoundtag1.putString(property.getName(), getName(property, entry.getValue()));
}
compoundtag.put("Properties", compoundtag1);
}
return compoundtag;
}
private static <T extends Comparable<T>> String getName(Property<T> p_129211_, Comparable<?> p_129212_) {
return p_129211_.getName((T)p_129212_);
}
public static String prettyPrint(Tag p_178058_) {
return prettyPrint(p_178058_, false);
}
public static String prettyPrint(Tag p_178051_, boolean p_178052_) {
return prettyPrint(new StringBuilder(), p_178051_, 0, p_178052_).toString();
}
public static StringBuilder prettyPrint(StringBuilder p_178027_, Tag p_178028_, int p_178029_, boolean p_178030_) {
switch (p_178028_.getId()) {
case 0:
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 8:
p_178027_.append((Object)p_178028_);
break;
case 7:
ByteArrayTag bytearraytag = (ByteArrayTag)p_178028_;
byte[] abyte = bytearraytag.getAsByteArray();
int k1 = abyte.length;
indent(p_178029_, p_178027_).append("byte[").append(k1).append("] {\n");
if (!p_178030_) {
indent(p_178029_ + 1, p_178027_).append(" // Skipped, supply withBinaryBlobs true");
} else {
indent(p_178029_ + 1, p_178027_);
for(int i2 = 0; i2 < abyte.length; ++i2) {
if (i2 != 0) {
p_178027_.append(',');
}
if (i2 % 16 == 0 && i2 / 16 > 0) {
p_178027_.append('\n');
if (i2 < abyte.length) {
indent(p_178029_ + 1, p_178027_);
}
} else if (i2 != 0) {
p_178027_.append(' ');
}
p_178027_.append(String.format(Locale.ROOT, "0x%02X", abyte[i2] & 255));
}
}
p_178027_.append('\n');
indent(p_178029_, p_178027_).append('}');
break;
case 9:
ListTag listtag = (ListTag)p_178028_;
int k = listtag.size();
int j1 = listtag.getElementType();
String s1 = j1 == 0 ? "undefined" : TagTypes.getType(j1).getPrettyName();
indent(p_178029_, p_178027_).append("list<").append(s1).append(">[").append(k).append("] [");
if (k != 0) {
p_178027_.append('\n');
}
for(int i3 = 0; i3 < k; ++i3) {
if (i3 != 0) {
p_178027_.append(",\n");
}
indent(p_178029_ + 1, p_178027_);
prettyPrint(p_178027_, listtag.get(i3), p_178029_ + 1, p_178030_);
}
if (k != 0) {
p_178027_.append('\n');
}
indent(p_178029_, p_178027_).append(']');
break;
case 10:
CompoundTag compoundtag = (CompoundTag)p_178028_;
List<String> list = Lists.newArrayList(compoundtag.getAllKeys());
Collections.sort(list);
indent(p_178029_, p_178027_).append('{');
if (p_178027_.length() - p_178027_.lastIndexOf("\n") > 2 * (p_178029_ + 1)) {
p_178027_.append('\n');
indent(p_178029_ + 1, p_178027_);
}
int i1 = list.stream().mapToInt(String::length).max().orElse(0);
String s = Strings.repeat(" ", i1);
for(int l2 = 0; l2 < list.size(); ++l2) {
if (l2 != 0) {
p_178027_.append(",\n");
}
String s2 = list.get(l2);
indent(p_178029_ + 1, p_178027_).append('"').append(s2).append('"').append((CharSequence)s, 0, s.length() - s2.length()).append(": ");
prettyPrint(p_178027_, compoundtag.get(s2), p_178029_ + 1, p_178030_);
}
if (!list.isEmpty()) {
p_178027_.append('\n');
}
indent(p_178029_, p_178027_).append('}');
break;
case 11:
IntArrayTag intarraytag = (IntArrayTag)p_178028_;
int[] aint = intarraytag.getAsIntArray();
int l = 0;
for(int k3 : aint) {
l = Math.max(l, String.format(Locale.ROOT, "%X", k3).length());
}
int l1 = aint.length;
indent(p_178029_, p_178027_).append("int[").append(l1).append("] {\n");
if (!p_178030_) {
indent(p_178029_ + 1, p_178027_).append(" // Skipped, supply withBinaryBlobs true");
} else {
indent(p_178029_ + 1, p_178027_);
for(int k2 = 0; k2 < aint.length; ++k2) {
if (k2 != 0) {
p_178027_.append(',');
}
if (k2 % 16 == 0 && k2 / 16 > 0) {
p_178027_.append('\n');
if (k2 < aint.length) {
indent(p_178029_ + 1, p_178027_);
}
} else if (k2 != 0) {
p_178027_.append(' ');
}
p_178027_.append(String.format(Locale.ROOT, "0x%0" + l + "X", aint[k2]));
}
}
p_178027_.append('\n');
indent(p_178029_, p_178027_).append('}');
break;
case 12:
LongArrayTag longarraytag = (LongArrayTag)p_178028_;
long[] along = longarraytag.getAsLongArray();
long i = 0L;
for(long j : along) {
i = Math.max(i, (long)String.format(Locale.ROOT, "%X", j).length());
}
long j2 = (long)along.length;
indent(p_178029_, p_178027_).append("long[").append(j2).append("] {\n");
if (!p_178030_) {
indent(p_178029_ + 1, p_178027_).append(" // Skipped, supply withBinaryBlobs true");
} else {
indent(p_178029_ + 1, p_178027_);
for(int j3 = 0; j3 < along.length; ++j3) {
if (j3 != 0) {
p_178027_.append(',');
}
if (j3 % 16 == 0 && j3 / 16 > 0) {
p_178027_.append('\n');
if (j3 < along.length) {
indent(p_178029_ + 1, p_178027_);
}
} else if (j3 != 0) {
p_178027_.append(' ');
}
p_178027_.append(String.format(Locale.ROOT, "0x%0" + i + "X", along[j3]));
}
}
p_178027_.append('\n');
indent(p_178029_, p_178027_).append('}');
break;
default:
p_178027_.append("<UNKNOWN :(>");
}
return p_178027_;
}
private static StringBuilder indent(int p_178020_, StringBuilder p_178021_) {
int i = p_178021_.lastIndexOf("\n") + 1;
int j = p_178021_.length() - i;
for(int k = 0; k < 2 * p_178020_ - j; ++k) {
p_178021_.append(' ');
}
return p_178021_;
}
public static Component toPrettyComponent(Tag p_178062_) {
return (new TextComponentTagVisitor("", 0)).visit(p_178062_);
}
public static String structureToSnbt(CompoundTag p_178064_) {
return (new SnbtPrinterTagVisitor()).visit(packStructureTemplate(p_178064_));
}
public static CompoundTag snbtToStructure(String p_178025_) throws CommandSyntaxException {
return unpackStructureTemplate(TagParser.parseTag(p_178025_));
}
@VisibleForTesting
static CompoundTag packStructureTemplate(CompoundTag p_178068_) {
boolean flag = p_178068_.contains("palettes", 9);
ListTag listtag;
if (flag) {
listtag = p_178068_.getList("palettes", 9).getList(0);
} else {
listtag = p_178068_.getList("palette", 10);
}
ListTag listtag1 = listtag.stream().map(CompoundTag.class::cast).map(NbtUtils::packBlockState).map(StringTag::valueOf).collect(Collectors.toCollection(ListTag::new));
p_178068_.put("palette", listtag1);
if (flag) {
ListTag listtag2 = new ListTag();
ListTag listtag3 = p_178068_.getList("palettes", 9);
listtag3.stream().map(ListTag.class::cast).forEach((p_178049_) -> {
CompoundTag compoundtag = new CompoundTag();
for(int i = 0; i < p_178049_.size(); ++i) {
compoundtag.putString(listtag1.getString(i), packBlockState(p_178049_.getCompound(i)));
}
listtag2.add(compoundtag);
});
p_178068_.put("palettes", listtag2);
}
if (p_178068_.contains("entities", 9)) {
ListTag listtag4 = p_178068_.getList("entities", 10);
ListTag listtag6 = listtag4.stream().map(CompoundTag.class::cast).sorted(Comparator.comparing((p_178080_) -> {
return p_178080_.getList("pos", 6);
}, YXZ_LISTTAG_DOUBLE_COMPARATOR)).collect(Collectors.toCollection(ListTag::new));
p_178068_.put("entities", listtag6);
}
ListTag listtag5 = p_178068_.getList("blocks", 10).stream().map(CompoundTag.class::cast).sorted(Comparator.comparing((p_178078_) -> {
return p_178078_.getList("pos", 3);
}, YXZ_LISTTAG_INT_COMPARATOR)).peek((p_178045_) -> {
p_178045_.putString("state", listtag1.getString(p_178045_.getInt("state")));
}).collect(Collectors.toCollection(ListTag::new));
p_178068_.put("data", listtag5);
p_178068_.remove("blocks");
return p_178068_;
}
@VisibleForTesting
static CompoundTag unpackStructureTemplate(CompoundTag p_178072_) {
ListTag listtag = p_178072_.getList("palette", 8);
Map<String, Tag> map = listtag.stream().map(StringTag.class::cast).map(StringTag::getAsString).collect(ImmutableMap.toImmutableMap(Function.identity(), NbtUtils::unpackBlockState));
if (p_178072_.contains("palettes", 9)) {
p_178072_.put("palettes", p_178072_.getList("palettes", 10).stream().map(CompoundTag.class::cast).map((p_178033_) -> {
return map.keySet().stream().map(p_178033_::getString).map(NbtUtils::unpackBlockState).collect(Collectors.toCollection(ListTag::new));
}).collect(Collectors.toCollection(ListTag::new)));
p_178072_.remove("palette");
} else {
p_178072_.put("palette", map.values().stream().collect(Collectors.toCollection(ListTag::new)));
}
if (p_178072_.contains("data", 9)) {
Object2IntMap<String> object2intmap = new Object2IntOpenHashMap<>();
object2intmap.defaultReturnValue(-1);
for(int i = 0; i < listtag.size(); ++i) {
object2intmap.put(listtag.getString(i), i);
}
ListTag listtag1 = p_178072_.getList("data", 10);
for(int j = 0; j < listtag1.size(); ++j) {
CompoundTag compoundtag = listtag1.getCompound(j);
String s = compoundtag.getString("state");
int k = object2intmap.getInt(s);
if (k == -1) {
throw new IllegalStateException("Entry " + s + " missing from palette");
}
compoundtag.putInt("state", k);
}
p_178072_.put("blocks", listtag1);
p_178072_.remove("data");
}
return p_178072_;
}
@VisibleForTesting
static String packBlockState(CompoundTag p_178076_) {
StringBuilder stringbuilder = new StringBuilder(p_178076_.getString("Name"));
if (p_178076_.contains("Properties", 10)) {
CompoundTag compoundtag = p_178076_.getCompound("Properties");
String s = compoundtag.getAllKeys().stream().sorted().map((p_178036_) -> {
return p_178036_ + ":" + compoundtag.get(p_178036_).getAsString();
}).collect(Collectors.joining(","));
stringbuilder.append('{').append(s).append('}');
}
return stringbuilder.toString();
}
@VisibleForTesting
static CompoundTag unpackBlockState(String p_178054_) {
CompoundTag compoundtag = new CompoundTag();
int i = p_178054_.indexOf(123);
String s;
if (i >= 0) {
s = p_178054_.substring(0, i);
CompoundTag compoundtag1 = new CompoundTag();
if (i + 2 <= p_178054_.length()) {
String s1 = p_178054_.substring(i + 1, p_178054_.indexOf(125, i));
COMMA_SPLITTER.split(s1).forEach((p_178040_) -> {
List<String> list = COLON_SPLITTER.splitToList(p_178040_);
if (list.size() == 2) {
compoundtag1.putString(list.get(0), list.get(1));
} else {
LOGGER.error("Something went wrong parsing: '{}' -- incorrect gamedata!", (Object)p_178054_);
}
});
compoundtag.put("Properties", compoundtag1);
}
} else {
s = p_178054_;
}
compoundtag.putString("Name", s);
return compoundtag;
}
public static CompoundTag addCurrentDataVersion(CompoundTag p_265050_) {
int i = SharedConstants.getCurrentVersion().getDataVersion().getVersion();
return addDataVersion(p_265050_, i);
}
public static CompoundTag addDataVersion(CompoundTag p_265534_, int p_265686_) {
p_265534_.putInt("DataVersion", p_265686_);
return p_265534_;
}
public static int getDataVersion(CompoundTag p_265397_, int p_265399_) {
return p_265397_.contains("DataVersion", 99) ? p_265397_.getInt("DataVersion") : p_265399_;
}
}

View File

@@ -0,0 +1,24 @@
package net.minecraft.nbt;
public abstract class NumericTag implements Tag {
protected NumericTag() {
}
public abstract long getAsLong();
public abstract int getAsInt();
public abstract short getAsShort();
public abstract byte getAsByte();
public abstract double getAsDouble();
public abstract float getAsFloat();
public abstract Number getAsNumber();
public String toString() {
return this.getAsString();
}
}

View File

@@ -0,0 +1,128 @@
package net.minecraft.nbt;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
public class ShortTag extends NumericTag {
private static final int SELF_SIZE_IN_BYTES = 10;
public static final TagType<ShortTag> TYPE = new TagType.StaticSize<ShortTag>() {
public ShortTag load(DataInput p_129277_, int p_129278_, NbtAccounter p_129279_) throws IOException {
p_129279_.accountBytes(10L);
return ShortTag.valueOf(p_129277_.readShort());
}
public StreamTagVisitor.ValueResult parse(DataInput p_197517_, StreamTagVisitor p_197518_) throws IOException {
return p_197518_.visit(p_197517_.readShort());
}
public int size() {
return 2;
}
public String getName() {
return "SHORT";
}
public String getPrettyName() {
return "TAG_Short";
}
public boolean isValue() {
return true;
}
};
private final short data;
ShortTag(short p_129248_) {
this.data = p_129248_;
}
public static ShortTag valueOf(short p_129259_) {
return p_129259_ >= -128 && p_129259_ <= 1024 ? ShortTag.Cache.cache[p_129259_ - -128] : new ShortTag(p_129259_);
}
public void write(DataOutput p_129254_) throws IOException {
p_129254_.writeShort(this.data);
}
public int sizeInBytes() {
return 10;
}
public byte getId() {
return 2;
}
public TagType<ShortTag> getType() {
return TYPE;
}
public ShortTag copy() {
return this;
}
public boolean equals(Object p_129265_) {
if (this == p_129265_) {
return true;
} else {
return p_129265_ instanceof ShortTag && this.data == ((ShortTag)p_129265_).data;
}
}
public int hashCode() {
return this.data;
}
public void accept(TagVisitor p_178084_) {
p_178084_.visitShort(this);
}
public long getAsLong() {
return (long)this.data;
}
public int getAsInt() {
return this.data;
}
public short getAsShort() {
return this.data;
}
public byte getAsByte() {
return (byte)(this.data & 255);
}
public double getAsDouble() {
return (double)this.data;
}
public float getAsFloat() {
return (float)this.data;
}
public Number getAsNumber() {
return this.data;
}
public StreamTagVisitor.ValueResult accept(StreamTagVisitor p_197515_) {
return p_197515_.visit(this.data);
}
static class Cache {
private static final int HIGH = 1024;
private static final int LOW = -128;
static final ShortTag[] cache = new ShortTag[1153];
private Cache() {
}
static {
for(int i = 0; i < cache.length; ++i) {
cache[i] = new ShortTag((short)(-128 + i));
}
}
}
}

View File

@@ -0,0 +1,231 @@
package net.minecraft.nbt;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import net.minecraft.Util;
public class SnbtPrinterTagVisitor implements TagVisitor {
private static final Map<String, List<String>> KEY_ORDER = Util.make(Maps.newHashMap(), (p_178114_) -> {
p_178114_.put("{}", Lists.newArrayList("DataVersion", "author", "size", "data", "entities", "palette", "palettes"));
p_178114_.put("{}.data.[].{}", Lists.newArrayList("pos", "state", "nbt"));
p_178114_.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 p_178107_, int p_178108_, List<String> p_178109_) {
this.indentation = p_178107_;
this.depth = p_178108_;
this.path = p_178109_;
}
public String visit(Tag p_178142_) {
p_178142_.accept(this);
return this.result;
}
public void visitString(StringTag p_178140_) {
this.result = StringTag.quoteAndEscape(p_178140_.getAsString());
}
public void visitByte(ByteTag p_178118_) {
this.result = p_178118_.getAsNumber() + "b";
}
public void visitShort(ShortTag p_178138_) {
this.result = p_178138_.getAsNumber() + "s";
}
public void visitInt(IntTag p_178130_) {
this.result = String.valueOf((Object)p_178130_.getAsNumber());
}
public void visitLong(LongTag p_178136_) {
this.result = p_178136_.getAsNumber() + "L";
}
public void visitFloat(FloatTag p_178126_) {
this.result = p_178126_.getAsFloat() + "f";
}
public void visitDouble(DoubleTag p_178122_) {
this.result = p_178122_.getAsDouble() + "d";
}
public void visitByteArray(ByteArrayTag p_178116_) {
StringBuilder stringbuilder = (new StringBuilder("[")).append("B").append(";");
byte[] abyte = p_178116_.getAsByteArray();
for(int i = 0; i < abyte.length; ++i) {
stringbuilder.append(" ").append((int)abyte[i]).append("B");
if (i != abyte.length - 1) {
stringbuilder.append(ELEMENT_SEPARATOR);
}
}
stringbuilder.append("]");
this.result = stringbuilder.toString();
}
public void visitIntArray(IntArrayTag p_178128_) {
StringBuilder stringbuilder = (new StringBuilder("[")).append("I").append(";");
int[] aint = p_178128_.getAsIntArray();
for(int i = 0; i < aint.length; ++i) {
stringbuilder.append(" ").append(aint[i]);
if (i != aint.length - 1) {
stringbuilder.append(ELEMENT_SEPARATOR);
}
}
stringbuilder.append("]");
this.result = stringbuilder.toString();
}
public void visitLongArray(LongArrayTag p_178134_) {
String s = "L";
StringBuilder stringbuilder = (new StringBuilder("[")).append("L").append(";");
long[] along = p_178134_.getAsLongArray();
for(int i = 0; i < along.length; ++i) {
stringbuilder.append(" ").append(along[i]).append("L");
if (i != along.length - 1) {
stringbuilder.append(ELEMENT_SEPARATOR);
}
}
stringbuilder.append("]");
this.result = stringbuilder.toString();
}
public void visitList(ListTag p_178132_) {
if (p_178132_.isEmpty()) {
this.result = "[]";
} else {
StringBuilder stringbuilder = new StringBuilder("[");
this.pushPath("[]");
String s = NO_INDENTATION.contains(this.pathString()) ? "" : this.indentation;
if (!s.isEmpty()) {
stringbuilder.append("\n");
}
for(int i = 0; i < p_178132_.size(); ++i) {
stringbuilder.append(Strings.repeat(s, this.depth + 1));
stringbuilder.append((new SnbtPrinterTagVisitor(s, this.depth + 1, this.path)).visit(p_178132_.get(i)));
if (i != p_178132_.size() - 1) {
stringbuilder.append(ELEMENT_SEPARATOR).append(s.isEmpty() ? " " : "\n");
}
}
if (!s.isEmpty()) {
stringbuilder.append("\n").append(Strings.repeat(s, this.depth));
}
stringbuilder.append("]");
this.result = stringbuilder.toString();
this.popPath();
}
}
public void visitCompound(CompoundTag p_178120_) {
if (p_178120_.isEmpty()) {
this.result = "{}";
} else {
StringBuilder stringbuilder = new StringBuilder("{");
this.pushPath("{}");
String s = NO_INDENTATION.contains(this.pathString()) ? "" : this.indentation;
if (!s.isEmpty()) {
stringbuilder.append("\n");
}
Collection<String> collection = this.getKeys(p_178120_);
Iterator<String> iterator = collection.iterator();
while(iterator.hasNext()) {
String s1 = iterator.next();
Tag tag = p_178120_.get(s1);
this.pushPath(s1);
stringbuilder.append(Strings.repeat(s, this.depth + 1)).append(handleEscapePretty(s1)).append(NAME_VALUE_SEPARATOR).append(" ").append((new SnbtPrinterTagVisitor(s, this.depth + 1, this.path)).visit(tag));
this.popPath();
if (iterator.hasNext()) {
stringbuilder.append(ELEMENT_SEPARATOR).append(s.isEmpty() ? " " : "\n");
}
}
if (!s.isEmpty()) {
stringbuilder.append("\n").append(Strings.repeat(s, this.depth));
}
stringbuilder.append("}");
this.result = stringbuilder.toString();
this.popPath();
}
}
private void popPath() {
this.path.remove(this.path.size() - 1);
}
private void pushPath(String p_178145_) {
this.path.add(p_178145_);
}
protected List<String> getKeys(CompoundTag p_178147_) {
Set<String> set = Sets.newHashSet(p_178147_.getAllKeys());
List<String> list = Lists.newArrayList();
List<String> list1 = KEY_ORDER.get(this.pathString());
if (list1 != null) {
for(String s : list1) {
if (set.remove(s)) {
list.add(s);
}
}
if (!set.isEmpty()) {
set.stream().sorted().forEach(list::add);
}
} else {
list.addAll(set);
Collections.sort(list);
}
return list;
}
public String pathString() {
return String.join(".", this.path);
}
protected static String handleEscapePretty(String p_178112_) {
return SIMPLE_VALUE.matcher(p_178112_).matches() ? p_178112_ : StringTag.quoteAndEscape(p_178112_);
}
public void visitEnd(EndTag p_178124_) {
}
}

View File

@@ -0,0 +1,50 @@
package net.minecraft.nbt;
public interface StreamTagVisitor {
StreamTagVisitor.ValueResult visitEnd();
StreamTagVisitor.ValueResult visit(String p_197525_);
StreamTagVisitor.ValueResult visit(byte p_197520_);
StreamTagVisitor.ValueResult visit(short p_197531_);
StreamTagVisitor.ValueResult visit(int p_197523_);
StreamTagVisitor.ValueResult visit(long p_197524_);
StreamTagVisitor.ValueResult visit(float p_197522_);
StreamTagVisitor.ValueResult visit(double p_197521_);
StreamTagVisitor.ValueResult visit(byte[] p_197532_);
StreamTagVisitor.ValueResult visit(int[] p_197533_);
StreamTagVisitor.ValueResult visit(long[] p_197534_);
StreamTagVisitor.ValueResult visitList(TagType<?> p_197527_, int p_197528_);
StreamTagVisitor.EntryResult visitEntry(TagType<?> p_197526_);
StreamTagVisitor.EntryResult visitEntry(TagType<?> p_197529_, String p_197530_);
StreamTagVisitor.EntryResult visitElement(TagType<?> p_197536_, int p_197537_);
StreamTagVisitor.ValueResult visitContainerEnd();
StreamTagVisitor.ValueResult visitRootEntry(TagType<?> p_197535_);
public static enum EntryResult {
ENTER,
SKIP,
BREAK,
HALT;
}
public static enum ValueResult {
CONTINUE,
BREAK,
HALT;
}
}

View File

@@ -0,0 +1,143 @@
package net.minecraft.nbt;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.UTFDataFormatException;
import java.util.Objects;
import net.minecraft.Util;
public class StringTag implements Tag {
private static final int SELF_SIZE_IN_BYTES = 36;
public static final TagType<StringTag> TYPE = new TagType.VariableSize<StringTag>() {
public StringTag load(DataInput p_129315_, int p_129316_, NbtAccounter p_129317_) throws IOException {
p_129317_.accountBytes(36L);
String s = p_129315_.readUTF();
p_129317_.readUTF(s);
return StringTag.valueOf(s);
}
public StreamTagVisitor.ValueResult parse(DataInput p_197570_, StreamTagVisitor p_197571_) throws IOException {
return p_197571_.visit(p_197570_.readUTF());
}
public void skip(DataInput p_197568_) throws IOException {
StringTag.skipString(p_197568_);
}
public String getName() {
return "STRING";
}
public String getPrettyName() {
return "TAG_String";
}
public boolean isValue() {
return true;
}
};
private static final StringTag EMPTY = new StringTag("");
private static final char DOUBLE_QUOTE = '"';
private static final char SINGLE_QUOTE = '\'';
private static final char ESCAPE = '\\';
private static final char NOT_SET = '\u0000';
private final String data;
public static void skipString(DataInput p_197564_) throws IOException {
p_197564_.skipBytes(p_197564_.readUnsignedShort());
}
private StringTag(String p_129293_) {
Objects.requireNonNull(p_129293_, "Null string not allowed");
this.data = p_129293_;
}
public static StringTag valueOf(String p_129298_) {
return p_129298_.isEmpty() ? EMPTY : new StringTag(p_129298_);
}
public void write(DataOutput p_129296_) throws IOException {
try {
p_129296_.writeUTF(this.data);
} catch (UTFDataFormatException utfdataformatexception) {
Util.logAndPauseIfInIde("Failed to write NBT String", utfdataformatexception);
p_129296_.writeUTF("");
}
}
public int sizeInBytes() {
return 36 + 2 * this.data.length();
}
public byte getId() {
return 8;
}
public TagType<StringTag> getType() {
return TYPE;
}
public String toString() {
return Tag.super.getAsString();
}
public StringTag copy() {
return this;
}
public boolean equals(Object p_129308_) {
if (this == p_129308_) {
return true;
} else {
return p_129308_ instanceof StringTag && Objects.equals(this.data, ((StringTag)p_129308_).data);
}
}
public int hashCode() {
return this.data.hashCode();
}
public String getAsString() {
return this.data;
}
public void accept(TagVisitor p_178154_) {
p_178154_.visitString(this);
}
public static String quoteAndEscape(String p_129304_) {
StringBuilder stringbuilder = new StringBuilder(" ");
char c0 = 0;
for(int i = 0; i < p_129304_.length(); ++i) {
char c1 = p_129304_.charAt(i);
if (c1 == '\\') {
stringbuilder.append('\\');
} else if (c1 == '"' || c1 == '\'') {
if (c0 == 0) {
c0 = (char)(c1 == '"' ? 39 : 34);
}
if (c0 == c1) {
stringbuilder.append('\\');
}
}
stringbuilder.append(c1);
}
if (c0 == 0) {
c0 = '"';
}
stringbuilder.setCharAt(0, c0);
stringbuilder.append(c0);
return stringbuilder.toString();
}
public StreamTagVisitor.ValueResult accept(StreamTagVisitor p_197566_) {
return p_197566_.visit(this.data);
}
}

View File

@@ -0,0 +1,127 @@
package net.minecraft.nbt;
import com.google.common.collect.Lists;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
public class StringTagVisitor implements TagVisitor {
private static final Pattern SIMPLE_VALUE = Pattern.compile("[A-Za-z0-9._+-]+");
private final StringBuilder builder = new StringBuilder();
public String visit(Tag p_178188_) {
p_178188_.accept(this);
return this.builder.toString();
}
public void visitString(StringTag p_178186_) {
this.builder.append(StringTag.quoteAndEscape(p_178186_.getAsString()));
}
public void visitByte(ByteTag p_178164_) {
this.builder.append((Object)p_178164_.getAsNumber()).append('b');
}
public void visitShort(ShortTag p_178184_) {
this.builder.append((Object)p_178184_.getAsNumber()).append('s');
}
public void visitInt(IntTag p_178176_) {
this.builder.append((Object)p_178176_.getAsNumber());
}
public void visitLong(LongTag p_178182_) {
this.builder.append((Object)p_178182_.getAsNumber()).append('L');
}
public void visitFloat(FloatTag p_178172_) {
this.builder.append(p_178172_.getAsFloat()).append('f');
}
public void visitDouble(DoubleTag p_178168_) {
this.builder.append(p_178168_.getAsDouble()).append('d');
}
public void visitByteArray(ByteArrayTag p_178162_) {
this.builder.append("[B;");
byte[] abyte = p_178162_.getAsByteArray();
for(int i = 0; i < abyte.length; ++i) {
if (i != 0) {
this.builder.append(',');
}
this.builder.append((int)abyte[i]).append('B');
}
this.builder.append(']');
}
public void visitIntArray(IntArrayTag p_178174_) {
this.builder.append("[I;");
int[] aint = p_178174_.getAsIntArray();
for(int i = 0; i < aint.length; ++i) {
if (i != 0) {
this.builder.append(',');
}
this.builder.append(aint[i]);
}
this.builder.append(']');
}
public void visitLongArray(LongArrayTag p_178180_) {
this.builder.append("[L;");
long[] along = p_178180_.getAsLongArray();
for(int i = 0; i < along.length; ++i) {
if (i != 0) {
this.builder.append(',');
}
this.builder.append(along[i]).append('L');
}
this.builder.append(']');
}
public void visitList(ListTag p_178178_) {
this.builder.append('[');
for(int i = 0; i < p_178178_.size(); ++i) {
if (i != 0) {
this.builder.append(',');
}
this.builder.append((new StringTagVisitor()).visit(p_178178_.get(i)));
}
this.builder.append(']');
}
public void visitCompound(CompoundTag p_178166_) {
this.builder.append('{');
List<String> list = Lists.newArrayList(p_178166_.getAllKeys());
Collections.sort(list);
for(String s : list) {
if (this.builder.length() != 1) {
this.builder.append(',');
}
this.builder.append(handleEscape(s)).append(':').append((new StringTagVisitor()).visit(p_178166_.get(s)));
}
this.builder.append('}');
}
protected static String handleEscape(String p_178160_) {
return SIMPLE_VALUE.matcher(p_178160_).matches() ? p_178160_ : StringTag.quoteAndEscape(p_178160_);
}
public void visitEnd(EndTag p_178170_) {
this.builder.append("END");
}
}

View File

@@ -0,0 +1,54 @@
package net.minecraft.nbt;
import java.io.DataOutput;
import java.io.IOException;
public interface Tag {
int OBJECT_HEADER = 8;
int ARRAY_HEADER = 12;
int OBJECT_REFERENCE = 4;
int STRING_SIZE = 28;
byte TAG_END = 0;
byte TAG_BYTE = 1;
byte TAG_SHORT = 2;
byte TAG_INT = 3;
byte TAG_LONG = 4;
byte TAG_FLOAT = 5;
byte TAG_DOUBLE = 6;
byte TAG_BYTE_ARRAY = 7;
byte TAG_STRING = 8;
byte TAG_LIST = 9;
byte TAG_COMPOUND = 10;
byte TAG_INT_ARRAY = 11;
byte TAG_LONG_ARRAY = 12;
byte TAG_ANY_NUMERIC = 99;
int MAX_DEPTH = 512;
void write(DataOutput p_129329_) throws IOException;
String toString();
byte getId();
TagType<?> getType();
Tag copy();
int sizeInBytes();
default String getAsString() {
return (new StringTagVisitor()).visit(this);
}
void accept(TagVisitor p_178208_);
StreamTagVisitor.ValueResult accept(StreamTagVisitor p_197572_);
default void acceptAsRoot(StreamTagVisitor p_197574_) {
StreamTagVisitor.ValueResult streamtagvisitor$valueresult = p_197574_.visitRootEntry(this.getType());
if (streamtagvisitor$valueresult == StreamTagVisitor.ValueResult.CONTINUE) {
this.accept(p_197574_);
}
}
}

View File

@@ -0,0 +1,279 @@
package net.minecraft.nbt;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Lists;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import java.util.List;
import java.util.regex.Pattern;
import net.minecraft.network.chat.Component;
public class TagParser {
public static final SimpleCommandExceptionType ERROR_TRAILING_DATA = new SimpleCommandExceptionType(Component.translatable("argument.nbt.trailing"));
public static final SimpleCommandExceptionType ERROR_EXPECTED_KEY = new SimpleCommandExceptionType(Component.translatable("argument.nbt.expected.key"));
public static final SimpleCommandExceptionType ERROR_EXPECTED_VALUE = new SimpleCommandExceptionType(Component.translatable("argument.nbt.expected.value"));
public static final Dynamic2CommandExceptionType ERROR_INSERT_MIXED_LIST = new Dynamic2CommandExceptionType((p_129366_, p_129367_) -> {
return Component.translatable("argument.nbt.list.mixed", p_129366_, p_129367_);
});
public static final Dynamic2CommandExceptionType ERROR_INSERT_MIXED_ARRAY = new Dynamic2CommandExceptionType((p_129357_, p_129358_) -> {
return Component.translatable("argument.nbt.array.mixed", p_129357_, p_129358_);
});
public static final DynamicCommandExceptionType ERROR_INVALID_ARRAY = new DynamicCommandExceptionType((p_129355_) -> {
return Component.translatable("argument.nbt.array.invalid", p_129355_);
});
public static final char ELEMENT_SEPARATOR = ',';
public static final char NAME_VALUE_SEPARATOR = ':';
private static final char LIST_OPEN = '[';
private static final char LIST_CLOSE = ']';
private static final char STRUCT_CLOSE = '}';
private static final char STRUCT_OPEN = '{';
private static final Pattern DOUBLE_PATTERN_NOSUFFIX = Pattern.compile("[-+]?(?:[0-9]+[.]|[0-9]*[.][0-9]+)(?:e[-+]?[0-9]+)?", 2);
private static final Pattern DOUBLE_PATTERN = Pattern.compile("[-+]?(?:[0-9]+[.]?|[0-9]*[.][0-9]+)(?:e[-+]?[0-9]+)?d", 2);
private static final Pattern FLOAT_PATTERN = Pattern.compile("[-+]?(?:[0-9]+[.]?|[0-9]*[.][0-9]+)(?:e[-+]?[0-9]+)?f", 2);
private static final Pattern BYTE_PATTERN = Pattern.compile("[-+]?(?:0|[1-9][0-9]*)b", 2);
private static final Pattern LONG_PATTERN = Pattern.compile("[-+]?(?:0|[1-9][0-9]*)l", 2);
private static final Pattern SHORT_PATTERN = Pattern.compile("[-+]?(?:0|[1-9][0-9]*)s", 2);
private static final Pattern INT_PATTERN = Pattern.compile("[-+]?(?:0|[1-9][0-9]*)");
private final StringReader reader;
public static CompoundTag parseTag(String p_129360_) throws CommandSyntaxException {
return (new TagParser(new StringReader(p_129360_))).readSingleStruct();
}
@VisibleForTesting
CompoundTag readSingleStruct() throws CommandSyntaxException {
CompoundTag compoundtag = this.readStruct();
this.reader.skipWhitespace();
if (this.reader.canRead()) {
throw ERROR_TRAILING_DATA.createWithContext(this.reader);
} else {
return compoundtag;
}
}
public TagParser(StringReader p_129350_) {
this.reader = p_129350_;
}
protected String readKey() throws CommandSyntaxException {
this.reader.skipWhitespace();
if (!this.reader.canRead()) {
throw ERROR_EXPECTED_KEY.createWithContext(this.reader);
} else {
return this.reader.readString();
}
}
protected Tag readTypedValue() throws CommandSyntaxException {
this.reader.skipWhitespace();
int i = this.reader.getCursor();
if (StringReader.isQuotedStringStart(this.reader.peek())) {
return StringTag.valueOf(this.reader.readQuotedString());
} else {
String s = this.reader.readUnquotedString();
if (s.isEmpty()) {
this.reader.setCursor(i);
throw ERROR_EXPECTED_VALUE.createWithContext(this.reader);
} else {
return this.type(s);
}
}
}
private Tag type(String p_129369_) {
try {
if (FLOAT_PATTERN.matcher(p_129369_).matches()) {
return FloatTag.valueOf(Float.parseFloat(p_129369_.substring(0, p_129369_.length() - 1)));
}
if (BYTE_PATTERN.matcher(p_129369_).matches()) {
return ByteTag.valueOf(Byte.parseByte(p_129369_.substring(0, p_129369_.length() - 1)));
}
if (LONG_PATTERN.matcher(p_129369_).matches()) {
return LongTag.valueOf(Long.parseLong(p_129369_.substring(0, p_129369_.length() - 1)));
}
if (SHORT_PATTERN.matcher(p_129369_).matches()) {
return ShortTag.valueOf(Short.parseShort(p_129369_.substring(0, p_129369_.length() - 1)));
}
if (INT_PATTERN.matcher(p_129369_).matches()) {
return IntTag.valueOf(Integer.parseInt(p_129369_));
}
if (DOUBLE_PATTERN.matcher(p_129369_).matches()) {
return DoubleTag.valueOf(Double.parseDouble(p_129369_.substring(0, p_129369_.length() - 1)));
}
if (DOUBLE_PATTERN_NOSUFFIX.matcher(p_129369_).matches()) {
return DoubleTag.valueOf(Double.parseDouble(p_129369_));
}
if ("true".equalsIgnoreCase(p_129369_)) {
return ByteTag.ONE;
}
if ("false".equalsIgnoreCase(p_129369_)) {
return ByteTag.ZERO;
}
} catch (NumberFormatException numberformatexception) {
}
return StringTag.valueOf(p_129369_);
}
public Tag readValue() throws CommandSyntaxException {
this.reader.skipWhitespace();
if (!this.reader.canRead()) {
throw ERROR_EXPECTED_VALUE.createWithContext(this.reader);
} else {
char c0 = this.reader.peek();
if (c0 == '{') {
return this.readStruct();
} else {
return c0 == '[' ? this.readList() : this.readTypedValue();
}
}
}
protected Tag readList() throws CommandSyntaxException {
return this.reader.canRead(3) && !StringReader.isQuotedStringStart(this.reader.peek(1)) && this.reader.peek(2) == ';' ? this.readArrayTag() : this.readListTag();
}
public CompoundTag readStruct() throws CommandSyntaxException {
this.expect('{');
CompoundTag compoundtag = new CompoundTag();
this.reader.skipWhitespace();
while(this.reader.canRead() && this.reader.peek() != '}') {
int i = this.reader.getCursor();
String s = this.readKey();
if (s.isEmpty()) {
this.reader.setCursor(i);
throw ERROR_EXPECTED_KEY.createWithContext(this.reader);
}
this.expect(':');
compoundtag.put(s, this.readValue());
if (!this.hasElementSeparator()) {
break;
}
if (!this.reader.canRead()) {
throw ERROR_EXPECTED_KEY.createWithContext(this.reader);
}
}
this.expect('}');
return compoundtag;
}
private Tag readListTag() throws CommandSyntaxException {
this.expect('[');
this.reader.skipWhitespace();
if (!this.reader.canRead()) {
throw ERROR_EXPECTED_VALUE.createWithContext(this.reader);
} else {
ListTag listtag = new ListTag();
TagType<?> tagtype = null;
while(this.reader.peek() != ']') {
int i = this.reader.getCursor();
Tag tag = this.readValue();
TagType<?> tagtype1 = tag.getType();
if (tagtype == null) {
tagtype = tagtype1;
} else if (tagtype1 != tagtype) {
this.reader.setCursor(i);
throw ERROR_INSERT_MIXED_LIST.createWithContext(this.reader, tagtype1.getPrettyName(), tagtype.getPrettyName());
}
listtag.add(tag);
if (!this.hasElementSeparator()) {
break;
}
if (!this.reader.canRead()) {
throw ERROR_EXPECTED_VALUE.createWithContext(this.reader);
}
}
this.expect(']');
return listtag;
}
}
private Tag readArrayTag() throws CommandSyntaxException {
this.expect('[');
int i = this.reader.getCursor();
char c0 = this.reader.read();
this.reader.read();
this.reader.skipWhitespace();
if (!this.reader.canRead()) {
throw ERROR_EXPECTED_VALUE.createWithContext(this.reader);
} else if (c0 == 'B') {
return new ByteArrayTag(this.readArray(ByteArrayTag.TYPE, ByteTag.TYPE));
} else if (c0 == 'L') {
return new LongArrayTag(this.readArray(LongArrayTag.TYPE, LongTag.TYPE));
} else if (c0 == 'I') {
return new IntArrayTag(this.readArray(IntArrayTag.TYPE, IntTag.TYPE));
} else {
this.reader.setCursor(i);
throw ERROR_INVALID_ARRAY.createWithContext(this.reader, String.valueOf(c0));
}
}
private <T extends Number> List<T> readArray(TagType<?> p_129362_, TagType<?> p_129363_) throws CommandSyntaxException {
List<T> list = Lists.newArrayList();
while(true) {
if (this.reader.peek() != ']') {
int i = this.reader.getCursor();
Tag tag = this.readValue();
TagType<?> tagtype = tag.getType();
if (tagtype != p_129363_) {
this.reader.setCursor(i);
throw ERROR_INSERT_MIXED_ARRAY.createWithContext(this.reader, tagtype.getPrettyName(), p_129362_.getPrettyName());
}
if (p_129363_ == ByteTag.TYPE) {
list.add((T)(Byte)((NumericTag)tag).getAsByte());
} else if (p_129363_ == LongTag.TYPE) {
list.add((T)(Long)((NumericTag)tag).getAsLong());
} else {
list.add((T)(Integer)((NumericTag)tag).getAsInt());
}
if (this.hasElementSeparator()) {
if (!this.reader.canRead()) {
throw ERROR_EXPECTED_VALUE.createWithContext(this.reader);
}
continue;
}
}
this.expect(']');
return list;
}
}
private boolean hasElementSeparator() {
this.reader.skipWhitespace();
if (this.reader.canRead() && this.reader.peek() == ',') {
this.reader.skip();
this.reader.skipWhitespace();
return true;
} else {
return false;
}
}
private void expect(char p_129353_) throws CommandSyntaxException {
this.reader.skipWhitespace();
this.reader.expect(p_129353_);
}
}

View File

@@ -0,0 +1,88 @@
package net.minecraft.nbt;
import java.io.DataInput;
import java.io.IOException;
public interface TagType<T extends Tag> {
T load(DataInput p_129379_, int p_129380_, NbtAccounter p_129381_) throws IOException;
StreamTagVisitor.ValueResult parse(DataInput p_197578_, StreamTagVisitor p_197579_) throws IOException;
default void parseRoot(DataInput p_197581_, StreamTagVisitor p_197582_) throws IOException {
switch (p_197582_.visitRootEntry(this)) {
case CONTINUE:
this.parse(p_197581_, p_197582_);
case HALT:
default:
break;
case BREAK:
this.skip(p_197581_);
}
}
void skip(DataInput p_197576_, int p_197577_) throws IOException;
void skip(DataInput p_197575_) throws IOException;
default boolean isValue() {
return false;
}
String getName();
String getPrettyName();
static TagType<EndTag> createInvalid(final int p_129378_) {
return new TagType<EndTag>() {
private IOException createException() {
return new IOException("Invalid tag id: " + p_129378_);
}
public EndTag load(DataInput p_129387_, int p_129388_, NbtAccounter p_129389_) throws IOException {
throw this.createException();
}
public StreamTagVisitor.ValueResult parse(DataInput p_197589_, StreamTagVisitor p_197590_) throws IOException {
throw this.createException();
}
public void skip(DataInput p_197586_, int p_197587_) throws IOException {
throw this.createException();
}
public void skip(DataInput p_197584_) throws IOException {
throw this.createException();
}
public String getName() {
return "INVALID[" + p_129378_ + "]";
}
public String getPrettyName() {
return "UNKNOWN_" + p_129378_;
}
};
}
public interface StaticSize<T extends Tag> extends TagType<T> {
default void skip(DataInput p_197595_) throws IOException {
p_197595_.skipBytes(this.size());
}
default void skip(DataInput p_197597_, int p_197598_) throws IOException {
p_197597_.skipBytes(this.size() * p_197598_);
}
int size();
}
public interface VariableSize<T extends Tag> extends TagType<T> {
default void skip(DataInput p_197600_, int p_197601_) throws IOException {
for(int i = 0; i < p_197601_; ++i) {
this.skip(p_197600_);
}
}
}
}

View File

@@ -0,0 +1,9 @@
package net.minecraft.nbt;
public class TagTypes {
private static final TagType<?>[] TYPES = new TagType[]{EndTag.TYPE, ByteTag.TYPE, ShortTag.TYPE, IntTag.TYPE, LongTag.TYPE, FloatTag.TYPE, DoubleTag.TYPE, ByteArrayTag.TYPE, StringTag.TYPE, ListTag.TYPE, CompoundTag.TYPE, IntArrayTag.TYPE, LongArrayTag.TYPE};
public static TagType<?> getType(int p_129398_) {
return p_129398_ >= 0 && p_129398_ < TYPES.length ? TYPES[p_129398_] : TagType.createInvalid(p_129398_);
}
}

View File

@@ -0,0 +1,29 @@
package net.minecraft.nbt;
public interface TagVisitor {
void visitString(StringTag p_178228_);
void visitByte(ByteTag p_178217_);
void visitShort(ShortTag p_178227_);
void visitInt(IntTag p_178223_);
void visitLong(LongTag p_178226_);
void visitFloat(FloatTag p_178221_);
void visitDouble(DoubleTag p_178219_);
void visitByteArray(ByteArrayTag p_178216_);
void visitIntArray(IntArrayTag p_178222_);
void visitLongArray(LongArrayTag p_178225_);
void visitList(ListTag p_178224_);
void visitCompound(CompoundTag p_178218_);
void visitEnd(EndTag p_178220_);
}

View File

@@ -0,0 +1,228 @@
package net.minecraft.nbt;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.mojang.logging.LogUtils;
import it.unimi.dsi.fastutil.bytes.ByteCollection;
import it.unimi.dsi.fastutil.bytes.ByteOpenHashSet;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import org.slf4j.Logger;
public class TextComponentTagVisitor implements TagVisitor {
private static final Logger LOGGER = LogUtils.getLogger();
private static final int INLINE_LIST_THRESHOLD = 8;
private static final ByteCollection INLINE_ELEMENT_TYPES = new ByteOpenHashSet(Arrays.asList((byte)1, (byte)2, (byte)3, (byte)4, (byte)5, (byte)6));
private static final ChatFormatting SYNTAX_HIGHLIGHTING_KEY = ChatFormatting.AQUA;
private static final ChatFormatting SYNTAX_HIGHLIGHTING_STRING = ChatFormatting.GREEN;
private static final ChatFormatting SYNTAX_HIGHLIGHTING_NUMBER = ChatFormatting.GOLD;
private static final ChatFormatting SYNTAX_HIGHLIGHTING_NUMBER_TYPE = ChatFormatting.RED;
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 Component result = CommonComponents.EMPTY;
public TextComponentTagVisitor(String p_178251_, int p_178252_) {
this.indentation = p_178251_;
this.depth = p_178252_;
}
public Component visit(Tag p_178282_) {
p_178282_.accept(this);
return this.result;
}
public void visitString(StringTag p_178280_) {
String s = StringTag.quoteAndEscape(p_178280_.getAsString());
String s1 = s.substring(0, 1);
Component component = Component.literal(s.substring(1, s.length() - 1)).withStyle(SYNTAX_HIGHLIGHTING_STRING);
this.result = Component.literal(s1).append(component).append(s1);
}
public void visitByte(ByteTag p_178258_) {
Component component = Component.literal("b").withStyle(SYNTAX_HIGHLIGHTING_NUMBER_TYPE);
this.result = Component.literal(String.valueOf((Object)p_178258_.getAsNumber())).append(component).withStyle(SYNTAX_HIGHLIGHTING_NUMBER);
}
public void visitShort(ShortTag p_178278_) {
Component component = Component.literal("s").withStyle(SYNTAX_HIGHLIGHTING_NUMBER_TYPE);
this.result = Component.literal(String.valueOf((Object)p_178278_.getAsNumber())).append(component).withStyle(SYNTAX_HIGHLIGHTING_NUMBER);
}
public void visitInt(IntTag p_178270_) {
this.result = Component.literal(String.valueOf((Object)p_178270_.getAsNumber())).withStyle(SYNTAX_HIGHLIGHTING_NUMBER);
}
public void visitLong(LongTag p_178276_) {
Component component = Component.literal("L").withStyle(SYNTAX_HIGHLIGHTING_NUMBER_TYPE);
this.result = Component.literal(String.valueOf((Object)p_178276_.getAsNumber())).append(component).withStyle(SYNTAX_HIGHLIGHTING_NUMBER);
}
public void visitFloat(FloatTag p_178266_) {
Component component = Component.literal("f").withStyle(SYNTAX_HIGHLIGHTING_NUMBER_TYPE);
this.result = Component.literal(String.valueOf(p_178266_.getAsFloat())).append(component).withStyle(SYNTAX_HIGHLIGHTING_NUMBER);
}
public void visitDouble(DoubleTag p_178262_) {
Component component = Component.literal("d").withStyle(SYNTAX_HIGHLIGHTING_NUMBER_TYPE);
this.result = Component.literal(String.valueOf(p_178262_.getAsDouble())).append(component).withStyle(SYNTAX_HIGHLIGHTING_NUMBER);
}
public void visitByteArray(ByteArrayTag p_178256_) {
Component component = Component.literal("B").withStyle(SYNTAX_HIGHLIGHTING_NUMBER_TYPE);
MutableComponent mutablecomponent = Component.literal("[").append(component).append(";");
byte[] abyte = p_178256_.getAsByteArray();
for(int i = 0; i < abyte.length; ++i) {
MutableComponent mutablecomponent1 = Component.literal(String.valueOf((int)abyte[i])).withStyle(SYNTAX_HIGHLIGHTING_NUMBER);
mutablecomponent.append(" ").append(mutablecomponent1).append(component);
if (i != abyte.length - 1) {
mutablecomponent.append(ELEMENT_SEPARATOR);
}
}
mutablecomponent.append("]");
this.result = mutablecomponent;
}
public void visitIntArray(IntArrayTag p_178268_) {
Component component = Component.literal("I").withStyle(SYNTAX_HIGHLIGHTING_NUMBER_TYPE);
MutableComponent mutablecomponent = Component.literal("[").append(component).append(";");
int[] aint = p_178268_.getAsIntArray();
for(int i = 0; i < aint.length; ++i) {
mutablecomponent.append(" ").append(Component.literal(String.valueOf(aint[i])).withStyle(SYNTAX_HIGHLIGHTING_NUMBER));
if (i != aint.length - 1) {
mutablecomponent.append(ELEMENT_SEPARATOR);
}
}
mutablecomponent.append("]");
this.result = mutablecomponent;
}
public void visitLongArray(LongArrayTag p_178274_) {
Component component = Component.literal("L").withStyle(SYNTAX_HIGHLIGHTING_NUMBER_TYPE);
MutableComponent mutablecomponent = Component.literal("[").append(component).append(";");
long[] along = p_178274_.getAsLongArray();
for(int i = 0; i < along.length; ++i) {
Component component1 = Component.literal(String.valueOf(along[i])).withStyle(SYNTAX_HIGHLIGHTING_NUMBER);
mutablecomponent.append(" ").append(component1).append(component);
if (i != along.length - 1) {
mutablecomponent.append(ELEMENT_SEPARATOR);
}
}
mutablecomponent.append("]");
this.result = mutablecomponent;
}
public void visitList(ListTag p_178272_) {
if (p_178272_.isEmpty()) {
this.result = Component.literal("[]");
} else if (INLINE_ELEMENT_TYPES.contains(p_178272_.getElementType()) && p_178272_.size() <= 8) {
String s = ELEMENT_SEPARATOR + " ";
MutableComponent mutablecomponent2 = Component.literal("[");
for(int j = 0; j < p_178272_.size(); ++j) {
if (j != 0) {
mutablecomponent2.append(s);
}
mutablecomponent2.append((new TextComponentTagVisitor(this.indentation, this.depth)).visit(p_178272_.get(j)));
}
mutablecomponent2.append("]");
this.result = mutablecomponent2;
} else {
MutableComponent mutablecomponent = Component.literal("[");
if (!this.indentation.isEmpty()) {
mutablecomponent.append("\n");
}
for(int i = 0; i < p_178272_.size(); ++i) {
MutableComponent mutablecomponent1 = Component.literal(Strings.repeat(this.indentation, this.depth + 1));
mutablecomponent1.append((new TextComponentTagVisitor(this.indentation, this.depth + 1)).visit(p_178272_.get(i)));
if (i != p_178272_.size() - 1) {
mutablecomponent1.append(ELEMENT_SEPARATOR).append(this.indentation.isEmpty() ? " " : "\n");
}
mutablecomponent.append(mutablecomponent1);
}
if (!this.indentation.isEmpty()) {
mutablecomponent.append("\n").append(Strings.repeat(this.indentation, this.depth));
}
mutablecomponent.append("]");
this.result = mutablecomponent;
}
}
public void visitCompound(CompoundTag p_178260_) {
if (p_178260_.isEmpty()) {
this.result = Component.literal("{}");
} else {
MutableComponent mutablecomponent = Component.literal("{");
Collection<String> collection = p_178260_.getAllKeys();
if (LOGGER.isDebugEnabled()) {
List<String> list = Lists.newArrayList(p_178260_.getAllKeys());
Collections.sort(list);
collection = list;
}
if (!this.indentation.isEmpty()) {
mutablecomponent.append("\n");
}
MutableComponent mutablecomponent1;
for(Iterator<String> iterator = collection.iterator(); iterator.hasNext(); mutablecomponent.append(mutablecomponent1)) {
String s = iterator.next();
mutablecomponent1 = Component.literal(Strings.repeat(this.indentation, this.depth + 1)).append(handleEscapePretty(s)).append(NAME_VALUE_SEPARATOR).append(" ").append((new TextComponentTagVisitor(this.indentation, this.depth + 1)).visit(p_178260_.get(s)));
if (iterator.hasNext()) {
mutablecomponent1.append(ELEMENT_SEPARATOR).append(this.indentation.isEmpty() ? " " : "\n");
}
}
if (!this.indentation.isEmpty()) {
mutablecomponent.append("\n").append(Strings.repeat(this.indentation, this.depth));
}
mutablecomponent.append("}");
this.result = mutablecomponent;
}
}
protected static Component handleEscapePretty(String p_178254_) {
if (SIMPLE_VALUE.matcher(p_178254_).matches()) {
return Component.literal(p_178254_).withStyle(SYNTAX_HIGHLIGHTING_KEY);
} else {
String s = StringTag.quoteAndEscape(p_178254_);
String s1 = s.substring(0, 1);
Component component = Component.literal(s.substring(1, s.length() - 1)).withStyle(SYNTAX_HIGHLIGHTING_KEY);
return Component.literal(s1).append(component).append(s1);
}
}
public void visitEnd(EndTag p_178264_) {
this.result = CommonComponents.EMPTY;
}
}

View File

@@ -0,0 +1,8 @@
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
@FieldsAreNonnullByDefault
package net.minecraft.nbt;
import javax.annotation.ParametersAreNonnullByDefault;
import net.minecraft.FieldsAreNonnullByDefault;
import net.minecraft.MethodsReturnNonnullByDefault;

View File

@@ -0,0 +1,77 @@
package net.minecraft.nbt.visitors;
import com.google.common.collect.ImmutableSet;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Set;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.StreamTagVisitor;
import net.minecraft.nbt.TagType;
public class CollectFields extends CollectToTag {
private int fieldsToGetCount;
private final Set<TagType<?>> wantedTypes;
private final Deque<FieldTree> stack = new ArrayDeque<>();
public CollectFields(FieldSelector... p_202496_) {
this.fieldsToGetCount = p_202496_.length;
ImmutableSet.Builder<TagType<?>> builder = ImmutableSet.builder();
FieldTree fieldtree = FieldTree.createRoot();
for(FieldSelector fieldselector : p_202496_) {
fieldtree.addEntry(fieldselector);
builder.add(fieldselector.type());
}
this.stack.push(fieldtree);
builder.add(CompoundTag.TYPE);
this.wantedTypes = builder.build();
}
public StreamTagVisitor.ValueResult visitRootEntry(TagType<?> p_197614_) {
return p_197614_ != CompoundTag.TYPE ? StreamTagVisitor.ValueResult.HALT : super.visitRootEntry(p_197614_);
}
public StreamTagVisitor.EntryResult visitEntry(TagType<?> p_197608_) {
FieldTree fieldtree = this.stack.element();
if (this.depth() > fieldtree.depth()) {
return super.visitEntry(p_197608_);
} else if (this.fieldsToGetCount <= 0) {
return StreamTagVisitor.EntryResult.HALT;
} else {
return !this.wantedTypes.contains(p_197608_) ? StreamTagVisitor.EntryResult.SKIP : super.visitEntry(p_197608_);
}
}
public StreamTagVisitor.EntryResult visitEntry(TagType<?> p_197610_, String p_197611_) {
FieldTree fieldtree = this.stack.element();
if (this.depth() > fieldtree.depth()) {
return super.visitEntry(p_197610_, p_197611_);
} else if (fieldtree.selectedFields().remove(p_197611_, p_197610_)) {
--this.fieldsToGetCount;
return super.visitEntry(p_197610_, p_197611_);
} else {
if (p_197610_ == CompoundTag.TYPE) {
FieldTree fieldtree1 = fieldtree.fieldsToRecurse().get(p_197611_);
if (fieldtree1 != null) {
this.stack.push(fieldtree1);
return super.visitEntry(p_197610_, p_197611_);
}
}
return StreamTagVisitor.EntryResult.SKIP;
}
}
public StreamTagVisitor.ValueResult visitContainerEnd() {
if (this.depth() == this.stack.element().depth()) {
this.stack.pop();
}
return super.visitContainerEnd();
}
public int getMissingFieldCount() {
return this.fieldsToGetCount;
}
}

View File

@@ -0,0 +1,156 @@
package net.minecraft.nbt.visitors;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.function.Consumer;
import javax.annotation.Nullable;
import net.minecraft.nbt.ByteArrayTag;
import net.minecraft.nbt.ByteTag;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.DoubleTag;
import net.minecraft.nbt.EndTag;
import net.minecraft.nbt.FloatTag;
import net.minecraft.nbt.IntArrayTag;
import net.minecraft.nbt.IntTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.LongArrayTag;
import net.minecraft.nbt.LongTag;
import net.minecraft.nbt.ShortTag;
import net.minecraft.nbt.StreamTagVisitor;
import net.minecraft.nbt.StringTag;
import net.minecraft.nbt.Tag;
import net.minecraft.nbt.TagType;
public class CollectToTag implements StreamTagVisitor {
private String lastId = "";
@Nullable
private Tag rootTag;
private final Deque<Consumer<Tag>> consumerStack = new ArrayDeque<>();
@Nullable
public Tag getResult() {
return this.rootTag;
}
protected int depth() {
return this.consumerStack.size();
}
private void appendEntry(Tag p_197683_) {
this.consumerStack.getLast().accept(p_197683_);
}
public StreamTagVisitor.ValueResult visitEnd() {
this.appendEntry(EndTag.INSTANCE);
return StreamTagVisitor.ValueResult.CONTINUE;
}
public StreamTagVisitor.ValueResult visit(String p_197678_) {
this.appendEntry(StringTag.valueOf(p_197678_));
return StreamTagVisitor.ValueResult.CONTINUE;
}
public StreamTagVisitor.ValueResult visit(byte p_197668_) {
this.appendEntry(ByteTag.valueOf(p_197668_));
return StreamTagVisitor.ValueResult.CONTINUE;
}
public StreamTagVisitor.ValueResult visit(short p_197693_) {
this.appendEntry(ShortTag.valueOf(p_197693_));
return StreamTagVisitor.ValueResult.CONTINUE;
}
public StreamTagVisitor.ValueResult visit(int p_197674_) {
this.appendEntry(IntTag.valueOf(p_197674_));
return StreamTagVisitor.ValueResult.CONTINUE;
}
public StreamTagVisitor.ValueResult visit(long p_197676_) {
this.appendEntry(LongTag.valueOf(p_197676_));
return StreamTagVisitor.ValueResult.CONTINUE;
}
public StreamTagVisitor.ValueResult visit(float p_197672_) {
this.appendEntry(FloatTag.valueOf(p_197672_));
return StreamTagVisitor.ValueResult.CONTINUE;
}
public StreamTagVisitor.ValueResult visit(double p_197670_) {
this.appendEntry(DoubleTag.valueOf(p_197670_));
return StreamTagVisitor.ValueResult.CONTINUE;
}
public StreamTagVisitor.ValueResult visit(byte[] p_197695_) {
this.appendEntry(new ByteArrayTag(p_197695_));
return StreamTagVisitor.ValueResult.CONTINUE;
}
public StreamTagVisitor.ValueResult visit(int[] p_197697_) {
this.appendEntry(new IntArrayTag(p_197697_));
return StreamTagVisitor.ValueResult.CONTINUE;
}
public StreamTagVisitor.ValueResult visit(long[] p_197699_) {
this.appendEntry(new LongArrayTag(p_197699_));
return StreamTagVisitor.ValueResult.CONTINUE;
}
public StreamTagVisitor.ValueResult visitList(TagType<?> p_197687_, int p_197688_) {
return StreamTagVisitor.ValueResult.CONTINUE;
}
public StreamTagVisitor.EntryResult visitElement(TagType<?> p_197709_, int p_197710_) {
this.enterContainerIfNeeded(p_197709_);
return StreamTagVisitor.EntryResult.ENTER;
}
public StreamTagVisitor.EntryResult visitEntry(TagType<?> p_197685_) {
return StreamTagVisitor.EntryResult.ENTER;
}
public StreamTagVisitor.EntryResult visitEntry(TagType<?> p_197690_, String p_197691_) {
this.lastId = p_197691_;
this.enterContainerIfNeeded(p_197690_);
return StreamTagVisitor.EntryResult.ENTER;
}
private void enterContainerIfNeeded(TagType<?> p_197712_) {
if (p_197712_ == ListTag.TYPE) {
ListTag listtag = new ListTag();
this.appendEntry(listtag);
this.consumerStack.addLast(listtag::add);
} else if (p_197712_ == CompoundTag.TYPE) {
CompoundTag compoundtag = new CompoundTag();
this.appendEntry(compoundtag);
this.consumerStack.addLast((p_197703_) -> {
compoundtag.put(this.lastId, p_197703_);
});
}
}
public StreamTagVisitor.ValueResult visitContainerEnd() {
this.consumerStack.removeLast();
return StreamTagVisitor.ValueResult.CONTINUE;
}
public StreamTagVisitor.ValueResult visitRootEntry(TagType<?> p_197707_) {
if (p_197707_ == ListTag.TYPE) {
ListTag listtag = new ListTag();
this.rootTag = listtag;
this.consumerStack.addLast(listtag::add);
} else if (p_197707_ == CompoundTag.TYPE) {
CompoundTag compoundtag = new CompoundTag();
this.rootTag = compoundtag;
this.consumerStack.addLast((p_197681_) -> {
compoundtag.put(this.lastId, p_197681_);
});
} else {
this.consumerStack.addLast((p_197705_) -> {
this.rootTag = p_197705_;
});
}
return StreamTagVisitor.ValueResult.CONTINUE;
}
}

View File

@@ -0,0 +1,18 @@
package net.minecraft.nbt.visitors;
import java.util.List;
import net.minecraft.nbt.TagType;
public record FieldSelector(List<String> path, TagType<?> type, String name) {
public FieldSelector(TagType<?> p_202514_, String p_202515_) {
this(List.of(), p_202514_, p_202515_);
}
public FieldSelector(String p_202506_, TagType<?> p_202507_, String p_202508_) {
this(List.of(p_202506_), p_202507_, p_202508_);
}
public FieldSelector(String p_202501_, String p_202502_, TagType<?> p_202503_, String p_202504_) {
this(List.of(p_202501_, p_202502_), p_202503_, p_202504_);
}
}

View File

@@ -0,0 +1,30 @@
package net.minecraft.nbt.visitors;
import java.util.HashMap;
import java.util.Map;
import net.minecraft.nbt.TagType;
public record FieldTree(int depth, Map<String, TagType<?>> selectedFields, Map<String, FieldTree> fieldsToRecurse) {
private FieldTree(int p_202527_) {
this(p_202527_, new HashMap<>(), new HashMap<>());
}
public static FieldTree createRoot() {
return new FieldTree(1);
}
public void addEntry(FieldSelector p_202539_) {
if (this.depth <= p_202539_.path().size()) {
this.fieldsToRecurse.computeIfAbsent(p_202539_.path().get(this.depth - 1), (p_202534_) -> {
return new FieldTree(this.depth + 1);
}).addEntry(p_202539_);
} else {
this.selectedFields.put(p_202539_.name(), p_202539_.type());
}
}
public boolean isSelected(TagType<?> p_202536_, String p_202537_) {
return p_202536_.equals(this.selectedFields().get(p_202537_));
}
}

View File

@@ -0,0 +1,77 @@
package net.minecraft.nbt.visitors;
import net.minecraft.nbt.StreamTagVisitor;
import net.minecraft.nbt.TagType;
public interface SkipAll extends StreamTagVisitor {
SkipAll INSTANCE = new SkipAll() {
};
default StreamTagVisitor.ValueResult visitEnd() {
return StreamTagVisitor.ValueResult.CONTINUE;
}
default StreamTagVisitor.ValueResult visit(String p_197729_) {
return StreamTagVisitor.ValueResult.CONTINUE;
}
default StreamTagVisitor.ValueResult visit(byte p_197719_) {
return StreamTagVisitor.ValueResult.CONTINUE;
}
default StreamTagVisitor.ValueResult visit(short p_197739_) {
return StreamTagVisitor.ValueResult.CONTINUE;
}
default StreamTagVisitor.ValueResult visit(int p_197725_) {
return StreamTagVisitor.ValueResult.CONTINUE;
}
default StreamTagVisitor.ValueResult visit(long p_197727_) {
return StreamTagVisitor.ValueResult.CONTINUE;
}
default StreamTagVisitor.ValueResult visit(float p_197723_) {
return StreamTagVisitor.ValueResult.CONTINUE;
}
default StreamTagVisitor.ValueResult visit(double p_197721_) {
return StreamTagVisitor.ValueResult.CONTINUE;
}
default StreamTagVisitor.ValueResult visit(byte[] p_197741_) {
return StreamTagVisitor.ValueResult.CONTINUE;
}
default StreamTagVisitor.ValueResult visit(int[] p_197743_) {
return StreamTagVisitor.ValueResult.CONTINUE;
}
default StreamTagVisitor.ValueResult visit(long[] p_197745_) {
return StreamTagVisitor.ValueResult.CONTINUE;
}
default StreamTagVisitor.ValueResult visitList(TagType<?> p_197733_, int p_197734_) {
return StreamTagVisitor.ValueResult.CONTINUE;
}
default StreamTagVisitor.EntryResult visitElement(TagType<?> p_197750_, int p_197751_) {
return StreamTagVisitor.EntryResult.SKIP;
}
default StreamTagVisitor.EntryResult visitEntry(TagType<?> p_197731_) {
return StreamTagVisitor.EntryResult.SKIP;
}
default StreamTagVisitor.EntryResult visitEntry(TagType<?> p_197736_, String p_197737_) {
return StreamTagVisitor.EntryResult.SKIP;
}
default StreamTagVisitor.ValueResult visitContainerEnd() {
return StreamTagVisitor.ValueResult.CONTINUE;
}
default StreamTagVisitor.ValueResult visitRootEntry(TagType<?> p_197748_) {
return StreamTagVisitor.ValueResult.CONTINUE;
}
}

View File

@@ -0,0 +1,45 @@
package net.minecraft.nbt.visitors;
import java.util.ArrayDeque;
import java.util.Deque;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.StreamTagVisitor;
import net.minecraft.nbt.TagType;
public class SkipFields extends CollectToTag {
private final Deque<FieldTree> stack = new ArrayDeque<>();
public SkipFields(FieldSelector... p_202549_) {
FieldTree fieldtree = FieldTree.createRoot();
for(FieldSelector fieldselector : p_202549_) {
fieldtree.addEntry(fieldselector);
}
this.stack.push(fieldtree);
}
public StreamTagVisitor.EntryResult visitEntry(TagType<?> p_202551_, String p_202552_) {
FieldTree fieldtree = this.stack.element();
if (fieldtree.isSelected(p_202551_, p_202552_)) {
return StreamTagVisitor.EntryResult.SKIP;
} else {
if (p_202551_ == CompoundTag.TYPE) {
FieldTree fieldtree1 = fieldtree.fieldsToRecurse().get(p_202552_);
if (fieldtree1 != null) {
this.stack.push(fieldtree1);
}
}
return super.visitEntry(p_202551_, p_202552_);
}
}
public StreamTagVisitor.ValueResult visitContainerEnd() {
if (this.depth() == this.stack.element().depth()) {
this.stack.pop();
}
return super.visitContainerEnd();
}
}

View File

@@ -0,0 +1,8 @@
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
@FieldsAreNonnullByDefault
package net.minecraft.nbt.visitors;
import javax.annotation.ParametersAreNonnullByDefault;
import net.minecraft.FieldsAreNonnullByDefault;
import net.minecraft.MethodsReturnNonnullByDefault;