Respackopts/src/main/java/io/gitlab/jfronny/respackopts/data/entry/ConfigBranch.java

160 lines
5.8 KiB
Java
Raw Normal View History

2021-06-10 16:07:22 +02:00
package io.gitlab.jfronny.respackopts.data.entry;
2021-06-10 13:10:12 +02:00
2021-06-10 14:55:03 +02:00
import com.google.common.collect.ImmutableMap;
import io.gitlab.jfronny.respackopts.Respackopts;
2021-09-15 18:37:07 +02:00
import io.gitlab.jfronny.respackopts.RpoModInfo;
import io.gitlab.jfronny.respackopts.util.RpoFormatException;
import me.shedaniel.clothconfig2.api.AbstractConfigListEntry;
import me.shedaniel.clothconfig2.api.ConfigEntryBuilder;
import me.shedaniel.clothconfig2.impl.builders.SubCategoryBuilder;
2021-08-24 17:42:46 +02:00
import meteordevelopment.starscript.value.Value;
import meteordevelopment.starscript.value.ValueMap;
import net.minecraft.text.Text;
2021-06-10 13:10:12 +02:00
import java.util.LinkedHashMap;
2021-06-10 13:10:12 +02:00
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
2021-06-10 13:10:12 +02:00
public class ConfigBranch extends ConfigEntry<Map<String, ConfigEntry<?>>> {
2021-06-10 14:55:03 +02:00
public ConfigBranch() {
setValue(new LinkedHashMap<>());
2021-06-10 14:55:03 +02:00
}
public boolean getBoolean(String name) throws RpoFormatException {
2021-06-10 13:10:12 +02:00
String[] sp = name.split("\\.");
2021-06-10 14:55:03 +02:00
if (!super.getValue().containsKey(sp[0]))
throw new RpoFormatException("Invalid path to key in " + getName() + ": " + name);
ConfigEntry<?> e = super.getValue().get(sp[0]);
2021-06-10 13:10:12 +02:00
if (sp.length == 1) {
if (e instanceof ConfigBooleanEntry b)
2021-06-10 14:55:03 +02:00
return b.getValue();
throw new RpoFormatException("Not a boolean in " + getName());
2021-06-10 13:10:12 +02:00
}
if (sp.length == 2 && e instanceof ConfigEnumEntry en) {
for (String entry : en.values) {
if (entry.equals(sp[1]))
return entry.equals(en.getValue());
2021-06-10 13:10:12 +02:00
}
throw new RpoFormatException("Could not find enum entry in " + getName());
2021-06-10 13:10:12 +02:00
}
if (e instanceof ConfigBranch b)
return b.getBoolean(name.substring(name.indexOf('.') + 1));
throw new RpoFormatException("Invalid path to key in " + getName());
2021-06-10 13:10:12 +02:00
}
2021-06-10 14:55:03 +02:00
@Override
public void sync(ConfigEntry<Map<String, ConfigEntry<?>>> source, SyncMode mode) {
for (Map.Entry<String, ConfigEntry<?>> e : source.getValue().entrySet()) {
2021-06-10 14:55:03 +02:00
if (!has(e.getKey())) {
if (mode == SyncMode.RESPACK_LOAD)
2021-06-10 19:26:59 +02:00
add(e.getKey(), e.getValue().clone());
2021-06-10 14:55:03 +02:00
} else {
ConfigEntry<?> current = get(e.getKey());
if (e.getValue().getClass().equals(current.getClass())) {
syncSub(current, (ConfigEntry)e.getValue(), mode);
2021-06-10 13:10:12 +02:00
}
else {
if (mode == SyncMode.RESPACK_LOAD) {
2021-09-15 18:37:07 +02:00
RpoModInfo.LOGGER.warn("Type mismatch in config (" + getName() + "), overwriting");
add(e.getKey(), e.getValue().clone());
} else
2021-09-15 18:37:07 +02:00
RpoModInfo.LOGGER.warn("Type mismatch in config (" + getName() + "), ignoring");
2021-06-10 13:10:12 +02:00
}
}
}
if (mode == SyncMode.RESPACK_LOAD)
2021-08-26 20:03:54 +02:00
for (Map.Entry<String, ConfigEntry<?>> e : getValue().entrySet()) {
if (!source.getValue().containsKey(e.getKey()))
2021-08-26 20:03:54 +02:00
super.getValue().remove(e.getKey());
}
2021-06-10 13:10:12 +02:00
}
private <T> void syncSub(ConfigEntry<T> current, ConfigEntry<T> next, SyncMode mode) {
2021-06-10 14:55:03 +02:00
current.sync(next, mode);
}
public <T> void add(String name, ConfigEntry<T> val) {
val.setVersion(version);
2021-07-13 13:33:27 +02:00
val.parent = this;
2021-06-10 14:55:03 +02:00
super.getValue().put(name, val);
2021-06-10 13:10:12 +02:00
}
public ConfigEntry<?> get(String key) {
2021-06-10 14:55:03 +02:00
return super.getValue().get(key);
2021-06-10 13:10:12 +02:00
}
2021-07-13 13:33:27 +02:00
public String getEntryName(ConfigEntry<?> entry) {
for (Map.Entry<String, ConfigEntry<?>> e : getValue().entrySet()) {
if (e.getValue() == entry)
return e.getKey();
}
throw new IndexOutOfBoundsException();
}
2021-06-10 13:10:12 +02:00
public boolean has(String key) {
2021-06-10 14:55:03 +02:00
return super.getValue().containsKey(key);
2021-06-10 13:10:12 +02:00
}
2021-06-10 14:55:03 +02:00
@Override
public Map<String, ConfigEntry<?>> getValue() {
2021-06-10 14:55:03 +02:00
return ImmutableMap.copyOf(super.getValue());
2021-06-10 13:10:12 +02:00
}
2021-06-10 14:55:03 +02:00
@Override
public void buildShader(StringBuilder sb, String valueName) {
for (Map.Entry<String, ConfigEntry<?>> e : super.getValue().entrySet()) {
e.getValue().buildShader(sb, valueName + "_" + Respackopts.sanitizeString(e.getKey()));
2021-06-10 13:10:12 +02:00
}
}
2021-06-10 14:55:03 +02:00
2021-08-24 17:42:46 +02:00
@Override
public Value buildStarscript() {
ValueMap vm = new ValueMap();
for (Map.Entry<String, ConfigEntry<?>> e : super.getValue().entrySet()) {
vm.set(Respackopts.sanitizeString(e.getKey()), () -> e.getValue().buildStarscript());
}
return Value.map(vm);
}
@Override
2021-07-08 17:49:15 +02:00
public AbstractConfigListEntry<?> buildEntry(ConfigEntryBuilder entryBuilder, Text name, Supplier<Optional<Text[]>> tooltipSupplier, String screenId, String entryName, String translationPrefix) {
SubCategoryBuilder sc = entryBuilder.startSubCategory(name);
2021-07-08 17:49:15 +02:00
Respackopts.factory.buildCategory(this, screenId, sc::add, entryBuilder, entryName);
sc.setTooltipSupplier(tooltipSupplier);
return sc.build();
}
@Override
public String getEntryType() {
return "title";
}
2021-06-10 14:55:03 +02:00
@Override
public void appendString(StringBuilder sb) {
for (Map.Entry<String, ConfigEntry<?>> e : getValue().entrySet()) {
2021-06-10 14:55:03 +02:00
sb.append("\n");
sb.append(e.getKey());
sb.append(": ");
sb.append(e.getValue());
}
}
2021-06-10 19:26:59 +02:00
@Override
public ConfigEntry<Map<String, ConfigEntry<?>>> clone() {
2021-06-10 19:26:59 +02:00
ConfigBranch branch = new ConfigBranch();
for (Map.Entry<String, ConfigEntry<?>> e : getValue().entrySet()) {
2021-06-10 19:26:59 +02:00
branch.add(e.getKey(), e.getValue().clone());
}
return branch;
}
@Override
public void setVersion(int version) {
super.setVersion(version);
for (ConfigEntry<?> value : getValue().values()) {
value.setVersion(version);
}
}
2021-06-10 13:10:12 +02:00
}