diff --git a/TerraForgedCore/src/main/java/com/terraforged/core/region/RegionCache.java b/TerraForgedCore/src/main/java/com/terraforged/core/region/RegionCache.java index 852508f..5320537 100644 --- a/TerraForgedCore/src/main/java/com/terraforged/core/region/RegionCache.java +++ b/TerraForgedCore/src/main/java/com/terraforged/core/region/RegionCache.java @@ -31,7 +31,6 @@ import com.terraforged.core.world.heightmap.RegionExtent; import me.dags.noise.util.NoiseUtil; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; public class RegionCache implements RegionExtent { @@ -39,13 +38,12 @@ public class RegionCache implements RegionExtent { private final boolean queuing; private final RegionGenerator renderer; private final Cache> cache; - - private Region cachedRegion = null; + private final ThreadLocal cachedRegion = new ThreadLocal<>(); public RegionCache(boolean queueNeighbours, RegionGenerator renderer) { this.renderer = renderer; this.queuing = queueNeighbours; - this.cache = new Cache<>(180, 60, TimeUnit.SECONDS, () -> new ConcurrentHashMap<>()); + this.cache = Cache.concurrent(180, 60, TimeUnit.SECONDS); } @Override @@ -74,25 +72,28 @@ public class RegionCache implements RegionExtent { @Override public Region getRegion(int regionX, int regionZ) { - if (cachedRegion != null && regionX == cachedRegion.getRegionX() && regionZ == cachedRegion.getRegionZ()) { - return cachedRegion; + Region cached = cachedRegion.get(); + if (cached != null && regionX == cached.getRegionX() && regionZ == cached.getRegionZ()) { + return cached; } long id = NoiseUtil.seed(regionX, regionZ); CompletableFuture futureRegion = cache.get(id); if (futureRegion == null) { - cachedRegion = renderer.generateRegion(regionX, regionZ); - cache.put(id, CompletableFuture.completedFuture(cachedRegion)); + cached = renderer.generateRegion(regionX, regionZ); + cache.put(id, CompletableFuture.completedFuture(cached)); } else { - cachedRegion = futureRegion.join(); + cached = futureRegion.join(); } if (queuing) { queueNeighbours(regionX, regionZ); } - return cachedRegion; + cachedRegion.set(cached); + + return cached; } private void queueNeighbours(int regionX, int regionZ) { diff --git a/TerraForgedCore/src/main/java/com/terraforged/core/util/Cache.java b/TerraForgedCore/src/main/java/com/terraforged/core/util/Cache.java index c8b72e9..172b7ad 100644 --- a/TerraForgedCore/src/main/java/com/terraforged/core/util/Cache.java +++ b/TerraForgedCore/src/main/java/com/terraforged/core/util/Cache.java @@ -27,6 +27,7 @@ package com.terraforged.core.util; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; import java.util.function.Supplier; @@ -94,4 +95,8 @@ public class Cache { return value; } } + + public static Cache concurrent(long lifespan, long interval, TimeUnit unit) { + return new Cache<>(lifespan, interval, unit, () -> new ConcurrentHashMap<>(100)); + } } diff --git a/TerraForgedMod/src/main/resources/data/terraforged/structures/trees/willow/large/weeping_willow_big_1.nbt b/TerraForgedMod/src/main/resources/data/terraforged/structures/trees/willow/large/weeping_willow_big_1.nbt index d8346fb..2894fa6 100644 Binary files a/TerraForgedMod/src/main/resources/data/terraforged/structures/trees/willow/large/weeping_willow_big_1.nbt and b/TerraForgedMod/src/main/resources/data/terraforged/structures/trees/willow/large/weeping_willow_big_1.nbt differ diff --git a/TerraForgedMod/src/main/resources/data/terraforged/structures/trees/willow/large/weeping_willow_big_2.nbt b/TerraForgedMod/src/main/resources/data/terraforged/structures/trees/willow/large/weeping_willow_big_2.nbt index 48ef0cf..b5852e6 100644 Binary files a/TerraForgedMod/src/main/resources/data/terraforged/structures/trees/willow/large/weeping_willow_big_2.nbt and b/TerraForgedMod/src/main/resources/data/terraforged/structures/trees/willow/large/weeping_willow_big_2.nbt differ diff --git a/TerraForgedMod/src/main/resources/data/terraforged/structures/trees/willow/small/weeping_willow_small_1.nbt b/TerraForgedMod/src/main/resources/data/terraforged/structures/trees/willow/small/weeping_willow_small_1.nbt index 5295f78..5066472 100644 Binary files a/TerraForgedMod/src/main/resources/data/terraforged/structures/trees/willow/small/weeping_willow_small_1.nbt and b/TerraForgedMod/src/main/resources/data/terraforged/structures/trees/willow/small/weeping_willow_small_1.nbt differ diff --git a/TerraForgedMod/src/main/resources/data/terraforged/structures/trees/willow/small/weeping_willow_small_2.nbt b/TerraForgedMod/src/main/resources/data/terraforged/structures/trees/willow/small/weeping_willow_small_2.nbt index a8b2573..5434dd9 100644 Binary files a/TerraForgedMod/src/main/resources/data/terraforged/structures/trees/willow/small/weeping_willow_small_2.nbt and b/TerraForgedMod/src/main/resources/data/terraforged/structures/trees/willow/small/weeping_willow_small_2.nbt differ