From 49c9345807ed4d13d035e27a0665e2f7c63b0c9e Mon Sep 17 00:00:00 2001 From: Roman / Linnea Gräf Date: Wed, 11 Jan 2023 22:03:47 +0100 Subject: Optional Oneconfig support (#401) --- .../compat/oneconfig/OneConfigCompat.java | 42 +++++ .../compat/oneconfig/OneFancyColor.java | 113 ++++++++++++ .../compat/oneconfig/OneFancyKeybind.java | 52 ++++++ .../compat/oneconfig/OneMoulConfig.java | 201 +++++++++++++++++++++ .../compat/oneconfig/OneWrappedKeyBind.java | 58 ++++++ .../compat/oneconfig/WrappedConfigSlider.java | 52 ++++++ 6 files changed, 518 insertions(+) create mode 100644 src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/OneConfigCompat.java create mode 100644 src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/OneFancyColor.java create mode 100644 src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/OneFancyKeybind.java create mode 100644 src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/OneMoulConfig.java create mode 100644 src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/OneWrappedKeyBind.java create mode 100644 src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/WrappedConfigSlider.java (limited to 'src/oneconfig/java/io') diff --git a/src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/OneConfigCompat.java b/src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/OneConfigCompat.java new file mode 100644 index 00000000..b80db70a --- /dev/null +++ b/src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/OneConfigCompat.java @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2022 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 . + */ + +package io.github.moulberry.notenoughupdates.compat.oneconfig; + +import cc.polyfrost.oneconfig.config.data.Mod; +import cc.polyfrost.oneconfig.config.data.ModType; +import io.github.moulberry.notenoughupdates.core.config.Config; +import io.github.moulberry.notenoughupdates.oneconfig.IOneConfigCompat; + +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.reflect.Field; +import java.util.ArrayList; + +public class OneConfigCompat extends IOneConfigCompat { + + Mod mod = new Mod("NotEnoughUpdates", ModType.SKYBLOCK, "/assets/notenoughupdates/logo.png"); + + OneMoulConfig omc; + + @Override + public void initConfig(Config moulConfig, Runnable saveCallback) { + omc = new OneMoulConfig(mod, moulConfig, saveCallback); + } +} diff --git a/src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/OneFancyColor.java b/src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/OneFancyColor.java new file mode 100644 index 00000000..3c70f483 --- /dev/null +++ b/src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/OneFancyColor.java @@ -0,0 +1,113 @@ +/* + * Copyright (C) 2022 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 . + */ + +package io.github.moulberry.notenoughupdates.compat.oneconfig; + +import cc.polyfrost.oneconfig.config.core.OneColor; +import cc.polyfrost.oneconfig.gui.elements.config.ConfigColorElement; +import io.github.moulberry.notenoughupdates.core.ChromaColour; +import lombok.var; + +import java.awt.*; +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.reflect.Field; + +public class OneFancyColor extends ConfigColorElement { + public OneFancyColor( + Field field, + Object parent, + String name, + String description, + String category, + String subcategory, + int size, + boolean allowAlpha + ) { + super(field, parent, name, description, category, subcategory, size, allowAlpha); + } + + @Override + public Object get() throws IllegalAccessException { + String chromaString = (String) super.get(); + + int[] decompose = ChromaColour.decompose(chromaString); + int r = decompose[2]; + int g = decompose[1]; + int b = decompose[0]; + int a = decompose[3]; + int chr = decompose[4]; + + short[] hsba = OneColor.ARGBtoHSBA(new Color(r, g, b, a).getRGB()); + if (chr > 0) { + return new OneColor(hsba[0], hsba[1], hsba[2], hsba[3], (int) ChromaColour.getSecondsForSpeed(chr)); + } + + return new OneColor(r, g, b, a); + } + + private static MethodHandle hsbaAccessor; + private static MethodHandle databitAccessor; + + static { + try { + Field f = OneColor.class.getDeclaredField("hsba"); + Field f2 = OneColor.class.getDeclaredField("dataBit"); + f.setAccessible(true); + f2.setAccessible(true); + var lookup = MethodHandles.lookup(); + hsbaAccessor = lookup.unreflectGetter(f); + databitAccessor = lookup.unreflectGetter(f2); + } catch (NoSuchFieldException | IllegalAccessException e) { + throw new RuntimeException(e); + } + } + + private static short[] getHsba(OneColor color) { + try { + return (short[]) hsbaAccessor.invokeExact(color); + } catch (Throwable e) { + throw new RuntimeException(e); + } + } + + private static int getDataBit(OneColor color) { + try { + return (int) databitAccessor.invokeExact(color); + } catch (Throwable e) { + throw new RuntimeException(e); + } + } + + @Override + protected void set(Object object) throws IllegalAccessException { + OneColor color = (OneColor) object; + float dataBit = getDataBit(color) / 1000F; + short[] hsba = getHsba(color); + int argb = OneColor.HSBAtoARGB(hsba[0], hsba[1], hsba[2], hsba[3]); + Color color1 = new Color(argb, true); + super.set(ChromaColour.special( + dataBit > 0 ? ChromaColour.getSpeedForSeconds(dataBit) : 0, // TODO chroma still sucks + color1.getAlpha(), + color1.getRed(), + color1.getGreen(), + color1.getBlue() + )); + } +} diff --git a/src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/OneFancyKeybind.java b/src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/OneFancyKeybind.java new file mode 100644 index 00000000..823d90ea --- /dev/null +++ b/src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/OneFancyKeybind.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2022 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 . + */ + +package io.github.moulberry.notenoughupdates.compat.oneconfig; + +import cc.polyfrost.oneconfig.config.core.OneKeyBind; +import cc.polyfrost.oneconfig.gui.elements.config.ConfigKeyBind; + +import java.lang.reflect.Field; + +public class OneFancyKeybind extends ConfigKeyBind { + public OneFancyKeybind( + Field field, + Object parent, + String name, + String description, + String category, + String subcategory, + int size + ) { + super(field, parent, name, description, category, subcategory, size); + } + + @Override + public Object get() throws IllegalAccessException { + OneWrappedKeyBind owk = new OneWrappedKeyBind(); + owk.value = (int) super.get(); + return owk; + } + + @Override + protected void set(Object object) throws IllegalAccessException { + OneWrappedKeyBind keyBind = (OneWrappedKeyBind) object; + super.set(keyBind.value); + } +} 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 new file mode 100644 index 00000000..59aa25d1 --- /dev/null +++ b/src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/OneMoulConfig.java @@ -0,0 +1,201 @@ +/* + * Copyright (C) 2022 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 . + */ + +package io.github.moulberry.notenoughupdates.compat.oneconfig; + +import cc.polyfrost.oneconfig.config.core.ConfigUtils; +import cc.polyfrost.oneconfig.config.data.InfoType; +import cc.polyfrost.oneconfig.config.data.Mod; +import cc.polyfrost.oneconfig.config.elements.OptionSubcategory; +import cc.polyfrost.oneconfig.gui.elements.config.ConfigButton; +import cc.polyfrost.oneconfig.gui.elements.config.ConfigDropdown; +import cc.polyfrost.oneconfig.gui.elements.config.ConfigInfo; +import cc.polyfrost.oneconfig.gui.elements.config.ConfigSlider; +import cc.polyfrost.oneconfig.gui.elements.config.ConfigSwitch; +import cc.polyfrost.oneconfig.gui.elements.config.ConfigTextBox; +import io.github.moulberry.notenoughupdates.core.config.Config; +import io.github.moulberry.notenoughupdates.core.config.annotations.Category; +import io.github.moulberry.notenoughupdates.core.config.annotations.ConfigEditorBoolean; +import io.github.moulberry.notenoughupdates.core.config.annotations.ConfigEditorButton; +import io.github.moulberry.notenoughupdates.core.config.annotations.ConfigEditorColour; +import io.github.moulberry.notenoughupdates.core.config.annotations.ConfigEditorDraggableList; +import io.github.moulberry.notenoughupdates.core.config.annotations.ConfigEditorDropdown; +import io.github.moulberry.notenoughupdates.core.config.annotations.ConfigEditorFSR; +import io.github.moulberry.notenoughupdates.core.config.annotations.ConfigEditorKeybind; +import io.github.moulberry.notenoughupdates.core.config.annotations.ConfigEditorSlider; +import io.github.moulberry.notenoughupdates.core.config.annotations.ConfigEditorText; +import io.github.moulberry.notenoughupdates.core.config.annotations.ConfigOption; +import io.github.moulberry.notenoughupdates.core.util.StringUtils; + +import java.lang.reflect.Field; + +public class OneMoulConfig extends cc.polyfrost.oneconfig.config.Config { + + final Config moulConfig; + private final Runnable saveCallback; + + public OneMoulConfig(Mod modData, Config moulConfig, Runnable saveCallback) { + super(modData, "_SHOULD_NOT_BE_WRITTEN.json"); + if (moulConfig == null) throw new IllegalArgumentException("mfw no moulconfig"); + this.moulConfig = moulConfig; + this.saveCallback = saveCallback; + initialize(); + } + + @Override + public void initialize() { + mod.config = this; + + try { + processMoulConfig(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + cc.polyfrost.oneconfig.config.Config.register(mod); + } + + private void processMoulConfig() throws IllegalAccessException { + for (Field categoryField : moulConfig.getClass().getDeclaredFields()) { + Category annotation = categoryField.getAnnotation(Category.class); + if (annotation == null) continue; + Object categoryInstance = categoryField.get(moulConfig); + OptionSubcategory subCategory = ConfigUtils.getSubCategory(mod.defaultPage, annotation.name(), ""); + createPageForCategory(subCategory, categoryInstance); + } + } + + private void createPageForCategory(OptionSubcategory category, Object categoryInstance) { + for (Field optionField : categoryInstance.getClass().getDeclaredFields()) { + ConfigOption annotation = optionField.getAnnotation(ConfigOption.class); + if (annotation == null) continue; + String cat = category.getName(); + String subcategory = ""; + String annotationName = StringUtils.cleanColour( annotation.name()); + String annotationDesc = StringUtils.cleanColour( annotation.desc()); + ConfigEditorBoolean configEditorBoolean = optionField.getAnnotation(ConfigEditorBoolean.class); + if (configEditorBoolean != null) { + category.options.add(new ConfigSwitch( + optionField, + categoryInstance, + annotationName, + annotationDesc, + cat, subcategory, 2 + )); + } + ConfigEditorText configEditorText = optionField.getAnnotation(ConfigEditorText.class); + if (configEditorText != null) { + category.options.add(new ConfigTextBox( + optionField, + categoryInstance, + annotationName, + annotationDesc, + cat, subcategory, 2, + annotationName, false, false + )); + } + ConfigEditorKeybind configEditorKeybind = optionField.getAnnotation(ConfigEditorKeybind.class); + if (configEditorKeybind != null) { + category.options.add(new OneFancyKeybind( + optionField, + categoryInstance, + annotationName, + annotationDesc, + cat, subcategory, 2 + )); + } + ConfigEditorColour configEditorColour = optionField.getAnnotation(ConfigEditorColour.class); + if (configEditorColour != null) { + category.options.add(new OneFancyColor( + optionField, + categoryInstance, + annotationName, + annotationDesc, + cat, subcategory, 2, true + )); + } + ConfigEditorSlider configEditorSlider = optionField.getAnnotation(ConfigEditorSlider.class); + if (configEditorSlider != null) { + category.options.add(new WrappedConfigSlider( + optionField, + categoryInstance, + annotationName, + annotationDesc, + cat, + subcategory, + configEditorSlider.minValue(), + configEditorSlider.maxValue(), + (int) configEditorSlider.minStep() + )); + } + ConfigEditorButton configEditorButton = optionField.getAnnotation(ConfigEditorButton.class); + if (configEditorButton != null) { + category.options.add(new ConfigButton( + () -> moulConfig.executeRunnable(configEditorButton.runnableId()), + categoryInstance, + annotationName, + annotationDesc, + cat, + subcategory, + 2, configEditorButton.buttonText() + )); + } + ConfigEditorDropdown configEditorDropdown = optionField.getAnnotation(ConfigEditorDropdown.class); + if (configEditorDropdown != null) { + category.options.add(new ConfigDropdown( + optionField, + categoryInstance, + annotationName, + annotationDesc, + cat, subcategory, + 2, configEditorDropdown.values() + )); + } + ConfigEditorDraggableList configEditorDraggableList = optionField.getAnnotation(ConfigEditorDraggableList.class); + if (configEditorDraggableList != null) { + category.options.add(new ConfigInfo( + optionField, categoryInstance, + "This option (" + annotationName + ") is not available via the oneconfig gui", + cat, subcategory, 2, InfoType.ERROR + )); + } + ConfigEditorFSR configEditorFSR = optionField.getAnnotation(ConfigEditorFSR.class); + if (configEditorFSR != null) { + category.options.add(new ConfigInfo( + optionField, categoryInstance, + annotationDesc, cat, subcategory, 2, InfoType.WARNING + )); + } + } + } + + @Override + public void save() { + saveCallback.run(); + } + + @Override + public void load() { + } + + @Override + public boolean supportsProfiles() { + return false; + } + +} diff --git a/src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/OneWrappedKeyBind.java b/src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/OneWrappedKeyBind.java new file mode 100644 index 00000000..fa29eaf8 --- /dev/null +++ b/src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/OneWrappedKeyBind.java @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2022 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 . + */ + +package io.github.moulberry.notenoughupdates.compat.oneconfig; + +import cc.polyfrost.oneconfig.config.core.OneKeyBind; +import org.lwjgl.input.Keyboard; + +import java.util.ArrayList; +import java.util.Arrays; + +public class OneWrappedKeyBind extends OneKeyBind { + + public int value = Keyboard.KEY_NONE; + + @Override + public String getDisplay() { + keyBinds.clear(); + keyBinds.addAll(getKeyBinds()); + return super.getDisplay(); + } + + @Override + public void addKey(int key) { + value = key; + } + + @Override + public void clearKeys() { + value = Keyboard.KEY_NONE; + } + + @Override + public int getSize() { + return getKeyBinds().size(); + } + + @Override + public ArrayList getKeyBinds() { + return value == Keyboard.KEY_NONE ? new ArrayList<>() : new ArrayList<>(Arrays.asList(value)); + } +} diff --git a/src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/WrappedConfigSlider.java b/src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/WrappedConfigSlider.java new file mode 100644 index 00000000..98b6a289 --- /dev/null +++ b/src/oneconfig/java/io/github/moulberry/notenoughupdates/compat/oneconfig/WrappedConfigSlider.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2022 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 . + */ + +package io.github.moulberry.notenoughupdates.compat.oneconfig; + +import cc.polyfrost.oneconfig.gui.elements.config.ConfigSlider; + +import java.lang.reflect.Field; + +public class WrappedConfigSlider extends ConfigSlider { + public WrappedConfigSlider( + Field field, + Object parent, + String name, + String description, + String category, + String subcategory, + float min, + float max, + int step + ) { + super(field, parent, name, description, category, subcategory, min, max, step); + } + + @Override + public Object get() throws IllegalAccessException { + Object g = super.get(); + if (g instanceof Double) { + return (float) (double) g; + } + if (g instanceof Long) { + return (int) (long) g; + } + return g; + } +} -- cgit