mirror of
https://github.com/Winds-Studio/Leaf.git
synced 2025-12-19 15:09:25 +00:00
195 lines
8.5 KiB
Diff
195 lines
8.5 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Martijn Muijsers <martijnmuijsers@live.nl>
|
|
Date: Thu, 1 Dec 2022 14:33:06 +0100
|
|
Subject: [PATCH] Optimize world generation chunk and block access
|
|
|
|
Removed since 1.21
|
|
|
|
License: LGPL-3.0 (https://www.gnu.org/licenses/lgpl-3.0.html)
|
|
Gale - https://galemc.org
|
|
|
|
This patch is based on the following mixins and classes:
|
|
* "me/jellysquid/mods/lithium/common/util/Pos.java"
|
|
* "me/jellysquid/mods/lithium/mixin/gen/chunk_region/ChunkRegionMixin.java"
|
|
By: SuperCoder7979 <k.pranav@gmail.com>, 2No2Name <2No2Name@web.de>
|
|
As part of: Lithium (https://github.com/CaffeineMC/lithium-fabric)
|
|
Licensed under: LGPL-3.0 (https://www.gnu.org/licenses/lgpl-3.0.html)
|
|
|
|
diff --git a/src/main/java/me/jellysquid/mods/lithium/common/util/Pos.java b/src/main/java/me/jellysquid/mods/lithium/common/util/Pos.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..f2f10b651e4fbecdd1ea1bc28ebf40d685a67cee
|
|
--- /dev/null
|
|
+++ b/src/main/java/me/jellysquid/mods/lithium/common/util/Pos.java
|
|
@@ -0,0 +1,95 @@
|
|
+// Gale - Lithium - position utility
|
|
+
|
|
+package me.jellysquid.mods.lithium.common.util;
|
|
+
|
|
+import net.minecraft.core.SectionPos;
|
|
+import net.minecraft.world.level.LevelHeightAccessor;
|
|
+
|
|
+public class Pos {
|
|
+
|
|
+ public static class BlockCoord {
|
|
+ public static int getYSize(LevelHeightAccessor view) {
|
|
+ return view.getHeight();
|
|
+ }
|
|
+ public static int getMinY(LevelHeightAccessor view) {
|
|
+ return view.getMinBuildHeight();
|
|
+ }
|
|
+ public static int getMaxYInclusive(LevelHeightAccessor view) {
|
|
+ return view.getMaxBuildHeight() - 1;
|
|
+ }
|
|
+ public static int getMaxYExclusive(LevelHeightAccessor view) {
|
|
+ return view.getMaxBuildHeight();
|
|
+ }
|
|
+
|
|
+ public static int getMaxInSectionCoord(int sectionCoord) {
|
|
+ return 15 + getMinInSectionCoord(sectionCoord);
|
|
+ }
|
|
+
|
|
+ public static int getMaxYInSectionIndex(LevelHeightAccessor view, int sectionIndex){
|
|
+ return getMaxInSectionCoord(SectionYCoord.fromSectionIndex(view, sectionIndex));
|
|
+ }
|
|
+
|
|
+ public static int getMinInSectionCoord(int sectionCoord) {
|
|
+ return SectionPos.sectionToBlockCoord(sectionCoord);
|
|
+ }
|
|
+
|
|
+ public static int getMinYInSectionIndex(LevelHeightAccessor view, int sectionIndex) {
|
|
+ return getMinInSectionCoord(SectionYCoord.fromSectionIndex(view, sectionIndex));
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public static class ChunkCoord {
|
|
+ public static int fromBlockCoord(int blockCoord) {
|
|
+ return SectionPos.blockToSectionCoord(blockCoord);
|
|
+ }
|
|
+
|
|
+ public static int fromBlockSize(int i) {
|
|
+ return i >> 4; //same method as fromBlockCoord, just be clear about coord/size semantic difference
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public static class SectionYCoord {
|
|
+ public static int getNumYSections(LevelHeightAccessor view) {
|
|
+ return view.getSectionsCount();
|
|
+ }
|
|
+ public static int getMinYSection(LevelHeightAccessor view) {
|
|
+ return view.getMinSection();
|
|
+ }
|
|
+ public static int getMaxYSectionInclusive(LevelHeightAccessor view) {
|
|
+ return view.getMaxSection() - 1;
|
|
+ }
|
|
+ public static int getMaxYSectionExclusive(LevelHeightAccessor view) {
|
|
+ return view.getMaxSection();
|
|
+ }
|
|
+
|
|
+ public static int fromSectionIndex(LevelHeightAccessor view, int sectionCoord) {
|
|
+ return sectionCoord + SectionYCoord.getMinYSection(view);
|
|
+ }
|
|
+ public static int fromBlockCoord(int blockCoord) {
|
|
+ return SectionPos.blockToSectionCoord(blockCoord);
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public static class SectionYIndex {
|
|
+ public static int getNumYSections(LevelHeightAccessor view) {
|
|
+ return view.getSectionsCount();
|
|
+ }
|
|
+ public static int getMinYSectionIndex(LevelHeightAccessor view) {
|
|
+ return 0;
|
|
+ }
|
|
+ public static int getMaxYSectionIndexInclusive(LevelHeightAccessor view) {
|
|
+ return view.getSectionsCount() - 1;
|
|
+ }
|
|
+ public static int getMaxYSectionIndexExclusive(LevelHeightAccessor view) {
|
|
+ return view.getSectionsCount();
|
|
+ }
|
|
+
|
|
+ public static int fromSectionCoord(LevelHeightAccessor view, int sectionCoord) {
|
|
+ return sectionCoord - SectionYCoord.getMinYSection(view);
|
|
+ }
|
|
+ public static int fromBlockCoord(LevelHeightAccessor view, int blockCoord) {
|
|
+ return fromSectionCoord(view, SectionPos.blockToSectionCoord(blockCoord));
|
|
+ }
|
|
+ }
|
|
+
|
|
+}
|
|
diff --git a/src/main/java/net/minecraft/server/level/WorldGenRegion.java b/src/main/java/net/minecraft/server/level/WorldGenRegion.java
|
|
index 59b7d9c9a2a0203f8135a9dcdce302bbf0b2d0f4..c8ff093e5bb8295e61e70d6fb447ed86baa350a4 100644
|
|
--- a/src/main/java/net/minecraft/server/level/WorldGenRegion.java
|
|
+++ b/src/main/java/net/minecraft/server/level/WorldGenRegion.java
|
|
@@ -87,6 +87,10 @@ public class WorldGenRegion implements WorldGenLevel {
|
|
private Supplier<String> currentlyGenerating;
|
|
private final AtomicLong subTickCount = new AtomicLong();
|
|
private static final ResourceLocation WORLDGEN_REGION_RANDOM = new ResourceLocation("worldgen_region_random");
|
|
+ // Gale start - Lithium - optimize world generation chunk and block access
|
|
+ private ChunkAccess[] chunksArr;
|
|
+ private int minChunkX, minChunkZ;
|
|
+ // Gale end - Lithium - optimize world generation chunk and block access
|
|
|
|
public WorldGenRegion(ServerLevel world, List<ChunkAccess> chunks, ChunkStatus status, int placementRadius) {
|
|
this.generatingStatus = status;
|
|
@@ -108,6 +112,11 @@ public class WorldGenRegion implements WorldGenLevel {
|
|
this.firstPos = ((ChunkAccess) chunks.get(0)).getPos();
|
|
this.lastPos = ((ChunkAccess) chunks.get(chunks.size() - 1)).getPos();
|
|
}
|
|
+ // Gale start - Lithium - optimize world generation chunk and block access
|
|
+ this.minChunkX = this.firstPos.x;
|
|
+ this.minChunkZ = this.firstPos.z;
|
|
+ this.chunksArr = chunks.toArray(new ChunkAccess[0]);
|
|
+ // Gale end - Lithium - optimize world generation chunk and block access
|
|
}
|
|
|
|
// Paper start - starlight
|
|
@@ -146,8 +155,28 @@ public class WorldGenRegion implements WorldGenLevel {
|
|
|
|
@Override
|
|
public ChunkAccess getChunk(int chunkX, int chunkZ) {
|
|
- return this.getChunk(chunkX, chunkZ, ChunkStatus.EMPTY);
|
|
+ // Gale start - Lithium - optimize world generation chunk and block access - use the chunk array for faster access
|
|
+ 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));
|
|
+ }
|
|
+ // Gale end - Lithium - optimize world generation chunk and block access - use the chunk array for faster access
|
|
+ }
|
|
+
|
|
+ // Gale start - Lithium - optimize world generation chunk and block access
|
|
+ /**
|
|
+ * Use our chunk fetch function
|
|
+ */
|
|
+ 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()));
|
|
}
|
|
+ // Gale end - Lithium - optimize world generation chunk and block access
|
|
|
|
@Nullable
|
|
@Override
|
|
@@ -212,7 +241,17 @@ public class WorldGenRegion implements WorldGenLevel {
|
|
|
|
@Override
|
|
public BlockState getBlockState(BlockPos pos) {
|
|
- return this.getChunk(SectionPos.blockToSectionCoord(pos.getX()), SectionPos.blockToSectionCoord(pos.getZ())).getBlockState(pos);
|
|
+ // Gale start - Lithium - optimize world generation chunk and block access - avoid pointer de-referencing, make method easier to inline
|
|
+ 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));
|
|
+ }
|
|
+ // Gale end - Lithium - optimize world generation chunk and block access - avoid pointer de-referencing, make method easier to inline
|
|
}
|
|
|
|
@Override
|