Skip to content

Commit

Permalink
fix issues from #18
Browse files Browse the repository at this point in the history
- tag ids should only be in one place
- correct wrong ListTag class java-doc
- unchecked empty list toString() throws NPE
  • Loading branch information
Querz committed Dec 15, 2018
1 parent c5a3a38 commit 4e19f71
Show file tree
Hide file tree
Showing 24 changed files with 29 additions and 95 deletions.
5 changes: 0 additions & 5 deletions src/main/java/net/querz/nbt/ByteArrayTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ public ByteArrayTag(byte[] value) {
super(value);
}

@Override
public byte getID() {
return 7;
}

@Override
protected byte[] getEmptyValue() {
return new byte[0];
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/net/querz/nbt/ByteTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ public ByteTag(boolean value) {
super((byte) (value ? 1 : 0));
}

@Override
public byte getID() {
return 1;
}

@Override
protected Byte getEmptyValue() {
return 0;
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/net/querz/nbt/CompoundTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ public class CompoundTag extends Tag<Map<String, Tag<?>>> implements Iterable<Ma

public CompoundTag() {}

@Override
public byte getID() {
return 10;
}

@Override
protected Map<String, Tag<?>> getEmptyValue() {
return new HashMap<>(8);
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/net/querz/nbt/DoubleTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ public DoubleTag(double value) {
super(value);
}

@Override
public byte getID() {
return 6;
}

@Override
protected Double getEmptyValue() {
return 0.0d;
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/net/querz/nbt/EndTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ public final class EndTag extends Tag<Void> {

private EndTag() {}

@Override
public byte getID() {
return 0;
}

@Override
protected Void getEmptyValue() {
return null;
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/net/querz/nbt/FloatTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ public FloatTag(float value) {
super(value);
}

@Override
public byte getID() {
return 5;
}

@Override
protected Float getEmptyValue() {
return 0.0f;
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/net/querz/nbt/IntArrayTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ public IntArrayTag(int[] value) {
super(value);
}

@Override
public byte getID() {
return 11;
}

@Override
protected int[] getEmptyValue() {
return new int[0];
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/net/querz/nbt/IntTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ public IntTag(int value) {
super(value);
}

@Override
public byte getID() {
return 3;
}

@Override
protected Integer getEmptyValue() {
return 0;
Expand Down
18 changes: 6 additions & 12 deletions src/main/java/net/querz/nbt/ListTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@

/**
* ListTag represents a typed List in the nbt structure.
* An empty ListTag is of type {@code EndTag}.
* Having this in mind, ListTag will always act consistent with serialization
* regarding its type. Therefore will return {@code 0}
* and {@link ListTag#getTypeClass()} will return {@code EndTag.class}
* when called on an empty ListTag.
* An empty {@link ListTag} created using {@link ListTag#createUnchecked()} will be of unknown type
* and returns an {@link EndTag}{@code .class} in {@link ListTag#getTypeClass()}.
* The type of an empty untyped {@link ListTag} can be set by using any of the {@code add()}
* methods or any of the {@code as...List()} methods.
* */
public class ListTag<T extends Tag<?>> extends Tag<List<T>> implements Iterable<T> {

Expand Down Expand Up @@ -50,11 +49,6 @@ public ListTag(Class<? super T> typeClass) throws IllegalArgumentException, Null
this.typeClass = checkNull(typeClass);
}

@Override
public byte getID() {
return 9;
}

public Class<?> getTypeClass() {
return typeClass == null ? EndTag.class : typeClass;
}
Expand Down Expand Up @@ -274,7 +268,7 @@ public void deserializeValue(DataInputStream dis, int depth) throws IOException

@Override
public String valueToString(int depth) {
StringBuilder sb = new StringBuilder("{\"type\":\"").append(typeClass.getSimpleName()).append("\",\"list\":[");
StringBuilder sb = new StringBuilder("{\"type\":\"").append(getTypeClass().getSimpleName()).append("\",\"list\":[");
for (int i = 0; i < size(); i++) {
sb.append(i > 0 ? "," : "").append(get(i).toString(incrementDepth(depth)));
}
Expand All @@ -297,7 +291,7 @@ public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!super.equals(other) || size() != ((ListTag<?>) other).size() || typeClass != ((ListTag<?>) other).getTypeClass()) {
if (!super.equals(other) || size() != ((ListTag<?>) other).size() || getTypeClass() != ((ListTag<?>) other).getTypeClass()) {
return false;
}
for (int i = 0; i < size(); i++) {
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/net/querz/nbt/LongArrayTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ public LongArrayTag(long[] value) {
super(value);
}

@Override
public byte getID() {
return 12;
}

@Override
protected long[] getEmptyValue() {
return new long[0];
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/net/querz/nbt/LongTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ public LongTag(long value) {
super(value);
}

@Override
public byte getID() {
return 4;
}

@Override
protected Long getEmptyValue() {
return 0L;
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/net/querz/nbt/ShortTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ public ShortTag(short value) {
super(value);
}

@Override
public byte getID() {
return 2;
}

@Override
protected Short getEmptyValue() {
return 0;
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/net/querz/nbt/StringTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ public StringTag(String value) {
super(value);
}

@Override
public byte getID() {
return 8;
}

@Override
protected String getEmptyValue() {
return "";
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/net/querz/nbt/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public Tag(T value) {
/**
* @return This Tag's ID, usually used for serialization and deserialization.
* */
public abstract byte getID();
public byte getID() {
return TagFactory.idFromClass(getClass());
}

/**
* @return A default value for this Tag.
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/querz/nbt/TagFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ public static Class<?> classFromID(int id) {
return mapping.clazz;
}

public static int idFromClass(Class<?> clazz) {
public static byte idFromClass(Class<?> clazz) {
TagMapping<?> mapping = classMapping.get(clazz);
if (mapping == null) {
throw new IllegalArgumentException("unknown Tag class " + clazz.getName());
}
return mapping.id;
return (byte) mapping.id;
}

public static <T extends Tag<?>> void registerCustomTag(int id, Supplier<T> factory, Class<T> clazz) {
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/net/querz/nbt/custom/CharTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ public CharTag(char value) {
super(value);
}

@Override
public byte getID() {
return 110;
}

@Override
protected Character getEmptyValue() {
return 0;
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/net/querz/nbt/custom/ObjectTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ public ObjectTag(T value) {
super(value);
}

@Override
public byte getID() {
return 90;
}

@Override
protected T getEmptyValue() {
return null;
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/net/querz/nbt/custom/ShortArrayTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ public ShortArrayTag(short[] value) {
super(value);
}

@Override
public byte getID() {
return 100;
}

@Override
protected short[] getEmptyValue() {
return new short[0];
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/net/querz/nbt/custom/StructTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ public static void register() {

public StructTag() {}

@Override
public byte getID() {
return 120;
}

@Override
protected List<Tag<?>> getEmptyValue() {
return new ArrayList<>(3);
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/net/querz/nbt/ListTagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.junit.Assert.assertNotEquals;

public class ListTagTest extends NBTTestCase {

public void testCreateInvalidList() {
assertThrowsException(() -> new ListTag<>(EndTag.class), IllegalArgumentException.class);
assertThrowsException(() -> new ListTag<>(null), NullPointerException.class);
Expand Down Expand Up @@ -33,6 +34,8 @@ public void testStringConversion() {
"{\"type\":\"ByteTag\",\"value\":-128}," +
"{\"type\":\"ByteTag\",\"value\":0}," +
"{\"type\":\"ByteTag\",\"value\":127}]}}", bl.toString());
ListTag<?> lu = ListTag.createUnchecked();
assertEquals("{\"type\":\"ListTag\",\"value\":{\"type\":\"EndTag\",\"list\":[]}}", lu.toString());
}

public void testEquals() {
Expand Down Expand Up @@ -61,6 +64,17 @@ public void testEquals() {
il.addInt(1);
il.clear();
assertEquals(il, new ListTag<>(IntTag.class));

// test empty untyped list
ListTag<?> lu = ListTag.createUnchecked();
ListTag<?> lu2 = ListTag.createUnchecked();
assertTrue(lu.equals(lu2));
lu2.asIntTagList();
assertFalse(lu.equals(lu2));
ListTag<IntTag> lie = new ListTag<>(IntTag.class);
assertFalse(lu.equals(lie));
lu.asIntTagList();
assertTrue(lie.equals(lu));
}

public void testHashCode() {
Expand Down
1 change: 1 addition & 0 deletions src/test/java/net/querz/nbt/custom/CharTagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
public class CharTagTest extends NBTTestCase {

public void testStringConversion() {
CharTag.register();
CharTag t = new CharTag('a');
assertEquals('a', (char) t.getValue());
assertEquals(110, t.getID());
Expand Down
1 change: 1 addition & 0 deletions src/test/java/net/querz/nbt/custom/ObjectTagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
public class ObjectTagTest extends NBTTestCase implements Serializable {

public void testStringConversion() {
ObjectTag.register();
DummyObject d = new DummyObject();
ObjectTag<DummyObject> o = new ObjectTag<>(d);
assertEquals(90, o.getID());
Expand Down
1 change: 1 addition & 0 deletions src/test/java/net/querz/nbt/custom/ShortArrayTagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public class ShortArrayTagTest extends NBTTestCase {

public void testStringConversion() {
ShortArrayTag.register();
ShortArrayTag t = new ShortArrayTag(new short[]{Short.MIN_VALUE, 0, Short.MAX_VALUE});
assertTrue(Arrays.equals(new short[]{Short.MIN_VALUE, 0, Short.MAX_VALUE}, t.getValue()));
assertEquals(100, t.getID());
Expand Down
1 change: 1 addition & 0 deletions src/test/java/net/querz/nbt/custom/StructTagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ private StructTag createStructTag() {
}

public void testStringConversion() {
StructTag.register();
StructTag s = createStructTag();
assertEquals(120, s.getID());
assertEquals("[127b,2147483647]", s.toTagString());
Expand Down

0 comments on commit 4e19f71

Please sign in to comment.