diff --git a/src/main/java/com/hbm/explosion/ExplosionNukeRayParallelized.java b/src/main/java/com/hbm/explosion/ExplosionNukeRayParallelized.java index 1e0aa6991..e6f01e18c 100644 --- a/src/main/java/com/hbm/explosion/ExplosionNukeRayParallelized.java +++ b/src/main/java/com/hbm/explosion/ExplosionNukeRayParallelized.java @@ -20,8 +20,11 @@ import java.util.*; import java.util.concurrent.*; import java.util.concurrent.atomic.DoubleAdder; -import static com.hbm.util.SubChunkSnapshot.getSnapshot; - +/** + * Threaded DDA raytracer for the nuke explosion. + * + * @author mlbv + */ public class ExplosionNukeRayParallelized implements IExplosionRay { private static final int WORLD_HEIGHT = 256; @@ -114,10 +117,7 @@ public class ExplosionNukeRayParallelized implements IExplosionRay { while (System.nanoTime() < deadline) { ChunkKey ck = cacheQueue.poll(); if (ck == null) break; - snapshots.computeIfAbsent(ck, key -> { - SubChunkSnapshot snap = getSnapshot(this.world, key.pos, key.subY); - return snap == null ? SubChunkSnapshot.EMPTY : snap; - }); + snapshots.computeIfAbsent(ck, k -> SubChunkSnapshot.getSnapshot(world, k, BombConfig.chunkloading)); } } @@ -188,7 +188,7 @@ public class ExplosionNukeRayParallelized implements IExplosionRay { if (bs.isEmpty()) { destructionMap.remove(cp); for (int sy = 0; sy < (WORLD_HEIGHT >> 4); sy++) { - snapshots.remove(new ChunkKey(cp.chunkXPos, cp.chunkZPos, sy)); + snapshots.remove(new ChunkKey(cp, sy)); } it.remove(); } @@ -244,7 +244,7 @@ public class ExplosionNukeRayParallelized implements IExplosionRay { private List generateSphereRays(int count) { List list = new ArrayList<>(count); - if (count <= 0) return list; + if (count == 0) return list; if (count == 1) { list.add(Vec3.createVectorHelper(1, 0, 0).normalize()); return list; @@ -293,7 +293,7 @@ public class ExplosionNukeRayParallelized implements IExplosionRay { continue; } - ChunkKey snapshotKey = new ChunkKey(cp.chunkXPos, cp.chunkZPos, subY); + ChunkKey snapshotKey = new ChunkKey(cp, subY); SubChunkSnapshot snap = snapshots.get(snapshotKey); Block originalBlock; diff --git a/src/main/java/com/hbm/util/ChunkKey.java b/src/main/java/com/hbm/util/ChunkKey.java index 62c2f7c9a..4f3bd0ba3 100644 --- a/src/main/java/com/hbm/util/ChunkKey.java +++ b/src/main/java/com/hbm/util/ChunkKey.java @@ -6,7 +6,7 @@ import java.util.Objects; /** * Unique identifier for sub-chunks. - * @Author mlbv + * @author mlbv */ public class ChunkKey { public final ChunkCoordIntPair pos; @@ -17,6 +17,11 @@ public class ChunkKey { this.subY = sy; } + public ChunkKey(ChunkCoordIntPair pos, int sy) { + this.pos = pos; + this.subY = sy; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/com/hbm/util/SubChunkSnapshot.java b/src/main/java/com/hbm/util/SubChunkSnapshot.java index 6e7b99cc0..cd51d125b 100644 --- a/src/main/java/com/hbm/util/SubChunkSnapshot.java +++ b/src/main/java/com/hbm/util/SubChunkSnapshot.java @@ -2,7 +2,6 @@ package com.hbm.util; import net.minecraft.block.Block; import net.minecraft.init.Blocks; -import net.minecraft.world.ChunkCoordIntPair; import net.minecraft.world.World; import net.minecraft.world.chunk.Chunk; import net.minecraft.world.chunk.storage.ExtendedBlockStorage; @@ -14,7 +13,7 @@ import java.util.Map; /** * A snapshot of a 16×16×16 sub-chunk. - * @Author mlbv + * @author mlbv */ public class SubChunkSnapshot { /** @@ -29,42 +28,25 @@ public class SubChunkSnapshot { this.data = d; } - /** - * Creates a SubChunkSnapshot from a loaded chunk. - * - * @param world - * The World instance from which to retrieve the chunk. - * @param cpos - * The ChunkCoordIntPair identifying the chunk coordinates (x, z). - * @param subY - * The vertical sub-chunk index (0–15) within the chunk. - * @return - * A SubChunkSnapshot containing the palette and block data for the sub-chunk, - * or SubChunkSnapshot.EMPTY if the region is unloaded or contains only air. - */ - public static SubChunkSnapshot getSnapshot(World world, ChunkCoordIntPair cpos, int subY) { - if (!world.getChunkProvider().chunkExists(cpos.chunkXPos, cpos.chunkZPos)) { - return SubChunkSnapshot.EMPTY; - } - return getOrLoadSnapshot(world, cpos, subY); - } - /** * Creates a SubChunkSnapshot. * * @param world * The World instance from which to retrieve the chunk. - * @param cpos - * The ChunkCoordIntPair identifying the chunk coordinates (x, z). - * @param subY - * The vertical sub-chunk index (0–15) within the chunk. + * @param key + * The ChunkKey identifying the sub-chunk. + * @param allowGeneration + * Whether to generate chunks. If false, attempting to retrieve a snapshot of a chunk that does not exist will return {@link SubChunkSnapshot#EMPTY}. * @return * A SubChunkSnapshot containing the palette and block data for the sub-chunk, - * or SubChunkSnapshot.EMPTY if the region contains only air. + * or {@link SubChunkSnapshot#EMPTY} if the region contains only air. */ - public static SubChunkSnapshot getOrLoadSnapshot(World world, ChunkCoordIntPair cpos, int subY){ - Chunk chunk = world.getChunkFromChunkCoords(cpos.chunkXPos, cpos.chunkZPos); - ExtendedBlockStorage ebs = chunk.getBlockStorageArray()[subY]; + public static SubChunkSnapshot getSnapshot(World world, ChunkKey key, boolean allowGeneration){ + if (!world.getChunkProvider().chunkExists(key.pos.chunkXPos, key.pos.chunkZPos) && !allowGeneration) { + return SubChunkSnapshot.EMPTY; + } + Chunk chunk = world.getChunkProvider().provideChunk(key.pos.chunkXPos, key.pos.chunkZPos); + ExtendedBlockStorage ebs = chunk.getBlockStorageArray()[key.subY]; if (ebs == null || ebs.isEmpty()) return SubChunkSnapshot.EMPTY; short[] data = new short[16 * 16 * 16];