make loadConfig less jank

This commit is contained in:
dfsek 2021-05-12 00:48:38 -07:00
parent 4203121d40
commit fa647e1e2c
3 changed files with 19 additions and 25 deletions

View File

@ -1,31 +1,30 @@
package com.dfsek.terra.api.event.events.config;
import com.dfsek.tectonic.config.ConfigTemplate;
import com.dfsek.tectonic.exception.ConfigException;
import com.dfsek.terra.config.pack.ConfigPack;
import java.util.ArrayList;
import java.util.List;
/**
* Called before a config pack's registries are filled. At this point, the pack manifest has been loaded, and all registries are empty.
*/
public class ConfigPackPreLoadEvent extends ConfigPackLoadEvent {
public ConfigPackPreLoadEvent(ConfigPack pack) {
private final ExceptionalConsumer<ConfigTemplate> configLoader;
public ConfigPackPreLoadEvent(ConfigPack pack, ExceptionalConsumer<ConfigTemplate> configLoader) {
super(pack);
this.configLoader = configLoader;
}
private final List<ConfigTemplate> templates = new ArrayList<>();
/**
* Add an additional config template to load using the pack manifest.
* Load a custom {@link ConfigTemplate} using the pack manifest.
*
* @param template Template to register.
*/
public void addTemplate(ConfigTemplate template) {
templates.add(template);
public void loadTemplate(ConfigTemplate template) throws ConfigException {
configLoader.accept(template);
}
public List<ConfigTemplate> getTemplates() {
return new ArrayList<>(templates);
public interface ExceptionalConsumer<T extends ConfigTemplate> {
void accept(T value) throws ConfigException;
}
}

View File

@ -2,7 +2,6 @@ package com.dfsek.terra.config.pack;
import com.dfsek.paralithic.eval.parser.Scope;
import com.dfsek.tectonic.abstraction.AbstractConfigLoader;
import com.dfsek.tectonic.config.ConfigTemplate;
import com.dfsek.tectonic.config.Configuration;
import com.dfsek.tectonic.exception.ConfigException;
import com.dfsek.tectonic.exception.LoadException;
@ -137,11 +136,7 @@ public class ConfigPack implements LoaderRegistrar {
main.logger().info("Loading config pack \"" + template.getID() + "\"");
ConfigPackPreLoadEvent event = new ConfigPackPreLoadEvent(this);
main.getEventManager().callEvent(event);
for(ConfigTemplate eventTemplate : event.getTemplates()) {
selfLoader.load(eventTemplate, configuration);
}
main.getEventManager().callEvent(new ConfigPackPreLoadEvent(this, template -> selfLoader.load(template, configuration)));
load(l, main);
@ -188,12 +183,7 @@ public class ConfigPack implements LoaderRegistrar {
selfLoader.load(template, configuration);
main.logger().info("Loading config pack \"" + template.getID() + "\"");
ConfigPackPreLoadEvent event = new ConfigPackPreLoadEvent(this);
main.getEventManager().callEvent(event);
for(ConfigTemplate eventTemplate : event.getTemplates()) {
selfLoader.load(eventTemplate, configuration);
}
main.getEventManager().callEvent(new ConfigPackPreLoadEvent(this, template -> selfLoader.load(template, configuration)));
load(l, main);

View File

@ -1,5 +1,6 @@
package com.dfsek.terra.fabric;
import com.dfsek.tectonic.exception.ConfigException;
import com.dfsek.tectonic.exception.LoadException;
import com.dfsek.tectonic.loading.TypeRegistry;
import com.dfsek.terra.api.TerraPlugin;
@ -329,7 +330,7 @@ public class TerraFabricPlugin implements TerraPlugin, ModInitializer {
};
//noinspection ConstantConditions
((GeneratorTypeAccessor) generatorType).setTranslationKey(new LiteralText("Terra:" + pack.getTemplate().getID()));
GeneratorTypeAccessor.getValues().add(generatorType);
GeneratorTypeAccessor.getValues().add(1, generatorType);
});
}
@ -475,7 +476,11 @@ public class TerraFabricPlugin implements TerraPlugin, ModInitializer {
injectTree(treeRegistry, "WARPED_FUNGUS", ConfiguredFeatures.WARPED_FUNGI);
PackFeatureOptionsTemplate template = new PackFeatureOptionsTemplate();
event.addTemplate(template);
try {
event.loadTemplate(template);
} catch(ConfigException e) {
e.printStackTrace();
}
templates.put(event.getPack(), template);
}