отделяем сериализацию от сущностей
This commit is contained in:
13
src/main/java/mc/nbt/codec/vanilla/Codec.java
Normal file
13
src/main/java/mc/nbt/codec/vanilla/Codec.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package mc.nbt.codec.vanilla;
|
||||
|
||||
import mc.nbt.codec.vanilla.io.NbtInputStream;
|
||||
import mc.nbt.codec.vanilla.io.NbtOutputStream;
|
||||
import mc.nbt.tag.Tag;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface Codec<T extends Tag> {
|
||||
|
||||
T decode(NbtInputStream in) throws IOException;
|
||||
void encode(T tag, NbtOutputStream out) throws IOException;
|
||||
}
|
||||
36
src/main/java/mc/nbt/codec/vanilla/CodecEnum.java
Normal file
36
src/main/java/mc/nbt/codec/vanilla/CodecEnum.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package mc.nbt.codec.vanilla;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import mc.nbt.TypeTag;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Getter
|
||||
public enum CodecEnum {
|
||||
|
||||
END(TypeTag.END, new TagEndCodec()),
|
||||
BYTE(TypeTag.BYTE, new TagByteCodec()),
|
||||
SHORT(TypeTag.SHORT, new TagShortCodec()),
|
||||
INT(TypeTag.INT, new TagIntCodec()),
|
||||
LONG(TypeTag.LONG, new TagLongCodec()),
|
||||
FLOAT(TypeTag.FLOAT, new TagFloatCodec()),
|
||||
DOUBLE(TypeTag.DOUBLE, new TagDoubleCodec()),
|
||||
BYTE_ARRAY(TypeTag.BYTE_ARRAY, new TagByteArrayCodec()),
|
||||
STRING(TypeTag.STRING, new TagStringCodec()),
|
||||
LIST(TypeTag.LIST, new TagListCodec()),
|
||||
COMPOUND(TypeTag.COMPOUND, new TagCompoundCodec()),
|
||||
INT_ARRAY(TypeTag.INT_ARRAY, new TagIntArrayCodec()),
|
||||
LONG_ARRAY(TypeTag.LONG_ARRAY, new TagLongArrayCodec());
|
||||
|
||||
public static CodecEnum valueOfTypeTag(TypeTag typeTag) {
|
||||
return Stream.of(CodecEnum.values())
|
||||
.filter(codecEnum -> codecEnum.getTypeTag() == typeTag)
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new RuntimeException("Codec for " + typeTag.name() + " not found"));
|
||||
}
|
||||
|
||||
private final TypeTag typeTag;
|
||||
private final Codec codec;
|
||||
}
|
||||
23
src/main/java/mc/nbt/codec/vanilla/TagByteArrayCodec.java
Normal file
23
src/main/java/mc/nbt/codec/vanilla/TagByteArrayCodec.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package mc.nbt.codec.vanilla;
|
||||
|
||||
import mc.nbt.codec.vanilla.io.NbtInputStream;
|
||||
import mc.nbt.codec.vanilla.io.NbtOutputStream;
|
||||
import mc.nbt.tag.TagByteArray;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
class TagByteArrayCodec implements Codec<TagByteArray> {
|
||||
|
||||
@Override
|
||||
public TagByteArray decode(NbtInputStream in) throws IOException {
|
||||
byte[] value = new byte[in.readInt()];
|
||||
in.read(value);
|
||||
return new TagByteArray(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(TagByteArray tag, NbtOutputStream out) throws IOException {
|
||||
out.writeInt(tag.getValue().length);
|
||||
out.write(tag.getValue());
|
||||
}
|
||||
}
|
||||
20
src/main/java/mc/nbt/codec/vanilla/TagByteCodec.java
Normal file
20
src/main/java/mc/nbt/codec/vanilla/TagByteCodec.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package mc.nbt.codec.vanilla;
|
||||
|
||||
import mc.nbt.codec.vanilla.io.NbtInputStream;
|
||||
import mc.nbt.codec.vanilla.io.NbtOutputStream;
|
||||
import mc.nbt.tag.TagByte;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
class TagByteCodec implements Codec<TagByte> {
|
||||
|
||||
@Override
|
||||
public TagByte decode(NbtInputStream in) throws IOException {
|
||||
return new TagByte(in.readByte());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(TagByte tag, NbtOutputStream out) throws IOException {
|
||||
out.writeByte(tag.getValue());
|
||||
}
|
||||
}
|
||||
42
src/main/java/mc/nbt/codec/vanilla/TagCompoundCodec.java
Normal file
42
src/main/java/mc/nbt/codec/vanilla/TagCompoundCodec.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package mc.nbt.codec.vanilla;
|
||||
|
||||
import mc.nbt.TypeTag;
|
||||
import mc.nbt.codec.vanilla.io.NbtInputStream;
|
||||
import mc.nbt.codec.vanilla.io.NbtOutputStream;
|
||||
import mc.nbt.tag.Tag;
|
||||
import mc.nbt.tag.TagCompound;
|
||||
import mc.nbt.tag.TagEnd;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class TagCompoundCodec implements Codec<TagCompound> {
|
||||
|
||||
@Override
|
||||
public TagCompound decode(NbtInputStream in) throws IOException {
|
||||
List<Tag> tagList = new ArrayList<>();
|
||||
|
||||
while (true) {
|
||||
Tag tag = in.readTag();
|
||||
if (tag.getType() == TypeTag.END) {
|
||||
break;
|
||||
}
|
||||
|
||||
tagList.add(tag);
|
||||
}
|
||||
|
||||
TagCompound tagCompound = new TagCompound();
|
||||
tagCompound.setValue(tagList);
|
||||
return tagCompound;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(TagCompound tag, NbtOutputStream out) throws IOException {
|
||||
for (Tag itemTag : tag.getValue()) {
|
||||
out.writeTag(itemTag);
|
||||
}
|
||||
|
||||
out.writeTag(TagEnd.INSTANCE);
|
||||
}
|
||||
}
|
||||
20
src/main/java/mc/nbt/codec/vanilla/TagDoubleCodec.java
Normal file
20
src/main/java/mc/nbt/codec/vanilla/TagDoubleCodec.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package mc.nbt.codec.vanilla;
|
||||
|
||||
import mc.nbt.codec.vanilla.io.NbtInputStream;
|
||||
import mc.nbt.codec.vanilla.io.NbtOutputStream;
|
||||
import mc.nbt.tag.TagDouble;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class TagDoubleCodec implements Codec<TagDouble> {
|
||||
|
||||
@Override
|
||||
public TagDouble decode(NbtInputStream in) throws IOException {
|
||||
return new TagDouble(in.readDouble());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(TagDouble tag, NbtOutputStream out) throws IOException {
|
||||
out.writeDouble(tag.getValue());
|
||||
}
|
||||
}
|
||||
18
src/main/java/mc/nbt/codec/vanilla/TagEndCodec.java
Normal file
18
src/main/java/mc/nbt/codec/vanilla/TagEndCodec.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package mc.nbt.codec.vanilla;
|
||||
|
||||
import mc.nbt.codec.vanilla.io.NbtInputStream;
|
||||
import mc.nbt.codec.vanilla.io.NbtOutputStream;
|
||||
import mc.nbt.tag.TagEnd;
|
||||
|
||||
class TagEndCodec implements Codec<TagEnd> {
|
||||
|
||||
@Override
|
||||
public TagEnd decode(NbtInputStream in) {
|
||||
return TagEnd.INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(TagEnd tag, NbtOutputStream out) {
|
||||
//nothing
|
||||
}
|
||||
}
|
||||
20
src/main/java/mc/nbt/codec/vanilla/TagFloatCodec.java
Normal file
20
src/main/java/mc/nbt/codec/vanilla/TagFloatCodec.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package mc.nbt.codec.vanilla;
|
||||
|
||||
import mc.nbt.codec.vanilla.io.NbtInputStream;
|
||||
import mc.nbt.codec.vanilla.io.NbtOutputStream;
|
||||
import mc.nbt.tag.TagFloat;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class TagFloatCodec implements Codec<TagFloat> {
|
||||
|
||||
@Override
|
||||
public TagFloat decode(NbtInputStream in) throws IOException {
|
||||
return new TagFloat(in.readFloat());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(TagFloat tag, NbtOutputStream out) throws IOException {
|
||||
out.writeFloat(tag.getValue());
|
||||
}
|
||||
}
|
||||
29
src/main/java/mc/nbt/codec/vanilla/TagIntArrayCodec.java
Normal file
29
src/main/java/mc/nbt/codec/vanilla/TagIntArrayCodec.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package mc.nbt.codec.vanilla;
|
||||
|
||||
import mc.nbt.codec.vanilla.io.NbtInputStream;
|
||||
import mc.nbt.codec.vanilla.io.NbtOutputStream;
|
||||
import mc.nbt.tag.TagIntArray;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class TagIntArrayCodec implements Codec<TagIntArray> {
|
||||
|
||||
@Override
|
||||
public TagIntArray decode(NbtInputStream in) throws IOException {
|
||||
final int[] value = new int[in.readInt()];
|
||||
|
||||
for (int i = 0; i < value.length; i++) {
|
||||
value[i] = in.readInt();
|
||||
}
|
||||
|
||||
return new TagIntArray(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(TagIntArray tag, NbtOutputStream out) throws IOException {
|
||||
out.writeInt(tag.getValue().length);
|
||||
for (int item : tag.getValue()) {
|
||||
out.writeInt(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
20
src/main/java/mc/nbt/codec/vanilla/TagIntCodec.java
Normal file
20
src/main/java/mc/nbt/codec/vanilla/TagIntCodec.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package mc.nbt.codec.vanilla;
|
||||
|
||||
import mc.nbt.codec.vanilla.io.NbtInputStream;
|
||||
import mc.nbt.codec.vanilla.io.NbtOutputStream;
|
||||
import mc.nbt.tag.TagInt;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
class TagIntCodec implements Codec<TagInt> {
|
||||
|
||||
@Override
|
||||
public TagInt decode(NbtInputStream in) throws IOException {
|
||||
return new TagInt(in.readInt());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(TagInt tag, NbtOutputStream out) throws IOException {
|
||||
out.writeInt(tag.getValue());
|
||||
}
|
||||
}
|
||||
46
src/main/java/mc/nbt/codec/vanilla/TagListCodec.java
Normal file
46
src/main/java/mc/nbt/codec/vanilla/TagListCodec.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package mc.nbt.codec.vanilla;
|
||||
|
||||
import mc.nbt.TypeTag;
|
||||
import mc.nbt.codec.vanilla.io.NbtInputStream;
|
||||
import mc.nbt.codec.vanilla.io.NbtOutputStream;
|
||||
import mc.nbt.tag.Tag;
|
||||
import mc.nbt.tag.TagList;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class TagListCodec implements Codec<TagList> {
|
||||
|
||||
@Override
|
||||
public TagList decode(NbtInputStream in) throws IOException {
|
||||
final Codec typeCodec = CodecEnum.valueOfTypeTag(TypeTag.getTypeById(in.readByte())).getCodec();
|
||||
final int size = in.readInt();
|
||||
|
||||
List<Tag> tagList;
|
||||
if (size > 0) {
|
||||
tagList = new ArrayList<>(size);
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
tagList.add(typeCodec.decode(in));
|
||||
}
|
||||
} else {
|
||||
tagList = Collections.emptyList();
|
||||
}
|
||||
|
||||
return new TagList(tagList);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void encode(TagList tag, NbtOutputStream out) throws IOException {
|
||||
out.writeByte(tag.getTypeList().getId());
|
||||
out.writeInt(tag.getValue().size());
|
||||
|
||||
final Codec typeCodec = CodecEnum.valueOfTypeTag(tag.getTypeList()).getCodec();
|
||||
for (Tag item : tag.getValue()) {
|
||||
typeCodec.encode(item, out);
|
||||
}
|
||||
}
|
||||
}
|
||||
29
src/main/java/mc/nbt/codec/vanilla/TagLongArrayCodec.java
Normal file
29
src/main/java/mc/nbt/codec/vanilla/TagLongArrayCodec.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package mc.nbt.codec.vanilla;
|
||||
|
||||
import mc.nbt.codec.vanilla.io.NbtInputStream;
|
||||
import mc.nbt.codec.vanilla.io.NbtOutputStream;
|
||||
import mc.nbt.tag.TagLongArray;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class TagLongArrayCodec implements Codec<TagLongArray> {
|
||||
|
||||
@Override
|
||||
public TagLongArray decode(NbtInputStream in) throws IOException {
|
||||
final long[] value = new long[in.readInt()];
|
||||
|
||||
for (int i = 0; i < value.length; i++) {
|
||||
value[i] = in.readLong();
|
||||
}
|
||||
|
||||
return new TagLongArray(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(TagLongArray tag, NbtOutputStream out) throws IOException {
|
||||
out.writeInt(tag.getValue().length);
|
||||
for (long item : tag.getValue()) {
|
||||
out.writeLong(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
21
src/main/java/mc/nbt/codec/vanilla/TagLongCodec.java
Normal file
21
src/main/java/mc/nbt/codec/vanilla/TagLongCodec.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package mc.nbt.codec.vanilla;
|
||||
|
||||
import mc.nbt.codec.vanilla.io.NbtInputStream;
|
||||
import mc.nbt.codec.vanilla.io.NbtOutputStream;
|
||||
import mc.nbt.tag.TagLong;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class TagLongCodec implements Codec<TagLong> {
|
||||
|
||||
@Override
|
||||
public TagLong decode(NbtInputStream in) throws IOException {
|
||||
long value = in.readLong();
|
||||
return new TagLong(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(TagLong tag, NbtOutputStream out) throws IOException {
|
||||
out.writeLong(tag.getValue());
|
||||
}
|
||||
}
|
||||
20
src/main/java/mc/nbt/codec/vanilla/TagShortCodec.java
Normal file
20
src/main/java/mc/nbt/codec/vanilla/TagShortCodec.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package mc.nbt.codec.vanilla;
|
||||
|
||||
import mc.nbt.codec.vanilla.io.NbtInputStream;
|
||||
import mc.nbt.codec.vanilla.io.NbtOutputStream;
|
||||
import mc.nbt.tag.TagShort;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
class TagShortCodec implements Codec<TagShort> {
|
||||
|
||||
@Override
|
||||
public TagShort decode(NbtInputStream in) throws IOException {
|
||||
return new TagShort(in.readShort());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(TagShort tag, NbtOutputStream out) throws IOException {
|
||||
out.writeShort(tag.getValue());
|
||||
}
|
||||
}
|
||||
20
src/main/java/mc/nbt/codec/vanilla/TagStringCodec.java
Normal file
20
src/main/java/mc/nbt/codec/vanilla/TagStringCodec.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package mc.nbt.codec.vanilla;
|
||||
|
||||
import mc.nbt.codec.vanilla.io.NbtInputStream;
|
||||
import mc.nbt.codec.vanilla.io.NbtOutputStream;
|
||||
import mc.nbt.tag.TagString;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class TagStringCodec implements Codec<TagString> {
|
||||
|
||||
@Override
|
||||
public TagString decode(NbtInputStream in) throws IOException {
|
||||
return new TagString(in.readString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(TagString tag, NbtOutputStream out) throws IOException {
|
||||
out.writeString(tag.getValue());
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
package mc.nbt.io;
|
||||
package mc.nbt.codec.vanilla.io;
|
||||
|
||||
import mc.nbt.TypeTag;
|
||||
import mc.nbt.codec.vanilla.Codec;
|
||||
import mc.nbt.codec.vanilla.CodecEnum;
|
||||
import mc.nbt.tag.Tag;
|
||||
import mc.nbt.tag.TagEnd;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.IOException;
|
||||
@@ -24,23 +27,20 @@ public class NbtInputStream extends InputStream {
|
||||
this(inputStream, false);
|
||||
}
|
||||
|
||||
public Tag readTag() {
|
||||
Tag tag = null;
|
||||
|
||||
try {
|
||||
tag = TypeTag.getTagById(read());
|
||||
if (tag.getType() != TypeTag.END) {
|
||||
tag.setName(readString());
|
||||
}
|
||||
|
||||
tag.readSelf(this);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace(); //FIXME
|
||||
}
|
||||
public Tag readTag() throws IOException {
|
||||
final TypeTag typeTag = TypeTag.getTypeById(read());
|
||||
if (typeTag == TypeTag.END) {
|
||||
return TagEnd.INSTANCE;
|
||||
} else {
|
||||
final CodecEnum codecEnum = CodecEnum.valueOfTypeTag(typeTag);
|
||||
final Codec codec = codecEnum.getCodec();
|
||||
final String name = readString();
|
||||
final Tag tag = codec.decode(this);
|
||||
|
||||
tag.setName(name);
|
||||
return tag;
|
||||
}
|
||||
}
|
||||
|
||||
public byte readByte() throws IOException {
|
||||
return dataInputStream.readByte();
|
||||
@@ -1,5 +1,8 @@
|
||||
package mc.nbt.io;
|
||||
package mc.nbt.codec.vanilla.io;
|
||||
|
||||
import mc.nbt.TypeTag;
|
||||
import mc.nbt.codec.vanilla.Codec;
|
||||
import mc.nbt.codec.vanilla.CodecEnum;
|
||||
import mc.nbt.tag.Tag;
|
||||
|
||||
import java.io.DataOutputStream;
|
||||
@@ -23,10 +26,16 @@ public class NbtOutputStream extends OutputStream {
|
||||
this(outputStream, false);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void writeTag(Tag tag) throws IOException {
|
||||
writeByte(tag.getType().getId());
|
||||
final TypeTag typeTag = tag.getType();
|
||||
final CodecEnum codecEnum = CodecEnum.valueOfTypeTag(typeTag);
|
||||
final Codec codec = codecEnum.getCodec();
|
||||
|
||||
writeByte(typeTag.getId());
|
||||
writeString(tag.getName());
|
||||
tag.writeSelf(this);
|
||||
|
||||
codec.encode(tag, this);
|
||||
}
|
||||
|
||||
public void writeByte(int value) throws IOException {
|
||||
@@ -3,10 +3,6 @@ package mc.nbt.tag;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import mc.nbt.TypeTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public abstract class Tag {
|
||||
|
||||
@@ -16,10 +12,6 @@ public abstract class Tag {
|
||||
|
||||
public abstract TypeTag getType();
|
||||
|
||||
public abstract void readSelf(NbtInputStream nbtInputStream) throws IOException;
|
||||
|
||||
public abstract void writeSelf(NbtOutputStream nbtOutputStream) throws IOException;
|
||||
|
||||
public TagByte asTagByte() {
|
||||
return (TagByte) this;
|
||||
}
|
||||
|
||||
@@ -5,10 +5,6 @@ import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import mc.nbt.TypeTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@NoArgsConstructor
|
||||
@ToString(callSuper = true)
|
||||
@@ -31,14 +27,4 @@ public class TagByte extends Tag {
|
||||
public TypeTag getType() {
|
||||
return TypeTag.BYTE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readSelf(NbtInputStream nbtInputStream) throws IOException {
|
||||
value = nbtInputStream.readByte();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeSelf(NbtOutputStream nbtOutputStream) throws IOException {
|
||||
nbtOutputStream.writeByte(value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,6 @@ import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import mc.nbt.TypeTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@NoArgsConstructor
|
||||
@ToString(callSuper = true)
|
||||
@@ -31,16 +27,4 @@ public class TagByteArray extends Tag {
|
||||
public TypeTag getType() {
|
||||
return TypeTag.BYTE_ARRAY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readSelf(NbtInputStream nbtInputStream) throws IOException {
|
||||
value = new byte[nbtInputStream.readInt()];
|
||||
nbtInputStream.read(value); //FIXME
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeSelf(NbtOutputStream nbtOutputStream) throws IOException {
|
||||
nbtOutputStream.writeInt(value.length);
|
||||
nbtOutputStream.write(value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,11 +5,7 @@ import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import mc.nbt.TypeTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@NoArgsConstructor
|
||||
@@ -33,26 +29,4 @@ public class TagCompound extends Tag {
|
||||
public TypeTag getType() {
|
||||
return TypeTag.COMPOUND;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readSelf(NbtInputStream nbtInputStream) {
|
||||
value = new ArrayList<>();
|
||||
|
||||
while (true) {
|
||||
Tag tag = nbtInputStream.readTag();
|
||||
if (tag.getType() == TypeTag.END) {
|
||||
break;
|
||||
}
|
||||
|
||||
value.add(tag);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeSelf(NbtOutputStream nbtOutputStream) throws IOException {
|
||||
for (Tag tag : value) {
|
||||
nbtOutputStream.writeTag(tag);
|
||||
}
|
||||
nbtOutputStream.writeTag(new TagEnd());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,6 @@ import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import mc.nbt.TypeTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@NoArgsConstructor
|
||||
@ToString(callSuper = true)
|
||||
@@ -31,14 +27,4 @@ public class TagDouble extends Tag {
|
||||
public TypeTag getType() {
|
||||
return TypeTag.DOUBLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readSelf(NbtInputStream nbtInputStream) throws IOException {
|
||||
value = nbtInputStream.readDouble();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeSelf(NbtOutputStream nbtOutputStream) throws IOException {
|
||||
nbtOutputStream.writeDouble(value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package mc.nbt.tag;
|
||||
|
||||
import mc.nbt.TypeTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
public class TagEnd extends Tag {
|
||||
|
||||
public static final TagEnd INSTANCE = new TagEnd();
|
||||
|
||||
TagEnd() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setName(String name) {
|
||||
throw new UnsupportedOperationException();
|
||||
@@ -20,14 +23,4 @@ public class TagEnd extends Tag {
|
||||
public TypeTag getType() {
|
||||
return TypeTag.END;
|
||||
}
|
||||
|
||||
@lombok.Generated // Dirt hack for jacoco
|
||||
@Override
|
||||
public void readSelf(NbtInputStream nbtInputStream) {
|
||||
}
|
||||
|
||||
@lombok.Generated // Dirt hack for jacoco
|
||||
@Override
|
||||
public void writeSelf(NbtOutputStream nbtOutputStream) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,6 @@ import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import mc.nbt.TypeTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@NoArgsConstructor
|
||||
@ToString(callSuper = true)
|
||||
@@ -31,14 +27,4 @@ public class TagFloat extends Tag {
|
||||
public TypeTag getType() {
|
||||
return TypeTag.FLOAT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readSelf(NbtInputStream nbtInputStream) throws IOException {
|
||||
value = nbtInputStream.readFloat();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeSelf(NbtOutputStream nbtOutputStream) throws IOException {
|
||||
nbtOutputStream.writeFloat(value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,6 @@ import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import mc.nbt.TypeTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@NoArgsConstructor
|
||||
@ToString(callSuper = true)
|
||||
@@ -31,14 +27,4 @@ public class TagInt extends Tag {
|
||||
public TypeTag getType() {
|
||||
return TypeTag.INT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readSelf(NbtInputStream nbtInputStream) throws IOException {
|
||||
value = nbtInputStream.readInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeSelf(NbtOutputStream nbtOutputStream) throws IOException {
|
||||
nbtOutputStream.writeInt(value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,6 @@ import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import mc.nbt.TypeTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@NoArgsConstructor
|
||||
@ToString(callSuper = true)
|
||||
@@ -31,22 +27,4 @@ public class TagIntArray extends Tag {
|
||||
public TypeTag getType() {
|
||||
return TypeTag.INT_ARRAY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readSelf(NbtInputStream nbtInputStream) throws IOException {
|
||||
int count = nbtInputStream.readInt();
|
||||
value = new int[count];
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
value[i] = nbtInputStream.readInt();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeSelf(NbtOutputStream nbtOutputStream) throws IOException {
|
||||
nbtOutputStream.writeInt(value.length);
|
||||
for (int item : value) {
|
||||
nbtOutputStream.writeInt(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,11 +5,7 @@ import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import mc.nbt.TypeTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@@ -52,30 +48,4 @@ public class TagList extends Tag {
|
||||
public TypeTag getType() {
|
||||
return TypeTag.LIST;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readSelf(NbtInputStream nbtInputStream) throws IOException {
|
||||
typeList = TypeTag.getTypeById(nbtInputStream.readByte());
|
||||
|
||||
int size = nbtInputStream.readInt();
|
||||
if (size > 0) {
|
||||
value = new ArrayList<>(size);
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
Tag tag = TypeTag.getTagById(typeList.getId());
|
||||
tag.readSelf(nbtInputStream);
|
||||
value.add(tag);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeSelf(NbtOutputStream nbtOutputStream) throws IOException {
|
||||
nbtOutputStream.writeByte(typeList.getId());
|
||||
nbtOutputStream.writeInt(value.size());
|
||||
|
||||
for (Tag tag : value) {
|
||||
tag.writeSelf(nbtOutputStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,6 @@ import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import mc.nbt.TypeTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@NoArgsConstructor
|
||||
@ToString(callSuper = true)
|
||||
@@ -31,14 +27,4 @@ public class TagLong extends Tag {
|
||||
public TypeTag getType() {
|
||||
return TypeTag.LONG;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readSelf(NbtInputStream nbtInputStream) throws IOException {
|
||||
value = nbtInputStream.readLong();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeSelf(NbtOutputStream nbtOutputStream) throws IOException {
|
||||
nbtOutputStream.writeLong(value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,6 @@ import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import mc.nbt.TypeTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@NoArgsConstructor
|
||||
@ToString(callSuper = true)
|
||||
@@ -31,22 +27,4 @@ public class TagLongArray extends Tag {
|
||||
public TypeTag getType() {
|
||||
return TypeTag.LONG_ARRAY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readSelf(NbtInputStream nbtInputStream) throws IOException {
|
||||
int count = nbtInputStream.readInt();
|
||||
value = new long[count];
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
value[i] = nbtInputStream.readLong();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeSelf(NbtOutputStream nbtOutputStream) throws IOException {
|
||||
nbtOutputStream.writeInt(value.length);
|
||||
for (long item : value) {
|
||||
nbtOutputStream.writeLong(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,6 @@ import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import mc.nbt.TypeTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@NoArgsConstructor
|
||||
@ToString(callSuper = true)
|
||||
@@ -31,14 +27,4 @@ public class TagShort extends Tag {
|
||||
public TypeTag getType() {
|
||||
return TypeTag.SHORT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readSelf(NbtInputStream nbtInputStream) throws IOException {
|
||||
value = nbtInputStream.readShort();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeSelf(NbtOutputStream nbtOutputStream) throws IOException {
|
||||
nbtOutputStream.writeShort(value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,6 @@ import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import mc.nbt.TypeTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@NoArgsConstructor
|
||||
@ToString(callSuper = true)
|
||||
@@ -31,14 +27,4 @@ public class TagString extends Tag {
|
||||
public TypeTag getType() {
|
||||
return TypeTag.STRING;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readSelf(NbtInputStream nbtInputStream) throws IOException {
|
||||
value = nbtInputStream.readString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeSelf(NbtOutputStream nbtOutputStream) throws IOException {
|
||||
nbtOutputStream.writeString(value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package mc.nbt.io;
|
||||
package mc.nbt.codec.vanilla.io;
|
||||
|
||||
import mc.nbt.TypeTag;
|
||||
import mc.nbt.tag.*;
|
||||
@@ -1,4 +1,4 @@
|
||||
package mc.nbt.io;
|
||||
package mc.nbt.codec.vanilla.io;
|
||||
|
||||
import mc.nbt.TypeTag;
|
||||
import mc.nbt.tag.Tag;
|
||||
@@ -1,4 +1,4 @@
|
||||
package mc.nbt.io;
|
||||
package mc.nbt.codec.vanilla.io;
|
||||
|
||||
import mc.nbt.TypeTag;
|
||||
import mc.nbt.tag.*;
|
||||
@@ -32,7 +32,7 @@ class NbtOutputStreamTest {
|
||||
|
||||
@Test
|
||||
void testWriteTagEnd() throws IOException {
|
||||
nbtOutputStream.writeTag(new TagEnd());
|
||||
nbtOutputStream.writeTag(TagEnd.INSTANCE);
|
||||
|
||||
Tag tag = createNbtInputStream().readTag();
|
||||
assertEquals(TypeTag.END, tag.getType());
|
||||
@@ -201,10 +201,11 @@ class NbtOutputStreamTest {
|
||||
|
||||
private static Stream<Arguments> streamOfTagList() {
|
||||
final List<Tag> value = new ArrayList<>();
|
||||
value.add(new TagLong("TestName", RandomUtils.nextLong()));
|
||||
value.add(new TagLong(RandomUtils.nextLong()));
|
||||
value.add(new TagLong(RandomUtils.nextLong()));
|
||||
value.add(new TagLong(RandomUtils.nextLong()));
|
||||
// value.add(new TagLong("TestName", RandomUtils.nextLong()));
|
||||
// value.add(new TagLong(RandomUtils.nextLong()));
|
||||
// value.add(new TagLong(RandomUtils.nextLong()));
|
||||
// value.add(new TagLong(RandomUtils.nextLong()));
|
||||
value.add(new TagLong(1L));
|
||||
|
||||
return Stream.of(
|
||||
Arguments.of(new TagList(value)),
|
||||
Reference in New Issue
Block a user