From 091ce4c72c123f43f317c097818ace15f3a085fa Mon Sep 17 00:00:00 2001 From: DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> Date: Wed, 10 Aug 2022 10:15:01 +0200 Subject: Profile command (#88) * e * omg finish profile shit * api and fix 1.12.2 preprocess * fix class names --- api/OneConfig.api | 22 +--- .../java/cc/polyfrost/oneconfig/config/Config.java | 23 +++- .../oneconfig/config/elements/BasicOption.java | 23 +++- .../oneconfig/config/profiles/Profiles.java | 120 ------------------- .../cc/polyfrost/oneconfig/gui/OneConfigGui.java | 11 +- .../java/cc/polyfrost/oneconfig/hud/HUDUtils.java | 19 ++- .../internal/command/OneConfigCommand.java | 82 ++++++++++++- .../oneconfig/internal/config/core/ConfigCore.java | 9 +- .../internal/config/core/KeyBindHandler.java | 38 +++++- .../internal/config/profiles/Profiles.java | 129 +++++++++++++++++++++ .../polyfrost/oneconfig/internal/gui/HudGui.java | 10 +- .../polyfrost/oneconfig/internal/hud/HudCore.java | 27 ++++- .../utils/commands/PlatformCommandManagerImpl.java | 23 ++-- 13 files changed, 348 insertions(+), 188 deletions(-) delete mode 100644 src/main/java/cc/polyfrost/oneconfig/config/profiles/Profiles.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/internal/config/profiles/Profiles.java diff --git a/api/OneConfig.api b/api/OneConfig.api index 3302d86..c904fe9 100644 --- a/api/OneConfig.api +++ b/api/OneConfig.api @@ -279,7 +279,7 @@ public abstract class cc/polyfrost/oneconfig/config/elements/BasicOption { public final field category Ljava/lang/String; protected final field field Ljava/lang/reflect/Field; public final field name Ljava/lang/String; - protected final field parent Ljava/lang/Object; + protected field parent Ljava/lang/Object; public final field size I public final field subcategory Ljava/lang/String; public fun (Ljava/lang/reflect/Field;Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V @@ -289,12 +289,15 @@ public abstract class cc/polyfrost/oneconfig/config/elements/BasicOption { public abstract fun draw (JII)V public fun drawLast (JII)V public fun get ()Ljava/lang/Object; + public fun getField ()Ljava/lang/reflect/Field; public abstract fun getHeight ()I + public fun getParent ()Ljava/lang/Object; public fun isEnabled ()Z public fun isHidden ()Z public fun keyTyped (CI)V public fun reset (Lcc/polyfrost/oneconfig/config/Config;)V protected fun set (Ljava/lang/Object;)V + public fun setParent (Ljava/lang/Object;)V } public class cc/polyfrost/oneconfig/config/elements/OptionCategory { @@ -386,22 +389,6 @@ public abstract interface annotation class cc/polyfrost/oneconfig/config/migrati public abstract fun subcategory ()Ljava/lang/String; } -public class cc/polyfrost/oneconfig/config/profiles/Profiles { - public static final field nonProfileSpecificDir Ljava/io/File; - public static final field profileDir Ljava/io/File; - public static field profiles Ljava/util/ArrayList; - public fun ()V - public static fun createProfile (Ljava/lang/String;)V - public static fun deleteProfile (Ljava/lang/String;)V - public static fun getCurrentProfile ()Ljava/lang/String; - public static fun getNonProfileSpecificDir (Ljava/lang/String;)Ljava/io/File; - public static fun getProfileDir ()Ljava/io/File; - public static fun getProfileDir (Ljava/lang/String;)Ljava/io/File; - public static fun getProfileFile (Ljava/lang/String;)Ljava/io/File; - public static fun loadProfile (Ljava/lang/String;)V - public static fun renameProfile (Ljava/lang/String;Ljava/lang/String;)V -} - public final class cc/polyfrost/oneconfig/events/EventManager { public static final field INSTANCE Lcc/polyfrost/oneconfig/events/EventManager; public fun getEventBus ()Lcc/polyfrost/oneconfig/libs/eventbus/EventBus; @@ -508,7 +495,6 @@ public class cc/polyfrost/oneconfig/gui/OneConfigGui : cc/polyfrost/oneconfig/li public field allowClose Z public field currentColorSelector Lcc/polyfrost/oneconfig/gui/elements/ColorSelector; protected field currentPage Lcc/polyfrost/oneconfig/gui/pages/Page; - public static field instanceToRestore Lcc/polyfrost/oneconfig/gui/OneConfigGui; public field mouseDown Z protected field prevPage Lcc/polyfrost/oneconfig/gui/pages/Page; public fun ()V 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 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. - * - * - * 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 . You should - * have also received a copy of the Additional Terms Applicable - * to OneConfig, as published by Polyfrost. If not, see - * - */ - -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 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"); - } - } -} diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java b/src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java index 429de09..dd802b5 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java @@ -58,7 +58,6 @@ import java.util.ArrayList; public class OneConfigGui extends UScreen implements GuiPause { public static OneConfigGui INSTANCE; - public static OneConfigGui instanceToRestore = null; private final SideBar sideBar = new SideBar(); private final TextInputField textInputField = new TextInputField(248, 40, "Search...", false, false, SVGs.MAGNIFYING_GLASS_BOLD); private final ArrayList previousPages = new ArrayList<>(); @@ -75,21 +74,15 @@ public class OneConfigGui extends UScreen implements GuiPause { public OneConfigGui() { INSTANCE = this; - instanceToRestore = null; } public OneConfigGui(Page page) { INSTANCE = this; - instanceToRestore = null; currentPage = page; } public static OneConfigGui create() { - try { - return instanceToRestore == null ? new OneConfigGui() : instanceToRestore; - } finally { - if (instanceToRestore != null) INSTANCE = instanceToRestore; - } + return INSTANCE == null ? new OneConfigGui() : INSTANCE; } @Override @@ -303,8 +296,6 @@ public class OneConfigGui extends UScreen implements GuiPause { @Override public void onScreenClose() { currentPage.finishUpAndClose(); - instanceToRestore = this; - INSTANCE = null; super.onScreenClose(); } diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/HUDUtils.java b/src/main/java/cc/polyfrost/oneconfig/hud/HUDUtils.java index 526464e..2bc99bd 100644 --- a/src/main/java/cc/polyfrost/oneconfig/hud/HUDUtils.java +++ b/src/main/java/cc/polyfrost/oneconfig/hud/HUDUtils.java @@ -40,6 +40,7 @@ import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import java.util.Map; public class HUDUtils { public static void addHudOptions(OptionPage page, Field field, Object instance, Config config) { @@ -48,7 +49,22 @@ public class HUDUtils { Hud hud = (Hud) ConfigUtils.getField(field, instance); if (hud == null) return; hud.setConfig(config); - HudCore.huds.add(hud); + HudCore.huds.put(new Map.Entry() { + @Override + public Field getKey() { + return field; + } + + @Override + public Object getValue() { + return instance; + } + + @Override + public Object setValue(Object value) { + return null; + } + }, hud); String category = hudAnnotation.category(); String subcategory = hudAnnotation.subcategory(); ArrayList options = new ArrayList<>(); @@ -78,6 +94,7 @@ public class HUDUtils { } } catch (Exception ignored) { } + HudCore.hudOptions.addAll(options); ConfigUtils.getSubCategory(page, hudAnnotation.category(), hudAnnotation.subcategory()).options.addAll(options); } } diff --git a/src/main/java/cc/polyfrost/oneconfig/internal/command/OneConfigCommand.java b/src/main/java/cc/polyfrost/oneconfig/internal/command/OneConfigCommand.java index c93831b..3c1aebb 100644 --- a/src/main/java/cc/polyfrost/oneconfig/internal/command/OneConfigCommand.java +++ b/src/main/java/cc/polyfrost/oneconfig/internal/command/OneConfigCommand.java @@ -26,13 +26,15 @@ package cc.polyfrost.oneconfig.internal.command; +import cc.polyfrost.oneconfig.internal.config.OneConfigConfig; +import cc.polyfrost.oneconfig.internal.config.profiles.Profiles; import cc.polyfrost.oneconfig.internal.gui.HudGui; import cc.polyfrost.oneconfig.gui.OneConfigGui; +import cc.polyfrost.oneconfig.libs.universal.ChatColor; +import cc.polyfrost.oneconfig.libs.universal.UChat; +import cc.polyfrost.oneconfig.utils.commands.annotations.*; import cc.polyfrost.oneconfig.utils.gui.GuiUtils; import cc.polyfrost.oneconfig.utils.InputUtils; -import cc.polyfrost.oneconfig.utils.commands.annotations.Command; -import cc.polyfrost.oneconfig.utils.commands.annotations.Main; -import cc.polyfrost.oneconfig.utils.commands.annotations.SubCommand; /** * The main OneConfig command. @@ -57,8 +59,80 @@ public class OneConfigCommand { private static class DestroySubCommand { @Main private static void main() { - OneConfigGui.instanceToRestore = null; + OneConfigGui.INSTANCE = null; InputUtils.stopBlockingInput(); } } + + @SubCommand(value = "profile", description = "Actions related to profiles.", aliases = {"profiles"}) + private static class ProfileSubCommand { + @SubCommand(value = "list", description = "View all profiles", aliases = {"view"}) + private static class List { + @Main + private static void main() { + StringBuilder builder = new StringBuilder() + .append(ChatColor.GOLD).append("Available profiles:"); + for (String profile : Profiles.getProfiles()) { + builder.append("\n"); + if (OneConfigConfig.currentProfile.equals(profile)) builder.append(ChatColor.GREEN); + else builder.append(ChatColor.RED); + builder.append(profile); + } + UChat.chat(builder.toString()); + } + } + + @SubCommand(value = "switch", description = "Switch to a profile", aliases = {"enable", "set", "load"}) + private static class SwitchProfile { + @Main + private static void main(@Name("profile") @Greedy String profile) { + if (!Profiles.doesProfileExist(profile)) { + UChat.chat(ChatColor.RED + "The profile \"" + profile + "\" does not exist!"); + } else { + Profiles.loadProfile(profile); + UChat.chat(ChatColor.GREEN + "Switched to the \"" + profile + "\" profile."); + } + } + } + + @SubCommand(value = "create", description = "Create a new profile", aliases = {"make"}) + private static class Create { + @Main + private static void main(@Name("profile") @Greedy String profile) { + if (Profiles.doesProfileExist(profile)) { + UChat.chat(ChatColor.RED + "The profile \"" + profile + "\" already exists!"); + } else { + Profiles.createProfile(profile); + if (Profiles.doesProfileExist(profile)) Profiles.loadProfile(profile); + UChat.chat(ChatColor.GREEN + "Created the \"" + profile + "\" profile."); + } + } + } + + @SubCommand(value = "rename", description = "Rename a profile") + private static class Rename { + @Main + private static void main(@Name("Old Name") String profile, @Name("New Name") @Greedy String newName) { + if (!Profiles.doesProfileExist(profile)) { + UChat.chat(ChatColor.RED + "The profile \"" + profile + "\" does not exist!"); + } else { + Profiles.renameProfile(profile, newName); + UChat.chat(ChatColor.GREEN + "Renamed the \"" + profile + "\" profile to \" " + newName + "\"."); + } + } + } + + @SubCommand(value = "delete", description = "Delete a profile", aliases = {"remove", "destroy"}) + private static class Delete { + @Main + private static void main(@Name("profile") @Greedy String profile) { + if (!Profiles.doesProfileExist(profile)) { + UChat.chat(ChatColor.RED + "The profile \"" + profile + "\" does not exist!"); + } else { + Profiles.deleteProfile(profile); + UChat.chat(ChatColor.GREEN + "Deleted the \"" + profile + "\" profile."); + } + } + } + } } \ No newline at end of file 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 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 keyBinds = new ArrayList<>(); + private final ConcurrentHashMap, 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() { + + @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 : 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. + * + * + * 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 . You should + * have also received a copy of the Additional Terms Applicable + * to OneConfig, as published by Polyfrost. If not, see + * + */ + +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 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 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"); + } + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/internal/gui/HudGui.java b/src/main/java/cc/polyfrost/oneconfig/internal/gui/HudGui.java index ad3c8fe..6169896 100644 --- a/src/main/java/cc/polyfrost/oneconfig/internal/gui/HudGui.java +++ b/src/main/java/cc/polyfrost/oneconfig/internal/gui/HudGui.java @@ -85,7 +85,7 @@ public class HudGui extends UScreen implements GuiPause { } float scaleFactor = (float) UResolution.getScaleFactor(); - for (Hud hud : HudCore.huds) { + for (Hud hud : HudCore.huds.values()) { if (!hud.isEnabled()) continue; Position position = hud.position; hud.drawAll(matrixStack, true); @@ -119,7 +119,7 @@ public class HudGui extends UScreen implements GuiPause { return; } } - for (Hud hud : HudCore.huds) { + for (Hud hud : HudCore.huds.values()) { if (!hud.isEnabled() || !mouseClickedHud(hud, (float) mouseX, (float) mouseY)) continue; if (!editingHuds.containsKey(hud)) { if (!UKeyboard.isCtrlKeyDown()) editingHuds.clear(); @@ -185,7 +185,7 @@ public class HudGui extends UScreen implements GuiPause { } editingHuds.clear(); - for (Hud hud : HudCore.huds) { + for (Hud hud : HudCore.huds.values()) { if (!hud.isEnabled()) continue; Position pos = hud.position; if ((x1 <= pos.getX() && x2 >= pos.getX() || x1 <= pos.getRightX() && x2 >= pos.getRightX()) @@ -252,7 +252,7 @@ public class HudGui extends UScreen implements GuiPause { private ArrayList getXSnappingLines() { ArrayList lines = new ArrayList<>(); lines.add(UResolution.getScaledWidth() / 2f); - for (Hud hud : HudCore.huds) { + for (Hud hud : HudCore.huds.values()) { if (!hud.isEnabled() || editingHuds.containsKey(hud)) continue; lines.add(hud.position.getX()); lines.add(hud.position.getCenterX()); @@ -284,7 +284,7 @@ public class HudGui extends UScreen implements GuiPause { private ArrayList getYSnappingLines() { ArrayList lines = new ArrayList<>(); lines.add(UResolution.getScaledHeight() / 2f); - for (Hud hud : HudCore.huds) { + for (Hud hud : HudCore.huds.values()) { if (!hud.isEnabled() || editingHuds.containsKey(hud)) continue; lines.add(hud.position.getY()); lines.add(hud.position.getCenterY()); diff --git a/src/main/java/cc/polyfrost/oneconfig/internal/hud/HudCore.java b/src/main/java/cc/polyfrost/oneconfig/internal/hud/HudCore.java index 5a392e4..d78df55 100644 --- a/src/main/java/cc/polyfrost/oneconfig/internal/hud/HudCore.java +++ b/src/main/java/cc/polyfrost/oneconfig/internal/hud/HudCore.java @@ -26,23 +26,44 @@ package cc.polyfrost.oneconfig.internal.hud; +import cc.polyfrost.oneconfig.config.elements.BasicOption; import cc.polyfrost.oneconfig.events.event.HudRenderEvent; import cc.polyfrost.oneconfig.hud.Hud; import cc.polyfrost.oneconfig.libs.eventbus.Subscribe; -import cc.polyfrost.oneconfig.libs.universal.UResolution; +import java.lang.reflect.Field; import java.util.ArrayList; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; public class HudCore { - public static ArrayList huds = new ArrayList<>(); + public static final ConcurrentHashMap, Hud> huds = new ConcurrentHashMap<>(); + public static final ArrayList hudOptions = new ArrayList<>(); public static boolean editing = false; @Subscribe public void onRender(HudRenderEvent event) { if (editing) return; - for (Hud hud : huds) { + for (Hud hud : huds.values()) { if (!hud.isEnabled()) continue; hud.drawAll(event.matrices, false); } } + + public static void reInitHuds() { + for (Map.Entry field : huds.keySet()) { + try { + field.getKey().setAccessible(true); + Hud oldHud = huds.get(field); + Hud newHud = (Hud) field.getKey().get(field.getValue()); + for (BasicOption option : hudOptions) { + if (option.getParent().equals(oldHud)) { + option.setParent(newHud); + } + } + huds.put(field, newHud); + } catch (IllegalAccessException ignored) { + } + } + } } diff --git a/versions/src/main/java/cc/polyfrost/oneconfig/utils/commands/PlatformCommandManagerImpl.java b/versions/src/main/java/cc/polyfrost/oneconfig/utils/commands/PlatformCommandManagerImpl.java index da44d3b..6399055 100644 --- a/versions/src/main/java/cc/polyfrost/oneconfig/utils/commands/PlatformCommandManagerImpl.java +++ b/versions/src/main/java/cc/polyfrost/oneconfig/utils/commands/PlatformCommandManagerImpl.java @@ -67,11 +67,11 @@ public class PlatformCommandManagerImpl extends PlatformCommandManager { @Override public void - //#if MC<=10809 - processCommand(ICommandSender sender, String[] args) - //#else - //$$ execute(net.minecraft.server.MinecraftServer server, ICommandSender sender, String[] args) - //#endif + //#if MC<=10809 + processCommand(ICommandSender sender, String[] args) + //#else + //$$ execute(net.minecraft.server.MinecraftServer server, ICommandSender sender, String[] args) + //#endif { if (args.length == 0) { if (!root.invokers.isEmpty()) { @@ -141,11 +141,11 @@ public class PlatformCommandManagerImpl extends PlatformCommandManager { @Override public List - //#if MC<=10809 - addTabCompletionOptions(ICommandSender sender, String[] args, BlockPos pos) - //#else - //$$ getTabCompletions(net.minecraft.server.MinecraftServer server, ICommandSender sender, String[] args, BlockPos targetPos) - //#endif + //#if MC<=10809 + addTabCompletionOptions(ICommandSender sender, String[] args, BlockPos pos) + //#else + //$$ getTabCompletions(net.minecraft.server.MinecraftServer server, ICommandSender sender, String[] args, BlockPos targetPos) + //#endif { try { Set> commands = new HashSet<>(); @@ -283,7 +283,8 @@ public class PlatformCommandManagerImpl extends PlatformCommandManager { } boolean added = false; for (CommandManager.InternalCommand.InternalCommandInvoker invoker : command.invokers) { - if (args.length - nextDepth == invoker.parameterTypes.length) { + if (args.length - nextDepth == invoker.parameterTypes.length || + invoker.method.getParameters()[invoker.parameterTypes.length - 1].isAnnotationPresent(Greedy.class)) { commands.add(invoker); added = true; } else { -- cgit