diff --git a/common/src/main/java/com/dfsek/terra/config/loaders/mod/ModDependentConfigSection.java b/common/src/main/java/com/dfsek/terra/config/loaders/mod/ModDependentConfigSection.java new file mode 100644 index 000000000..d1a76a5a4 --- /dev/null +++ b/common/src/main/java/com/dfsek/terra/config/loaders/mod/ModDependentConfigSection.java @@ -0,0 +1,32 @@ +package com.dfsek.terra.config.loaders.mod; + +import com.dfsek.terra.api.TerraPlugin; +import com.dfsek.terra.api.platform.modloader.Mod; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +public class ModDependentConfigSection { + private final TerraPlugin main; + private final Map results = new HashMap<>(); + private final T defaultValue; + + public ModDependentConfigSection(TerraPlugin main, T defaultValue) { + this.main = main; + this.defaultValue = defaultValue; + } + + public void add(String id, T value) { + results.put(id, value); + } + + public T get() { + Set mods = main.getMods().stream().map(Mod::getID).collect(Collectors.toSet()); + for(Map.Entry entry : results.entrySet()) { + if(mods.contains(entry.getKey())) return entry.getValue(); + } + return defaultValue; + } +} diff --git a/common/src/main/java/com/dfsek/terra/config/loaders/mod/ModDependentConfigSectionLoader.java b/common/src/main/java/com/dfsek/terra/config/loaders/mod/ModDependentConfigSectionLoader.java new file mode 100644 index 000000000..6c5901768 --- /dev/null +++ b/common/src/main/java/com/dfsek/terra/config/loaders/mod/ModDependentConfigSectionLoader.java @@ -0,0 +1,35 @@ +package com.dfsek.terra.config.loaders.mod; + +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 java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.Map; + +public class ModDependentConfigSectionLoader implements TypeLoader> { + private final TerraPlugin main; + + public ModDependentConfigSectionLoader(TerraPlugin main) { + this.main = main; + } + + @SuppressWarnings("unchecked") + @Override + public ModDependentConfigSection load(Type type, Object c, ConfigLoader loader) throws LoadException { + if(type instanceof ParameterizedType) { + ParameterizedType pType = (ParameterizedType) type; + Type generic = pType.getActualTypeArguments()[0]; + + Map map = (Map) c; + + ModDependentConfigSection configSection = new ModDependentConfigSection<>(main, loader.loadType(generic, map.get("default"))); + + ((Map) ((Map) c).get("mods")).forEach(configSection::add); + + return configSection; + } else throw new LoadException("Unable to load config! Could not retrieve parameterized type: " + type); + } +}