diff --git a/core/src/main/java/com/seibel/lod/core/config/types/AbstractConfigType.java b/core/src/main/java/com/seibel/lod/core/config/types/AbstractConfigType.java index f81a655df..bb8bcee82 100644 --- a/core/src/main/java/com/seibel/lod/core/config/types/AbstractConfigType.java +++ b/core/src/main/java/com/seibel/lod/core/config/types/AbstractConfigType.java @@ -11,24 +11,15 @@ public abstract class AbstractConfigType { // 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 { // 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 { // The S is the class that is e protected static abstract class Builder { 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 { // 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; - } } } diff --git a/core/src/main/java/com/seibel/lod/core/config/types/ConfigCategory.java b/core/src/main/java/com/seibel/lod/core/config/types/ConfigCategory.java index 72a2111d1..64960cb1d 100644 --- a/core/src/main/java/com/seibel/lod/core/config/types/ConfigCategory.java +++ b/core/src/main/java/com/seibel/lod/core/config/types/ConfigCategory.java @@ -9,8 +9,8 @@ package com.seibel.lod.core.config.types; public class ConfigCategory extends AbstractConfigType { 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 { } public ConfigCategory build() { - return new ConfigCategory(tmpAppearance, tmpValue, tmpDestination, tmpListener); + return new ConfigCategory(tmpAppearance, tmpValue, tmpDestination); } } } diff --git a/core/src/main/java/com/seibel/lod/core/config/types/ConfigEntry.java b/core/src/main/java/com/seibel/lod/core/config/types/ConfigEntry.java index f572d11bd..940d1fa33 100644 --- a/core/src/main/java/com/seibel/lod/core/config/types/ConfigEntry.java +++ b/core/src/main/java/com/seibel/lod/core/config/types/ConfigEntry.java @@ -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 extends AbstractConfigType> implements IConfigEntry { + 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; // API control // /** @@ -24,20 +35,20 @@ public class ConfigEntry extends AbstractConfigType> 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) { + 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 extends AbstractConfigType> 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 extends AbstractConfigType> 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 getListeners() { + return this.listener; + } + /** Sets/Replaces the listener list */ + public void setListeners(ArrayList 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 extends AbstractConfigType> implem private T tmpMax; private boolean tmpUseApiOverwrite; private ConfigEntryPerformance tmpPerformance = ConfigEntryPerformance.DONT_SHOW; + protected ArrayList tmpListener = new ArrayList(); public Builder comment(String newComment) { this.tmpComment = newComment; @@ -216,6 +259,23 @@ public class ConfigEntry extends AbstractConfigType> implem return this; } + public Builder replaceListener(ArrayList newListener) { + this.tmpListener = newListener; + return this; + } + public Builder addListeners(Listener... newListener) { + this.tmpListener.addAll(Arrays.asList(newListener)); + return this; + } + public Builder addListener(Listener newListener) { + this.tmpListener.add(newListener); + return this; + } + public Builder clearListeners() { + this.tmpListener.clear(); + return this; + } + public ConfigEntry build() { diff --git a/core/src/main/java/com/seibel/lod/core/config/types/ConfigUIComment.java b/core/src/main/java/com/seibel/lod/core/config/types/ConfigUIComment.java index fbec4b6cb..ff0b08558 100644 --- a/core/src/main/java/com/seibel/lod/core/config/types/ConfigUIComment.java +++ b/core/src/main/java/com/seibel/lod/core/config/types/ConfigUIComment.java @@ -7,7 +7,7 @@ package com.seibel.lod.core.config.types; */ public class ConfigUIComment extends AbstractConfigType{ 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