refac: переосмысление
This commit is contained in:
41
src/main/java/mc/nbt/ByteArrayTag.java
Normal file
41
src/main/java/mc/nbt/ByteArrayTag.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package mc.nbt;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
public class ByteArrayTag extends TagValue {
|
||||
private final byte[] value;
|
||||
|
||||
public ByteArrayTag(byte[] value) {
|
||||
this.value = Objects.requireNonNull(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType getType() {
|
||||
return TagType.BYTE_ARRAY;
|
||||
}
|
||||
|
||||
public byte[] getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
ByteArrayTag that = (ByteArrayTag) o;
|
||||
return Arrays.equals(getValue(), that.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ByteArrayTag{" +
|
||||
"value.length=" + value.length +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
40
src/main/java/mc/nbt/ByteTag.java
Normal file
40
src/main/java/mc/nbt/ByteTag.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package mc.nbt;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class ByteTag extends TagValue {
|
||||
private final byte value;
|
||||
|
||||
public ByteTag(byte value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType getType() {
|
||||
return TagType.BYTE;
|
||||
}
|
||||
|
||||
public byte getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
ByteTag byteTag = (ByteTag) o;
|
||||
return getValue() == byteTag.getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ByteTag{" +
|
||||
"value=" + value +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
113
src/main/java/mc/nbt/CompoundTag.java
Normal file
113
src/main/java/mc/nbt/CompoundTag.java
Normal file
@@ -0,0 +1,113 @@
|
||||
package mc.nbt;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.*;
|
||||
|
||||
public class CompoundTag extends TagValue implements Map<String, TagValue>, Serializable {
|
||||
private final LinkedHashMap<String, TagValue> map;
|
||||
|
||||
public CompoundTag() {
|
||||
this.map = new LinkedHashMap<>();
|
||||
}
|
||||
|
||||
public CompoundTag(String key, TagValue value) {
|
||||
this.map = new LinkedHashMap<>();
|
||||
this.map.put(key, value);
|
||||
}
|
||||
|
||||
public CompoundTag(Map<String, TagValue> map) {
|
||||
this.map = new LinkedHashMap<>(map);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType getType() {
|
||||
return TagType.COMPOUND;
|
||||
}
|
||||
|
||||
public CompoundTag append(String key, TagValue value) {
|
||||
this.map.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
// Map methods
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return map.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return map.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsKey(Object key) {
|
||||
return map.containsKey(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsValue(Object value) {
|
||||
return map.containsValue(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagValue get(Object key) {
|
||||
return map.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagValue put(String key, TagValue value) {
|
||||
return map.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagValue remove(Object value) {
|
||||
return map.remove(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putAll(Map<? extends String, ? extends TagValue> map) {
|
||||
this.map.putAll(map);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
map.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> keySet() {
|
||||
return map.keySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<TagValue> values() {
|
||||
return map.values();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Entry<String, TagValue>> entrySet() {
|
||||
return map.entrySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
CompoundTag that = (CompoundTag) o;
|
||||
return map.equals(that.map);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(map);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CompoundTag{"
|
||||
+ map
|
||||
+ '}';
|
||||
}
|
||||
}
|
||||
40
src/main/java/mc/nbt/DoubleTag.java
Normal file
40
src/main/java/mc/nbt/DoubleTag.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package mc.nbt;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class DoubleTag extends TagValue {
|
||||
private final double value;
|
||||
|
||||
public DoubleTag(double value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType getType() {
|
||||
return TagType.DOUBLE;
|
||||
}
|
||||
|
||||
public double getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
DoubleTag doubleTag = (DoubleTag) o;
|
||||
return Double.compare(doubleTag.getValue(), getValue()) == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DoubleTag{" +
|
||||
"value=" + value +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
27
src/main/java/mc/nbt/EndTag.java
Normal file
27
src/main/java/mc/nbt/EndTag.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package mc.nbt;
|
||||
|
||||
public class EndTag extends TagValue {
|
||||
private static final EndTag INSTANCE = new EndTag();
|
||||
|
||||
private EndTag() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType getType() {
|
||||
return TagType.END;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setName(String name) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public static EndTag getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
}
|
||||
40
src/main/java/mc/nbt/FloatTag.java
Normal file
40
src/main/java/mc/nbt/FloatTag.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package mc.nbt;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class FloatTag extends TagValue {
|
||||
private final float value;
|
||||
|
||||
public FloatTag(float value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType getType() {
|
||||
return TagType.FLOAT;
|
||||
}
|
||||
|
||||
public float getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
FloatTag floatTag = (FloatTag) o;
|
||||
return Float.compare(floatTag.getValue(), getValue()) == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "FloatTag{" +
|
||||
"value=" + value +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
41
src/main/java/mc/nbt/IntegerArrayTag.java
Normal file
41
src/main/java/mc/nbt/IntegerArrayTag.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package mc.nbt;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
public class IntegerArrayTag extends TagValue {
|
||||
private final int[] value;
|
||||
|
||||
public IntegerArrayTag(int[] value) {
|
||||
this.value = Objects.requireNonNull(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType getType() {
|
||||
return TagType.INTEGER_ARRAY;
|
||||
}
|
||||
|
||||
public int[] getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
IntegerArrayTag that = (IntegerArrayTag) o;
|
||||
return Arrays.equals(getValue(), that.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "IntegerArrayTag{" +
|
||||
"value.length=" + value.length +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
40
src/main/java/mc/nbt/IntegerTag.java
Normal file
40
src/main/java/mc/nbt/IntegerTag.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package mc.nbt;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class IntegerTag extends TagValue {
|
||||
private final int value;
|
||||
|
||||
public IntegerTag(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType getType() {
|
||||
return TagType.INTEGER;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
IntegerTag that = (IntegerTag) o;
|
||||
return getValue() == that.getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "IntegerTag{" +
|
||||
"value=" + value +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
173
src/main/java/mc/nbt/ListTag.java
Normal file
173
src/main/java/mc/nbt/ListTag.java
Normal file
@@ -0,0 +1,173 @@
|
||||
package mc.nbt;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class ListTag extends TagValue implements List<TagValue> {
|
||||
|
||||
private final TagType listType;
|
||||
private final List<TagValue> list;
|
||||
|
||||
public ListTag(TagType listType, int initialCapacity) {
|
||||
this.listType = Objects.requireNonNull(listType);
|
||||
this.list = new ArrayList<>(initialCapacity);
|
||||
}
|
||||
|
||||
public ListTag(TagType listType) {
|
||||
this.listType = Objects.requireNonNull(listType);
|
||||
this.list = new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType getType() {
|
||||
return TagType.LIST;
|
||||
}
|
||||
|
||||
public TagType getListType() {
|
||||
return listType;
|
||||
}
|
||||
|
||||
public ListTag append(TagValue tag) {
|
||||
list.add(tag);
|
||||
return this;
|
||||
}
|
||||
|
||||
// List methods
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return list.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return list.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object o) {
|
||||
return list.contains(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<TagValue> iterator() {
|
||||
return list.iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] toArray() {
|
||||
return list.toArray();
|
||||
}
|
||||
|
||||
@SuppressWarnings("SuspiciousToArrayCall")
|
||||
@Override
|
||||
public <A> A[] toArray(A[] array) {
|
||||
return list.toArray(array);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(TagValue tag) {
|
||||
return list.add(tag);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Object o) {
|
||||
return list.remove(o);
|
||||
}
|
||||
|
||||
@SuppressWarnings("SlowListContainsAll")
|
||||
@Override
|
||||
public boolean containsAll(Collection<?> collection) {
|
||||
return list.containsAll(collection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(Collection<? extends TagValue> collection) {
|
||||
return list.addAll(collection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(int index, Collection<? extends TagValue> collection) {
|
||||
return list.addAll(index, collection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAll(Collection<?> collection) {
|
||||
return list.removeAll(collection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll(Collection<?> collection) {
|
||||
return list.retainAll(collection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
list.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagValue get(int index) {
|
||||
return list.get(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagValue set(int index, TagValue element) {
|
||||
return list.set(index, element);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(int index, TagValue element) {
|
||||
list.add(index, element);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagValue remove(int index) {
|
||||
return list.remove(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int indexOf(Object o) {
|
||||
return list.indexOf(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int lastIndexOf(Object o) {
|
||||
return list.lastIndexOf(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListIterator<TagValue> listIterator() {
|
||||
return list.listIterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListIterator<TagValue> listIterator(int index) {
|
||||
return list.listIterator(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TagValue> subList(int fromIndex, int toIndex) {
|
||||
return list.subList(fromIndex, toIndex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
ListTag listTag = (ListTag) o;
|
||||
return list.equals(listTag.list) && getListType() == listTag.getListType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(list, getListType());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ListTag{" +
|
||||
"listType=" + listType +
|
||||
", list=" + list +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
41
src/main/java/mc/nbt/LongArrayTag.java
Normal file
41
src/main/java/mc/nbt/LongArrayTag.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package mc.nbt;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
public class LongArrayTag extends TagValue {
|
||||
private final long[] value;
|
||||
|
||||
public LongArrayTag(long[] value) {
|
||||
this.value = Objects.requireNonNull(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType getType() {
|
||||
return TagType.LONG_ARRAY;
|
||||
}
|
||||
|
||||
public long[] getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
LongArrayTag that = (LongArrayTag) o;
|
||||
return Arrays.equals(getValue(), that.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "LongArrayTag{" +
|
||||
"value.length=" + value.length +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
40
src/main/java/mc/nbt/LongTag.java
Normal file
40
src/main/java/mc/nbt/LongTag.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package mc.nbt;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class LongTag extends TagValue {
|
||||
private final long value;
|
||||
|
||||
public LongTag(long value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType getType() {
|
||||
return TagType.LONG;
|
||||
}
|
||||
|
||||
public long getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
LongTag longTag = (LongTag) o;
|
||||
return getValue() == longTag.getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "LongTag{" +
|
||||
"value=" + value +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
40
src/main/java/mc/nbt/ShortTag.java
Normal file
40
src/main/java/mc/nbt/ShortTag.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package mc.nbt;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class ShortTag extends TagValue {
|
||||
private final short value;
|
||||
|
||||
public ShortTag(short value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType getType() {
|
||||
return TagType.SHORT;
|
||||
}
|
||||
|
||||
public short getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
ShortTag shortTag = (ShortTag) o;
|
||||
return getValue() == shortTag.getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ShortTag{" +
|
||||
"value=" + value +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
41
src/main/java/mc/nbt/StringTag.java
Normal file
41
src/main/java/mc/nbt/StringTag.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package mc.nbt;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class StringTag extends TagValue {
|
||||
|
||||
private final String value;
|
||||
|
||||
public StringTag(String value) {
|
||||
this.value = Objects.requireNonNull(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType getType() {
|
||||
return TagType.STRING;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
StringTag stringTag = (StringTag) o;
|
||||
return getValue().equals(stringTag.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "StringTag{" +
|
||||
"value='" + value + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
40
src/main/java/mc/nbt/TagType.java
Normal file
40
src/main/java/mc/nbt/TagType.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package mc.nbt;
|
||||
|
||||
public enum TagType {
|
||||
BYTE(1),
|
||||
SHORT(2),
|
||||
INTEGER(3),
|
||||
LONG(4),
|
||||
FLOAT(5),
|
||||
DOUBLE(6),
|
||||
|
||||
BYTE_ARRAY(7),
|
||||
INTEGER_ARRAY(11),
|
||||
LONG_ARRAY(12),
|
||||
|
||||
STRING(8),
|
||||
LIST(9),
|
||||
COMPOUND(10),
|
||||
|
||||
END(0);
|
||||
|
||||
private final int id;
|
||||
|
||||
TagType(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public static TagType valueOfId(int id) {
|
||||
for (TagType type : TagType.values()) {
|
||||
if (type.id == id) {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
||||
throw new RuntimeException("Unknown tag id: " + id); //TODO семантический exception
|
||||
}
|
||||
}
|
||||
135
src/main/java/mc/nbt/TagValue.java
Normal file
135
src/main/java/mc/nbt/TagValue.java
Normal file
@@ -0,0 +1,135 @@
|
||||
package mc.nbt;
|
||||
|
||||
public abstract class TagValue {
|
||||
protected String name;
|
||||
|
||||
TagValue() {
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public abstract TagType getType();
|
||||
|
||||
public ByteTag asByte() {
|
||||
throwIfInvalidType(TagType.BYTE);
|
||||
return (ByteTag) this;
|
||||
}
|
||||
|
||||
public ByteArrayTag asByteArray() {
|
||||
throwIfInvalidType(TagType.BYTE_ARRAY);
|
||||
return (ByteArrayTag) this;
|
||||
}
|
||||
|
||||
public ShortTag asShort() {
|
||||
throwIfInvalidType(TagType.SHORT);
|
||||
return (ShortTag) this;
|
||||
}
|
||||
|
||||
public IntegerTag asInteger() {
|
||||
throwIfInvalidType(TagType.INTEGER);
|
||||
return (IntegerTag) this;
|
||||
}
|
||||
|
||||
public IntegerArrayTag asIntegerArray() {
|
||||
throwIfInvalidType(TagType.INTEGER_ARRAY);
|
||||
return (IntegerArrayTag) this;
|
||||
}
|
||||
|
||||
public LongTag asLong() {
|
||||
throwIfInvalidType(TagType.LONG);
|
||||
return (LongTag) this;
|
||||
}
|
||||
|
||||
public LongArrayTag asLongArray() {
|
||||
throwIfInvalidType(TagType.LONG_ARRAY);
|
||||
return (LongArrayTag) this;
|
||||
}
|
||||
|
||||
public FloatTag asFloat() {
|
||||
throwIfInvalidType(TagType.FLOAT);
|
||||
return (FloatTag) this;
|
||||
}
|
||||
|
||||
public DoubleTag asDouble() {
|
||||
throwIfInvalidType(TagType.DOUBLE);
|
||||
return (DoubleTag) this;
|
||||
}
|
||||
|
||||
public StringTag asString() {
|
||||
throwIfInvalidType(TagType.STRING);
|
||||
return (StringTag) this;
|
||||
}
|
||||
|
||||
public ListTag asList() {
|
||||
throwIfInvalidType(TagType.LIST);
|
||||
return (ListTag) this;
|
||||
}
|
||||
|
||||
public CompoundTag asCompound() {
|
||||
throwIfInvalidType(TagType.COMPOUND);
|
||||
return (CompoundTag) this;
|
||||
}
|
||||
|
||||
public boolean isByte() {
|
||||
return getType() == TagType.BYTE;
|
||||
}
|
||||
|
||||
public boolean isByteArray() {
|
||||
return getType() == TagType.BYTE_ARRAY;
|
||||
}
|
||||
|
||||
public boolean isShort() {
|
||||
return getType() == TagType.SHORT;
|
||||
}
|
||||
|
||||
public boolean isInteger() {
|
||||
return getType() == TagType.INTEGER;
|
||||
}
|
||||
|
||||
public boolean isIntegerArray() {
|
||||
return getType() == TagType.INTEGER_ARRAY;
|
||||
}
|
||||
|
||||
public boolean isLong() {
|
||||
return getType() == TagType.LONG;
|
||||
}
|
||||
|
||||
public boolean isLongArray() {
|
||||
return getType() == TagType.LONG_ARRAY;
|
||||
}
|
||||
|
||||
public boolean isFloat() {
|
||||
return getType() == TagType.FLOAT;
|
||||
}
|
||||
|
||||
public boolean isDouble() {
|
||||
return getType() == TagType.DOUBLE;
|
||||
}
|
||||
|
||||
public boolean isString() {
|
||||
return getType() == TagType.STRING;
|
||||
}
|
||||
|
||||
public boolean isCompound() {
|
||||
return getType() == TagType.COMPOUND;
|
||||
}
|
||||
|
||||
public boolean isEnd() {
|
||||
return getType() == TagType.END;
|
||||
}
|
||||
|
||||
private void throwIfInvalidType(TagType expectedType) {
|
||||
if (getType() != expectedType) {
|
||||
//TODO семантический exception
|
||||
throw new RuntimeException(
|
||||
"Value expected to be type %s is of unexpected type %s"
|
||||
.formatted(expectedType, getType()));
|
||||
}
|
||||
}
|
||||
}
|
||||
26
src/main/java/mc/nbt/codec/ByteArrayTagCodec.java
Normal file
26
src/main/java/mc/nbt/codec/ByteArrayTagCodec.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.ByteArrayTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class ByteArrayTagCodec implements Codec<ByteArrayTag> {
|
||||
|
||||
@Override
|
||||
public void encode(ByteArrayTag tag, NbtOutputStream outputStream) throws IOException {
|
||||
outputStream.writeInt(tag.getValue().length);
|
||||
outputStream.write(tag.getValue());
|
||||
}
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
@Override
|
||||
public ByteArrayTag decode(NbtInputStream inputStream) throws IOException {
|
||||
int len = inputStream.readInt();
|
||||
byte[] bytes = new byte[len];
|
||||
inputStream.read(bytes);
|
||||
|
||||
return new ByteArrayTag(bytes);
|
||||
}
|
||||
}
|
||||
20
src/main/java/mc/nbt/codec/ByteTagCodec.java
Normal file
20
src/main/java/mc/nbt/codec/ByteTagCodec.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.ByteTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class ByteTagCodec implements Codec<ByteTag> {
|
||||
|
||||
@Override
|
||||
public void encode(ByteTag tag, NbtOutputStream outputStream) throws IOException {
|
||||
outputStream.writeByte(tag.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteTag decode(NbtInputStream inputStream) throws IOException {
|
||||
return new ByteTag(inputStream.readByte());
|
||||
}
|
||||
}
|
||||
14
src/main/java/mc/nbt/codec/Codec.java
Normal file
14
src/main/java/mc/nbt/codec/Codec.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.TagValue;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface Codec<T extends TagValue> {
|
||||
|
||||
void encode(T tag, NbtOutputStream outputStream) throws IOException;
|
||||
|
||||
T decode(NbtInputStream inputStream) throws IOException;
|
||||
}
|
||||
39
src/main/java/mc/nbt/codec/CodecRegistry.java
Normal file
39
src/main/java/mc/nbt/codec/CodecRegistry.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.TagType;
|
||||
import mc.nbt.TagValue;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public class CodecRegistry {
|
||||
private static final CodecRegistry INSTANCE = new CodecRegistry();
|
||||
|
||||
private final Map<TagType, Codec<? extends TagValue>> codecs;
|
||||
|
||||
private CodecRegistry() {
|
||||
codecs = Map.ofEntries(
|
||||
Map.entry(TagType.BYTE, new ByteTagCodec()),
|
||||
Map.entry(TagType.SHORT, new ShortTagCodec()),
|
||||
Map.entry(TagType.INTEGER, new IntegerTagCodec()),
|
||||
Map.entry(TagType.LONG, new LongTagCodec()),
|
||||
Map.entry(TagType.FLOAT, new FloatTagCodec()),
|
||||
Map.entry(TagType.DOUBLE, new DoubleTagCodec()),
|
||||
Map.entry(TagType.BYTE_ARRAY, new ByteArrayTagCodec()),
|
||||
Map.entry(TagType.INTEGER_ARRAY, new IntegerArrayTagCodec()),
|
||||
Map.entry(TagType.LONG_ARRAY, new LongArrayTagCodec()),
|
||||
Map.entry(TagType.STRING, new StringTagCodec()),
|
||||
Map.entry(TagType.LIST, new ListTagCodec()),
|
||||
Map.entry(TagType.COMPOUND, new CompoundTagCodec()),
|
||||
Map.entry(TagType.END, new EndTagCodec())
|
||||
);
|
||||
}
|
||||
|
||||
public Codec<? extends TagValue> get(TagType tagType) {
|
||||
return codecs.get(Objects.requireNonNull(tagType));
|
||||
}
|
||||
|
||||
public static CodecRegistry getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
}
|
||||
40
src/main/java/mc/nbt/codec/CompoundTagCodec.java
Normal file
40
src/main/java/mc/nbt/codec/CompoundTagCodec.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.CompoundTag;
|
||||
import mc.nbt.EndTag;
|
||||
import mc.nbt.TagValue;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
public class CompoundTagCodec implements Codec<CompoundTag> {
|
||||
|
||||
@Override
|
||||
public void encode(CompoundTag tag, NbtOutputStream outputStream) throws IOException {
|
||||
for (Map.Entry<String, TagValue> entry : tag.entrySet()) {
|
||||
if (entry.getValue() == null) //TODO по-хорошему, пользователю нужно запретить вставлять null-теги
|
||||
continue;
|
||||
|
||||
outputStream.writeTag(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
outputStream.writeTag(EndTag.getInstance());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag decode(NbtInputStream inputStream) throws IOException {
|
||||
var compoundTag = new CompoundTag();
|
||||
|
||||
do {
|
||||
var tagValue = inputStream.readTag();
|
||||
if (tagValue.isEnd())
|
||||
break;
|
||||
|
||||
compoundTag.append(tagValue.getName(), tagValue);
|
||||
} while (true);
|
||||
|
||||
return compoundTag;
|
||||
}
|
||||
}
|
||||
20
src/main/java/mc/nbt/codec/DoubleTagCodec.java
Normal file
20
src/main/java/mc/nbt/codec/DoubleTagCodec.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.DoubleTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class DoubleTagCodec implements Codec<DoubleTag> {
|
||||
|
||||
@Override
|
||||
public void encode(DoubleTag tag, NbtOutputStream outputStream) throws IOException {
|
||||
outputStream.writeDouble(tag.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public DoubleTag decode(NbtInputStream inputStream) throws IOException {
|
||||
return new DoubleTag(inputStream.readDouble());
|
||||
}
|
||||
}
|
||||
20
src/main/java/mc/nbt/codec/EndTagCodec.java
Normal file
20
src/main/java/mc/nbt/codec/EndTagCodec.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.EndTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class EndTagCodec implements Codec<EndTag> {
|
||||
|
||||
@Override
|
||||
public void encode(EndTag tag, NbtOutputStream outputStream) throws IOException {
|
||||
// nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public EndTag decode(NbtInputStream inputStream) throws IOException {
|
||||
return EndTag.getInstance();
|
||||
}
|
||||
}
|
||||
20
src/main/java/mc/nbt/codec/FloatTagCodec.java
Normal file
20
src/main/java/mc/nbt/codec/FloatTagCodec.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.FloatTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class FloatTagCodec implements Codec<FloatTag> {
|
||||
|
||||
@Override
|
||||
public void encode(FloatTag tag, NbtOutputStream outputStream) throws IOException {
|
||||
outputStream.writeFloat(tag.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public FloatTag decode(NbtInputStream inputStream) throws IOException {
|
||||
return new FloatTag(inputStream.readFloat());
|
||||
}
|
||||
}
|
||||
30
src/main/java/mc/nbt/codec/IntegerArrayTagCodec.java
Normal file
30
src/main/java/mc/nbt/codec/IntegerArrayTagCodec.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.IntegerArrayTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class IntegerArrayTagCodec implements Codec<IntegerArrayTag> {
|
||||
|
||||
@Override
|
||||
public void encode(IntegerArrayTag tag, NbtOutputStream outputStream) throws IOException {
|
||||
outputStream.writeInt(tag.getValue().length);
|
||||
for (int item : tag.getValue()) {
|
||||
outputStream.writeInt(item);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IntegerArrayTag decode(NbtInputStream inputStream) throws IOException {
|
||||
int len = inputStream.readInt();
|
||||
int[] ints = new int[len];
|
||||
|
||||
for (int i = 0; i < len; i++) {
|
||||
ints[i] = inputStream.readInt();
|
||||
}
|
||||
|
||||
return new IntegerArrayTag(ints);
|
||||
}
|
||||
}
|
||||
20
src/main/java/mc/nbt/codec/IntegerTagCodec.java
Normal file
20
src/main/java/mc/nbt/codec/IntegerTagCodec.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.IntegerTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class IntegerTagCodec implements Codec<IntegerTag> {
|
||||
|
||||
@Override
|
||||
public void encode(IntegerTag tag, NbtOutputStream outputStream) throws IOException {
|
||||
outputStream.writeInt(tag.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public IntegerTag decode(NbtInputStream inputStream) throws IOException {
|
||||
return new IntegerTag(inputStream.readInt());
|
||||
}
|
||||
}
|
||||
45
src/main/java/mc/nbt/codec/ListTagCodec.java
Normal file
45
src/main/java/mc/nbt/codec/ListTagCodec.java
Normal file
@@ -0,0 +1,45 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.ListTag;
|
||||
import mc.nbt.TagType;
|
||||
import mc.nbt.TagValue;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class ListTagCodec implements Codec<ListTag> {
|
||||
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
@Override
|
||||
public void encode(ListTag tag, NbtOutputStream outputStream) throws IOException {
|
||||
outputStream.writeByte(tag.getListType().getId());
|
||||
outputStream.writeInt(tag.size());
|
||||
|
||||
Codec codec = CodecRegistry.getInstance().get(tag.getListType());
|
||||
for (TagValue tagValue : tag) {
|
||||
codec.encode(tagValue, outputStream);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Override
|
||||
public ListTag decode(NbtInputStream inputStream) throws IOException {
|
||||
int listTypeId = inputStream.readByte();
|
||||
TagType listType = TagType.valueOfId(listTypeId);
|
||||
|
||||
int size = inputStream.readInt();
|
||||
ListTag list = new ListTag(listType, size);
|
||||
|
||||
Codec codec = CodecRegistry.getInstance().get(listType);
|
||||
|
||||
if (size > 0) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
var tagValue = codec.decode(inputStream);
|
||||
list.add(tagValue);
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
30
src/main/java/mc/nbt/codec/LongArrayTagCodec.java
Normal file
30
src/main/java/mc/nbt/codec/LongArrayTagCodec.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.LongArrayTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class LongArrayTagCodec implements Codec<LongArrayTag> {
|
||||
|
||||
@Override
|
||||
public void encode(LongArrayTag tag, NbtOutputStream outputStream) throws IOException {
|
||||
outputStream.writeInt(tag.getValue().length);
|
||||
for (long value : tag.getValue()) {
|
||||
outputStream.writeLong(value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public LongArrayTag decode(NbtInputStream inputStream) throws IOException {
|
||||
int len = inputStream.readInt();
|
||||
long[] longs = new long[len];
|
||||
|
||||
for (int i = 0; i < len; i++) {
|
||||
longs[i] = inputStream.readLong();
|
||||
}
|
||||
|
||||
return new LongArrayTag(longs);
|
||||
}
|
||||
}
|
||||
20
src/main/java/mc/nbt/codec/LongTagCodec.java
Normal file
20
src/main/java/mc/nbt/codec/LongTagCodec.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.LongTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class LongTagCodec implements Codec<LongTag> {
|
||||
|
||||
@Override
|
||||
public void encode(LongTag tag, NbtOutputStream outputStream) throws IOException {
|
||||
outputStream.writeLong(tag.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public LongTag decode(NbtInputStream inputStream) throws IOException {
|
||||
return new LongTag(inputStream.readLong());
|
||||
}
|
||||
}
|
||||
20
src/main/java/mc/nbt/codec/ShortTagCodec.java
Normal file
20
src/main/java/mc/nbt/codec/ShortTagCodec.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.ShortTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class ShortTagCodec implements Codec<ShortTag> {
|
||||
|
||||
@Override
|
||||
public void encode(ShortTag tag, NbtOutputStream outputStream) throws IOException {
|
||||
outputStream.writeShort(tag.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ShortTag decode(NbtInputStream inputStream) throws IOException {
|
||||
return new ShortTag(inputStream.readShort());
|
||||
}
|
||||
}
|
||||
19
src/main/java/mc/nbt/codec/StringTagCodec.java
Normal file
19
src/main/java/mc/nbt/codec/StringTagCodec.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.StringTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class StringTagCodec implements Codec<StringTag> {
|
||||
@Override
|
||||
public void encode(StringTag tag, NbtOutputStream outputStream) throws IOException {
|
||||
outputStream.writeString(tag.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public StringTag decode(NbtInputStream inputStream) throws IOException {
|
||||
return new StringTag(inputStream.readString());
|
||||
}
|
||||
}
|
||||
91
src/main/java/mc/nbt/io/NbtInputStream.java
Normal file
91
src/main/java/mc/nbt/io/NbtInputStream.java
Normal file
@@ -0,0 +1,91 @@
|
||||
package mc.nbt.io;
|
||||
|
||||
import mc.nbt.EndTag;
|
||||
import mc.nbt.TagType;
|
||||
import mc.nbt.TagValue;
|
||||
import mc.nbt.codec.Codec;
|
||||
import mc.nbt.codec.CodecRegistry;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
public class NbtInputStream extends InputStream {
|
||||
|
||||
private final DataInputStream dataInputStream;
|
||||
|
||||
public NbtInputStream(InputStream inputStream, boolean compressed) throws IOException {
|
||||
this.dataInputStream = new DataInputStream(compressed
|
||||
? new GZIPInputStream(inputStream)
|
||||
: inputStream);
|
||||
}
|
||||
|
||||
public NbtInputStream(InputStream inputStream) throws IOException {
|
||||
this(inputStream, false);
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public TagValue readTag() throws IOException {
|
||||
var tagType = TagType.valueOfId(read());
|
||||
if (tagType == TagType.END) {
|
||||
return EndTag.getInstance();
|
||||
}
|
||||
|
||||
Codec codec = CodecRegistry.getInstance().get(tagType);
|
||||
String name = readString();
|
||||
var tagValue = codec.decode(this);
|
||||
|
||||
tagValue.setName(name);
|
||||
return tagValue;
|
||||
}
|
||||
|
||||
public byte readByte() throws IOException {
|
||||
return dataInputStream.readByte();
|
||||
}
|
||||
|
||||
public short readShort() throws IOException {
|
||||
return dataInputStream.readShort();
|
||||
}
|
||||
|
||||
public int readInt() throws IOException {
|
||||
return dataInputStream.readInt();
|
||||
}
|
||||
|
||||
public long readLong() throws IOException {
|
||||
return dataInputStream.readLong();
|
||||
}
|
||||
|
||||
public float readFloat() throws IOException {
|
||||
return dataInputStream.readFloat();
|
||||
}
|
||||
|
||||
public double readDouble() throws IOException {
|
||||
return dataInputStream.readDouble();
|
||||
}
|
||||
|
||||
public String readString() throws IOException {
|
||||
int length = readShort();
|
||||
if (length <= 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
byte[] buff = new byte[length];
|
||||
if (read(buff, 0, length) < length) {
|
||||
throw new IOException("Unexpected end of stream");
|
||||
}
|
||||
|
||||
return new String(buff, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
return dataInputStream.read();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
dataInputStream.close();
|
||||
}
|
||||
}
|
||||
84
src/main/java/mc/nbt/io/NbtOutputStream.java
Normal file
84
src/main/java/mc/nbt/io/NbtOutputStream.java
Normal file
@@ -0,0 +1,84 @@
|
||||
package mc.nbt.io;
|
||||
|
||||
import mc.nbt.TagValue;
|
||||
import mc.nbt.codec.Codec;
|
||||
import mc.nbt.codec.CodecRegistry;
|
||||
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
public class NbtOutputStream extends OutputStream {
|
||||
|
||||
private final DataOutputStream dataOutputStream;
|
||||
|
||||
public NbtOutputStream(OutputStream outputStream, boolean compressed) throws IOException {
|
||||
this.dataOutputStream = new DataOutputStream(compressed
|
||||
? new GZIPOutputStream(outputStream)
|
||||
: outputStream);
|
||||
}
|
||||
|
||||
public NbtOutputStream(OutputStream outputStream) throws IOException {
|
||||
this(outputStream, false);
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public void writeTag(String name, TagValue tagValue) throws IOException {
|
||||
Codec codec = CodecRegistry.getInstance().get(tagValue.getType());
|
||||
|
||||
writeByte(tagValue.getType().getId());
|
||||
writeString(name);
|
||||
|
||||
codec.encode(tagValue, this);
|
||||
}
|
||||
|
||||
public void writeTag(TagValue tagValue) throws IOException {
|
||||
writeTag(tagValue.getName(), tagValue);
|
||||
}
|
||||
|
||||
public void writeByte(int value) throws IOException {
|
||||
dataOutputStream.writeByte(value);
|
||||
}
|
||||
|
||||
public void writeShort(int value) throws IOException {
|
||||
dataOutputStream.writeShort(value);
|
||||
}
|
||||
|
||||
public void writeInt(int value) throws IOException {
|
||||
dataOutputStream.writeInt(value);
|
||||
}
|
||||
|
||||
public void writeLong(long value) throws IOException {
|
||||
dataOutputStream.writeLong(value);
|
||||
}
|
||||
|
||||
public void writeFloat(float value) throws IOException {
|
||||
dataOutputStream.writeFloat(value);
|
||||
}
|
||||
|
||||
public void writeDouble(double value) throws IOException {
|
||||
dataOutputStream.writeDouble(value);
|
||||
}
|
||||
|
||||
public void writeString(String value) throws IOException {
|
||||
if (value == null || value.isEmpty()) {
|
||||
writeShort(0);
|
||||
} else {
|
||||
writeShort(value.length());
|
||||
write(value.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(int b) throws IOException {
|
||||
dataOutputStream.write(b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
this.dataOutputStream.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user