diff options
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/config/Config.java | 23 | ||||
-rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java | 23 | ||||
-rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java | 11 | ||||
-rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/hud/HUDUtils.java | 19 | ||||
-rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/internal/command/OneConfigCommand.java | 82 | ||||
-rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/internal/config/core/ConfigCore.java | 9 | ||||
-rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/internal/config/core/KeyBindHandler.java | 38 | ||||
-rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/internal/config/profiles/Profiles.java (renamed from src/main/java/cc/polyfrost/oneconfig/config/profiles/Profiles.java) | 13 | ||||
-rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/internal/gui/HudGui.java | 10 | ||||
-rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/internal/hud/HudCore.java | 27 |
10 files changed, 214 insertions, 41 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/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<Page> 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<Field, Object>() { + @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<BasicOption> 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<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/config/profiles/Profiles.java b/src/main/java/cc/polyfrost/oneconfig/internal/config/profiles/Profiles.java index e40de3d..baa5d48 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/profiles/Profiles.java +++ b/src/main/java/cc/polyfrost/oneconfig/internal/config/profiles/Profiles.java @@ -24,7 +24,7 @@ * <https://polyfrost.cc/legal/oneconfig/additional-terms> */ -package cc.polyfrost.oneconfig.config.profiles; +package cc.polyfrost.oneconfig.internal.config.profiles; import cc.polyfrost.oneconfig.internal.config.OneConfigConfig; import cc.polyfrost.oneconfig.internal.config.core.ConfigCore; @@ -36,12 +36,13 @@ 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"); - public static ArrayList<String> profiles; + private static ArrayList<String> profiles; public static String getCurrentProfile() { if (!profileDir.exists() && !profileDir.mkdir()) { @@ -83,6 +84,14 @@ public class Profiles { 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; 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<Float> getXSnappingLines() { ArrayList<Float> 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<Float> getYSnappingLines() { ArrayList<Float> 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<Hud> huds = new ArrayList<>(); + public static final ConcurrentHashMap<Map.Entry<Field, Object>, Hud> huds = new ConcurrentHashMap<>(); + public static final ArrayList<BasicOption> 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, Object> 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) { + } + } + } } |