Skip to content

Commit 3cb5ed0

Browse files
fix: improve lookup time
1 parent 5a0ce8f commit 3cb5ed0

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

common/src/main/java/dev/ftb/mods/ftbchunks/client/FTBChunksClientConfig.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ static void saveConfig() {
114114
}
115115

116116
class ChunkPosCustomYSetValue extends BaseValue<Set<HeightUtils.ChunkPosWithMinY>> {
117+
private final HashMap<Long, Integer> lookup = new HashMap<>();
118+
117119
protected ChunkPosCustomYSetValue(@Nullable SNBTConfig c, String n, Set<HeightUtils.ChunkPosWithMinY> def) {
118120
super(c, n, def);
119121
super.set(new HashSet<>());
@@ -147,6 +149,16 @@ public void read(SNBTCompoundTag tag) {
147149
}
148150

149151
set(set);
152+
153+
// Rebuild lookup
154+
lookup.clear();
155+
for (HeightUtils.ChunkPosWithMinY pos : set) {
156+
lookup.put(ChunkPos.asLong(pos.chunkX(), pos.chunkZ()), pos.minY());
157+
}
158+
}
159+
160+
public HashMap<Long, Integer> lookup() {
161+
return lookup;
150162
}
151163
}
152164
}

common/src/main/java/dev/ftb/mods/ftbchunks/util/HeightUtils.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
import dev.ftb.mods.ftbchunks.core.BlockStateFTBC;
66
import net.minecraft.core.BlockPos;
77
import net.minecraft.util.Mth;
8+
import net.minecraft.world.level.ChunkPos;
89
import net.minecraft.world.level.Level;
910
import net.minecraft.world.level.block.Blocks;
1011
import net.minecraft.world.level.block.state.BlockState;
1112
import net.minecraft.world.level.chunk.ChunkAccess;
1213
import net.minecraft.world.level.material.Fluids;
1314
import org.jetbrains.annotations.Nullable;
1415

15-
import java.util.Set;
16-
1716
public class HeightUtils {
1817
public static final int UNKNOWN = Short.MIN_VALUE + 1;
1918

@@ -89,14 +88,10 @@ public static int getHeight(Level level, @Nullable ChunkAccess chunkAccess, Bloc
8988
}
9089

9190
private static int getMinYFromChunkOrConfig(int x, int z) {
92-
Set<ChunkPosWithMinY> chunkPosWithMinY = FTBChunksClientConfig.CHUNKS_WITH_CUSTOM_Y.get();
93-
for (ChunkPosWithMinY c : chunkPosWithMinY) {
94-
if (c.chunkX == x && c.chunkZ == z) {
95-
return c.minY;
96-
}
97-
}
98-
99-
return FTBChunksClientConfig.OVERRIDE_MIN_Y_LEVEL_VALUE.get();
91+
Long chunkPos = ChunkPos.asLong(x, z);
92+
93+
return FTBChunksClientConfig.CHUNKS_WITH_CUSTOM_Y.lookup()
94+
.getOrDefault(chunkPos, FTBChunksClientConfig.OVERRIDE_MIN_Y_LEVEL_VALUE.get());
10095
}
10196

10297
public record ChunkPosWithMinY(int chunkX, int chunkZ, int minY) {}

0 commit comments

Comments
 (0)