diff --git a/src/main/java/com/gtnewhorizons/postea/utility/ChunkFixerUtility.java b/src/main/java/com/gtnewhorizons/postea/utility/ChunkFixerUtility.java index 057b0af..a1d0675 100644 --- a/src/main/java/com/gtnewhorizons/postea/utility/ChunkFixerUtility.java +++ b/src/main/java/com/gtnewhorizons/postea/utility/ChunkFixerUtility.java @@ -98,30 +98,10 @@ public class ChunkFixerUtility { for (ExtendedBlockStorage section : chunk.getBlockStorageArray()) { if (section != null) section.removeInvalidBlocks(); } - regenerateHeightMap(chunk); + HeightMaps.regenerate(chunk); chunk.isLightPopulated = false; } - /// Rebuilds the height map from the chunk's own storage, which is safe on the chunk I/O thread. - /// `Chunk.generateHeightMap` is client-only and `generateSkylightMap` reaches into the world. - private static void regenerateHeightMap(Chunk chunk) { - int top = chunk.getTopFilledSegment() + 16 - 1; - chunk.heightMapMinimum = Integer.MAX_VALUE; - for (int x = 0; x < 16; x++) { - for (int z = 0; z < 16; z++) { - chunk.precipitationHeightMap[x + (z << 4)] = -999; - int y = top; - while (y > 0 && chunk.func_150808_b(x, y - 1, z) == 0) { - y--; - } - if (y > 0) { - chunk.heightMap[z << 4 | x] = y; - chunk.heightMapMinimum = Math.min(chunk.heightMapMinimum, y); - } - } - } - } - public static CrashReport describe(Throwable cause, IVersionedTransformer transformer, int stored, int current, String subjectName, String subject) { CrashReport report = CrashReport.makeCrashReport(cause, "Running versioned transformer " + transformer.key()); diff --git a/src/main/java/com/gtnewhorizons/postea/utility/HeightMaps.java b/src/main/java/com/gtnewhorizons/postea/utility/HeightMaps.java new file mode 100644 index 0000000..cdd5e28 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/postea/utility/HeightMaps.java @@ -0,0 +1,26 @@ +package com.gtnewhorizons.postea.utility; + +import net.minecraft.world.chunk.Chunk; + +/// Rebuilds a chunk's height map from the chunk's own storage, which is safe on the chunk I/O thread. +/// `Chunk.generateHeightMap` is client-only and `generateSkylightMap` reaches into the world. +final class HeightMaps { + + private HeightMaps() {} + + static void regenerate(Chunk chunk) { + int top = chunk.getTopFilledSegment() + 16; + chunk.heightMapMinimum = Integer.MAX_VALUE; + for (int x = 0; x < 16; x++) { + for (int z = 0; z < 16; z++) { + chunk.precipitationHeightMap[x + (z << 4)] = -999; + int y = top; + while (y > 0 && chunk.func_150808_b(x, y - 1, z) == 0) { + y--; + } + chunk.heightMap[z << 4 | x] = y; + chunk.heightMapMinimum = Math.min(chunk.heightMapMinimum, y); + } + } + } +} diff --git a/src/test/java/com/gtnewhorizons/postea/utility/HeightMapsTest.java b/src/test/java/com/gtnewhorizons/postea/utility/HeightMapsTest.java new file mode 100644 index 0000000..d2de16b --- /dev/null +++ b/src/test/java/com/gtnewhorizons/postea/utility/HeightMapsTest.java @@ -0,0 +1,62 @@ +package com.gtnewhorizons.postea.utility; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.HashSet; +import java.util.Set; + +import net.minecraft.world.chunk.Chunk; +import net.minecraft.world.chunk.storage.ExtendedBlockStorage; + +import org.junit.jupiter.api.Test; + +class HeightMapsTest { + + /// A chunk with empty sections at `sections`, opaque only at positions set through [#setOpaque], with no block + /// registry behind it. + private static final class OpacityChunk extends Chunk { + + private final Set opaque = new HashSet<>(); + + OpacityChunk(int... sections) { + super(null, 0, 0); + for (int section : sections) getBlockStorageArray()[section] = new ExtendedBlockStorage(section << 4, true); + } + + void setOpaque(int x, int y, int z) { + opaque.add(position(x, y, z)); + } + + @Override + public int func_150808_b(int x, int y, int z) { + return opaque.contains(position(x, y, z)) ? 255 : 0; + } + + private static long position(int x, int y, int z) { + return ((long) y << 8) | (z << 4) | x; + } + } + + @Test + void anEmptiedColumnGetsHeightZero() { + OpacityChunk chunk = new OpacityChunk(0); + chunk.setOpaque(0, 10, 0); + chunk.heightMap[5 << 4 | 3] = 64; + + HeightMaps.regenerate(chunk); + + assertEquals(0, chunk.heightMap[5 << 4 | 3]); + assertEquals(11, chunk.heightMap[0]); + assertEquals(0, chunk.heightMapMinimum); + } + + @Test + void aBlockAtTheTopOfTheWorldGetsHeight256() { + OpacityChunk chunk = new OpacityChunk(15); + chunk.setOpaque(7, 255, 9); + + HeightMaps.regenerate(chunk); + + assertEquals(256, chunk.heightMap[9 << 4 | 7]); + } +}