mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-02-16 10:30:42 +00:00
implement caching biome provider
This commit is contained in:
@@ -59,4 +59,8 @@ public interface BiomeProvider {
|
||||
* @return {@link Iterable} of all biomes this provider can generate.
|
||||
*/
|
||||
Iterable<Biome> getBiomes();
|
||||
|
||||
default BiomeProvider caching() {
|
||||
return new CachingBiomeProvider(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.dfsek.terra.api.world.biome.generation;
|
||||
|
||||
import com.dfsek.terra.api.Handle;
|
||||
import com.dfsek.terra.api.util.MathUtil;
|
||||
import com.dfsek.terra.api.world.biome.Biome;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* A biome provider implementation that lazily evaluates biomes, and caches them.
|
||||
*
|
||||
* This is for use in chunk generators, it makes the assumption that <b>the seed remains the same for the duration of its use!</b>
|
||||
*/
|
||||
public class CachingBiomeProvider implements BiomeProvider, Handle {
|
||||
private final BiomeProvider delegate;
|
||||
private final Map<Long, Biome> cache = new HashMap<>();
|
||||
|
||||
protected CachingBiomeProvider(BiomeProvider delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BiomeProvider getHandle() {
|
||||
return delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Biome getBiome(int x, int z, long seed) {
|
||||
return cache.computeIfAbsent(MathUtil.squash(x, z), key -> delegate.getBiome(x, z, seed));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Biome> getBiomes() {
|
||||
return delegate.getBiomes();
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
package com.dfsek.terra.api.world.chunk.generation;
|
||||
|
||||
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
|
||||
import com.dfsek.terra.api.world.info.WorldProperties;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -18,7 +19,7 @@ import com.dfsek.terra.api.world.WritableWorld;
|
||||
|
||||
|
||||
public interface ChunkGenerator {
|
||||
void generateChunkData(@NotNull ProtoChunk chunk, @NotNull WorldProperties world,
|
||||
void generateChunkData(@NotNull ProtoChunk chunk, @NotNull WorldProperties world, @NotNull BiomeProvider biomeProvider,
|
||||
int chunkX, int chunkZ);
|
||||
|
||||
BlockState getBlock(WorldProperties world, int x, int y, int z);
|
||||
|
||||
Reference in New Issue
Block a user