From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: violetc <58360096+s-yh-china@users.noreply.github.com> Date: Tue, 18 Jul 2023 13:36:25 +0800 Subject: [PATCH] Optimize world generation chunk and block access This patch is Powered by Gale(https://github.com/GaleMC/Gale) diff --git a/src/main/java/net/minecraft/server/level/WorldGenRegion.java b/src/main/java/net/minecraft/server/level/WorldGenRegion.java index a6c31a558794a6e626e83176a1cbe78b6bd90f6e..4a4b8ea0e8ba89d5a5ad890493d525a0f2a596fc 100644 --- a/src/main/java/net/minecraft/server/level/WorldGenRegion.java +++ b/src/main/java/net/minecraft/server/level/WorldGenRegion.java @@ -86,6 +86,10 @@ public class WorldGenRegion implements WorldGenLevel { private Supplier currentlyGenerating; private final AtomicLong subTickCount = new AtomicLong(); private static final ResourceLocation WORLDGEN_REGION_RANDOM = new ResourceLocation("worldgen_region_random"); + // Leaves start - optimize world generation chunk and block access + private ChunkAccess[] chunksArr; + private int minChunkX, minChunkZ; + // Leaves end - optimize world generation chunk and block access public WorldGenRegion(ServerLevel world, List chunks, ChunkStatus status, int placementRadius) { this.generatingStatus = status; @@ -107,6 +111,11 @@ public class WorldGenRegion implements WorldGenLevel { this.firstPos = ((ChunkAccess) chunks.get(0)).getPos(); this.lastPos = ((ChunkAccess) chunks.get(chunks.size() - 1)).getPos(); } + // Leaves start - optimize world generation chunk and block access + this.minChunkX = this.firstPos.x; + this.minChunkZ = this.firstPos.z; + this.chunksArr = chunks.toArray(new ChunkAccess[0]); + // Leaves end - optimize world generation chunk and block access } // Paper start - starlight @@ -145,8 +154,29 @@ public class WorldGenRegion implements WorldGenLevel { @Override public ChunkAccess getChunk(int chunkX, int chunkZ) { - return this.getChunk(chunkX, chunkZ, ChunkStatus.EMPTY); + // Leaves start - optimize world generation chunk and block access + if (!org.leavesmc.leaves.LeavesConfig.optimizeWorldGenerationAccess) { + return this.getChunk(chunkX, chunkZ, ChunkStatus.EMPTY); + } else { + int x = chunkX - this.minChunkX; + int z = chunkZ - this.minChunkZ; + int w = this.size; + + if (x >= 0 && z >= 0 && x < w && z < w) { + return this.chunksArr[x + z * w]; + } else { + throw new NullPointerException("No chunk exists at " + new ChunkPos(chunkX, chunkZ)); + } + } + // Leaves end - optimize world generation chunk and block access + } + + // Leaves start - optimize world generation chunk and block access + public ChunkAccess getChunk(BlockPos pos) { + // Skip checking chunk.getStatus().isAtLeast(ChunkStatus.EMPTY) here, because it is always true + return this.getChunk(SectionPos.blockToSectionCoord(pos.getX()), SectionPos.blockToSectionCoord(pos.getZ())); } + // Leaves end - optimize world generation chunk and block access @Nullable @Override @@ -211,7 +241,21 @@ public class WorldGenRegion implements WorldGenLevel { @Override public BlockState getBlockState(BlockPos pos) { - return this.getChunk(SectionPos.blockToSectionCoord(pos.getX()), SectionPos.blockToSectionCoord(pos.getZ())).getBlockState(pos); + // Leaves start - optimize world generation chunk and block access + if (!org.leavesmc.leaves.LeavesConfig.optimizeWorldGenerationAccess) { + return this.getChunk(SectionPos.blockToSectionCoord(pos.getX()), SectionPos.blockToSectionCoord(pos.getZ())).getBlockState(pos); + } else { + int x = SectionPos.blockToSectionCoord(pos.getX()) - this.minChunkX; + int z = SectionPos.blockToSectionCoord(pos.getZ()) - this.minChunkZ; + int w = this.size; + + if (x >= 0 && z >= 0 && x < w && z < w) { + return this.chunksArr[x + z * w].getBlockState(pos); + } else { + throw new NullPointerException("No chunk exists at " + new ChunkPos(pos)); + } + } + // Leaves end - optimize world generation chunk and block access } @Override