Skip to content

Commit

Permalink
incorrectly set list type of empty list when casting / fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Querz committed Sep 2, 2020
1 parent 021f1d8 commit 90646ab
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
8 changes: 6 additions & 2 deletions src/main/java/net/querz/mca/Chunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public void setBiomeAt(int blockX, int blockY, int blockZ, int biomeID) {
}

int getBiomeIndex(int biomeX, int biomeY, int biomeZ) {
return biomeY * 64 + biomeZ * 4 + biomeX;
return biomeY * 16 + biomeZ * 4 + biomeX;
}

public CompoundTag getBlockStateAt(int blockX, int blockY, int blockZ) {
Expand Down Expand Up @@ -632,8 +632,12 @@ private void checkRaw() {
}

public static Chunk newChunk() {
return newChunk(DEFAULT_DATA_VERSION);
}

public static Chunk newChunk(int dataVersion) {
Chunk c = new Chunk(0);
c.dataVersion = DEFAULT_DATA_VERSION;
c.dataVersion = dataVersion;
c.data = new CompoundTag();
c.data.put("Level", new CompoundTag());
c.status = "mobs_spawned";
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/net/querz/nbt/tag/ListTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@

/**
* ListTag represents a typed List in the nbt structure.
* An empty {@link ListTag} created using {@link ListTag#createUnchecked(Class)} will be of unknown type
* and returns an {@link EndTag}{@code .class} in {@link ListTag#getTypeClass()}.
* An empty {@link ListTag} will be of type {@link EndTag} (unknown type).
* 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.
* */
Expand Down Expand Up @@ -123,7 +122,7 @@ public void add(T t) {

public void add(int index, T t) {
Objects.requireNonNull(t);
if (typeClass == null || typeClass == EndTag.class) {
if (getTypeClass() == EndTag.class) {
typeClass = t.getClass();
} else if (typeClass != t.getClass()) {
throw new ClassCastException(
Expand Down Expand Up @@ -203,7 +202,7 @@ public int indexOf(T t) {
@SuppressWarnings("unchecked")
public <L extends Tag<?>> ListTag<L> asTypedList(Class<L> type) {
checkTypeClass(type);
typeClass = type;
// typeClass = type;
return (ListTag<L>) this;
}

Expand Down Expand Up @@ -309,7 +308,7 @@ public ListTag<T> clone() {
//TODO: make private
@SuppressWarnings("unchecked")
public void addUnchecked(Tag<?> tag) {
if (typeClass != null && typeClass != tag.getClass() && typeClass != EndTag.class) {
if (getTypeClass() != EndTag.class && typeClass != tag.getClass()) {
throw new IllegalArgumentException(String.format(
"cannot add %s to ListTag<%s>",
tag.getClass().getSimpleName(), typeClass.getSimpleName()));
Expand All @@ -318,7 +317,7 @@ public void addUnchecked(Tag<?> tag) {
}

private void checkTypeClass(Class<?> clazz) {
if (typeClass != null && typeClass != EndTag.class && typeClass != clazz) {
if (getTypeClass() != EndTag.class && typeClass != clazz) {
throw new ClassCastException(String.format(
"cannot cast ListTag<%s> to ListTag<%s>",
typeClass.getSimpleName(), clazz.getSimpleName()));
Expand Down
11 changes: 6 additions & 5 deletions src/test/java/net/querz/mca/MCAFileTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.querz.mca;

import net.querz.nbt.tag.CompoundTag;
import net.querz.nbt.tag.EndTag;
import net.querz.nbt.tag.ListTag;
import static net.querz.mca.LoadFlags.*;
import java.io.File;
Expand Down Expand Up @@ -90,7 +91,7 @@ public void testGetters() {
assertNotNull(f.getChunk(0).getBiomes());
assertNull(f.getChunk(0).getHeightMaps());
assertNull(f.getChunk(0).getCarvingMasks());
assertEquals(new ListTag<>(CompoundTag.class), f.getChunk(0).getEntities());
assertEquals(ListTag.createUnchecked(null), f.getChunk(0).getEntities());
assertNull(f.getChunk(0).getTileEntities());
assertNull(f.getChunk(0).getTileTicks());
assertNull(f.getChunk(0).getLiquidTicks());
Expand Down Expand Up @@ -184,7 +185,7 @@ public void testGetBiomeAt() {
MCAFile f = assertThrowsNoException(() -> MCAUtil.read(copyResourceToTmp("r.2.2.mca")));
assertEquals(21, f.getBiomeAt(1024, 1024));
assertEquals(-1, f.getBiomeAt(1040, 1024));
f.setChunk(0, 1, Chunk.newChunk());
f.setChunk(0, 1, Chunk.newChunk(2201));
assertEquals(-1, f.getBiomeAt(1024, 1040));

}
Expand All @@ -197,9 +198,9 @@ public void testSetBiomeAt() {
assertEquals(47, f.getChunk(64, 64).updateHandle(64, 64).getCompoundTag("Level").getIntArray("Biomes")[255]);
f.setBiomeAt(1040, 1024, 20);
int[] biomes = f.getChunk(65, 64).updateHandle(65, 64).getCompoundTag("Level").getIntArray("Biomes");
assertEquals(256, biomes.length);
for (int i = 0; i < 256; i++) {
assertTrue(i == 0 ? biomes[i] == 20 : biomes[i] == -1);
assertEquals(1024, biomes.length);
for (int i = 0; i < 1024; i++) {
assertTrue(i % 16 == 0 ? biomes[i] == 20 : biomes[i] == -1);
}
}

Expand Down
11 changes: 6 additions & 5 deletions src/test/java/net/querz/nbt/tag/ListTagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ public void testEquals() {
ListTag<?> lu2 = ListTag.createUnchecked(null);
assertTrue(lu.equals(lu2));
lu2.asIntTagList();
assertFalse(lu.equals(lu2));
assertTrue(lu.equals(lu2));
ListTag<IntTag> lie = new ListTag<>(IntTag.class);
assertFalse(lu.equals(lie));
lu.asIntTagList();
assertTrue(lie.equals(lu));
assertFalse(lie.equals(lu));
}

public void testHashCode() {
Expand All @@ -91,7 +91,8 @@ public void testClone() {
ListTag<IntTag> i = new ListTag<>(IntTag.class);
ListTag<IntTag> c = i.clone();
assertThrowsRuntimeException(() -> c.addString("wrong type in clone"), IllegalArgumentException.class);
c.addInt(123);
assertThrowsNoRuntimeException(() -> c.addInt(123));

assertFalse(i.equals(c));
c.clear();
assertTrue(i.equals(c));
Expand Down Expand Up @@ -203,8 +204,8 @@ public void testCasting() {
ListTag<?> lg = ListTag.createUnchecked(null);
ListTag<ByteTag> lb = assertThrowsNoRuntimeException(lg::asByteTagList);
assertEquals(lb, lg);
//only allow casting once from untyped list to typed list
assertThrowsRuntimeException(lg::asShortTagList, ClassCastException.class);
// allow casting to a different list type if the list is empty
assertThrowsNoException(lg::asShortTagList);
}

public void testCompareTo() {
Expand Down

0 comments on commit 90646ab

Please sign in to comment.