diff options
author | ev chang <wyvestbusiness@gmail.com> | 2024-05-02 13:22:47 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-02 19:22:47 +0200 |
commit | 585b29b990f586cd10f11dcaaa8b274b8a6ac21f (patch) | |
tree | 69ea895858ef30921e0c2eed51d8516e0e685aca | |
parent | d9171f7557d00d6d53de8a33bc00bc39c40f32b6 (diff) | |
download | NotEnoughUpdates-585b29b990f586cd10f11dcaaa8b274b8a6ac21f.tar.gz NotEnoughUpdates-585b29b990f586cd10f11dcaaa8b274b8a6ac21f.tar.bz2 NotEnoughUpdates-585b29b990f586cd10f11dcaaa8b274b8a6ac21f.zip |
Add support for `Property` option for dropdown (#1132)
3 files changed, 97 insertions, 9 deletions
diff --git a/build.gradle.kts b/build.gradle.kts index 7dccd301..64c1a884 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -348,4 +348,4 @@ applyPublishingInformation( "deobf" to tasks.jar, "all" to tasks.remapJar, "sources" to tasks["sourcesJar"], -) +)
\ No newline at end of file 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]); + } +} |