Skip to content

Commit 00ae47a

Browse files
committed
fix: mark chunk loaded to fix lighting chunk load, bump minestom
1 parent d828508 commit 00ae47a

File tree

3 files changed

+58
-36
lines changed

3 files changed

+58
-36
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 = "2025.07.03-1.21.5"
4+
minestom = "2025.07.30-1.21.8"
55
zstd = "1.5.5-3"
66
fastutil = "8.5.12"
77

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

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ final class StreamingPolarLoader {
4141
private final Object2IntMap<String> biomeToIdCache = new Object2IntOpenHashMap<>();
4242
private final int plainsBiomeId;
4343

44-
StreamingPolarLoader(@NotNull InstanceContainer instance, @NotNull PolarDataConverter dataConverter,
45-
@Nullable PolarWorldAccess worldAccess, boolean loadLighting) {
44+
StreamingPolarLoader(
45+
@NotNull InstanceContainer instance, @NotNull PolarDataConverter dataConverter,
46+
@Nullable PolarWorldAccess worldAccess, boolean loadLighting
47+
) {
4648
this.instance = instance;
4749
this.dataConverter = dataConverter;
4850
this.worldAccess = worldAccess;
@@ -111,7 +113,8 @@ private NetworkBuffer readHeader(@NotNull ReadableByteChannel channel, long file
111113
final var dst = NetworkBuffer.staticBuffer(compressedDataLength, MinecraftServer.process());
112114
final var srcAddress = networkBufferAddress(buffer) + buffer.readIndex();
113115
final var dstAddress = networkBufferAddress(dst);
114-
long count = Zstd.decompressUnsafe(dstAddress, compressedDataLength, srcAddress, buffer.readableBytes());
116+
long count = Zstd.decompressUnsafe(dstAddress, compressedDataLength, srcAddress,
117+
buffer.readableBytes());
115118
if (Zstd.isError(count)) {
116119
throw new RuntimeException("decompression failed: " + Zstd.getErrorName(count));
117120
}
@@ -142,7 +145,8 @@ private void readChunk(@NotNull NetworkBuffer buffer, int minSection, int maxSec
142145
final var blockEntity = readBlockEntity(dataConverter, version, dataVersion, buffer);
143146
if (chunkEntries != null && chunkTickables != null) {
144147
final var block = createBlockEntity(chunk, blockEntity);
145-
final int index = CoordConversion.chunkBlockIndex(blockEntity.x(), blockEntity.y(), blockEntity.z());
148+
final int index = CoordConversion.chunkBlockIndex(
149+
blockEntity.x(), blockEntity.y(), blockEntity.z());
146150
chunkEntries.put(index, block);
147151
if (block.handler() != null && block.handler().isTickable())
148152
chunkTickables.put(index, block);
@@ -157,6 +161,7 @@ private void readChunk(@NotNull NetworkBuffer buffer, int minSection, int maxSec
157161
else unsafeSetNeedsCompleteHeightmapRefresh(chunk, true);
158162
}
159163

164+
unsafeChunkOnLoad(chunk);
160165
unsafeCacheChunk(instance, chunk);
161166

162167
// Load user data
@@ -170,7 +175,10 @@ private void readChunk(@NotNull NetworkBuffer buffer, int minSection, int maxSec
170175
}
171176
}
172177

173-
private void readSection(@NotNull NetworkBuffer buffer, @NotNull Section section, int sectionY, @Nullable Int2ObjectMap<Block> chunkEntires) {
178+
private void readSection(
179+
@NotNull NetworkBuffer buffer, @NotNull Section section, int sectionY,
180+
@Nullable Int2ObjectMap<Block> chunkEntires
181+
) {
174182
if (buffer.read(BOOLEAN)) return; // Empty section
175183

176184
int[] blockPalette = readBlockPalette(buffer);
@@ -202,20 +210,20 @@ private void readSection(@NotNull NetworkBuffer buffer, @NotNull Section section
202210
}
203211
}
204212
}
205-
// section.blockPalette().setAll((x, y, z) -> {
206-
// int index = y * CHUNK_SECTION_SIZE * CHUNK_SECTION_SIZE + z * CHUNK_SECTION_SIZE + x;
207-
// return blockPalette[blockData[index]];
208-
// });
213+
// section.blockPalette().setAll((x, y, z) -> {
214+
// int index = y * CHUNK_SECTION_SIZE * CHUNK_SECTION_SIZE + z * CHUNK_SECTION_SIZE + x;
215+
// return blockPalette[blockData[index]];
216+
// });
209217

210218
// Below was some previous logic, leaving it around for now I would like to fix it up.
211-
// System.out.println(Arrays.toString(blockPalette));
212-
// var rawBlockData = buffer.read(LONG_ARRAY);
213-
// var bitsPerEntry = (int) Math.ceil(Math.log(blockPalette.length) / Math.log(2));
214-
//
215-
//// int count = computeCount(blockPalette, rawBlockData, bitsPerEntry);
216-
// int count = 16 * 16 * 16;
217-
// directReplaceInnerPaletteBlock(section.blockPalette(), (byte) bitsPerEntry, count,
218-
// blockPalette, rawBlockData);
219+
// System.out.println(Arrays.toString(blockPalette));
220+
// var rawBlockData = buffer.read(LONG_ARRAY);
221+
// var bitsPerEntry = (int) Math.ceil(Math.log(blockPalette.length) / Math.log(2));
222+
//
223+
//// int count = computeCount(blockPalette, rawBlockData, bitsPerEntry);
224+
// int count = 16 * 16 * 16;
225+
// directReplaceInnerPaletteBlock(section.blockPalette(), (byte) bitsPerEntry, count,
226+
// blockPalette, rawBlockData);
219227
}
220228

221229
int[] biomePalette = readBiomePalette(buffer);
@@ -236,17 +244,17 @@ private void readSection(@NotNull NetworkBuffer buffer, @NotNull Section section
236244
}
237245
}
238246
}
239-
// section.biomePalette().setAll((x, y, z) -> {
240-
// int index = x / 4 + (z / 4) * 4 + (y / 4) * 16;
241-
// return biomePalette[biomeData[index]];
242-
// });
243-
244-
// var rawBiomeData = buffer.read(LONG_ARRAY);
245-
// var bitsPerEntry = (int) Math.ceil(Math.log(biomePalette.length) / Math.log(2));
246-
// // Biome count is irrelevant to the client. Though it might be worth computing it anyway here
247-
// // in case a server implementation uses it for anything.
248-
// directReplaceInnerPaletteBiome(section.biomePalette(), (byte) bitsPerEntry, 4 * 4 * 4,
249-
// biomePalette, rawBiomeData);
247+
// section.biomePalette().setAll((x, y, z) -> {
248+
// int index = x / 4 + (z / 4) * 4 + (y / 4) * 16;
249+
// return biomePalette[biomeData[index]];
250+
// });
251+
252+
// var rawBiomeData = buffer.read(LONG_ARRAY);
253+
// var bitsPerEntry = (int) Math.ceil(Math.log(biomePalette.length) / Math.log(2));
254+
// // Biome count is irrelevant to the client. Though it might be worth computing it anyway here
255+
// // in case a server implementation uses it for anything.
256+
// directReplaceInnerPaletteBiome(section.biomePalette(), (byte) bitsPerEntry, 4 * 4 * 4,
257+
// biomePalette, rawBiomeData);
250258
}
251259

252260
if (version > PolarWorld.VERSION_UNIFIED_LIGHT) {
@@ -298,7 +306,8 @@ private int[] readBiomePalette(@NotNull NetworkBuffer buffer) {
298306
int[] biomePalette = new int[rawBiomePalette.length];
299307
for (int i = 0; i < rawBiomePalette.length; i++) {
300308
biomePalette[i] = biomeToIdCache.computeIfAbsent(rawBiomePalette[i], (String name) -> {
301-
PolarWorldAccess searchWorldAccess = Objects.requireNonNullElse(this.worldAccess, PolarWorldAccess.DEFAULT);
309+
PolarWorldAccess searchWorldAccess = Objects.requireNonNullElse(this.worldAccess,
310+
PolarWorldAccess.DEFAULT);
302311
var biomeId = searchWorldAccess.getBiomeId(name);
303312
if (biomeId == -1) {
304313
logger.error("Failed to find biome: {}", name);

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
final class UnsafeOps {
1717
private static final MethodHandle CACHE_CHUNK_HANDLE;
18+
private static final MethodHandle CHUNK_ON_LOAD_HANDLE;
1819
private static final MethodHandle NEEDS_HEIGHTMAP_REFRESH_SETTER;
1920
private static final MethodHandle DYNAMIC_CHUNK_ENTRIES_GETTER;
2021
private static final MethodHandle DYNAMIC_CHUNK_TICKABLE_MAP_GETTER;
@@ -36,6 +37,14 @@ static void unsafeCacheChunk(@NotNull InstanceContainer instance, @NotNull Chunk
3637
}
3738
}
3839

40+
static void unsafeChunkOnLoad(@NotNull Chunk chunk) {
41+
try {
42+
CHUNK_ON_LOAD_HANDLE.invokeExact(chunk);
43+
} catch (Throwable t) {
44+
throw new RuntimeException(t);
45+
}
46+
}
47+
3948
static void unsafeSetNeedsCompleteHeightmapRefresh(@NotNull Chunk chunk, boolean value) {
4049
if (chunk instanceof DynamicChunk dynamicChunk) {
4150
try {
@@ -102,20 +111,23 @@ static void unsafeUpdateSkyLightArray(@NotNull Light light, byte[] content) {
102111
try {
103112
var lookup = MethodHandles.privateLookupIn(DynamicChunk.class, MethodHandles.lookup());
104113
NEEDS_HEIGHTMAP_REFRESH_SETTER = lookup.unreflectSetter(DynamicChunk.class
105-
.getDeclaredField("needsCompleteHeightmapRefresh"));
114+
.getDeclaredField(
115+
"needsCompleteHeightmapRefresh"));
106116
DYNAMIC_CHUNK_ENTRIES_GETTER = lookup.unreflectGetter(DynamicChunk.class
107-
.getDeclaredField("entries"));
117+
.getDeclaredField("entries"));
108118
DYNAMIC_CHUNK_TICKABLE_MAP_GETTER = lookup.unreflectGetter(DynamicChunk.class
109-
.getDeclaredField("tickableMap"));
110-
} catch (IllegalAccessException | NoSuchFieldException e) {
119+
.getDeclaredField("tickableMap"));
120+
CHUNK_ON_LOAD_HANDLE = lookup.unreflect(Chunk.class.getDeclaredMethod("onLoad"));
121+
} catch (IllegalAccessException | NoSuchFieldException | NoSuchMethodException e) {
111122
throw new RuntimeException(e);
112123
}
113124

114125
try {
115126
var blockLight = Class.forName("net.minestom.server.instance.light.BlockLight");
116127
var lookup = MethodHandles.privateLookupIn(blockLight, MethodHandles.lookup());
117128
BLOCK_LIGHT_CONTENT_SETTER = lookup.unreflectSetter(blockLight.getDeclaredField("content"));
118-
BLOCK_LIGHT_CONTENT_PROPAGATION_SETTER = lookup.unreflectSetter(blockLight.getDeclaredField("contentPropagation"));
129+
BLOCK_LIGHT_CONTENT_PROPAGATION_SETTER = lookup.unreflectSetter(
130+
blockLight.getDeclaredField("contentPropagation"));
119131
BLOCK_LIGHT_IS_VALID_BORDERS_SETTER = lookup.unreflectSetter(blockLight.getDeclaredField("isValidBorders"));
120132
BLOCK_LIGHT_NEEDS_SEND_GETTER = lookup.unreflectGetter(blockLight.getDeclaredField("needsSend"));
121133
} catch (NoSuchFieldException | ClassNotFoundException | IllegalAccessException e) {
@@ -125,7 +137,8 @@ static void unsafeUpdateSkyLightArray(@NotNull Light light, byte[] content) {
125137
var skyLight = Class.forName("net.minestom.server.instance.light.SkyLight");
126138
var lookup = MethodHandles.privateLookupIn(skyLight, MethodHandles.lookup());
127139
SKY_LIGHT_CONTENT_SETTER = lookup.unreflectSetter(skyLight.getDeclaredField("content"));
128-
SKY_LIGHT_CONTENT_PROPAGATION_SETTER = lookup.unreflectSetter(skyLight.getDeclaredField("contentPropagation"));
140+
SKY_LIGHT_CONTENT_PROPAGATION_SETTER = lookup.unreflectSetter(
141+
skyLight.getDeclaredField("contentPropagation"));
129142
SKY_LIGHT_IS_VALID_BORDERS_SETTER = lookup.unreflectSetter(skyLight.getDeclaredField("isValidBorders"));
130143
SKY_LIGHT_NEEDS_SEND_GETTER = lookup.unreflectGetter(skyLight.getDeclaredField("needsSend"));
131144
} catch (NoSuchFieldException | ClassNotFoundException | IllegalAccessException e) {

0 commit comments

Comments
 (0)