mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2025-07-01 23:47:50 +00:00
commit
5ea08eb898
@ -13,13 +13,11 @@ import com.dfsek.terra.api.world.biome.TerraBiome;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class BiomeConfigType implements ConfigType<BiomeTemplate, TerraBiome> {
|
||||
private final ConfigPack pack;
|
||||
private final BiomeFactory factory;
|
||||
|
||||
public static final TypeKey<TerraBiome> BIOME_TYPE_TOKEN = new TypeKey<>() {};
|
||||
|
||||
public BiomeConfigType(ConfigPack pack) {
|
||||
this.pack = pack;
|
||||
this.factory = new BiomeFactory(pack);
|
||||
}
|
||||
|
||||
@ -39,7 +37,7 @@ public class BiomeConfigType implements ConfigType<BiomeTemplate, TerraBiome> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Supplier<OpenRegistry<TerraBiome>> registrySupplier() {
|
||||
public Supplier<OpenRegistry<TerraBiome>> registrySupplier(ConfigPack pack) {
|
||||
return () -> pack.getRegistryFactory().create(registry -> (TypeLoader<TerraBiome>) (t, c, loader) -> {
|
||||
if(c.equals("SELF")) return null;
|
||||
TerraBiome obj = registry.get((String) c);
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.dfsek.terra.addons.feature.distributor.config;
|
||||
|
||||
import com.dfsek.tectonic.annotations.Default;
|
||||
import com.dfsek.tectonic.annotations.Value;
|
||||
import com.dfsek.tectonic.loading.object.ObjectTemplate;
|
||||
import com.dfsek.terra.addons.feature.distributor.distributors.NoiseDistributor;
|
||||
@ -10,8 +11,12 @@ public class NoiseDistributorTemplate implements ObjectTemplate<Distributor> {
|
||||
@Value("distribution")
|
||||
private NoiseSampler noise;
|
||||
|
||||
@Value("threshold")
|
||||
@Default
|
||||
private double threshold = 0;
|
||||
|
||||
@Override
|
||||
public Distributor get() {
|
||||
return new NoiseDistributor(noise);
|
||||
return new NoiseDistributor(noise, threshold);
|
||||
}
|
||||
}
|
||||
|
@ -6,12 +6,14 @@ import com.dfsek.terra.api.structure.feature.Distributor;
|
||||
public class NoiseDistributor implements Distributor {
|
||||
private final NoiseSampler sampler;
|
||||
|
||||
public NoiseDistributor(NoiseSampler sampler) {
|
||||
private final double threshold;
|
||||
public NoiseDistributor(NoiseSampler sampler, double threshold) {
|
||||
this.sampler = sampler;
|
||||
this.threshold = threshold;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(int x, int z, long seed) {
|
||||
return sampler.getNoiseSeeded(seed, x, z) > 0;
|
||||
return sampler.getNoiseSeeded(seed, x, z) > threshold;
|
||||
}
|
||||
}
|
||||
|
45
common/addons/config-feature/build.gradle.kts
Normal file
45
common/addons/config-feature/build.gradle.kts
Normal file
@ -0,0 +1,45 @@
|
||||
import com.dfsek.terra.configureCompilation
|
||||
import com.dfsek.terra.configureDependencies
|
||||
|
||||
plugins {
|
||||
`java-library`
|
||||
`maven-publish`
|
||||
idea
|
||||
}
|
||||
|
||||
configureCompilation()
|
||||
configureDependencies()
|
||||
|
||||
group = "com.dfsek.terra.common"
|
||||
|
||||
dependencies {
|
||||
"shadedApi"(project(":common:api"))
|
||||
"compileOnly"("com.google.guava:guava:30.0-jre")
|
||||
|
||||
"testImplementation"("com.google.guava:guava:30.0-jre")
|
||||
}
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
create<MavenPublication>("mavenJava") {
|
||||
artifact(tasks["sourcesJar"])
|
||||
artifact(tasks["jar"])
|
||||
}
|
||||
}
|
||||
|
||||
repositories {
|
||||
val mavenUrl = "https://repo.codemc.io/repository/maven-releases/"
|
||||
val mavenSnapshotUrl = "https://repo.codemc.io/repository/maven-snapshots/"
|
||||
|
||||
maven(mavenUrl) {
|
||||
val mavenUsername: String? by project
|
||||
val mavenPassword: String? by project
|
||||
if (mavenUsername != null && mavenPassword != null) {
|
||||
credentials {
|
||||
username = mavenUsername
|
||||
password = mavenPassword
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.dfsek.terra.addons.feature;
|
||||
|
||||
import com.dfsek.terra.api.noise.NoiseSampler;
|
||||
import com.dfsek.terra.api.structure.Structure;
|
||||
import com.dfsek.terra.api.structure.feature.Distributor;
|
||||
import com.dfsek.terra.api.structure.feature.Feature;
|
||||
import com.dfsek.terra.api.structure.feature.Locator;
|
||||
import com.dfsek.terra.api.util.collection.ProbabilityCollection;
|
||||
import com.dfsek.terra.api.world.World;
|
||||
|
||||
public class ConfiguredFeature implements Feature {
|
||||
private final ProbabilityCollection<Structure> structures;
|
||||
|
||||
private final NoiseSampler structureSelector;
|
||||
private final Distributor distributor;
|
||||
private final Locator locator;
|
||||
|
||||
public ConfiguredFeature(ProbabilityCollection<Structure> structures, NoiseSampler structureSelector, Distributor distributor, Locator locator) {
|
||||
this.structures = structures;
|
||||
this.structureSelector = structureSelector;
|
||||
this.distributor = distributor;
|
||||
this.locator = locator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Structure getStructure(World world, int x, int y, int z) {
|
||||
return structures.get(structureSelector, x, y, z, world.getSeed());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Distributor getDistributor() {
|
||||
return distributor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Locator getLocator() {
|
||||
return locator;
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.dfsek.terra.addons.feature;
|
||||
|
||||
import com.dfsek.terra.api.TerraPlugin;
|
||||
import com.dfsek.terra.api.addon.TerraAddon;
|
||||
import com.dfsek.terra.api.addon.annotations.Addon;
|
||||
import com.dfsek.terra.api.addon.annotations.Author;
|
||||
import com.dfsek.terra.api.addon.annotations.Version;
|
||||
import com.dfsek.terra.api.event.EventListener;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.injection.annotations.Inject;
|
||||
|
||||
@Addon("config-feature")
|
||||
@Version("1.0.0")
|
||||
@Author("Terra")
|
||||
public class FeatureAddon extends TerraAddon implements EventListener {
|
||||
@Inject
|
||||
private TerraPlugin main;
|
||||
|
||||
@Override
|
||||
public void initialize() {
|
||||
main.getEventManager().registerListener(this, this);
|
||||
}
|
||||
|
||||
public void onPackLoad(ConfigPackPreLoadEvent event) {
|
||||
event.getPack().registerConfigType(new FeatureConfigType(), "FEATURE", 2);
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.dfsek.terra.addons.feature;
|
||||
|
||||
import com.dfsek.terra.api.TerraPlugin;
|
||||
import com.dfsek.terra.api.config.ConfigFactory;
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.config.ConfigType;
|
||||
import com.dfsek.terra.api.registry.OpenRegistry;
|
||||
import com.dfsek.terra.api.structure.feature.Feature;
|
||||
import com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class FeatureConfigType implements ConfigType<FeatureTemplate, Feature> {
|
||||
public static final TypeKey<Feature> FEATURE_TYPE_KEY = new TypeKey<>() {};
|
||||
|
||||
private final FeatureFactory factory = new FeatureFactory();
|
||||
|
||||
@Override
|
||||
public FeatureTemplate getTemplate(ConfigPack pack, TerraPlugin main) {
|
||||
return new FeatureTemplate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConfigFactory<FeatureTemplate, Feature> getFactory() {
|
||||
return factory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeKey<Feature> getTypeClass() {
|
||||
return FEATURE_TYPE_KEY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Supplier<OpenRegistry<Feature>> registrySupplier(ConfigPack pack) {
|
||||
return pack.getRegistryFactory()::create;
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.dfsek.terra.addons.feature;
|
||||
|
||||
import com.dfsek.tectonic.exception.LoadException;
|
||||
import com.dfsek.terra.api.TerraPlugin;
|
||||
import com.dfsek.terra.api.config.ConfigFactory;
|
||||
import com.dfsek.terra.api.structure.feature.Feature;
|
||||
|
||||
public class FeatureFactory implements ConfigFactory<FeatureTemplate, Feature> {
|
||||
@Override
|
||||
public Feature build(FeatureTemplate config, TerraPlugin main) throws LoadException {
|
||||
return new ConfiguredFeature(config.getStructures(), config.getStructureNoise(), config.getDistributor(), config.getLocator());
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package com.dfsek.terra.addons.feature;
|
||||
|
||||
import com.dfsek.tectonic.annotations.Final;
|
||||
import com.dfsek.tectonic.annotations.Value;
|
||||
import com.dfsek.tectonic.config.ConfigTemplate;
|
||||
import com.dfsek.terra.api.config.AbstractableTemplate;
|
||||
import com.dfsek.terra.api.noise.NoiseSampler;
|
||||
import com.dfsek.terra.api.structure.Structure;
|
||||
import com.dfsek.terra.api.structure.feature.Distributor;
|
||||
import com.dfsek.terra.api.structure.feature.Locator;
|
||||
import com.dfsek.terra.api.util.collection.ProbabilityCollection;
|
||||
|
||||
public class FeatureTemplate implements AbstractableTemplate {
|
||||
@Value("id")
|
||||
@Final
|
||||
private String id;
|
||||
|
||||
@Value("distributor")
|
||||
private Distributor distributor;
|
||||
|
||||
@Value("locator")
|
||||
private Locator locator;
|
||||
|
||||
@Value("structures.distribution")
|
||||
private NoiseSampler structureNoise;
|
||||
|
||||
@Value("structures.structures")
|
||||
private ProbabilityCollection<Structure> structures;
|
||||
|
||||
@Override
|
||||
public String getID() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Distributor getDistributor() {
|
||||
return distributor;
|
||||
}
|
||||
|
||||
public Locator getLocator() {
|
||||
return locator;
|
||||
}
|
||||
|
||||
public NoiseSampler getStructureNoise() {
|
||||
return structureNoise;
|
||||
}
|
||||
|
||||
public ProbabilityCollection<Structure> getStructures() {
|
||||
return structures;
|
||||
}
|
||||
}
|
@ -25,7 +25,7 @@ public class FloraAddon extends TerraAddon implements EventListener {
|
||||
}
|
||||
|
||||
public void onPackLoad(ConfigPackPreLoadEvent event) throws DuplicateEntryException {
|
||||
event.getPack().registerConfigType(new FloraConfigType(event.getPack()), "FLORA", 2);
|
||||
event.getPack().registerConfigType(new FloraConfigType(), "FLORA", 2);
|
||||
event.getPack().applyLoader(BlockLayer.class, BlockLayerTemplate::new);
|
||||
}
|
||||
}
|
||||
|
@ -12,14 +12,9 @@ import java.util.function.Supplier;
|
||||
|
||||
public class FloraConfigType implements ConfigType<FloraTemplate, Flora> {
|
||||
private final FloraFactory factory = new FloraFactory();
|
||||
private final ConfigPack pack;
|
||||
|
||||
public static final TypeKey<Flora> FLORA_TYPE_TOKEN = new TypeKey<>(){};
|
||||
|
||||
public FloraConfigType(ConfigPack pack) {
|
||||
this.pack = pack;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FloraTemplate getTemplate(ConfigPack pack, TerraPlugin main) {
|
||||
return new FloraTemplate();
|
||||
@ -36,7 +31,7 @@ public class FloraConfigType implements ConfigType<FloraTemplate, Flora> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Supplier<OpenRegistry<Flora>> registrySupplier() {
|
||||
public Supplier<OpenRegistry<Flora>> registrySupplier(ConfigPack pack) {
|
||||
return pack.getRegistryFactory()::create;
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ package com.dfsek.terra.addons.noise;
|
||||
|
||||
import com.dfsek.tectonic.loading.object.ObjectTemplate;
|
||||
import com.dfsek.terra.addons.noise.config.DimensionApplicableNoiseSampler;
|
||||
import com.dfsek.terra.addons.noise.config.NoiseSamplerBuilderLoader;
|
||||
import com.dfsek.terra.addons.noise.config.templates.DomainWarpTemplate;
|
||||
import com.dfsek.terra.addons.noise.config.templates.ImageSamplerTemplate;
|
||||
import com.dfsek.terra.addons.noise.config.templates.KernelTemplate;
|
||||
@ -59,14 +58,8 @@ public class NoiseAddon extends TerraAddon implements EventListener {
|
||||
public void packPreLoad(ConfigPackPreLoadEvent event) {
|
||||
CheckedRegistry<Supplier<ObjectTemplate<NoiseSampler>>> noiseRegistry = event.getPack().getOrCreateRegistry(NOISE_SAMPLER_TOKEN);
|
||||
event.getPack()
|
||||
.applyLoader(NoiseSampler.class, new NoiseSamplerBuilderLoader(noiseRegistry))
|
||||
.applyLoader(ImageSamplerTemplate.class, ImageSamplerTemplate::new)
|
||||
.applyLoader(CellularSampler.DistanceFunction.class, (t, o, l) -> CellularSampler.DistanceFunction.valueOf((String) o))
|
||||
.applyLoader(CellularSampler.ReturnType.class, (t, o, l) -> CellularSampler.ReturnType.valueOf((String) o))
|
||||
.applyLoader(DomainWarpTemplate.class, DomainWarpTemplate::new)
|
||||
.applyLoader(LinearNormalizerTemplate.class, LinearNormalizerTemplate::new)
|
||||
.applyLoader(NormalNormalizerTemplate.class, NormalNormalizerTemplate::new)
|
||||
.applyLoader(ClampNormalizerTemplate.class, ClampNormalizerTemplate::new)
|
||||
.applyLoader(DimensionApplicableNoiseSampler.class, DimensionApplicableNoiseSampler::new);
|
||||
|
||||
noiseRegistry.register("LINEAR", LinearNormalizerTemplate::new);
|
||||
|
@ -1,38 +0,0 @@
|
||||
package com.dfsek.terra.addons.noise.config;
|
||||
|
||||
import com.dfsek.tectonic.config.MapConfiguration;
|
||||
import com.dfsek.tectonic.exception.ConfigException;
|
||||
import com.dfsek.tectonic.exception.LoadException;
|
||||
import com.dfsek.tectonic.loading.ConfigLoader;
|
||||
import com.dfsek.tectonic.loading.TypeLoader;
|
||||
import com.dfsek.tectonic.loading.object.ObjectTemplate;
|
||||
import com.dfsek.terra.api.noise.NoiseSampler;
|
||||
import com.dfsek.terra.api.registry.Registry;
|
||||
|
||||
import java.lang.reflect.AnnotatedType;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public class NoiseSamplerBuilderLoader implements TypeLoader<NoiseSampler> {
|
||||
private final Registry<Supplier<ObjectTemplate<NoiseSampler>>> noiseRegistry;
|
||||
|
||||
public NoiseSamplerBuilderLoader(Registry<Supplier<ObjectTemplate<NoiseSampler>>> noiseRegistry) {
|
||||
this.noiseRegistry = noiseRegistry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NoiseSampler load(AnnotatedType t, Object c, ConfigLoader loader) throws LoadException {
|
||||
Map<String, Object> map = (Map<String, Object>) c;
|
||||
try {
|
||||
if(!noiseRegistry.contains((String) map.get("type"))) {
|
||||
throw new LoadException("No such noise function: " + map.get("type"));
|
||||
}
|
||||
ObjectTemplate<NoiseSampler> normalizerTemplate = noiseRegistry.get(((String) map.get("type"))).get();
|
||||
loader.load(normalizerTemplate, new MapConfiguration(map));
|
||||
return normalizerTemplate.get();
|
||||
} catch(ConfigException e) {
|
||||
throw new LoadException("Unable to load noise function: ", e);
|
||||
}
|
||||
}
|
||||
}
|
@ -33,6 +33,7 @@ public class CellularNoiseTemplate extends NoiseTemplate<CellularSampler> {
|
||||
sampler.setJitterModifier(cellularJitter);
|
||||
sampler.setReturnType(cellularReturnType);
|
||||
sampler.setDistanceFunction(cellularDistanceFunction);
|
||||
sampler.setSalt(salt);
|
||||
return sampler;
|
||||
}
|
||||
}
|
||||
|
@ -35,6 +35,7 @@ public class GaborNoiseTemplate extends NoiseTemplate<GaborNoiseSampler> {
|
||||
gaborNoiseSampler.setDeviation(deviation);
|
||||
gaborNoiseSampler.setImpulsesPerKernel(impulses);
|
||||
gaborNoiseSampler.setFrequency0(f0);
|
||||
gaborNoiseSampler.setSalt(salt);
|
||||
return gaborNoiseSampler;
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import com.dfsek.tectonic.annotations.Default;
|
||||
import com.dfsek.tectonic.annotations.Value;
|
||||
import com.dfsek.terra.addons.noise.config.templates.SamplerTemplate;
|
||||
import com.dfsek.terra.addons.noise.samplers.noise.NoiseFunction;
|
||||
import com.dfsek.terra.api.noise.NoiseSampler;
|
||||
|
||||
@SuppressWarnings({"unused", "FieldMayBeFinal"})
|
||||
public abstract class NoiseTemplate<T extends NoiseFunction> extends SamplerTemplate<T> {
|
||||
@ -13,5 +14,5 @@ public abstract class NoiseTemplate<T extends NoiseFunction> extends SamplerTemp
|
||||
|
||||
@Value("salt")
|
||||
@Default
|
||||
protected int salt = 0;
|
||||
protected long salt = 0;
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ public class SimpleNoiseTemplate extends NoiseTemplate<NoiseFunction> {
|
||||
public NoiseSampler get() {
|
||||
NoiseFunction sampler = samplerSupplier.get();
|
||||
sampler.setFrequency(frequency);
|
||||
sampler.setSalt(salt);
|
||||
return sampler;
|
||||
}
|
||||
}
|
||||
|
@ -21,6 +21,16 @@ public abstract class NoiseFunction implements NoiseSampler {
|
||||
|
||||
protected double frequency = 0.02d;
|
||||
|
||||
protected long salt;
|
||||
|
||||
public void setSalt(long salt) {
|
||||
this.salt = salt;
|
||||
}
|
||||
|
||||
public NoiseFunction() {
|
||||
this.salt = 0;
|
||||
}
|
||||
|
||||
protected static int fastFloor(double f) {
|
||||
return f >= 0 ? (int) f : (int) f - 1;
|
||||
}
|
||||
@ -119,12 +129,12 @@ public abstract class NoiseFunction implements NoiseSampler {
|
||||
|
||||
@Override
|
||||
public double getNoiseSeeded(long seed, double x, double y) {
|
||||
return getNoiseRaw(seed, x * frequency, y * frequency);
|
||||
return getNoiseRaw(seed + salt, x * frequency, y * frequency);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getNoiseSeeded(long seed, double x, double y, double z) {
|
||||
return getNoiseRaw(seed, x * frequency, y * frequency, z * frequency);
|
||||
return getNoiseRaw(seed + salt, x * frequency, y * frequency, z * frequency);
|
||||
}
|
||||
|
||||
public abstract double getNoiseRaw(long seed, double x, double y);
|
||||
|
@ -25,7 +25,7 @@ public class OreAddon extends TerraAddon implements EventListener {
|
||||
}
|
||||
|
||||
public void onPackLoad(ConfigPackPreLoadEvent event) throws DuplicateEntryException {
|
||||
event.getPack().registerConfigType(new OreConfigType(event.getPack()), "ORE", 1);
|
||||
event.getPack().registerConfigType(new OreConfigType(), "ORE", 1);
|
||||
event.getPack().getOrCreateRegistry(GenerationStageProvider.class).register("ORE", pack -> new OrePopulator(main));
|
||||
}
|
||||
}
|
||||
|
@ -12,13 +12,8 @@ import java.util.function.Supplier;
|
||||
|
||||
public class OreConfigType implements ConfigType<OreTemplate, Ore> {
|
||||
private final OreFactory factory = new OreFactory();
|
||||
private final ConfigPack pack;
|
||||
public static final TypeKey<Ore> ORE_TYPE_TOKEN = new TypeKey<>(){};
|
||||
|
||||
public OreConfigType(ConfigPack pack) {
|
||||
this.pack = pack;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OreTemplate getTemplate(ConfigPack pack, TerraPlugin main) {
|
||||
return new OreTemplate();
|
||||
@ -35,7 +30,7 @@ public class OreConfigType implements ConfigType<OreTemplate, Ore> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Supplier<OpenRegistry<Ore>> registrySupplier() {
|
||||
public Supplier<OpenRegistry<Ore>> registrySupplier(ConfigPack pack) {
|
||||
return pack.getRegistryFactory()::create;
|
||||
}
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ public class PaletteAddon extends TerraAddon implements EventListener {
|
||||
}
|
||||
|
||||
public void onPackLoad(ConfigPackPreLoadEvent event) {
|
||||
event.getPack().registerConfigType(new PaletteConfigType(event.getPack(), main), "PALETTE", 2);
|
||||
event.getPack().registerConfigType(new PaletteConfigType(main), "PALETTE", 2);
|
||||
event.getPack().applyLoader(PaletteLayerHolder.class, new PaletteLayerLoader());
|
||||
}
|
||||
}
|
||||
|
@ -15,13 +15,11 @@ import java.util.function.Supplier;
|
||||
|
||||
public class PaletteConfigType implements ConfigType<PaletteTemplate, Palette> {
|
||||
private final PaletteFactory factory = new PaletteFactory();
|
||||
private final ConfigPack pack;
|
||||
private final TerraPlugin main;
|
||||
|
||||
public static final TypeKey<Palette> PALETTE_TYPE_TOKEN = new TypeKey<>(){};
|
||||
|
||||
public PaletteConfigType(ConfigPack pack, TerraPlugin main) {
|
||||
this.pack = pack;
|
||||
public PaletteConfigType(TerraPlugin main) {
|
||||
this.main = main;
|
||||
}
|
||||
|
||||
@ -41,7 +39,7 @@ public class PaletteConfigType implements ConfigType<PaletteTemplate, Palette> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Supplier<OpenRegistry<Palette>> registrySupplier() {
|
||||
public Supplier<OpenRegistry<Palette>> registrySupplier(ConfigPack pack) {
|
||||
return () -> pack.getRegistryFactory().create(registry -> (TypeLoader<Palette>) (t, c, loader) -> {
|
||||
if(((String) c).startsWith("BLOCK:"))
|
||||
return new PaletteImpl.Singleton(main.getWorldHandle().createBlockData(((String) c).substring(6))); // Return single palette for BLOCK: shortcut.
|
||||
|
@ -23,6 +23,6 @@ public class TreeAddon extends TerraAddon implements EventListener {
|
||||
}
|
||||
|
||||
public void onPackLoad(ConfigPackPreLoadEvent event) throws DuplicateEntryException {
|
||||
event.getPack().registerConfigType(new TreeConfigType(event.getPack()), "TREE", 2);
|
||||
event.getPack().registerConfigType(new TreeConfigType(), "TREE", 2);
|
||||
}
|
||||
}
|
||||
|
@ -12,12 +12,8 @@ import java.util.function.Supplier;
|
||||
|
||||
public class TreeConfigType implements ConfigType<TreeTemplate, Tree> {
|
||||
private final TreeFactory factory = new TreeFactory();
|
||||
private final ConfigPack pack;
|
||||
|
||||
public static final TypeKey<Tree> TREE_TYPE_TOKEN = new TypeKey<>(){};
|
||||
public TreeConfigType(ConfigPack pack) {
|
||||
this.pack = pack;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TreeTemplate getTemplate(ConfigPack pack, TerraPlugin main) {
|
||||
@ -35,7 +31,7 @@ public class TreeConfigType implements ConfigType<TreeTemplate, Tree> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Supplier<OpenRegistry<Tree>> registrySupplier() {
|
||||
public Supplier<OpenRegistry<Tree>> registrySupplier(ConfigPack pack) {
|
||||
return pack.getRegistryFactory()::create;
|
||||
}
|
||||
}
|
||||
|
@ -13,5 +13,5 @@ public interface ConfigType<T extends AbstractableTemplate, R> {
|
||||
|
||||
TypeKey<R> getTypeClass();
|
||||
|
||||
Supplier<OpenRegistry<R>> registrySupplier();
|
||||
Supplier<OpenRegistry<R>> registrySupplier(ConfigPack pack);
|
||||
}
|
||||
|
@ -0,0 +1,38 @@
|
||||
package com.dfsek.terra.config.loaders;
|
||||
|
||||
import com.dfsek.tectonic.config.MapConfiguration;
|
||||
import com.dfsek.tectonic.exception.ConfigException;
|
||||
import com.dfsek.tectonic.exception.LoadException;
|
||||
import com.dfsek.tectonic.loading.ConfigLoader;
|
||||
import com.dfsek.tectonic.loading.TypeLoader;
|
||||
import com.dfsek.tectonic.loading.object.ObjectTemplate;
|
||||
import com.dfsek.terra.api.noise.NoiseSampler;
|
||||
import com.dfsek.terra.api.registry.Registry;
|
||||
|
||||
import java.lang.reflect.AnnotatedType;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class GenericTemplateSupplierLoader<T> implements TypeLoader<T> {
|
||||
private final Registry<Supplier<ObjectTemplate<T>>> registry;
|
||||
|
||||
public GenericTemplateSupplierLoader(Registry<Supplier<ObjectTemplate<T>>> registry) {
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public T load(AnnotatedType t, Object c, ConfigLoader loader) throws LoadException {
|
||||
Map<String, Object> map = (Map<String, Object>) c;
|
||||
try {
|
||||
if(!registry.contains((String) map.get("type"))) {
|
||||
throw new LoadException("No such entry: " + map.get("type"));
|
||||
}
|
||||
ObjectTemplate<T> template = registry.get(((String) map.get("type"))).get();
|
||||
loader.load(template, new MapConfiguration(map));
|
||||
return template.get();
|
||||
} catch(ConfigException e) {
|
||||
throw new LoadException("Unable to load object: ", e);
|
||||
}
|
||||
}
|
||||
}
|
@ -18,18 +18,19 @@ import com.dfsek.terra.api.config.ConfigFactory;
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.config.ConfigType;
|
||||
import com.dfsek.terra.api.config.Loader;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigurationDiscoveryEvent;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigurationLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPostLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigurationDiscoveryEvent;
|
||||
import com.dfsek.terra.api.event.events.config.type.ConfigTypePostLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.type.ConfigTypePreLoadEvent;
|
||||
import com.dfsek.terra.api.registry.CheckedRegistry;
|
||||
import com.dfsek.terra.api.registry.OpenRegistry;
|
||||
import com.dfsek.terra.api.registry.Registry;
|
||||
import com.dfsek.terra.api.registry.exception.DuplicateEntryException;
|
||||
import com.dfsek.terra.api.registry.meta.RegistryFactory;
|
||||
import com.dfsek.terra.api.util.reflection.ReflectionUtil;
|
||||
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.biome.generation.BiomeProvider;
|
||||
import com.dfsek.terra.api.world.generator.ChunkGeneratorProvider;
|
||||
@ -37,6 +38,7 @@ import com.dfsek.terra.api.world.generator.GenerationStageProvider;
|
||||
import com.dfsek.terra.config.dummy.DummyWorld;
|
||||
import com.dfsek.terra.config.fileloaders.FolderLoader;
|
||||
import com.dfsek.terra.config.fileloaders.ZIPLoader;
|
||||
import com.dfsek.terra.config.loaders.GenericTemplateSupplierLoader;
|
||||
import com.dfsek.terra.config.loaders.config.BufferedImageLoader;
|
||||
import com.dfsek.terra.config.prototype.ProtoConfig;
|
||||
import com.dfsek.terra.registry.CheckedRegistryImpl;
|
||||
@ -50,6 +52,7 @@ import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
@ -191,7 +194,7 @@ public class ConfigPackImpl implements ConfigPack {
|
||||
@SuppressWarnings("unchecked")
|
||||
private ConfigTypeRegistry createRegistry() {
|
||||
return new ConfigTypeRegistry(main, (id, configType) -> {
|
||||
OpenRegistry<?> openRegistry = configType.registrySupplier().get();
|
||||
OpenRegistry<?> openRegistry = configType.registrySupplier(this).get();
|
||||
if(registryMap.containsKey(configType.getTypeClass().getType())) { // Someone already registered something; we need to copy things to the new registry.
|
||||
registryMap.get(configType.getTypeClass().getType()).getLeft().forEach(((OpenRegistry<Object>) openRegistry)::register);
|
||||
}
|
||||
@ -304,6 +307,28 @@ public class ConfigPackImpl implements ConfigPack {
|
||||
selfLoader.registerLoader(c, registry);
|
||||
abstractConfigLoader.registerLoader(c, registry);
|
||||
main.getDebugLogger().info("Registered loader for registry of class " + ReflectionUtil.typeToString(c));
|
||||
|
||||
if(type instanceof ParameterizedType) {
|
||||
ParameterizedType param = (ParameterizedType) type;
|
||||
Type base = param.getRawType();
|
||||
if(base instanceof Class // should always be true but we'll check anyways
|
||||
&& Supplier.class.isAssignableFrom((Class<?>) base)) { // If it's a supplier
|
||||
Type supplied = param.getActualTypeArguments()[0]; // Grab the supplied type
|
||||
if(supplied instanceof ParameterizedType) {
|
||||
ParameterizedType suppliedParam = (ParameterizedType) supplied;
|
||||
Type suppliedBase = suppliedParam.getRawType();
|
||||
if(suppliedBase instanceof Class // should always be true but we'll check anyways
|
||||
&& ObjectTemplate.class.isAssignableFrom((Class<?>) suppliedBase)) {
|
||||
Type templateType = suppliedParam.getActualTypeArguments()[0];
|
||||
GenericTemplateSupplierLoader<?> loader = new GenericTemplateSupplierLoader<>((Registry<Supplier<ObjectTemplate<Supplier<ObjectTemplate<?>>>>>) registry);
|
||||
selfLoader.registerLoader(templateType, loader);
|
||||
abstractConfigLoader.registerLoader(templateType, loader);
|
||||
main.getDebugLogger().info("Registered template loader for registry of class " + ReflectionUtil.typeToString(templateType));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ImmutablePair.of(registry, new CheckedRegistryImpl<>(registry));
|
||||
}).getRight();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user