2021-11-14 15:37:01 +01:00
|
|
|
package io.gitlab.jfronny.respackopts.model.tree;
|
2021-06-10 13:10:12 +02:00
|
|
|
|
2024-04-24 19:33:50 +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;
|
2021-10-03 16:45:29 +02:00
|
|
|
import io.gitlab.jfronny.respackopts.Respackopts;
|
2024-04-24 19:33:50 +02:00
|
|
|
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;
|
2022-01-12 18:23:49 +01:00
|
|
|
import io.gitlab.jfronny.respackopts.util.IndentingStringBuilder;
|
2021-06-19 13:32:16 +02:00
|
|
|
|
2022-01-12 18:23:49 +01:00
|
|
|
import java.util.Objects;
|
|
|
|
|
2024-04-24 19:33:50 +02:00
|
|
|
@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;
|
2021-10-03 16:45:29 +02:00
|
|
|
private PackReloadType reloadType = PackReloadType.Resource;
|
2021-06-14 17:04:43 +02:00
|
|
|
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("."))
|
2022-01-12 18:23:49 +01:00
|
|
|
n = n.substring(1);
|
2021-07-13 13:33:27 +02:00
|
|
|
return n;
|
|
|
|
}
|
2021-06-14 17:04:43 +02:00
|
|
|
|
|
|
|
public void setVersion(int version) {
|
|
|
|
this.version = version;
|
|
|
|
}
|
2021-07-11 21:26:14 +02:00
|
|
|
public int getVersion() {
|
|
|
|
return version;
|
|
|
|
}
|
2021-06-10 14:55:03 +02:00
|
|
|
|
|
|
|
public T getValue() {
|
2021-07-01 16:19:42 +02:00
|
|
|
if (value == null) {
|
|
|
|
if (defaultValue == null) {
|
2021-10-03 16:45:29 +02:00
|
|
|
Respackopts.LOGGER.warn("No default value or current value set for entry, returning null in " + getName());
|
2021-07-01 16:19:42 +02:00
|
|
|
return null;
|
|
|
|
}
|
2021-07-01 16:17:38 +02:00
|
|
|
value = getDefault();
|
2021-07-01 16:19:42 +02:00
|
|
|
}
|
2021-06-10 14:55:03 +02:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2021-10-03 16:45:29 +02:00
|
|
|
public T setValue(T value) {
|
2021-06-10 14:55:03 +02:00
|
|
|
if (value != null)
|
|
|
|
this.value = value;
|
2021-10-03 16:45:29 +02:00
|
|
|
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();
|
2021-10-03 16:45:29 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-10-03 16:45:29 +02:00
|
|
|
public T setDefault(T value) {
|
2021-06-10 14:55:03 +02:00
|
|
|
if (value != null)
|
|
|
|
defaultValue = value;
|
2021-10-03 16:45:29 +02:00
|
|
|
return defaultValue;
|
2021-06-10 14:55:03 +02:00
|
|
|
}
|
2024-04-24 19:33:50 +02:00
|
|
|
|
|
|
|
public boolean hasDefault() {
|
|
|
|
return defaultValue != null;
|
|
|
|
}
|
2021-06-10 14:55:03 +02:00
|
|
|
|
2021-10-03 16:45:29 +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());
|
2021-10-03 16:45:29 +02:00
|
|
|
if (mode == ConfigSyncMode.CONF_LOAD)
|
2021-06-10 14:55:03 +02:00
|
|
|
setValue(source.getValue());
|
|
|
|
}
|
|
|
|
|
2022-01-12 18:23:49 +01:00
|
|
|
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() {
|
2022-01-12 18:23:49 +01:00
|
|
|
IndentingStringBuilder log = new IndentingStringBuilder();
|
2021-06-10 14:55:03 +02:00
|
|
|
appendString(log);
|
|
|
|
return log.toString();
|
|
|
|
}
|
2022-01-12 18:23:49 +01:00
|
|
|
|
|
|
|
@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);
|
|
|
|
}
|
|
|
|
|
2021-06-14 16:33:15 +02:00
|
|
|
public abstract ConfigEntry<T> clone();
|
2021-06-14 17:04:43 +02:00
|
|
|
|
2021-06-19 13:32:16 +02:00
|
|
|
public abstract void buildShader(StringBuilder sb, String valueName);
|
|
|
|
|
2022-08-28 20:06:59 +02:00
|
|
|
public abstract CategoryBuilder<?> buildEntry(GuiEntryBuilderParam args);
|
2021-10-03 16:45:29 +02:00
|
|
|
|
2021-10-21 18:45:31 +02:00
|
|
|
public Class<? super T> getEntryClass() {
|
|
|
|
return entryClass;
|
2021-10-03 16:45:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public PackReloadType getReloadType() {
|
|
|
|
return reloadType;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setReloadType(PackReloadType reloadType) {
|
|
|
|
this.reloadType = reloadType;
|
|
|
|
}
|
2022-01-12 18:23:49 +01:00
|
|
|
|
|
|
|
public ConfigBranch getParent() {
|
|
|
|
return parent;
|
|
|
|
}
|
2021-06-10 13:10:12 +02:00
|
|
|
}
|