Fixed up some listener stuff in the config

This commit is contained in:
coolGi
2022-09-25 12:24:41 +09:30
parent 825c72d572
commit 52b26c2046
4 changed files with 68 additions and 24 deletions
@@ -11,24 +11,15 @@ public abstract class AbstractConfigType<T, S> { // The S is the class that is e
public String category = ""; // This should only be set once in the init
public String name; // This should only be set once in the init
protected T value;
protected final Listener listener;
public ConfigBase configBase;
public Object guiValue; // This is a storage variable something like the gui can use
protected ConfigEntryAppearance appearance;
public AbstractConfigType(ConfigEntryAppearance appearance, T value, Listener listener) {
public AbstractConfigType(ConfigEntryAppearance appearance, T value) {
this.appearance = appearance;
this.value = value;
this.listener = listener;
}
public interface Listener {
/** Called whenever the value changes at all (including in the code iself) */
void onModify();
/** Called whenever the value is changed through the UI (only when the done button is pressed) */
void onUiModify(); // TODO
}
@@ -39,8 +30,6 @@ public abstract class AbstractConfigType<T, S> { // The S is the class that is e
/** Sets the value */
public void set(T newValue) {
this.value = newValue;
if (this.listener != null)
this.listener.onModify();
}
public ConfigEntryAppearance getAppearance() {
@@ -73,7 +62,6 @@ public abstract class AbstractConfigType<T, S> { // The S is the class that is e
protected static abstract class Builder<T, S> {
protected ConfigEntryAppearance tmpAppearance = ConfigEntryAppearance.ALL;
protected T tmpValue;
protected Listener tmpListener;
// Put this into your own builder
@@ -85,9 +73,5 @@ public abstract class AbstractConfigType<T, S> { // The S is the class that is e
this.tmpValue = newValue;
return (S) this;
}
public S setListener(Listener newListener) {
this.tmpListener = newListener;
return (S) this;
}
}
}
@@ -9,8 +9,8 @@ package com.seibel.lod.core.config.types;
public class ConfigCategory extends AbstractConfigType<Class, ConfigCategory> {
public String destination; // Where the category goes to
public ConfigCategory(ConfigEntryAppearance appearance, Class value, String destination, Listener listener) {
super(appearance, value, listener);
public ConfigCategory(ConfigEntryAppearance appearance, Class value, String destination) {
super(appearance, value);
this.destination = destination;
}
@@ -39,7 +39,7 @@ public class ConfigCategory extends AbstractConfigType<Class, ConfigCategory> {
}
public ConfigCategory build() {
return new ConfigCategory(tmpAppearance, tmpValue, tmpDestination, tmpListener);
return new ConfigCategory(tmpAppearance, tmpValue, tmpDestination);
}
}
}
@@ -3,6 +3,9 @@ package com.seibel.lod.core.config.types;
import com.seibel.lod.core.interfaces.config.IConfigEntry;
import java.util.ArrayList;
import java.util.Arrays;
/**
* Use for making the config variables
* for types that are not supported by it look in ConfigBase
@@ -12,10 +15,18 @@ import com.seibel.lod.core.interfaces.config.IConfigEntry;
*/
public class ConfigEntry<T> extends AbstractConfigType<T, ConfigEntry<T>> implements IConfigEntry<T>
{
public interface Listener {
/** Called whenever the value changes at all (including in the code iself) */
void onModify();
/** Called whenever the value is changed through the UI (only when the done button is pressed) */
void onUiModify(); // TODO
}
private final T defaultValue;
private String comment;
private T min;
private T max;
private final ArrayList<Listener> listener;
// API control //
/**
@@ -24,20 +35,20 @@ public class ConfigEntry<T> extends AbstractConfigType<T, ConfigEntry<T>> implem
*/
public final boolean allowApiOverride;
private T apiValue;
private Runnable runnable; // What to run when the value gets changed
private final ConfigEntryPerformance performance;
/** Creates the entry */
private ConfigEntry(ConfigEntryAppearance appearance, T value, String comment, T min, T max, boolean allowApiOverride, ConfigEntryPerformance performance, Listener listener) {
super(appearance, value, listener);
private ConfigEntry(ConfigEntryAppearance appearance, T value, String comment, T min, T max, boolean allowApiOverride, ConfigEntryPerformance performance, ArrayList<Listener> listener) {
super(appearance, value);
this.defaultValue = value;
this.comment = comment;
this.min = min;
this.max = max;
this.allowApiOverride = allowApiOverride;
this.performance = performance;
this.listener = listener;
}
@@ -61,6 +72,11 @@ public class ConfigEntry<T> extends AbstractConfigType<T, ConfigEntry<T>> implem
public void set(T newValue) {
super.set(newValue);
save();
this.listener.forEach(Listener::onModify);
}
public void uiSet(T newValue) {
set(newValue);
this.listener.forEach(Listener::onUiModify);
}
@Override
public T get() {
@@ -122,6 +138,32 @@ public class ConfigEntry<T> extends AbstractConfigType<T, ConfigEntry<T>> implem
return this.performance;
}
/** Adds a listener */
public void addListener(Listener newListener) {
this.listener.add(newListener);
}
/** Removes a listener */
public void removeListener(Listener oldListener) {
this.listener.remove(oldListener);
}
/** Removes all listeners */
public void clearListeners() {
this.listener.clear();
}
/** Gets all the listeners */
public ArrayList<Listener> getListeners() {
return this.listener;
}
/** Sets/Replaces the listener list */
public void setListeners(ArrayList<Listener> newListeners) {
this.listener.clear();
this.listener.addAll(newListeners);
}
public void setListeners(Listener... newListeners) {
this.listener.addAll(Arrays.asList(newListeners));
}
/**
* Checks if the option is valid
*
@@ -178,6 +220,7 @@ public class ConfigEntry<T> extends AbstractConfigType<T, ConfigEntry<T>> implem
private T tmpMax;
private boolean tmpUseApiOverwrite;
private ConfigEntryPerformance tmpPerformance = ConfigEntryPerformance.DONT_SHOW;
protected ArrayList<Listener> tmpListener = new ArrayList<Listener>();
public Builder<T> comment(String newComment) {
this.tmpComment = newComment;
@@ -216,6 +259,23 @@ public class ConfigEntry<T> extends AbstractConfigType<T, ConfigEntry<T>> implem
return this;
}
public Builder<T> replaceListener(ArrayList<Listener> newListener) {
this.tmpListener = newListener;
return this;
}
public Builder<T> addListeners(Listener... newListener) {
this.tmpListener.addAll(Arrays.asList(newListener));
return this;
}
public Builder<T> addListener(Listener newListener) {
this.tmpListener.add(newListener);
return this;
}
public Builder<T> clearListeners() {
this.tmpListener.clear();
return this;
}
public ConfigEntry<T> build() {
@@ -7,7 +7,7 @@ package com.seibel.lod.core.config.types;
*/
public class ConfigUIComment extends AbstractConfigType<String, ConfigUIComment>{
public ConfigUIComment(String value) {
super(ConfigEntryAppearance.ONLY_SHOW, value, null); //TODO: Is the listener: null right?
super(ConfigEntryAppearance.ONLY_SHOW, value); //TODO: Is the listener: null right?
}
@Override