diff options
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/internal')
6 files changed, 272 insertions, 23 deletions
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/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"); + } + } +} 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) { + } + } + } } |