Skip to content

Commit 108646e

Browse files
committed
allow block and sky light to exist separately
1 parent 877892d commit 108646e

File tree

12 files changed

+39
-26
lines changed

12 files changed

+39
-26
lines changed

FORMAT.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ The polar format resembles the anvil format in many ways, though it is binary, n
77
| Name | Type | Notes |
88
|----------------|--------|-------------------------------------------------------------------------|
99
| Magic Number | int | `Polr` |
10-
| Major Version | byte | |
11-
| Minor Version | byte | |
10+
| Version | short | |
1211
| Compression | byte | 0 = None, 1 = Zstd |
1312
| Length of data | varint | Uncompressed length of data (or just length of data if `Compression=0`) |
1413
| World | world | |
@@ -49,8 +48,9 @@ Entities or some other extra data field needs to be added to chunks in the futur
4948
| Biome Palette | array[string] | |
5049
| Biome Palette Data Length | varint | Only present if `Biome Palette Size > 1` |
5150
| Biome Palette Data | array[long] | See the anvil format for more information about this type |
52-
| Has Light Data | bool | If unset, block and sky light are both ommitted |
51+
| Has Block Light Data | bool | If unset, block light is ommitted |
5352
| Block Light | bytes | A 2048 byte long nibble array |
53+
| Has Sky Light Data | bool | If unset, sky light is ommitted |
5454
| Sky Light | bytes | A 2048 byte long nibble array |
5555

5656
### Block Entity

src/main/java/net/hollowcube/polar/AnvilPolar.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,12 @@ public class AnvilPolar {
178178
}
179179

180180
// Lighting data, if present
181-
byte[] blockLight = null, skyLight = null;
182-
if (sectionReader.getBlockLight() != null && sectionReader.getSkyLight() != null) {
181+
byte[] blockLight = null;
182+
if (sectionReader.getBlockLight() != null) {
183183
blockLight = sectionReader.getBlockLight().copyArray();
184+
}
185+
byte[] skyLight = null;
186+
if (sectionReader.getSkyLight() != null) {
184187
skyLight = sectionReader.getSkyLight().copyArray();
185188
}
186189

src/main/java/net/hollowcube/polar/PolarLoader.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package net.hollowcube.polar;
22

3-
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
4-
import it.unimi.dsi.fastutil.shorts.Short2ObjectArrayMap;
53
import it.unimi.dsi.fastutil.shorts.Short2ObjectMap;
64
import it.unimi.dsi.fastutil.shorts.Short2ObjectOpenHashMap;
75
import net.hollowcube.polar.compat.ChunkSupplierShim;
@@ -161,10 +159,10 @@ private void loadSection(@NotNull PolarSection sectionData, @NotNull Section sec
161159
}
162160

163161
// Light
164-
if (sectionData.hasLightData()) {
162+
if (sectionData.hasBlockLightData())
165163
section.setBlockLight(sectionData.blockLight());
164+
if (sectionData.hasSkyLightData())
166165
section.setSkyLight(sectionData.skyLight());
167-
}
168166
}
169167

170168
private void loadBlockEntity(@NotNull PolarChunk.BlockEntity blockEntity, @NotNull Chunk chunk) {

src/main/java/net/hollowcube/polar/PolarReader.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,18 @@ private PolarReader() {}
3535
byte minSection = buffer.read(BYTE), maxSection = buffer.read(BYTE);
3636
assertThat(minSection < maxSection, "Invalid section range");
3737

38-
var chunks = buffer.readCollection(b -> readChunk(b, maxSection - minSection + 1));
38+
var chunks = buffer.readCollection(b -> readChunk(version, b, maxSection - minSection + 1));
3939

4040
return new PolarWorld(version, compression, minSection, maxSection, chunks);
4141
}
4242

43-
private static @NotNull PolarChunk readChunk(@NotNull NetworkBuffer buffer, int sectionCount) {
43+
private static @NotNull PolarChunk readChunk(short version, @NotNull NetworkBuffer buffer, int sectionCount) {
4444
var chunkX = buffer.read(VAR_INT);
4545
var chunkZ = buffer.read(VAR_INT);
4646

4747
var sections = new PolarSection[sectionCount];
4848
for (int i = 0; i < sectionCount; i++) {
49-
sections[i] = readSection(buffer);
49+
sections[i] = readSection(version, buffer);
5050
}
5151

5252
var blockEntities = buffer.readCollection(PolarReader::readBlockEntity);
@@ -68,7 +68,7 @@ private PolarReader() {}
6868
);
6969
}
7070

71-
private static @NotNull PolarSection readSection(@NotNull NetworkBuffer buffer) {
71+
private static @NotNull PolarSection readSection(short version, @NotNull NetworkBuffer buffer) {
7272
// If section is empty exit immediately
7373
if (buffer.read(BOOLEAN)) return new PolarSection();
7474

@@ -93,7 +93,13 @@ private PolarReader() {}
9393
}
9494

9595
byte[] blockLight = null, skyLight = null;
96-
if (buffer.read(BOOLEAN)) {
96+
97+
if (version > PolarWorld.VERSION_UNIFIED_LIGHT) {
98+
if (buffer.read(BOOLEAN))
99+
blockLight = buffer.readBytes(2048);
100+
if (buffer.read(BOOLEAN))
101+
skyLight = buffer.readBytes(2048);
102+
} else {
97103
blockLight = buffer.readBytes(2048);
98104
skyLight = buffer.readBytes(2048);
99105
}

src/main/java/net/hollowcube/polar/PolarSection.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,21 @@ public int[] biomeData() {
8585
return biomeData;
8686
}
8787

88-
public boolean hasLightData() {
89-
return blockLight != null && skyLight != null;
88+
public boolean hasBlockLightData() {
89+
return blockLight != null;
90+
}
91+
92+
public boolean hasSkyLightData() {
93+
return skyLight != null;
9094
}
9195

9296
public byte[] blockLight() {
93-
assert blockLight != null : "must check hasLightData() before calling blockLight()";
97+
assert blockLight != null : "must check hasBlockLightData() before calling blockLight()";
9498
return blockLight;
9599
}
96100

97101
public byte[] skyLight() {
98-
assert skyLight != null : "must check hasLightData() before calling skyLight()";
102+
assert skyLight != null : "must check hasSkyLightData() before calling skyLight()";
99103
return skyLight;
100104
}
101105
}

src/main/java/net/hollowcube/polar/PolarWorld.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
@SuppressWarnings("UnstableApiUsage")
1616
public class PolarWorld {
1717
public static final int MAGIC_NUMBER = 0x506F6C72;
18-
public static final short LATEST_VERSION = 1;
18+
public static final short LATEST_VERSION = 2;
19+
20+
static final short VERSION_UNIFIED_LIGHT = 1;
1921

2022
public static CompressionType DEFAULT_COMPRESSION = CompressionType.ZSTD;
2123

src/main/java/net/hollowcube/polar/PolarWriter.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,12 @@ private static void writeSection(@NotNull NetworkBuffer buffer, @NotNull PolarSe
7676
}
7777

7878
// Light
79-
buffer.write(BOOLEAN, section.hasLightData());
80-
if (section.hasLightData()) {
79+
buffer.write(BOOLEAN, section.hasBlockLightData());
80+
if (section.hasBlockLightData())
8181
buffer.write(RAW_BYTES, section.blockLight());
82+
buffer.write(BOOLEAN, section.hasSkyLightData());
83+
if (section.hasSkyLightData())
8284
buffer.write(RAW_BYTES, section.skyLight());
83-
}
8485
}
8586

8687
private static void writeBlockEntity(@NotNull NetworkBuffer buffer, @NotNull PolarChunk.BlockEntity blockEntity) {

src/test/java/net/hollowcube/polar/TestAnvilPolar.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@ class TestAnvilPolar {
1212
@Test
1313
void testConvertAnvilWorld() throws Exception {
1414
var world = AnvilPolar.anvilToPolar(
15-
Path.of("./src/test/resources/abc").toRealPath(),
16-
ChunkSelector.radius(5)
15+
Path.of("./src/test/resources/emclobby").toRealPath()
1716
);
1817
assertEquals(-4, world.minSection());
1918

2019
var result = PolarWriter.write(world);
2120
System.out.println(result.length);
22-
Files.write(Path.of("./src/test/resources/bench/abc.polar"), result);
21+
Files.write(Path.of("./src/test/resources/emclobby.polar"), result);
2322
}
2423

2524
}

src/test/java/net/hollowcube/polar/demo/DemoServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static void main(String[] args) throws Exception {
1818
var server = MinecraftServer.init();
1919

2020
var instance = MinecraftServer.getInstanceManager().createInstanceContainer();
21-
instance.setChunkLoader(new PolarLoader(Path.of("./src/test/resources/bench.polar")));
21+
instance.setChunkLoader(new PolarLoader(Path.of("./src/test/resources/emclobby.polar")));
2222
// instance.setChunkSupplier(LightingChunk::new);
2323

2424
MinecraftServer.getGlobalEventHandler()

src/test/resources/bench.polar

-5.57 MB
Binary file not shown.

0 commit comments

Comments
 (0)