diff --git a/common/src/main/java/com/dfsek/terra/config/base/PluginConfig.java b/common/src/main/java/com/dfsek/terra/config/base/PluginConfig.java index 3608049aa..b61d8291a 100644 --- a/common/src/main/java/com/dfsek/terra/config/base/PluginConfig.java +++ b/common/src/main/java/com/dfsek/terra/config/base/PluginConfig.java @@ -38,13 +38,14 @@ public class PluginConfig implements ConfigTemplate { @Value("cache.carver") @Default private int carverCache = 512; + @Value("cache.structure") @Default private int structureCache = 128; - @Value("cache.checks") + @Value("cache.sampler") @Default - private int checkCache = 128; + private int samplerCache = 512; @Value("dump-default") @Default @@ -95,7 +96,7 @@ public class PluginConfig implements ConfigTemplate { return structureCache; } - public int getCheckCache() { - return checkCache; + public int getSamplerCache() { + return samplerCache; } } diff --git a/common/src/main/java/com/dfsek/terra/generation/math/SamplerCache.java b/common/src/main/java/com/dfsek/terra/generation/math/SamplerCache.java index 6c915fd2e..967f13488 100644 --- a/common/src/main/java/com/dfsek/terra/generation/math/SamplerCache.java +++ b/common/src/main/java/com/dfsek/terra/generation/math/SamplerCache.java @@ -5,45 +5,42 @@ import com.dfsek.terra.api.platform.TerraPlugin; import com.dfsek.terra.api.platform.world.World; import net.jafama.FastMath; +import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; public class SamplerCache { - private final Map cache; + private final Map containerMap; private final TerraPlugin main; public SamplerCache(TerraPlugin main) { - cache = new HashMap<>(); + containerMap = new HashMap<>(); this.main = main; } public Sampler get(World world, int x, int z) { - return cache.computeIfAbsent(world.getSeed(), seed -> new Container(world, new LinkedHashMap() { - @Override - protected boolean removeEldestEntry(Map.Entry eldest) { - return size() > main.getTerraConfig().getCheckCache(); - } - })).get(x, z); + return containerMap.computeIfAbsent(world.getSeed(), seed -> new Container(world)).get(x, z); } public Sampler getChunk(World world, int chunkX, int chunkZ) { - return cache.computeIfAbsent(world.getSeed(), seed -> new Container(world, new LinkedHashMap() { - @Override - protected boolean removeEldestEntry(Map.Entry eldest) { - return size() > main.getTerraConfig().getCheckCache(); - } - })).getChunk(chunkX, chunkZ); + return containerMap.computeIfAbsent(world.getSeed(), seed -> new Container(world)).getChunk(chunkX, chunkZ); } private class Container { private final World world; - private final Map cache; + private final TerraWorld terraWorld; + private final Map cache = Collections.synchronizedMap(new LinkedHashMap() { + @Override + protected boolean removeEldestEntry(Map.Entry eldest) { + return this.size() > main.getTerraConfig().getSamplerCache(); + } + }); - private Container(World world, Map cache) { + private Container(World world) { this.world = world; - this.cache = cache; + terraWorld = main.getWorld(world); } public Sampler get(int x, int z) { @@ -54,8 +51,9 @@ public class SamplerCache { public Sampler getChunk(int cx, int cz) { long key = (((long) cx) << 32) | (cz & 0xffffffffL); - TerraWorld tw = main.getWorld(world); - return cache.computeIfAbsent(key, k -> new Sampler(cx, cz, tw.getGrid(), world, tw.getConfig().getTemplate().getBaseBlend(), tw.getConfig().getTemplate().getElevationBlend())); + synchronized(cache) { + return cache.computeIfAbsent(key, k -> new Sampler(cx, cz, terraWorld.getGrid(), world, terraWorld.getConfig().getTemplate().getBaseBlend(), terraWorld.getConfig().getTemplate().getElevationBlend())); + } } } } diff --git a/platforms/bukkit/src/main/resources/config.yml b/platforms/bukkit/src/main/resources/config.yml index c262958ae..fc5ac9ad3 100644 --- a/platforms/bukkit/src/main/resources/config.yml +++ b/platforms/bukkit/src/main/resources/config.yml @@ -6,5 +6,6 @@ biome-search-resolution: 4 cache: carver: 512 structure: 128 + sampler: 512 master-disable: caves: false \ No newline at end of file