directly instantiate biomes

This commit is contained in:
dfsek
2021-07-19 21:29:41 -07:00
parent 04bdd0b4a3
commit 458fc422de
4 changed files with 16 additions and 56 deletions
@@ -9,14 +9,15 @@ import com.dfsek.terra.api.config.ConfigType;
import com.dfsek.terra.api.registry.OpenRegistry; import com.dfsek.terra.api.registry.OpenRegistry;
import com.dfsek.terra.api.util.reflection.TypeKey; import com.dfsek.terra.api.util.reflection.TypeKey;
import com.dfsek.terra.api.util.seeded.SeededTerraBiome; import com.dfsek.terra.api.util.seeded.SeededTerraBiome;
import com.dfsek.terra.api.world.biome.TerraBiome;
import java.util.function.Supplier; import java.util.function.Supplier;
public class BiomeConfigType implements ConfigType<BiomeTemplate, SeededTerraBiome> { public class BiomeConfigType implements ConfigType<BiomeTemplate, TerraBiome> {
private final ConfigPack pack; private final ConfigPack pack;
private final BiomeFactory factory; private final BiomeFactory factory;
public static final TypeKey<SeededTerraBiome> BIOME_TYPE_TOKEN = new TypeKey<>() {}; public static final TypeKey<TerraBiome> BIOME_TYPE_TOKEN = new TypeKey<>() {};
public BiomeConfigType(ConfigPack pack) { public BiomeConfigType(ConfigPack pack) {
this.pack = pack; this.pack = pack;
@@ -29,20 +30,20 @@ public class BiomeConfigType implements ConfigType<BiomeTemplate, SeededTerraBio
} }
@Override @Override
public ConfigFactory<BiomeTemplate, SeededTerraBiome> getFactory() { public ConfigFactory<BiomeTemplate, TerraBiome> getFactory() {
return factory; return factory;
} }
@Override @Override
public TypeKey<SeededTerraBiome> getTypeClass() { public TypeKey<TerraBiome> getTypeClass() {
return BIOME_TYPE_TOKEN; return BIOME_TYPE_TOKEN;
} }
@Override @Override
public Supplier<OpenRegistry<SeededTerraBiome>> registrySupplier() { public Supplier<OpenRegistry<TerraBiome>> registrySupplier() {
return () -> pack.getRegistryFactory().create(registry -> (TypeLoader<SeededTerraBiome>) (t, c, loader) -> { return () -> pack.getRegistryFactory().create(registry -> (TypeLoader<TerraBiome>) (t, c, loader) -> {
if(c.equals("SELF")) return null; if(c.equals("SELF")) return null;
SeededTerraBiome obj = registry.get((String) c); TerraBiome obj = registry.get((String) c);
if(obj == null) if(obj == null)
throw new LoadException("No such " + t.getType().getTypeName() + " matching \"" + c + "\" was found in this registry."); throw new LoadException("No such " + t.getType().getTypeName() + " matching \"" + c + "\" was found in this registry.");
return obj; return obj;
@@ -4,8 +4,9 @@ import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.api.config.ConfigFactory; import com.dfsek.terra.api.config.ConfigFactory;
import com.dfsek.terra.api.config.ConfigPack; import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.util.seeded.SeededTerraBiome; import com.dfsek.terra.api.util.seeded.SeededTerraBiome;
import com.dfsek.terra.api.world.biome.TerraBiome;
public class BiomeFactory implements ConfigFactory<BiomeTemplate, SeededTerraBiome> { public class BiomeFactory implements ConfigFactory<BiomeTemplate, TerraBiome> {
private final ConfigPack pack; private final ConfigPack pack;
public BiomeFactory(ConfigPack pack) { public BiomeFactory(ConfigPack pack) {
@@ -13,7 +14,9 @@ public class BiomeFactory implements ConfigFactory<BiomeTemplate, SeededTerraBio
} }
@Override @Override
public SeededTerraBiome build(BiomeTemplate template, TerraPlugin main) { public TerraBiome build(BiomeTemplate template, TerraPlugin main) {
return new UserDefinedSeededTerraBiome(template); UserDefinedGenerator generator = new UserDefinedGenerator(template.getNoiseEquation(), template.getElevationEquation(), template.getCarvingEquation(), template.getBiomeNoise(), template.getElevationWeight(),
template.getBlendDistance(), template.getBlendStep(), template.getBlendWeight());
return new UserDefinedBiome(template.getVanilla(), generator, template);
} }
} }
@@ -20,16 +20,15 @@ public class UserDefinedBiome implements TerraBiome {
private final int color; private final int color;
private final Set<String> tags; private final Set<String> tags;
private final Context context; private final Context context = new Context();
public UserDefinedBiome(ProbabilityCollection<Biome> vanilla, UserDefinedGenerator gen, BiomeTemplate config, Context context) { public UserDefinedBiome(ProbabilityCollection<Biome> vanilla, UserDefinedGenerator gen, BiomeTemplate config) {
this.vanilla = vanilla; this.vanilla = vanilla;
this.gen = gen; this.gen = gen;
this.id = config.getID(); this.id = config.getID();
this.config = config; this.config = config;
this.color = config.getColor(); this.color = config.getColor();
this.tags = config.getTags(); this.tags = config.getTags();
this.context = context;
tags.add("BIOME:" + id); tags.add("BIOME:" + id);
} }
@@ -1,43 +0,0 @@
package com.dfsek.terra.addons.biome;
import com.dfsek.terra.api.properties.Context;
import com.dfsek.terra.api.util.collection.ProbabilityCollection;
import com.dfsek.terra.api.util.seeded.SeededTerraBiome;
import com.dfsek.terra.api.world.biome.Biome;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class UserDefinedSeededTerraBiome implements SeededTerraBiome {
private final BiomeTemplate template;
private final Context context = new Context();
private final Map<Long, UserDefinedBiome> biomeMap = new ConcurrentHashMap<>();
public UserDefinedSeededTerraBiome(BiomeTemplate template) {
this.template = template;
}
@Override
public UserDefinedBiome build(long seed) {
synchronized(biomeMap) {
return biomeMap.computeIfAbsent(seed,
s -> {
UserDefinedGenerator generator = new UserDefinedGenerator(template.getNoiseEquation(), template.getElevationEquation(), template.getCarvingEquation(), template.getBiomeNoise(), template.getElevationWeight(),
template.getBlendDistance(), template.getBlendStep(), template.getBlendWeight());
return new UserDefinedBiome(template.getVanilla(), generator, template, context);
}
);
}
}
@Override
public ProbabilityCollection<Biome> getVanillaBiomes() {
return template.getVanilla();
}
@Override
public Context getContext() {
return context;
}
}