mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-02-16 10:30:42 +00:00
working biome config addon
This commit is contained in:
@@ -34,6 +34,10 @@ dependencies {
|
||||
"testImplementation"("com.google.guava:guava:30.0-jre")
|
||||
}
|
||||
|
||||
tasks.named<Jar>("jar") {
|
||||
archiveBaseName.set("Terra-biome")
|
||||
}
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
create<MavenPublication>("mavenJava") {
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.dfsek.terra.addons.biome;
|
||||
|
||||
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.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.injection.annotations.Inject;
|
||||
|
||||
@Addon("core-biome-config")
|
||||
@Author("Terra")
|
||||
@Version("1.0.0")
|
||||
public class BiomeConfigAddon 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 BiomeConfigType(event.getPack()), "BIOME", 5);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.dfsek.terra.addons.biome;
|
||||
|
||||
import com.dfsek.tectonic.exception.LoadException;
|
||||
import com.dfsek.tectonic.loading.ConfigLoader;
|
||||
import com.dfsek.tectonic.loading.TypeLoader;
|
||||
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.util.seeded.BiomeBuilder;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class BiomeConfigType implements ConfigType<BiomeTemplate, BiomeBuilder> {
|
||||
private final ConfigPack pack;
|
||||
private final BiomeFactory factory;
|
||||
|
||||
public BiomeConfigType(ConfigPack pack) {
|
||||
this.pack = pack;
|
||||
this.factory = new BiomeFactory(pack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BiomeTemplate getTemplate(ConfigPack pack, TerraPlugin main) {
|
||||
return new BiomeTemplate(pack, main);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConfigFactory<BiomeTemplate, BiomeBuilder> getFactory() {
|
||||
return factory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<BiomeBuilder> getTypeClass() {
|
||||
return BiomeBuilder.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Supplier<OpenRegistry<BiomeBuilder>> registrySupplier() {
|
||||
return () -> pack.getRegistryFactory().create(registry -> (TypeLoader<BiomeBuilder>) (t, c, loader) -> {
|
||||
if(c.equals("SELF")) return null;
|
||||
return registry.load(t, c, loader);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.dfsek.terra.api.config;
|
||||
|
||||
import com.dfsek.terra.api.registry.meta.RegistryFactory;
|
||||
import com.dfsek.terra.api.registry.meta.RegistryHolder;
|
||||
import com.dfsek.terra.api.tectonic.LoaderHolder;
|
||||
import com.dfsek.terra.api.tectonic.LoaderRegistrar;
|
||||
@@ -41,4 +42,6 @@ public interface ConfigPack extends LoaderRegistrar, LoaderHolder, RegistryHolde
|
||||
boolean doBetaCarvers();
|
||||
|
||||
boolean vanillaFlora();
|
||||
|
||||
RegistryFactory getRegistryFactory();
|
||||
}
|
||||
|
||||
@@ -7,10 +7,10 @@ import com.dfsek.terra.api.registry.OpenRegistry;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public interface ConfigType<T extends ConfigTemplate, R> {
|
||||
public interface ConfigType<T extends AbstractableTemplate, R> {
|
||||
T getTemplate(ConfigPack pack, TerraPlugin main);
|
||||
|
||||
void callback(ConfigPack pack, TerraPlugin main, T loadedConfig) throws LoadException;
|
||||
ConfigFactory<T, R> getFactory();
|
||||
|
||||
Class<R> getTypeClass();
|
||||
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.dfsek.terra.api.registry.meta;
|
||||
|
||||
import com.dfsek.tectonic.loading.TypeLoader;
|
||||
import com.dfsek.terra.api.registry.OpenRegistry;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Helpers to avoid creating entire registry implementations for simple overrides.
|
||||
*/
|
||||
public interface RegistryFactory {
|
||||
/**
|
||||
* Create a generic OpenRegistry.
|
||||
* @param <T> Type of registry.
|
||||
* @return New OpenRegistry
|
||||
*/
|
||||
<T> OpenRegistry<T> create();
|
||||
|
||||
/**
|
||||
* Create an OpenRegistry with custom {@link TypeLoader}
|
||||
* @param loader Function to create loader.
|
||||
* @param <T> Type of registry.
|
||||
* @return New OpenRegistry.
|
||||
*/
|
||||
<T> OpenRegistry<T> create(Function<OpenRegistry<T>, TypeLoader<T>> loader);
|
||||
}
|
||||
@@ -32,6 +32,7 @@ public class FolderLoader extends Loader {
|
||||
paths.filter(Files::isRegularFile).filter(file -> file.toString().toLowerCase().endsWith(extension)).forEach(file -> {
|
||||
try {
|
||||
String rel = newPath.toPath().relativize(file).toString();
|
||||
if(rel.equals("pack.yml")) return;
|
||||
streams.put(rel, new FileInputStream(file.toFile()));
|
||||
} catch(FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
@@ -28,6 +28,7 @@ public class ZIPLoader extends Loader {
|
||||
Enumeration<? extends ZipEntry> entries = file.entries();
|
||||
while(entries.hasMoreElements()) {
|
||||
ZipEntry entry = entries.nextElement();
|
||||
if(entry.getName().equals("pack.yml")) continue;
|
||||
if(!entry.isDirectory() && entry.getName().startsWith(directory) && entry.getName().endsWith(extension)) {
|
||||
try {
|
||||
String rel = entry.getName().substring(directory.length());
|
||||
|
||||
@@ -13,12 +13,16 @@ import com.dfsek.tectonic.loading.TypeRegistry;
|
||||
import com.dfsek.tectonic.loading.object.ObjectTemplate;
|
||||
import com.dfsek.terra.api.TerraPlugin;
|
||||
import com.dfsek.terra.api.addon.TerraAddon;
|
||||
import com.dfsek.terra.api.config.AbstractableTemplate;
|
||||
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.event.events.config.ConfigPackPostLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.registry.CheckedRegistry;
|
||||
import com.dfsek.terra.api.registry.OpenRegistry;
|
||||
import com.dfsek.terra.api.registry.exception.DuplicateEntryException;
|
||||
import com.dfsek.terra.api.registry.meta.RegistryFactory;
|
||||
import com.dfsek.terra.api.structure.LootTable;
|
||||
import com.dfsek.terra.api.structure.Structure;
|
||||
import com.dfsek.terra.api.util.generic.pair.ImmutablePair;
|
||||
@@ -33,6 +37,7 @@ import com.dfsek.terra.config.loaders.config.BufferedImageLoader;
|
||||
import com.dfsek.terra.config.prototype.ProtoConfig;
|
||||
import com.dfsek.terra.registry.CheckedRegistryImpl;
|
||||
import com.dfsek.terra.registry.OpenRegistryImpl;
|
||||
import com.dfsek.terra.registry.RegistryFactoryImpl;
|
||||
import com.dfsek.terra.registry.config.ConfigTypeRegistry;
|
||||
import com.dfsek.terra.registry.config.NoiseRegistry;
|
||||
import com.dfsek.terra.world.TerraWorldImpl;
|
||||
@@ -63,6 +68,8 @@ import java.util.zip.ZipFile;
|
||||
public class ConfigPackImpl implements ConfigPack {
|
||||
private final ConfigPackTemplate template = new ConfigPackTemplate();
|
||||
|
||||
private final RegistryFactory registryFactory = new RegistryFactoryImpl();
|
||||
|
||||
private final Map<Type, TypeLoader<?>> loaders = new HashMap<>();
|
||||
private final Map<Type, TemplateProvider<ObjectTemplate<?>>> objectLoaders = new HashMap<>();
|
||||
|
||||
@@ -84,7 +91,7 @@ public class ConfigPackImpl implements ConfigPack {
|
||||
|
||||
public ConfigPackImpl(File folder, TerraPlugin main) throws ConfigException {
|
||||
try {
|
||||
this.configTypeRegistry = new ConfigTypeRegistry((id, configType) -> {
|
||||
this.configTypeRegistry = new ConfigTypeRegistry(main, (id, configType) -> {
|
||||
OpenRegistry<?> openRegistry = configType.registrySupplier().get();
|
||||
registryMap.put(configType.getTypeClass(), ImmutablePair.of(openRegistry, new CheckedRegistryImpl<>(openRegistry)));
|
||||
});
|
||||
@@ -127,7 +134,7 @@ public class ConfigPackImpl implements ConfigPack {
|
||||
|
||||
public ConfigPackImpl(ZipFile file, TerraPlugin main) throws ConfigException {
|
||||
try {
|
||||
this.configTypeRegistry = new ConfigTypeRegistry((id, configType) -> {
|
||||
this.configTypeRegistry = new ConfigTypeRegistry(main, (id, configType) -> {
|
||||
OpenRegistry<?> openRegistry = configType.registrySupplier().get();
|
||||
registryMap.put(configType.getTypeClass(), ImmutablePair.of(openRegistry, new CheckedRegistryImpl<>(openRegistry)));
|
||||
});
|
||||
@@ -238,8 +245,12 @@ public class ConfigPackImpl implements ConfigPack {
|
||||
}
|
||||
|
||||
for(ConfigType<?, ?> configType : configTypeRegistry.entries()) {
|
||||
for(ConfigTemplate config : abstractConfigLoader.loadConfigs(configs.getOrDefault(configType, Collections.emptyList()), () -> configType.getTemplate(this, main))) {
|
||||
((ConfigType) configType).callback(this, main, config);
|
||||
for(AbstractableTemplate config : abstractConfigLoader.loadConfigs(configs.getOrDefault(configType, Collections.emptyList()), () -> configType.getTemplate(this, main))) {
|
||||
try {
|
||||
((CheckedRegistry) getRegistry(configType.getTypeClass())).add(config.getID(), ((ConfigFactory) configType.getFactory()).build(config, main));
|
||||
} catch(DuplicateEntryException e) {
|
||||
throw new LoadException("Duplicate registry entry: ", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -361,4 +372,9 @@ public class ConfigPackImpl implements ConfigPack {
|
||||
public boolean vanillaFlora() {
|
||||
return template.vanillaDecorations();
|
||||
}
|
||||
|
||||
@Override
|
||||
public RegistryFactory getRegistryFactory() {
|
||||
return registryFactory;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.dfsek.terra.registry;
|
||||
|
||||
import com.dfsek.tectonic.exception.LoadException;
|
||||
import com.dfsek.tectonic.loading.ConfigLoader;
|
||||
import com.dfsek.tectonic.loading.TypeLoader;
|
||||
import com.dfsek.terra.api.registry.OpenRegistry;
|
||||
import com.dfsek.terra.api.registry.meta.RegistryFactory;
|
||||
import com.dfsek.terra.api.util.generic.Lazy;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class RegistryFactoryImpl implements RegistryFactory {
|
||||
@Override
|
||||
public <T> OpenRegistry<T> create() {
|
||||
return new OpenRegistryImpl<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> OpenRegistry<T> create(Function<OpenRegistry<T>, TypeLoader<T>> loader) {
|
||||
return new OpenRegistryImpl<>() {
|
||||
private final Lazy<TypeLoader<T>> loaderCache = Lazy.of(() -> loader.apply(this));
|
||||
@Override
|
||||
public T load(Type type, Object o, ConfigLoader configLoader) throws LoadException {
|
||||
return loaderCache.value().load(type, o, configLoader);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package com.dfsek.terra.registry.config;
|
||||
|
||||
import com.dfsek.tectonic.exception.LoadException;
|
||||
import com.dfsek.tectonic.loading.ConfigLoader;
|
||||
import com.dfsek.terra.api.util.seeded.BiomeBuilder;
|
||||
import com.dfsek.terra.registry.OpenRegistryImpl;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class BiomeRegistry extends OpenRegistryImpl<BiomeBuilder> {
|
||||
@Override
|
||||
public BiomeBuilder load(Type type, Object o, ConfigLoader configLoader) throws LoadException {
|
||||
if(o.equals("SELF")) return null;
|
||||
BiomeBuilder biome = get((String) o);
|
||||
if(biome == null)
|
||||
throw new LoadException("No such " + type.getTypeName() + " matching \"" + o + "\" was found in this registry.");
|
||||
return biome;
|
||||
}
|
||||
}
|
||||
@@ -1,91 +1,26 @@
|
||||
package com.dfsek.terra.registry.config;
|
||||
|
||||
import com.dfsek.tectonic.config.ConfigTemplate;
|
||||
import com.dfsek.tectonic.exception.LoadException;
|
||||
import com.dfsek.terra.api.TerraPlugin;
|
||||
import com.dfsek.terra.api.config.AbstractableTemplate;
|
||||
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.config.pack.ConfigPackImpl;
|
||||
import com.dfsek.terra.registry.OpenRegistryImpl;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class ConfigTypeRegistry extends OpenRegistryImpl<ConfigType<?, ?>> {
|
||||
private final BiConsumer<String, ConfigType<?, ?>> callback;
|
||||
|
||||
public ConfigTypeRegistry(BiConsumer<String, ConfigType<?, ?>> callback) {
|
||||
private final TerraPlugin main;
|
||||
public ConfigTypeRegistry(TerraPlugin main, BiConsumer<String, ConfigType<?, ?>> callback) {
|
||||
super(new LinkedHashMap<>()); // Ordered
|
||||
this.callback = callback;
|
||||
add("PACK", new PackBuilder());
|
||||
this.main = main;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(String identifier, Entry<ConfigType<?, ?>> value) {
|
||||
callback.accept(identifier, value.getValue());
|
||||
main.getDebugLogger().info("Registered config registry with ID " + identifier + " to class " + value.getValue().getTypeClass().getCanonicalName());
|
||||
return super.add(identifier, value);
|
||||
}
|
||||
|
||||
private static final class PackBuilder implements ConfigType<ConfigTemplate, ConfigPack> {
|
||||
|
||||
@Override
|
||||
public ConfigTemplate getTemplate(ConfigPack pack, TerraPlugin main) {
|
||||
return new ConfigTemplate() {
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void callback(ConfigPack pack, TerraPlugin main, ConfigTemplate loadedConfig) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<ConfigPack> getTypeClass() {
|
||||
return ConfigPack.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Supplier<OpenRegistry<ConfigPack>> registrySupplier() {
|
||||
return OpenRegistryImpl::new;
|
||||
}
|
||||
}
|
||||
|
||||
private static final class ConfigBuilder<T extends AbstractableTemplate, O> implements ConfigType<T, O> {
|
||||
private final ConfigFactory<T, O> factory;
|
||||
private final Supplier<T> provider;
|
||||
private final Class<O> clazz;
|
||||
private final Supplier<OpenRegistry<O>> registrySupplier;
|
||||
|
||||
private ConfigBuilder(ConfigFactory<T, O> factory, Supplier<T> provider, Class<O> clazz, Supplier<OpenRegistry<O>> registrySupplier) {
|
||||
this.factory = factory;
|
||||
this.provider = provider;
|
||||
this.clazz = clazz;
|
||||
this.registrySupplier = registrySupplier;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getTemplate(ConfigPack pack, TerraPlugin main) {
|
||||
return provider.get();
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public void callback(ConfigPack pack, TerraPlugin main, T loadedConfig) throws LoadException {
|
||||
pack.getRegistry(clazz).addUnchecked(loadedConfig.getID(), factory.build(loadedConfig, main));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<O> getTypeClass() {
|
||||
return clazz;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Supplier<OpenRegistry<O>> registrySupplier() {
|
||||
return registrySupplier;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user