Skip to content

Commit

Permalink
fix: allow reading and writing zero length heightmaps
Browse files Browse the repository at this point in the history
  • Loading branch information
mworzala committed Mar 26, 2024
1 parent 3a76ff8 commit 8e7671d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/main/java/net/hollowcube/polar/PolarReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,13 @@ private PolarReader() {}
continue;

var packed = buffer.read(LONG_ARRAY);
var bitsPerEntry = packed.length * 64 / PolarChunk.HEIGHTMAP_SIZE;
heightmaps[i] = new int[PolarChunk.HEIGHTMAP_SIZE];
PaletteUtil.unpack(heightmaps[i], packed, bitsPerEntry);
if (packed.length == 0) {
heightmaps[i] = new int[0];
} else {
var bitsPerEntry = packed.length * 64 / PolarChunk.HEIGHTMAP_SIZE;
heightmaps[i] = new int[PolarChunk.HEIGHTMAP_SIZE];
PaletteUtil.unpack(heightmaps[i], packed, bitsPerEntry);
}
}

// Objects
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/hollowcube/polar/PolarWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ private static void writeChunk(@NotNull NetworkBuffer buffer, @NotNull PolarChun
for (int i = 0; i < PolarChunk.MAX_HEIGHTMAPS; i++) {
var heightmap = chunk.heightmap(i);
if (heightmap == null) continue;
buffer.write(LONG_ARRAY, PaletteUtil.pack(heightmap, bitsPerEntry));
if (heightmap.length == 0) buffer.write(LONG_ARRAY, new long[0]);
else buffer.write(LONG_ARRAY, PaletteUtil.pack(heightmap, bitsPerEntry));
}
}

Expand Down

0 comments on commit 8e7671d

Please sign in to comment.