aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/config
diff options
context:
space:
mode:
authorDeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com>2022-06-10 19:27:52 +0200
committerDeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com>2022-06-10 19:27:52 +0200
commit8f3d048dae3269034e4a1685eb4432b268a84880 (patch)
treec166a3dd62213496d9d96f701021944d5fbc56cd /src/main/java/cc/polyfrost/oneconfig/config
parent79731b2cae7405244e1ca305a0ca3de5754fabea (diff)
downloadOneConfig-8f3d048dae3269034e4a1685eb4432b268a84880.tar.gz
OneConfig-8f3d048dae3269034e4a1685eb4432b268a84880.tar.bz2
OneConfig-8f3d048dae3269034e4a1685eb4432b268a84880.zip
listener system
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/config')
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/config/Config.java11
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java22
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);
}
}