implement World#getConfig

This commit is contained in:
dfsek
2021-07-22 13:51:51 -07:00
parent 7f050b37a4
commit a56d1818c8
10 changed files with 69 additions and 12 deletions

View File

@@ -2,6 +2,7 @@ package com.dfsek.terra.config.dummy;
import com.dfsek.terra.api.block.entity.BlockEntity;
import com.dfsek.terra.api.block.state.BlockState;
import com.dfsek.terra.api.config.WorldConfig;
import com.dfsek.terra.api.entity.Entity;
import com.dfsek.terra.api.entity.EntityType;
import com.dfsek.terra.api.vector.Vector3;
@@ -66,4 +67,9 @@ public class DummyWorld implements World {
public BiomeProvider getBiomeProvider() {
throw new UnsupportedOperationException("Cannot get biome provider of DummyWorld");
}
@Override
public WorldConfig getConfig() {
throw new UnsupportedOperationException("Cannot get config of DummyWorld");
}
}

View File

@@ -32,7 +32,7 @@ import com.dfsek.terra.api.registry.exception.DuplicateEntryException;
import com.dfsek.terra.api.registry.meta.RegistryFactory;
import com.dfsek.terra.api.util.generic.pair.ImmutablePair;
import com.dfsek.terra.api.util.reflection.ReflectionUtil;
import com.dfsek.terra.api.world.TerraWorld;
import com.dfsek.terra.api.world.World;
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
import com.dfsek.terra.api.world.generator.ChunkGeneratorProvider;
import com.dfsek.terra.api.world.generator.GenerationStageProvider;
@@ -139,7 +139,7 @@ public class ConfigPackImpl implements ConfigPack {
main.logger().severe("Failed to load config pack from folder \"" + folder.getAbsolutePath() + "\"");
throw e;
}
toWorldConfig(new TerraWorldImpl(new DummyWorld(), this, main)); // Build now to catch any errors immediately.
toWorldConfig(new DummyWorld()); // Build now to catch any errors immediately.
}
public ConfigPackImpl(ZipFile file, TerraPlugin main) throws ConfigException {
@@ -192,7 +192,7 @@ public class ConfigPackImpl implements ConfigPack {
throw e;
}
toWorldConfig(new TerraWorldImpl(new DummyWorld(), this, main)); // Build now to catch any errors immediately.
toWorldConfig(new DummyWorld()); // Build now to catch any errors immediately.
}
@SuppressWarnings("unchecked")
@@ -353,7 +353,7 @@ public class ConfigPackImpl implements ConfigPack {
@Override
public WorldConfigImpl toWorldConfig(TerraWorld world) {
public WorldConfigImpl toWorldConfig(World world) {
return new WorldConfigImpl(world, this, main);
}

View File

@@ -4,6 +4,7 @@ import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.api.config.WorldConfig;
import com.dfsek.terra.api.registry.Registry;
import com.dfsek.terra.api.world.TerraWorld;
import com.dfsek.terra.api.world.World;
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
import com.dfsek.terra.api.world.generator.SamplerCache;
import com.dfsek.terra.registry.LockedRegistryImpl;
@@ -18,12 +19,12 @@ public class WorldConfigImpl implements WorldConfig {
private final BiomeProvider provider;
private final TerraWorld world;
private final World world;
private final ConfigPackImpl pack;
private final Map<Type, Registry<?>> registryMap = new HashMap<>();
public WorldConfigImpl(TerraWorld world, ConfigPackImpl pack, TerraPlugin main) {
public WorldConfigImpl(World world, ConfigPackImpl pack, TerraPlugin main) {
this.world = world;
this.pack = pack;
this.samplerCache = new SamplerCacheImpl(main, world);
@@ -40,7 +41,7 @@ public class WorldConfigImpl implements WorldConfig {
}
@Override
public TerraWorld getWorld() {
public World getWorld() {
return world;
}

View File

@@ -3,6 +3,7 @@ package com.dfsek.terra.world;
import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.api.util.MathUtil;
import com.dfsek.terra.api.world.TerraWorld;
import com.dfsek.terra.api.world.World;
import com.dfsek.terra.api.world.generator.Sampler;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
@@ -13,14 +14,14 @@ import org.jetbrains.annotations.NotNull;
public class SamplerCacheImpl implements com.dfsek.terra.api.world.generator.SamplerCache {
private final LoadingCache<Long, Sampler> cache;
public SamplerCacheImpl(TerraPlugin main, TerraWorld world) {
public SamplerCacheImpl(TerraPlugin main, World world) {
cache = CacheBuilder.newBuilder().maximumSize(main.getTerraConfig().getSamplerCache())
.build(new CacheLoader<>() {
@Override
public Sampler load(@NotNull Long key) {
int cx = (int) (key >> 32);
int cz = (int) key.longValue();
return world.getWorld().getTerraGenerator().createSampler(cx, cz, world.getWorld().getBiomeProvider(), world.getWorld(), world.getConfig().elevationBlend());
return world.getTerraGenerator().createSampler(cx, cz, world.getBiomeProvider(), world, world.getConfig().elevationBlend());
}
});
}

View File

@@ -17,7 +17,7 @@ public class TerraWorldImpl implements TerraWorld {
public TerraWorldImpl(World w, ConfigPack c, TerraPlugin main) {
if(!w.isTerraWorld()) throw new IllegalArgumentException("World " + w + " is not a Terra World!");
this.world = w;
config = (WorldConfigImpl) c.toWorldConfig(this);
config = (WorldConfigImpl) c.toWorldConfig(w);
main.getEventManager().callEvent(new TerraWorldLoadEvent(this, c));
}