aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/config
diff options
context:
space:
mode:
authorDeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com>2022-06-28 14:14:14 +0200
committerDeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com>2022-06-28 14:14:14 +0200
commitf33aaadbb09e0691479192ad0b5342e8c47f2a1e (patch)
tree077e26f9cb53e269e036aa57afa7bf67c04f87c4 /src/main/java/cc/polyfrost/oneconfig/config
parent59ede4318ef283ac9d81cbe2f97aa6136b30983b (diff)
downloadOneConfig-f33aaadbb09e0691479192ad0b5342e8c47f2a1e.tar.gz
OneConfig-f33aaadbb09e0691479192ad0b5342e8c47f2a1e.tar.bz2
OneConfig-f33aaadbb09e0691479192ad0b5342e8c47f2a1e.zip
hide condition things
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/config')
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/config/Config.java39
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java20
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/config/elements/OptionSubcategory.java5
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);