aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/cc')
-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
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/hud/TextHud.java15
4 files changed, 74 insertions, 5 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);
diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/TextHud.java b/src/main/java/cc/polyfrost/oneconfig/hud/TextHud.java
index a604b69..efe2556 100644
--- a/src/main/java/cc/polyfrost/oneconfig/hud/TextHud.java
+++ b/src/main/java/cc/polyfrost/oneconfig/hud/TextHud.java
@@ -3,6 +3,8 @@ package cc.polyfrost.oneconfig.hud;
import cc.polyfrost.oneconfig.config.annotations.Color;
import cc.polyfrost.oneconfig.config.annotations.Dropdown;
import cc.polyfrost.oneconfig.config.core.OneColor;
+import cc.polyfrost.oneconfig.events.EventManager;
+import cc.polyfrost.oneconfig.events.event.Stage;
import cc.polyfrost.oneconfig.events.event.TickEvent;
import cc.polyfrost.oneconfig.internal.hud.HudCore;
import cc.polyfrost.oneconfig.libs.eventbus.Subscribe;
@@ -29,6 +31,7 @@ public abstract class TextHud extends Hud {
public TextHud(boolean enabled, int x, int y) {
super(enabled, x, y);
+ EventManager.INSTANCE.register(new TickHandler());
}
public TextHud(boolean enabled) {
@@ -95,9 +98,13 @@ public abstract class TextHud extends Hud {
return (int) (height * scale);
}
- @Subscribe
- public void onTick(TickEvent event) {
- if (!HudCore.editing) lines = getLines();
- else lines = getExampleLines();
+
+ private class TickHandler {
+ @Subscribe
+ private void onTick(TickEvent event) {
+ if (event.stage != Stage.START) return;
+ if (!HudCore.editing) lines = getLines();
+ else lines = getExampleLines();
+ }
}
} \ No newline at end of file