aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/internal/config
diff options
context:
space:
mode:
authorDeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com>2022-08-10 10:15:01 +0200
committerGitHub <noreply@github.com>2022-08-10 17:15:01 +0900
commit091ce4c72c123f43f317c097818ace15f3a085fa (patch)
tree9a151e29494ef2683b2bff59fec19b56d4a05fb0 /src/main/java/cc/polyfrost/oneconfig/internal/config
parent799c389fdb993e363d71d268e0df9ae9f0a0c8a1 (diff)
downloadOneConfig-091ce4c72c123f43f317c097818ace15f3a085fa.tar.gz
OneConfig-091ce4c72c123f43f317c097818ace15f3a085fa.tar.bz2
OneConfig-091ce4c72c123f43f317c097818ace15f3a085fa.zip
Profile command (#88)
* e * omg finish profile shit * api and fix 1.12.2 preprocess * fix class names
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/internal/config')
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/internal/config/core/ConfigCore.java9
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/internal/config/core/KeyBindHandler.java38
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/internal/config/profiles/Profiles.java129
3 files changed, 165 insertions, 11 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/internal/config/core/ConfigCore.java b/src/main/java/cc/polyfrost/oneconfig/internal/config/core/ConfigCore.java
index a5b8282..ae58760 100644
--- a/src/main/java/cc/polyfrost/oneconfig/internal/config/core/ConfigCore.java
+++ b/src/main/java/cc/polyfrost/oneconfig/internal/config/core/ConfigCore.java
@@ -55,14 +55,11 @@ public class ConfigCore {
}
public static void reInitAll() {
- ArrayList<Mod> data = new ArrayList<>(mods);
- mods.clear();
- HudCore.huds.clear();
- KeyBindHandler.INSTANCE.clearKeyBinds();
- for (Mod modData : data) {
+ for (Mod modData : mods) {
modData.config.initialize();
}
- sortMods();
+ HudCore.reInitHuds();
+ KeyBindHandler.INSTANCE.reInitKeyBinds();
}
public static void sortMods() {
diff --git a/src/main/java/cc/polyfrost/oneconfig/internal/config/core/KeyBindHandler.java b/src/main/java/cc/polyfrost/oneconfig/internal/config/core/KeyBindHandler.java
index dad6176..c893ca1 100644
--- a/src/main/java/cc/polyfrost/oneconfig/internal/config/core/KeyBindHandler.java
+++ b/src/main/java/cc/polyfrost/oneconfig/internal/config/core/KeyBindHandler.java
@@ -30,21 +30,49 @@ import cc.polyfrost.oneconfig.config.core.OneKeyBind;
import cc.polyfrost.oneconfig.events.event.KeyInputEvent;
import cc.polyfrost.oneconfig.libs.eventbus.Subscribe;
-import java.util.ArrayList;
+import java.lang.reflect.Field;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
public class KeyBindHandler {
public static final KeyBindHandler INSTANCE = new KeyBindHandler();
- private final ArrayList<OneKeyBind> keyBinds = new ArrayList<>();
+ private final ConcurrentHashMap<Map.Entry<Field, Object>, OneKeyBind> keyBinds = new ConcurrentHashMap<>();
@Subscribe
private void onKeyPressed(KeyInputEvent event) {
- for (OneKeyBind keyBind : keyBinds) {
+ for (OneKeyBind keyBind : keyBinds.values()) {
if (keyBind.isActive()) keyBind.run();
}
}
- public void addKeyBind(OneKeyBind keyBind) {
- keyBinds.add(keyBind);
+ public void addKeyBind(Field field, Object instance, OneKeyBind keyBind) {
+ keyBinds.put(new Map.Entry<Field, Object>() {
+
+ @Override
+ public Field getKey() {
+ return field;
+ }
+
+ @Override
+ public Object getValue() {
+ return instance;
+ }
+
+ @Override
+ public Object setValue(Object value) {
+ return null;
+ }
+ }, keyBind);
+ }
+
+ public void reInitKeyBinds() {
+ for (Map.Entry<Field, Object> field : keyBinds.keySet()) {
+ if (field.getValue() == null) continue;
+ try {
+ keyBinds.put(field, (OneKeyBind) field.getKey().get(field.getValue()));
+ } catch (IllegalAccessException ignored) {
+ }
+ }
}
public void clearKeyBinds() {
diff --git a/src/main/java/cc/polyfrost/oneconfig/internal/config/profiles/Profiles.java b/src/main/java/cc/polyfrost/oneconfig/internal/config/profiles/Profiles.java
new file mode 100644
index 0000000..baa5d48
--- /dev/null
+++ b/src/main/java/cc/polyfrost/oneconfig/internal/config/profiles/Profiles.java
@@ -0,0 +1,129 @@
+/*
+ * 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.internal.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;
+import java.util.List;
+
+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");
+ private 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 List<String> getProfiles() {
+ return new ArrayList<>(profiles);
+ }
+
+ public static boolean doesProfileExist(String profile) {
+ return profiles.contains(profile);
+ }
+
+ 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");
+ }
+ }
+}