implement ModDependentConfigSection#withDefault

This commit is contained in:
dfsek
2021-05-23 15:57:36 -07:00
parent 486dcfc63d
commit 3f37907256
@@ -13,7 +13,7 @@ public class ModDependentConfigSection<T> {
private final Map<String, T> results = new HashMap<>(); private final Map<String, T> results = new HashMap<>();
private final T defaultValue; private final T defaultValue;
public ModDependentConfigSection(TerraPlugin main, T defaultValue) { protected ModDependentConfigSection(TerraPlugin main, T defaultValue) {
this.main = main; this.main = main;
this.defaultValue = defaultValue; this.defaultValue = defaultValue;
} }
@@ -22,11 +22,17 @@ public class ModDependentConfigSection<T> {
results.put(id, value); results.put(id, value);
} }
public static <T1> ModDependentConfigSection<T1> withDefault(T1 defaultValue) {
return new ModDependentConfigSection<>(null, defaultValue);
}
public T get() { public T get() {
if(main != null) {
Set<String> mods = main.getMods().stream().map(Mod::getID).collect(Collectors.toSet()); Set<String> mods = main.getMods().stream().map(Mod::getID).collect(Collectors.toSet());
for(Map.Entry<String, T> entry : results.entrySet()) { for(Map.Entry<String, T> entry : results.entrySet()) {
if(mods.contains(entry.getKey())) return entry.getValue(); if(mods.contains(entry.getKey())) return entry.getValue();
} }
}
return defaultValue; return defaultValue;
} }
} }