Skip to content

Commit

Permalink
new BlockStates array length is not calculated correctly for DataVers…
Browse files Browse the repository at this point in the history
…ion >= 2527
  • Loading branch information
Querz committed Apr 6, 2021
1 parent ecf8294 commit 479e154
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/java/net/querz/mca/Section.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Section {
private byte[] blockLight;
private long[] blockStates;
private byte[] skyLight;
private int dataVersion;
int dataVersion;

public Section(CompoundTag sectionRoot, int dataVersion) {
this(sectionRoot, dataVersion, ALL_DATA);
Expand Down Expand Up @@ -282,7 +282,7 @@ void adjustBlockStateBits(Map<Integer, Integer> oldToNewMapping, long[] blockSta
if (dataVersion < 2527) {
newBlockStates = newBits == blockStates.length / 64 ? blockStates : new long[newBits * 64];
} else {
int newLength = (int) Math.ceil(4096D / (64D / newBits));
int newLength = (int) Math.ceil(4096D / (Math.floor(64D / newBits)));
newBlockStates = newBits == blockStates.length / 64 ? blockStates : new long[newLength];
}
if (oldToNewMapping != null) {
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/net/querz/mca/MCAFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,25 @@ public void testSetBlockDataAt() {
assertEquals(256, sss.get(0).getLongArray("BlockStates").length);
}

public void testSetBlockDataAt2527() {
//test "line break" for DataVersion 2527
MCAFile f = assertThrowsNoException(() -> MCAUtil.read(copyResourceToTmp("r.2.2.mca")));
Chunk p = f.getChunk(0, 0);
p.setDataVersion(3000);
Section section = f.getChunk(0, 0).getSection(0);
assertEquals(10, section.getPalette().size());
assertEquals(0b0001000100010001000100010001000100010001000100010001000100010001L, section.getBlockStates()[0]);
f.setBlockStateAt(0, 0, 0, block("minecraft:custom"), false);
assertEquals(11, section.getPalette().size());
assertEquals(0b0001000100010001000100010001000100010001000100010001000100011010L, section.getBlockStates()[0]);
int y = 1;
for (int i = 12; i <= 17; i++) {
f.setBlockStateAt(0, y++, 0, block("minecraft:" + i), false);
}
assertEquals(17, section.getPalette().size());
assertEquals(342, section.getBlockStates().length);
}

public void testGetBlockDataAt() {
MCAFile f = assertThrowsNoException(() -> MCAUtil.read(copyResourceToTmp("r.2.2.mca")));
assertEquals(block("minecraft:bedrock"), f.getBlockStateAt(0, 0, 0));
Expand Down

0 comments on commit 479e154

Please sign in to comment.