mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-04 23:06:05 +00:00
finish implementing GenericMetaValueLoader
This commit is contained in:
@@ -1,10 +1,16 @@
|
||||
package com.dfsek.terra.api.config.meta;
|
||||
|
||||
import com.dfsek.tectonic.exception.ConfigException;
|
||||
import com.dfsek.tectonic.exception.LoadException;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* Context from which to pull {@link MetaValue}s
|
||||
*/
|
||||
public interface MetaContext {
|
||||
<T> T load(String meta, Class<T> clazz) throws ConfigException;
|
||||
default <T> T load(String meta, Class<T> clazz) throws LoadException {
|
||||
return load(meta, (Type) clazz);
|
||||
}
|
||||
|
||||
<T> T load(String meta, Type type) throws LoadException;
|
||||
}
|
||||
|
||||
@@ -4,25 +4,26 @@ import com.dfsek.tectonic.exception.LoadException;
|
||||
import com.dfsek.tectonic.loading.ConfigLoader;
|
||||
import com.dfsek.terra.api.config.meta.MetaContext;
|
||||
import com.dfsek.terra.api.config.meta.MetaValue;
|
||||
import com.dfsek.terra.api.util.collections.ProbabilityCollection;
|
||||
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class GenericMetaValueLoader extends MetaValueLoader<MetaValue<Object>, Object> {
|
||||
protected GenericMetaValueLoader(MetaContext context) {
|
||||
public GenericMetaValueLoader(MetaContext context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MetaValue<Object> load(Type type, Object c, ConfigLoader loader) throws LoadException {
|
||||
ProbabilityCollection<Object> collection = new ProbabilityCollection<>();
|
||||
|
||||
if(type instanceof ParameterizedType) {
|
||||
ParameterizedType pType = (ParameterizedType) type;
|
||||
Type generic = pType.getActualTypeArguments()[0];
|
||||
if(c instanceof String) {
|
||||
|
||||
String possibleMeta = (String) c;
|
||||
if(possibleMeta.startsWith("$")) {
|
||||
String meta = possibleMeta.substring(1);
|
||||
return context.load(meta, generic);
|
||||
}
|
||||
}
|
||||
|
||||
return MetaValue.of(loader.loadType(generic, c));
|
||||
|
||||
@@ -10,6 +10,7 @@ import com.dfsek.tectonic.loading.TypeRegistry;
|
||||
import com.dfsek.tectonic.loading.object.ObjectTemplate;
|
||||
import com.dfsek.terra.api.LoaderRegistrar;
|
||||
import com.dfsek.terra.api.TerraPlugin;
|
||||
import com.dfsek.terra.api.config.meta.MetaValue;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigPackPostLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.platform.block.BlockData;
|
||||
@@ -42,6 +43,8 @@ import com.dfsek.terra.config.loaders.config.biome.templates.provider.ImageProvi
|
||||
import com.dfsek.terra.config.loaders.config.biome.templates.provider.SingleBiomeProviderTemplate;
|
||||
import com.dfsek.terra.config.loaders.config.sampler.NoiseSamplerBuilderLoader;
|
||||
import com.dfsek.terra.config.loaders.config.sampler.templates.ImageSamplerTemplate;
|
||||
import com.dfsek.terra.config.loaders.meta.GenericMetaValueLoader;
|
||||
import com.dfsek.terra.config.pack.meta.PackMetaContext;
|
||||
import com.dfsek.terra.config.templates.AbstractableTemplate;
|
||||
import com.dfsek.terra.config.templates.BiomeTemplate;
|
||||
import com.dfsek.terra.config.templates.CarverTemplate;
|
||||
@@ -115,6 +118,8 @@ public class ConfigPack implements LoaderRegistrar {
|
||||
|
||||
private final BiomeProvider.BiomeProviderBuilder biomeProviderBuilder;
|
||||
|
||||
private final PackMetaContext context;
|
||||
|
||||
|
||||
public ConfigPack(File folder, TerraPlugin main) throws ConfigException {
|
||||
try {
|
||||
@@ -124,6 +129,9 @@ public class ConfigPack implements LoaderRegistrar {
|
||||
floraRegistry = new FloraRegistry(main);
|
||||
paletteRegistry = new PaletteRegistry(main);
|
||||
treeRegistry = new TreeRegistry();
|
||||
|
||||
context = new PackMetaContext(loader, selfLoader);
|
||||
|
||||
register(abstractConfigLoader);
|
||||
register(selfLoader);
|
||||
|
||||
@@ -165,6 +173,9 @@ public class ConfigPack implements LoaderRegistrar {
|
||||
floraRegistry = new FloraRegistry(main);
|
||||
paletteRegistry = new PaletteRegistry(main);
|
||||
treeRegistry = new TreeRegistry();
|
||||
|
||||
context = new PackMetaContext(loader, selfLoader);
|
||||
|
||||
register(abstractConfigLoader);
|
||||
register(selfLoader);
|
||||
|
||||
@@ -295,7 +306,8 @@ public class ConfigPack implements LoaderRegistrar {
|
||||
.registerLoader(SingleBiomeProviderTemplate.class, SingleBiomeProviderTemplate::new)
|
||||
.registerLoader(BiomePipelineTemplate.class, () -> new BiomePipelineTemplate(main))
|
||||
.registerLoader(ImageProviderTemplate.class, () -> new ImageProviderTemplate(biomeRegistry))
|
||||
.registerLoader(ImageSamplerTemplate.class, () -> new ImageProviderTemplate(biomeRegistry));
|
||||
.registerLoader(ImageSamplerTemplate.class, () -> new ImageProviderTemplate(biomeRegistry))
|
||||
.registerLoader(MetaValue.class, new GenericMetaValueLoader(context));
|
||||
}
|
||||
|
||||
public Set<UserDefinedCarver> getCarvers() {
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
package com.dfsek.terra.config.pack.meta;
|
||||
|
||||
import com.dfsek.tectonic.config.Configuration;
|
||||
import com.dfsek.tectonic.exception.ConfigException;
|
||||
import com.dfsek.tectonic.exception.LoadException;
|
||||
import com.dfsek.tectonic.loading.ConfigLoader;
|
||||
import com.dfsek.terra.api.config.meta.MetaContext;
|
||||
import com.dfsek.terra.config.fileloaders.Loader;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -21,8 +21,9 @@ public class PackMetaContext implements MetaContext {
|
||||
this.loader = loader;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T> T load(String meta, Class<T> clazz) throws ConfigException {
|
||||
public <T> T load(String meta, Type clazz) throws LoadException {
|
||||
if(meta.indexOf(":") != meta.lastIndexOf(":")) { // We just need to know if there are >1.
|
||||
throw new LoadException("Malformed metavalue string: " + meta);
|
||||
}
|
||||
@@ -42,6 +43,6 @@ public class PackMetaContext implements MetaContext {
|
||||
throw new LoadException("Failed to load config file \"" + file + "\":", e);
|
||||
}
|
||||
|
||||
return loader.loadClass(clazz, configuration.get(key));
|
||||
return (T) loader.loadType(clazz, configuration.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user