2022-08-26 20:51:00 +02:00
|
|
|
package io.gitlab.jfronny.libjf.config.impl.ui.tiny;
|
2022-04-29 15:48:27 +02:00
|
|
|
|
2022-08-21 22:34:55 +02:00
|
|
|
import io.gitlab.jfronny.libjf.LibJf;
|
2022-08-26 20:51:00 +02:00
|
|
|
import io.gitlab.jfronny.libjf.config.api.v1.EntryInfo;
|
|
|
|
import io.gitlab.jfronny.libjf.config.api.v1.ui.tiny.WidgetFactory;
|
2022-04-29 15:48:27 +02:00
|
|
|
import net.minecraft.text.Text;
|
2023-03-11 21:21:53 +01:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2022-04-29 15:48:27 +02:00
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
2022-04-29 16:07:12 +02:00
|
|
|
public class WidgetState<T> {
|
|
|
|
public EntryInfo<T> entry;
|
|
|
|
public List<WidgetState<?>> knownStates;
|
2023-03-11 21:21:53 +01:00
|
|
|
public String id;
|
2022-04-29 16:07:12 +02:00
|
|
|
|
2023-03-11 21:21:53 +01:00
|
|
|
public Text error;
|
2022-04-29 15:48:27 +02:00
|
|
|
public boolean inLimits = true;
|
|
|
|
public String tempValue;
|
|
|
|
public T cachedValue;
|
2023-03-11 21:21:53 +01:00
|
|
|
@Nullable public WidgetFactory factory;
|
2022-04-29 16:07:12 +02:00
|
|
|
|
2023-03-11 21:21:53 +01:00
|
|
|
public void initialize(EntryInfo<T> entry, List<WidgetState<?>> knownStates, @Nullable WidgetFactory factory, String prefix) {
|
2022-04-29 16:07:12 +02:00
|
|
|
this.entry = entry;
|
|
|
|
this.knownStates = knownStates;
|
|
|
|
this.factory = factory;
|
2023-03-11 21:21:53 +01:00
|
|
|
this.id = prefix + entry.getName();
|
2022-08-27 17:41:56 +02:00
|
|
|
updateCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateCache() {
|
2022-04-29 16:07:12 +02:00
|
|
|
try {
|
|
|
|
updateCache(entry.getValue());
|
|
|
|
} catch (IllegalAccessException e) {
|
2022-08-27 17:41:56 +02:00
|
|
|
LibJf.LOGGER.error("Could not set widget state cache to current value", e);
|
2022-04-29 16:07:12 +02:00
|
|
|
}
|
|
|
|
}
|
2022-04-29 15:48:27 +02:00
|
|
|
|
|
|
|
public void updateCache(T newValue) {
|
|
|
|
cachedValue = newValue;
|
|
|
|
tempValue = newValue == null ? null : newValue.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void writeToEntry() throws IllegalAccessException {
|
|
|
|
entry.setValue(cachedValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reset() {
|
|
|
|
cachedValue = entry.getDefault();
|
|
|
|
}
|
|
|
|
}
|