aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/config
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/config')
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/config/Config.java23
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java23
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/config/profiles/Profiles.java120
3 files changed, 41 insertions, 125 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/config/Config.java b/src/main/java/cc/polyfrost/oneconfig/config/Config.java
index 26aee7e..0f74b31 100644
--- a/src/main/java/cc/polyfrost/oneconfig/config/Config.java
+++ b/src/main/java/cc/polyfrost/oneconfig/config/Config.java
@@ -36,9 +36,9 @@ import cc.polyfrost.oneconfig.config.elements.OptionPage;
import cc.polyfrost.oneconfig.config.elements.OptionSubcategory;
import cc.polyfrost.oneconfig.config.gson.NonProfileSpecificExclusionStrategy;
import cc.polyfrost.oneconfig.config.gson.ProfileExclusionStrategy;
-import cc.polyfrost.oneconfig.config.profiles.Profiles;
+import cc.polyfrost.oneconfig.gui.elements.config.ConfigKeyBind;
+import cc.polyfrost.oneconfig.internal.config.profiles.Profiles;
import cc.polyfrost.oneconfig.gui.OneConfigGui;
-import cc.polyfrost.oneconfig.gui.elements.config.ConfigButton;
import cc.polyfrost.oneconfig.gui.elements.config.ConfigPageButton;
import cc.polyfrost.oneconfig.gui.pages.ModConfigPage;
import cc.polyfrost.oneconfig.hud.HUDUtils;
@@ -54,7 +54,6 @@ import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.reflect.Field;
-import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
@@ -96,6 +95,7 @@ public class Config {
if (Profiles.getProfileFile(configFile).exists()) load();
else if (!hasBeenInitialized && mod.migrator != null) migrate = true;
else save();
+ if (hasBeenInitialized) return;
mod.config = this;
generateOptionList(this, mod.defaultPage, mod, migrate);
if (migrate) save();
@@ -347,8 +347,23 @@ public class Config {
* @param runnable The code to be executed
*/
protected void registerKeyBind(OneKeyBind keyBind, Runnable runnable) {
+ Field field = null;
+ Object instance = null;
+ for (BasicOption option : optionNames.values()) {
+ if (!(option instanceof ConfigKeyBind)) continue;
+ try {
+ Field f = option.getField();
+ OneKeyBind keyBind1 = (OneKeyBind) option.get();
+ if (keyBind1 != keyBind) continue;
+ field = f;
+ instance = option.getParent();
+ } catch (IllegalAccessException ignored) {
+ continue;
+ }
+ break;
+ }
keyBind.setRunnable(runnable);
- KeyBindHandler.INSTANCE.addKeyBind(keyBind);
+ KeyBindHandler.INSTANCE.addKeyBind(field, instance, keyBind);
}
/**
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 7fbc32d..64c31cf 100644
--- a/src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java
+++ b/src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java
@@ -36,7 +36,7 @@ import java.util.function.Supplier;
public abstract class BasicOption {
public final int size;
protected final Field field;
- protected final Object parent;
+ protected Object parent;
public final String name;
public final String category;
public final String subcategory;
@@ -170,4 +170,25 @@ public abstract class BasicOption {
public void addHideCondition(Supplier<Boolean> supplier) {
this.hideConditions.add(supplier);
}
+
+ /**
+ * @return The field
+ */
+ public Field getField() {
+ return field;
+ }
+
+ /**
+ * @return The parent of the field
+ */
+ public Object getParent() {
+ return parent;
+ }
+
+ /**
+ * @param parent The new parent object
+ */
+ public void setParent(Object parent) {
+ this.parent = parent;
+ }
}
diff --git a/src/main/java/cc/polyfrost/oneconfig/config/profiles/Profiles.java b/src/main/java/cc/polyfrost/oneconfig/config/profiles/Profiles.java
deleted file mode 100644
index e40de3d..0000000
--- a/src/main/java/cc/polyfrost/oneconfig/config/profiles/Profiles.java
+++ /dev/null
@@ -1,120 +0,0 @@
-/*
- * This file is part of OneConfig.
- * OneConfig - Next Generation Config Library for Minecraft: Java Edition
- * Copyright (C) 2021, 2022 Polyfrost.
- * <https://polyfrost.cc> <https://github.com/Polyfrost/>
- *
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * OneConfig is licensed under the terms of version 3 of the GNU Lesser
- * General Public License as published by the Free Software Foundation, AND
- * under the Additional Terms Applicable to OneConfig, as published by Polyfrost,
- * either version 1.0 of the Additional Terms, or (at your option) any later
- * version.
- *
- * This program 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. If not, see <https://www.gnu.org/licenses/>. You should
- * have also received a copy of the Additional Terms Applicable
- * to OneConfig, as published by Polyfrost. If not, see
- * <https://polyfrost.cc/legal/oneconfig/additional-terms>
- */
-
-package cc.polyfrost.oneconfig.config.profiles;
-
-import cc.polyfrost.oneconfig.internal.config.OneConfigConfig;
-import cc.polyfrost.oneconfig.internal.config.core.ConfigCore;
-import org.apache.commons.io.FileUtils;
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Logger;
-
-import java.io.File;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Arrays;
-
-public class Profiles {
- private static final Logger LOGGER = LogManager.getLogger("OneConfig Profiles");
- public static final File nonProfileSpecificDir = new File("OneConfig/config");
- public static final File profileDir = new File("OneConfig/profiles");
- public static ArrayList<String> profiles;
-
- public static String getCurrentProfile() {
- if (!profileDir.exists() && !profileDir.mkdir()) {
- LOGGER.fatal("Could not create profiles folder");
- return null;
- }
- if (profiles == null) {
- String[] profilesArray = profileDir.list((file, s) -> file.isDirectory());
- if (profilesArray != null) profiles = new ArrayList<>(Arrays.asList(profilesArray));
- }
- if (!getProfileDir(OneConfigConfig.currentProfile).exists()) {
- createProfile(OneConfigConfig.currentProfile);
- }
- return OneConfigConfig.currentProfile;
- }
-
- public static void createProfile(String name) {
- File folder = new File(profileDir, name);
- if (!folder.exists() && !folder.mkdir()) {
- LOGGER.fatal("Could not create profile folder");
- return;
- }
- profiles.add(name);
- }
-
- public static File getProfileDir() {
- return getProfileDir(getCurrentProfile());
- }
-
- public static File getProfileDir(String profile) {
- return new File(profileDir, profile);
- }
-
- public static File getProfileFile(String file) {
- return new File(getProfileDir(), file);
- }
-
- public static File getNonProfileSpecificDir(String file) {
- return new File(nonProfileSpecificDir, file);
- }
-
- public static void loadProfile(String profile) {
- ConfigCore.saveAll();
- OneConfigConfig.currentProfile = profile;
- OneConfigConfig.getInstance().save();
- ConfigCore.reInitAll();
- }
-
- public static void renameProfile(String name, String newName) {
- try {
- File newFile = new File(profileDir, newName);
- FileUtils.moveDirectory(getProfileDir(name), newFile);
- if (OneConfigConfig.currentProfile.equals(name)) OneConfigConfig.currentProfile = newName;
- profiles.remove(name);
- profiles.add(newName);
- } catch (IOException e) {
- LOGGER.error("Failed to rename profile");
- }
- }
-
- public static void deleteProfile(String name) {
- if (name.equals(getCurrentProfile())) {
- if (profiles.size() == 1) {
- LOGGER.error("Cannot delete only profile!");
- return;
- }
- loadProfile(profiles.stream().filter(entry -> !entry.equals(name)).findFirst().get());
- }
- try {
- FileUtils.deleteDirectory(getProfileDir(name));
- profiles.remove(name);
- } catch (IOException e) {
- LOGGER.error("Failed to delete profile");
- }
- }
-}