diff options
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/config')
3 files changed, 63 insertions, 1 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/config/Config.java b/src/main/java/cc/polyfrost/oneconfig/config/Config.java index 7af4e0c..d297805 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/Config.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/Config.java @@ -236,6 +236,45 @@ public class Config { } /** + * Hide an option if a certain condition is met + * + * @param option The name of the field, or if the field is in a page "pageName.fieldName" + * @param condition The condition that has to be met for the option to be hidden + */ + protected void hideIf(String option, Supplier<Boolean> condition) { + if (!optionNames.containsKey(option)) return; + optionNames.get(option).addHideCondition(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 hidden + */ + protected void hideIf(String option, String dependentOption) { + if (!optionNames.containsKey(option) || !optionNames.containsKey(dependentOption)) return; + optionNames.get(option).addHideCondition(() -> { + try { + return (boolean) optionNames.get(dependentOption).get(); + } catch (IllegalAccessException ignored) { + return true; + } + }); + } + + /** + * Hide an option if a certain condition is met + * + * @param option The name of the field, or if the field is in a page "pageName.fieldName" + * @param value The value of the condition + */ + protected void hideIf(String option, boolean value) { + if (!optionNames.containsKey(option)) return; + optionNames.get(option).addHideCondition(() -> value); + } + + /** * 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" 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 eba2f9f..86b339b 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java @@ -14,6 +14,7 @@ public abstract class BasicOption { public final String subcategory; private final ArrayList<Supplier<Boolean>> dependencies = new ArrayList<>(); private final ArrayList<Runnable> listeners = new ArrayList<>(); + private final ArrayList<Supplier<Boolean>> hideConditions = new ArrayList<>(); /** * Initialize option @@ -39,6 +40,7 @@ public abstract class BasicOption { protected void set(Object object) throws IllegalAccessException { if (field == null) return; field.set(parent, object); + for (Runnable listener : listeners) listener.run(); } /** @@ -86,13 +88,20 @@ public abstract class BasicOption { /** * @return If the option is enabled, based on the dependencies */ - protected boolean isEnabled() { + public boolean isEnabled() { for (Supplier<Boolean> dependency : dependencies) { if (!dependency.get()) return false; } return true; } + public boolean isHidden() { + for (Supplier<Boolean> condition : hideConditions) { + if (condition.get()) return true; + } + return false; + } + /** * Add a condition to this option * @@ -110,4 +119,13 @@ public abstract class BasicOption { public void addListener(Runnable runnable) { this.listeners.add(runnable); } + + /** + * Hide an option if a condition is met + * + * @param supplier The condition + */ + public void addHideCondition(Supplier<Boolean> supplier) { + this.hideConditions.add(supplier); + } } diff --git a/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionSubcategory.java b/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionSubcategory.java index 66714a4..0066dd8 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionSubcategory.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionSubcategory.java @@ -49,6 +49,7 @@ public class OptionSubcategory { } for (ConfigPageButton page : filteredTop) { + if (page.isHidden()) continue; page.draw(vg, x, optionY); optionY += page.getHeight() + 16; } @@ -57,6 +58,7 @@ public class OptionSubcategory { int backgroundSize = 16; for (int i = 0; i < filteredOptions.size(); i++) { BasicOption option = filteredOptions.get(i); + if (option.isHidden()) continue; if (i + 1 < filteredOptions.size()) { BasicOption nextOption = filteredOptions.get(i + 1); if (option.size == 1 && nextOption.size == 1) { @@ -75,6 +77,7 @@ public class OptionSubcategory { if (filteredOptions.size() > 0) { for (int i = 0; i < filteredOptions.size(); i++) { BasicOption option = filteredOptions.get(i); + if (option.isHidden()) continue; option.draw(vg, x, optionY); if (i + 1 < filteredOptions.size()) { BasicOption nextOption = filteredOptions.get(i + 1); @@ -91,6 +94,7 @@ public class OptionSubcategory { } for (ConfigPageButton page : filteredBottom) { + if (page.isHidden()) continue; page.draw(vg, x, optionY); optionY += page.getHeight() + 16; } @@ -101,6 +105,7 @@ public class OptionSubcategory { public void drawLast(long vg, int x) { for (int i = 0; i < filteredOptions.size(); i++) { BasicOption option = filteredOptions.get(i); + if (option.isHidden()) continue; option.drawLast(vg, x, drawLastY); if (i + 1 < filteredOptions.size()) { BasicOption nextOption = filteredOptions.get(i + 1); |