diff options
-rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/config/Config.java | 11 | ||||
-rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java | 22 |
2 files changed, 27 insertions, 6 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/config/Config.java b/src/main/java/cc/polyfrost/oneconfig/config/Config.java index c3cbe8b..f640610 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/Config.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/Config.java @@ -175,4 +175,15 @@ public class Config { if (!optionNames.containsKey(option)) return; optionNames.get(option).addDependency(condition); } + + /** + * Register a new listener for when an option changes + * + * @param option The name of the field, or if the field is in a page "pageName.fieldName" + * @param runnable What should be executed after the option is changed + */ + protected void addListener(String option, Runnable runnable) { + if (!optionNames.containsKey(option)) return; + optionNames.get(option).addListener(runnable); + } } diff --git a/src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java b/src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java index 947156a..0408c0a 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java @@ -13,6 +13,7 @@ public abstract class BasicOption { public final String category; public final String subcategory; private final ArrayList<Supplier<Boolean>> dependencies = new ArrayList<>(); + private final ArrayList<Runnable> listeners = new ArrayList<>(); /** * Initialize option @@ -83,6 +84,16 @@ public abstract class BasicOption { } /** + * @return If the option is enabled, based on the dependencies + */ + protected boolean isEnabled() { + for (Supplier<Boolean> dependency : dependencies) { + if (!dependency.get()) return false; + } + return true; + } + + /** * Add a condition to this option * * @param supplier The dependency @@ -92,12 +103,11 @@ public abstract class BasicOption { } /** - * @return If the option is enabled, based on the dependencies + * Add a listener to this option + * + * @param runnable The listener */ - protected boolean isEnabled() { - for (Supplier<Boolean> dependency : dependencies) { - if (!dependency.get()) return false; - } - return true; + public void addListener(Runnable runnable) { + this.listeners.add(runnable); } } |