From 8f3d048dae3269034e4a1685eb4432b268a84880 Mon Sep 17 00:00:00 2001 From: DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> Date: Fri, 10 Jun 2022 19:27:52 +0200 Subject: listener system --- .../java/cc/polyfrost/oneconfig/config/Config.java | 11 +++++++++++ .../oneconfig/config/elements/BasicOption.java | 22 ++++++++++++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) (limited to 'src/main/java/cc/polyfrost/oneconfig') 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> dependencies = new ArrayList<>(); + private final ArrayList listeners = new ArrayList<>(); /** * Initialize option @@ -82,6 +83,16 @@ public abstract class BasicOption { public void keyTyped(char key, int keyCode) { } + /** + * @return If the option is enabled, based on the dependencies + */ + protected boolean isEnabled() { + for (Supplier dependency : dependencies) { + if (!dependency.get()) return false; + } + return true; + } + /** * Add a condition to this option * @@ -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 dependency : dependencies) { - if (!dependency.get()) return false; - } - return true; + public void addListener(Runnable runnable) { + this.listeners.add(runnable); } } -- cgit