aboutsummaryrefslogtreecommitdiff
path: root/src/oneconfig/java
diff options
context:
space:
mode:
authorev chang <wyvestbusiness@gmail.com>2024-05-02 13:22:47 -0400
committerGitHub <noreply@github.com>2024-05-02 19:22:47 +0200
commit585b29b990f586cd10f11dcaaa8b274b8a6ac21f (patch)
tree69ea895858ef30921e0c2eed51d8516e0e685aca /src/oneconfig/java
parentd9171f7557d00d6d53de8a33bc00bc39c40f32b6 (diff)
downloadNotEnoughUpdates-585b29b990f586cd10f11dcaaa8b274b8a6ac21f.tar.gz
NotEnoughUpdates-585b29b990f586cd10f11dcaaa8b274b8a6ac21f.tar.bz2
NotEnoughUpdates-585b29b990f586cd10f11dcaaa8b274b8a6ac21f.zip
Add support for `Property` option for dropdown (#1132)
Diffstat (limited to 'src/oneconfig/java')
-rw-r--r--src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/OneMoulConfig.java46
-rw-r--r--src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/WrappedConfigDropdown.java58
2 files changed, 96 insertions, 8 deletions
diff --git a/src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/OneMoulConfig.java b/src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/OneMoulConfig.java
index 3e37f12a..179b2103 100644
--- a/src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/OneMoulConfig.java
+++ b/src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/OneMoulConfig.java
@@ -40,9 +40,11 @@ import io.github.moulberry.moulconfig.annotations.ConfigEditorKeybind;
import io.github.moulberry.moulconfig.annotations.ConfigEditorSlider;
import io.github.moulberry.moulconfig.annotations.ConfigEditorText;
import io.github.moulberry.moulconfig.annotations.ConfigOption;
+import io.github.moulberry.moulconfig.observer.Property;
import io.github.moulberry.notenoughupdates.core.util.StringUtils;
import java.lang.reflect.Field;
+import java.util.Arrays;
public class OneMoulConfig extends cc.polyfrost.oneconfig.config.Config {
@@ -154,14 +156,42 @@ public class OneMoulConfig extends cc.polyfrost.oneconfig.config.Config {
}
ConfigEditorDropdown configEditorDropdown = optionField.getAnnotation(ConfigEditorDropdown.class);
if (configEditorDropdown != null) {
- category.options.add(new ConfigDropdown(
- optionField,
- categoryInstance,
- annotationName,
- annotationDesc,
- cat, subcategory,
- 2, configEditorDropdown.values()
- ));
+ if (optionField.getType() == Property.class) {
+ try {
+ Property<?> property = (Property<?>) optionField.get(categoryInstance);
+ Object value = property.get();
+ if (value instanceof Enum<?>) {
+ category.options.add(new WrappedConfigDropdown(
+ optionField,
+ categoryInstance,
+ annotationName,
+ annotationDesc,
+ cat, subcategory,
+ 2, Arrays.stream(value.getClass().getEnumConstants()).map(Object::toString).toArray(String[]::new)
+ ));
+ } else if (value instanceof Integer) {
+ category.options.add(new ConfigDropdown(
+ optionField,
+ categoryInstance,
+ annotationName,
+ annotationDesc,
+ cat, subcategory,
+ 2, configEditorDropdown.values()
+ ));
+ }
+ } catch (IllegalAccessException e) {
+ e.printStackTrace();
+ }
+ } else {
+ category.options.add(new ConfigDropdown(
+ optionField,
+ categoryInstance,
+ annotationName,
+ annotationDesc,
+ cat, subcategory,
+ 2, configEditorDropdown.values()
+ ));
+ }
}
ConfigEditorDraggableList configEditorDraggableList = optionField.getAnnotation(ConfigEditorDraggableList.class);
if (configEditorDraggableList != null) {
diff --git a/src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/WrappedConfigDropdown.java b/src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/WrappedConfigDropdown.java
new file mode 100644
index 00000000..30d1ca15
--- /dev/null
+++ b/src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/WrappedConfigDropdown.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2024 NotEnoughUpdates contributors
+ *
+ * This file is part of NotEnoughUpdates.
+ *
+ * NotEnoughUpdates is free software: you can redistribute it
+ * and/or modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * NotEnoughUpdates is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package io.github.moulberry.notenoughupdates.compat.oneconfig;
+
+import cc.polyfrost.oneconfig.gui.elements.config.ConfigDropdown;
+import io.github.moulberry.moulconfig.observer.Property;
+
+import java.lang.reflect.Field;
+
+public class WrappedConfigDropdown extends ConfigDropdown {
+
+ public WrappedConfigDropdown(
+ Field field,
+ Object parent,
+ String name,
+ String description,
+ String category,
+ String subcategory,
+ int size,
+ String[] options
+ ) {
+ super(field, parent, name, description, category, subcategory, size, options);
+ if (field.getType() != Property.class) {
+ throw new IllegalArgumentException("field must be of type Property");
+ }
+ }
+
+ @Override
+ public Object get() throws IllegalAccessException {
+ if (field == null) return null;
+ Property<Enum> property = (Property<Enum>) field.get(parent);
+ return property.get().ordinal();
+ }
+
+ @Override
+ protected void set(Object object) throws IllegalAccessException {
+ if (field == null) return;
+ Property<Enum> property = (Property<Enum>) field.get(parent);
+ property.set(((Enum[]) property.get().getDeclaringClass().getEnumConstants())[(int) object]);
+ }
+}