From 82fe6d5aa4d4ff5975da504bfcb3ac86de3cb8f7 Mon Sep 17 00:00:00 2001 From: dfsek Date: Wed, 12 May 2021 00:09:21 -0700 Subject: [PATCH] add api to load custom values from pack manifest. --- .../events/config/ConfigPackPreLoadEvent.java | 19 +++++++++++++++ .../dfsek/terra/config/pack/ConfigPack.java | 24 +++++++++++++++---- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/common/src/main/java/com/dfsek/terra/api/event/events/config/ConfigPackPreLoadEvent.java b/common/src/main/java/com/dfsek/terra/api/event/events/config/ConfigPackPreLoadEvent.java index 317603c9e..390fd8aab 100644 --- a/common/src/main/java/com/dfsek/terra/api/event/events/config/ConfigPackPreLoadEvent.java +++ b/common/src/main/java/com/dfsek/terra/api/event/events/config/ConfigPackPreLoadEvent.java @@ -1,7 +1,11 @@ package com.dfsek.terra.api.event.events.config; +import com.dfsek.tectonic.config.ConfigTemplate; 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. */ @@ -9,4 +13,19 @@ public class ConfigPackPreLoadEvent extends ConfigPackLoadEvent { public ConfigPackPreLoadEvent(ConfigPack pack) { super(pack); } + + private final List templates = new ArrayList<>(); + + /** + * Add an additional config template to load using the pack manifest. + * + * @param template Template to register. + */ + public void addTemplate(ConfigTemplate template) { + templates.add(template); + } + + public List getTemplates() { + return new ArrayList<>(templates); + } } diff --git a/common/src/main/java/com/dfsek/terra/config/pack/ConfigPack.java b/common/src/main/java/com/dfsek/terra/config/pack/ConfigPack.java index 02772f633..737c76412 100644 --- a/common/src/main/java/com/dfsek/terra/config/pack/ConfigPack.java +++ b/common/src/main/java/com/dfsek/terra/config/pack/ConfigPack.java @@ -2,6 +2,8 @@ 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; import com.dfsek.tectonic.loading.ConfigLoader; @@ -130,11 +132,19 @@ public class ConfigPack implements LoaderRegistrar { File pack = new File(folder, "pack.yml"); try { - selfLoader.load(template, new FileInputStream(pack)); + Configuration configuration = new Configuration(new FileInputStream(pack)); + 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); + } + load(l, main); + ConfigPackPostTemplate packPostTemplate = new ConfigPackPostTemplate(); selfLoader.load(packPostTemplate, new FileInputStream(pack)); biomeProviderBuilder = packPostTemplate.getProviderBuilder(); @@ -174,9 +184,17 @@ public class ConfigPack implements LoaderRegistrar { if(pack == null) throw new LoadException("No pack.yml file found in " + file.getName()); - selfLoader.load(template, file.getInputStream(pack)); + Configuration configuration = new Configuration(file.getInputStream(pack)); + 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); + } + + load(l, main); ConfigPackPostTemplate packPostTemplate = new ConfigPackPostTemplate(); @@ -211,8 +229,6 @@ public class ConfigPack implements LoaderRegistrar { private void load(long start, TerraPlugin main) throws ConfigException { - main.getEventManager().callEvent(new ConfigPackPreLoadEvent(this)); - for(Map.Entry var : template.getVariables().entrySet()) { varScope.create(var.getKey(), var.getValue()); }