mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-04 14:56:28 +00:00
implement ModDependentConfigSection
This commit is contained in:
@@ -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<T> {
|
||||
private final TerraPlugin main;
|
||||
private final Map<String, T> 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<String> mods = main.getMods().stream().map(Mod::getID).collect(Collectors.toSet());
|
||||
for(Map.Entry<String, T> entry : results.entrySet()) {
|
||||
if(mods.contains(entry.getKey())) return entry.getValue();
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
@@ -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<ModDependentConfigSection<?>> {
|
||||
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<String, Object> map = (Map<String, Object>) c;
|
||||
|
||||
ModDependentConfigSection<Object> configSection = new ModDependentConfigSection<>(main, loader.loadType(generic, map.get("default")));
|
||||
|
||||
((Map<String, Object>) ((Map<?, ?>) c).get("mods")).forEach(configSection::add);
|
||||
|
||||
return configSection;
|
||||
} else throw new LoadException("Unable to load config! Could not retrieve parameterized type: " + type);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user