From ff3cfeefce2304cd52f6baa3aeea8069602a6176 Mon Sep 17 00:00:00 2001 From: DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> Date: Fri, 10 Jun 2022 20:35:35 +0200 Subject: more dependency options --- .../java/cc/polyfrost/oneconfig/config/Config.java | 28 ++++++++++++++++++++++ .../oneconfig/config/elements/BasicOption.java | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/main/java/cc/polyfrost/oneconfig/config/Config.java b/src/main/java/cc/polyfrost/oneconfig/config/Config.java index f640610..2ba5077 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/Config.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/Config.java @@ -176,6 +176,34 @@ public class Config { optionNames.get(option).addDependency(condition); } + /** + * Disable an option if a certain condition is not met + * + * @param option The name of the field, or if the field is in a page "pageName.fieldName" + * @param dependentOption The option that has to be enabled + */ + protected void addDependency(String option, String dependentOption) { + if (!optionNames.containsKey(option) || !optionNames.containsKey(dependentOption)) return; + optionNames.get(option).addDependency(() -> { + try { + return (boolean) optionNames.get(dependentOption).get(); + } catch (IllegalAccessException ignored) { + return true; + } + }); + } + + /** + * Disable an option if a certain condition is not met + * + * @param option The name of the field, or if the field is in a page "pageName.fieldName" + * @param value The value of the dependency + */ + protected void addDependency(String option, boolean value) { + if (!optionNames.containsKey(option)) return; + optionNames.get(option).addDependency(() -> value); + } + /** * Register a new listener for when an option changes * 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 0408c0a..eba2f9f 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java @@ -44,7 +44,7 @@ public abstract class BasicOption { /** * @return value of variable as Java object */ - protected Object get() throws IllegalAccessException { + public Object get() throws IllegalAccessException { if (field == null) return null; return field.get(parent); } -- cgit