Skip to content

Commit 089e6a4

Browse files
committed
fix: bump minestom, for now stop using Palette#setAll until further investigation
1 parent 5e1b2c7 commit 089e6a4

File tree

7 files changed

+161
-31
lines changed

7 files changed

+161
-31
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
metadata.format.version = "1.1"
22

33
[versions]
4-
minestom = "d760a60a5c"
4+
minestom = "96543a894f"
55
zstd = "1.5.5-3"
66
fastutil = "8.5.12"
77

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

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,14 @@ private void loadSection(@NotNull PolarSection sectionData, @NotNull Section sec
219219
section.blockPalette().fill(blockPalette[0].stateId());
220220
} else {
221221
final var paletteData = sectionData.blockData();
222-
section.blockPalette().setAll((x, y, z) -> {
223-
int index = y * CHUNK_SECTION_SIZE * CHUNK_SECTION_SIZE + z * CHUNK_SECTION_SIZE + x;
224-
return blockPalette[paletteData[index]].stateId();
225-
});
222+
for (int y = 0; y < CHUNK_SECTION_SIZE; y++) {
223+
for (int z = 0; z < CHUNK_SECTION_SIZE; z++) {
224+
for (int x = 0; x < CHUNK_SECTION_SIZE; x++) {
225+
int index = y * CHUNK_SECTION_SIZE * CHUNK_SECTION_SIZE + z * CHUNK_SECTION_SIZE + x;
226+
section.blockPalette().set(x, y, z, blockPalette[paletteData[index]].stateId());
227+
}
228+
}
229+
}
226230
}
227231

228232
// Biomes
@@ -242,18 +246,22 @@ private void loadSection(@NotNull PolarSection sectionData, @NotNull Section sec
242246
section.biomePalette().fill(biomePalette[0]);
243247
} else {
244248
final var paletteData = sectionData.biomeData();
245-
section.biomePalette().setAll((x, y, z) -> {
246-
int index = x / 4 + (z / 4) * 4 + (y / 4) * 16;
247-
248-
var paletteIndex = paletteData[index];
249-
if (paletteIndex >= biomePalette.length) {
250-
logger.error("Invalid biome palette index. This is probably a corrupted world, " +
251-
"but it has been loaded with plains instead. No data has been written.");
252-
return PLAINS_BIOME_ID;
253-
}
249+
for (int y = 0; y < CHUNK_SECTION_SIZE; y++) {
250+
for (int z = 0; z < CHUNK_SECTION_SIZE; z++) {
251+
for (int x = 0; x < CHUNK_SECTION_SIZE; x++) {
252+
int index = x / 4 + (z / 4) * 4 + (y / 4) * 16;
253+
254+
var paletteIndex = paletteData[index];
255+
if (paletteIndex >= biomePalette.length) {
256+
logger.error("Invalid biome palette index. This is probably a corrupted world, " +
257+
"but it has been loaded with plains instead. No data has been written.");
258+
section.biomePalette().set(x, y, z, PLAINS_BIOME_ID);
259+
}
254260

255-
return biomePalette[paletteIndex];
256-
});
261+
section.biomePalette().set(x, y, z, biomePalette[paletteIndex]);
262+
}
263+
}
264+
}
257265
}
258266

259267
// Light

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ public int[] biomeData() {
102102
}
103103

104104
public byte[] blockLight() {
105-
assert blockLight != null : "must check hasBlockLightData() before calling blockLight()";
106105
return blockLight;
107106
}
108107

@@ -111,7 +110,6 @@ public byte[] blockLight() {
111110
}
112111

113112
public byte[] skyLight() {
114-
assert skyLight != null : "must check hasSkyLightData() before calling skyLight()";
115113
return skyLight;
116114
}
117115
}

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,18 @@ private void readSection(@NotNull NetworkBuffer buffer, @NotNull Section section
176176
var bitsPerEntry = (int) Math.ceil(Math.log(blockPalette.length) / Math.log(2));
177177
PaletteUtil.unpack(blockData, rawBlockData, bitsPerEntry);
178178

179-
section.blockPalette().setAll((x, y, z) -> {
180-
int index = y * CHUNK_SECTION_SIZE * CHUNK_SECTION_SIZE + z * CHUNK_SECTION_SIZE + x;
181-
return blockPalette[blockData[index]];
182-
});
179+
for (int y = 0; y < CHUNK_SECTION_SIZE; y++) {
180+
for (int z = 0; z < CHUNK_SECTION_SIZE; z++) {
181+
for (int x = 0; x < CHUNK_SECTION_SIZE; x++) {
182+
int index = y * CHUNK_SECTION_SIZE * CHUNK_SECTION_SIZE + z * CHUNK_SECTION_SIZE + x;
183+
section.blockPalette().set(x, y, z, blockPalette[blockData[index]]);
184+
}
185+
}
186+
}
187+
// section.blockPalette().setAll((x, y, z) -> {
188+
// int index = y * CHUNK_SECTION_SIZE * CHUNK_SECTION_SIZE + z * CHUNK_SECTION_SIZE + x;
189+
// return blockPalette[blockData[index]];
190+
// });
183191

184192
// Below was some previous logic, leaving it around for now I would like to fix it up.
185193
// System.out.println(Arrays.toString(blockPalette));
@@ -202,10 +210,18 @@ private void readSection(@NotNull NetworkBuffer buffer, @NotNull Section section
202210
var bitsPerEntry = (int) Math.ceil(Math.log(biomePalette.length) / Math.log(2));
203211
PaletteUtil.unpack(biomeData, rawBiomeData, bitsPerEntry);
204212

205-
section.biomePalette().setAll((x, y, z) -> {
206-
int index = x / 4 + (z / 4) * 4 + (y / 4) * 16;
207-
return biomePalette[biomeData[index]];
208-
});
213+
for (int y = 0; y < 4; y++) {
214+
for (int z = 0; z < 4; z++) {
215+
for (int x = 0; x < 4; x++) {
216+
int index = x + z * 4 + y * 16;
217+
section.biomePalette().set(x, y, z, biomePalette[biomeData[index]]);
218+
}
219+
}
220+
}
221+
// section.biomePalette().setAll((x, y, z) -> {
222+
// int index = x / 4 + (z / 4) * 4 + (y / 4) * 16;
223+
// return biomePalette[biomeData[index]];
224+
// });
209225

210226
// var rawBiomeData = buffer.read(LONG_ARRAY);
211227
// var bitsPerEntry = (int) Math.ceil(Math.log(biomePalette.length) / Math.log(2));
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package net.hollowcube.polar;
2+
3+
import net.minestom.server.MinecraftServer;
4+
import net.minestom.server.instance.InstanceContainer;
5+
import net.minestom.server.instance.block.Block;
6+
import net.minestom.server.world.DimensionType;
7+
import org.junit.jupiter.api.Test;
8+
9+
import java.io.ByteArrayInputStream;
10+
import java.nio.channels.Channels;
11+
import java.util.Random;
12+
import java.util.UUID;
13+
14+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
15+
import static org.junit.jupiter.api.Assertions.assertEquals;
16+
17+
public class TestBigPaletteReadWrite {
18+
19+
static {
20+
MinecraftServer.init();
21+
}
22+
23+
@Test
24+
void testPackUnpackDirect() {
25+
var ints = new int[4096];
26+
for (int i = 0; i < 510; i++) {
27+
ints[i] = i;
28+
}
29+
var bitsPerEntry = 9;
30+
31+
var longs = PaletteUtil.pack(ints, bitsPerEntry);
32+
var out = new int[ints.length];
33+
PaletteUtil.unpack(out, longs, bitsPerEntry);
34+
35+
assertArrayEquals(ints, out);
36+
}
37+
38+
@Test
39+
void testStreamLoader() {
40+
var instance = new InstanceContainer(UUID.randomUUID(), DimensionType.OVERWORLD);
41+
42+
var random = new Random(22);
43+
int blockCount = 256;
44+
45+
for (int y = 0; y < 16; y++) {
46+
for (int z = 0; z < 16; z++) {
47+
for (int x = 0; x < 16; x++) {
48+
var block = Block.fromStateId(random.nextInt(blockCount));
49+
instance.setBlock(x, y, z, block);
50+
}
51+
}
52+
}
53+
54+
var loader = new PolarLoader(new PolarWorld());
55+
instance.setChunkLoader(loader);
56+
instance.saveChunksToStorage().join();
57+
var worldBytes = PolarWriter.write(loader.world());
58+
59+
var loadInstance = new InstanceContainer(UUID.randomUUID(), DimensionType.OVERWORLD);
60+
PolarLoader.streamLoad(loadInstance, Channels.newChannel(new ByteArrayInputStream(worldBytes)),
61+
worldBytes.length, null, null, false).join();
62+
63+
random = new Random(22);
64+
for (int y = 0; y < 16; y++) {
65+
for (int z = 0; z < 16; z++) {
66+
for (int x = 0; x < 16; x++) {
67+
var block = Block.fromStateId(random.nextInt(blockCount));
68+
assertEquals(block, loadInstance.getBlock(x, y, z));
69+
}
70+
}
71+
}
72+
}
73+
74+
@Test
75+
void testLegacyLoader() {
76+
var instance = new InstanceContainer(UUID.randomUUID(), DimensionType.OVERWORLD);
77+
78+
var random = new Random(22);
79+
int blockCount = 256;
80+
81+
for (int y = 0; y < 16; y++) {
82+
for (int z = 0; z < 16; z++) {
83+
for (int x = 0; x < 16; x++) {
84+
var block = Block.fromStateId(random.nextInt(blockCount));
85+
instance.setBlock(x, y, z, block);
86+
}
87+
}
88+
}
89+
90+
var loader = new PolarLoader(new PolarWorld());
91+
instance.setChunkLoader(loader);
92+
instance.saveChunksToStorage().join();
93+
var worldBytes = PolarWriter.write(loader.world());
94+
95+
var loadInstance = new InstanceContainer(UUID.randomUUID(), DimensionType.OVERWORLD);
96+
loadInstance.setChunkLoader(new PolarLoader(PolarReader.read(worldBytes)));
97+
loadInstance.loadChunk(0, 0).join();
98+
99+
random = new Random(22);
100+
for (int y = 0; y < 16; y++) {
101+
for (int z = 0; z < 16; z++) {
102+
for (int x = 0; x < 16; x++) {
103+
var block = Block.fromStateId(random.nextInt(blockCount));
104+
assertEquals(block, loadInstance.getBlock(x, y, z));
105+
}
106+
}
107+
}
108+
}
109+
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import net.minestom.server.world.DimensionType;
77
import org.junit.jupiter.api.Test;
88

9-
import java.nio.ByteBuffer;
109
import java.util.Arrays;
1110
import java.util.List;
1211
import java.util.UUID;
@@ -32,15 +31,15 @@ void testWriteRead() {
3231
var wa = new UpdateTimeWorldAccess();
3332
var loader = new PolarLoader(world).setWorldAccess(wa);
3433
var instance = new InstanceContainer(UUID.randomUUID(), DimensionType.OVERWORLD, loader);
35-
var chunk = loader.loadChunk(instance, 0, 0).join();
34+
var chunk = loader.loadChunk(instance, 0, 0);
3635

3736
loader.saveChunk(chunk);
3837

3938
var newPolarChunk = world.chunkAt(0, 0);
40-
var savedTime = new NetworkBuffer(ByteBuffer.wrap(newPolarChunk.userData())).read(NetworkBuffer.LONG);
39+
var savedTime = NetworkBuffer.wrap(newPolarChunk.userData(), 0, newPolarChunk.userData().length).read(NetworkBuffer.LONG);
4140
assertEquals(wa.saveTime, savedTime);
4241

43-
loader.loadChunk(instance, 0, 0).join();
42+
loader.loadChunk(instance, 0, 0);
4443
assertEquals(wa.loadTime, savedTime);
4544
}
4645

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static void main(String[] args) throws Exception {
4646
event.getPlayer().setGameMode(GameMode.CREATIVE);
4747
})
4848
.addListener(PlayerChatEvent.class, event -> {
49-
if (!event.getMessage().equals("save")) return;
49+
if (!event.getRawMessage().equals("save")) return;
5050

5151
var start = System.currentTimeMillis();
5252
instance.saveChunksToStorage().join();

0 commit comments

Comments
 (0)