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 --- gradle.properties | 2 +- .../java/cc/polyfrost/oneconfig/config/Config.java | 28 ++++++++++++++++++++++ .../oneconfig/config/elements/BasicOption.java | 2 +- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/gradle.properties b/gradle.properties index 5ce40ae..0d10386 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ mod_name = OneConfig mod_id = oneconfig -mod_version = 0.1.0-alpha1 +mod_version = 0.1.0-alpha2 loom.platform = forge essential.defaults.loom=0 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