Respackopts/src/main/java/io/gitlab/jfronny/respackopts/model/tree/ConfigEntry.java

130 lines
3.8 KiB
Java
Raw Normal View History

2021-11-14 15:37:01 +01:00
package io.gitlab.jfronny.respackopts.model.tree;
2021-06-10 13:10:12 +02:00
import io.gitlab.jfronny.commons.serialize.databind.api.SerializeWithAdapter;
2023-09-22 19:19:55 +02:00
import io.gitlab.jfronny.libjf.config.api.v2.dsl.CategoryBuilder;
2023-08-14 17:04:59 +02:00
import io.gitlab.jfronny.muscript.data.dynamic.DynamicBase;
import io.gitlab.jfronny.respackopts.Respackopts;
import io.gitlab.jfronny.respackopts.gson.ConfigEntryTypeAdapter;
2021-11-14 15:37:01 +01:00
import io.gitlab.jfronny.respackopts.model.enums.ConfigSyncMode;
import io.gitlab.jfronny.respackopts.model.enums.PackReloadType;
import io.gitlab.jfronny.respackopts.util.IndentingStringBuilder;
import java.util.Objects;
@SerializeWithAdapter(adapter = ConfigEntryTypeAdapter.class)
2023-08-14 17:04:59 +02:00
public abstract class ConfigEntry<T> implements DynamicBase {
2021-10-21 18:45:31 +02:00
private final Class<? super T> entryClass;
2021-06-10 14:55:03 +02:00
private T defaultValue;
private T value;
private PackReloadType reloadType = PackReloadType.Resource;
protected int version;
2021-07-13 13:33:27 +02:00
protected ConfigBranch parent;
2021-10-21 18:45:31 +02:00
public ConfigEntry(Class<? super T> entryClass) {
this.entryClass = entryClass;
}
2021-07-13 13:33:27 +02:00
public String getName() {
if (parent == null)
return "";
String n = parent.getName() + "." + parent.getEntryName(this);
if (n.startsWith("."))
n = n.substring(1);
2021-07-13 13:33:27 +02:00
return n;
}
public void setVersion(int version) {
this.version = version;
}
public int getVersion() {
return version;
}
2021-06-10 14:55:03 +02:00
public T getValue() {
if (value == null) {
if (defaultValue == null) {
Respackopts.LOGGER.warn("No default value or current value set for entry, returning null in " + getName());
return null;
}
2021-07-01 16:17:38 +02:00
value = getDefault();
}
2021-06-10 14:55:03 +02:00
return value;
}
public T setValue(T value) {
2021-06-10 14:55:03 +02:00
if (value != null)
this.value = value;
return this.value;
2021-06-10 14:55:03 +02:00
}
2023-08-14 17:04:59 +02:00
2021-06-10 14:55:03 +02:00
public T getDefault() {
2021-07-01 16:17:38 +02:00
if (defaultValue == null) {
defaultValue = getValue();
Respackopts.LOGGER.warn("No default value set for entry, using current in " + getName());
2021-07-01 16:17:38 +02:00
}
2021-06-10 14:55:03 +02:00
return defaultValue;
}
public T setDefault(T value) {
2021-06-10 14:55:03 +02:00
if (value != null)
defaultValue = value;
return defaultValue;
2021-06-10 14:55:03 +02:00
}
public boolean hasDefault() {
return defaultValue != null;
}
2021-06-10 14:55:03 +02:00
public void sync(ConfigEntry<T> source, ConfigSyncMode mode) {
if (mode == ConfigSyncMode.RESPACK_LOAD)
2021-06-10 14:55:03 +02:00
setDefault(source.getDefault());
if (mode == ConfigSyncMode.CONF_LOAD)
2021-06-10 14:55:03 +02:00
setValue(source.getValue());
}
public void appendString(IndentingStringBuilder sb) {
2023-08-14 17:04:59 +02:00
sb.line(getValue() + " (" + getDefault() + ")");
2021-06-10 14:55:03 +02:00
}
@Override
public String toString() {
IndentingStringBuilder log = new IndentingStringBuilder();
2021-06-10 14:55:03 +02:00
appendString(log);
return log.toString();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ConfigEntry<?> that)) return false;
return version == that.version && Objects.equals(entryClass, that.entryClass) && reloadType == that.reloadType && (parent == null) == (that.parent == null);
}
@Override
public int hashCode() {
return Objects.hash(entryClass, reloadType, version, parent == null);
}
public abstract ConfigEntry<T> clone();
public abstract void buildShader(StringBuilder sb, String valueName);
2022-08-28 20:06:59 +02:00
public abstract CategoryBuilder<?> buildEntry(GuiEntryBuilderParam args);
2021-10-21 18:45:31 +02:00
public Class<? super T> getEntryClass() {
return entryClass;
}
public PackReloadType getReloadType() {
return reloadType;
}
public void setReloadType(PackReloadType reloadType) {
this.reloadType = reloadType;
}
public ConfigBranch getParent() {
return parent;
}
2021-06-10 13:10:12 +02:00
}