diff --git a/src/main/java/keystrokesmod/Raven.java b/src/main/java/keystrokesmod/Raven.java index 88b3a317..362cef74 100644 --- a/src/main/java/keystrokesmod/Raven.java +++ b/src/main/java/keystrokesmod/Raven.java @@ -88,6 +88,7 @@ public void init(FMLInitializationEvent ignored) { MinecraftForge.EVENT_BUS.register(ModuleManager.slotHandler); MinecraftForge.EVENT_BUS.register(ModuleManager.dynamicManager); MinecraftForge.EVENT_BUS.register(new MoveableManager()); + MinecraftForge.EVENT_BUS.register(profileManager); I18nManager.init(); AutoUpdate.init(); diff --git a/src/main/java/keystrokesmod/utility/profile/ProfileManager.java b/src/main/java/keystrokesmod/utility/profile/ProfileManager.java index f0f7af0e..d82084ad 100644 --- a/src/main/java/keystrokesmod/utility/profile/ProfileManager.java +++ b/src/main/java/keystrokesmod/utility/profile/ProfileManager.java @@ -4,6 +4,7 @@ import keystrokesmod.Raven; import keystrokesmod.clickgui.ClickGui; import keystrokesmod.clickgui.components.impl.CategoryComponent; +import keystrokesmod.event.WorldChangeEvent; import keystrokesmod.module.Module; import keystrokesmod.module.impl.client.Gui; import keystrokesmod.module.impl.other.KillMessage; @@ -17,7 +18,9 @@ import keystrokesmod.script.Manager; import keystrokesmod.utility.Utils; import net.minecraft.client.Minecraft; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import org.jetbrains.annotations.NotNull; +import org.lwjgl.opengl.GL11; import java.io.File; import java.io.FileReader; @@ -26,6 +29,7 @@ import java.util.List; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.concurrent.TimeUnit; public class ProfileManager { public static Minecraft mc = Minecraft.getMinecraft(); @@ -45,13 +49,24 @@ public ProfileManager() { // create a new default profile saveProfile(new Profile("default", 0)); } + + Raven.getExecutor().schedule(this::updateLatest, 5, TimeUnit.MINUTES); + } + + @SubscribeEvent + public void onWorldChange(WorldChangeEvent event) { + updateLatest(); + } + + public void updateLatest() { + saveToLatest(fromCurrentState(-1)); } - public void saveProfile(Profile profile) { + private @NotNull JsonObject fromCurrentState(int keyBind) { JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("clientName", Watermark.customName); jsonObject.addProperty("killmessage", KillMessage.killMessage); - jsonObject.addProperty("keybind", profile.getModule().getKeycode()); + jsonObject.addProperty("keybind", keyBind); JsonArray jsonArray = new JsonArray(); for (Module module : Raven.moduleManager.getModules()) { if (module.ignoreOnSave) { @@ -70,6 +85,11 @@ public void saveProfile(Profile profile) { } } jsonObject.add("modules", jsonArray); + return jsonObject; + } + + public void saveProfile(@NotNull Profile profile) { + JsonObject jsonObject = fromCurrentState(profile.getModule().getKeycode()); try (FileWriter fileWriter = new FileWriter(new File(directory, profile.getName() + ".json"))) { Gson gson = new GsonBuilder().setPrettyPrinting().create(); gson.toJson(jsonObject, fileWriter); @@ -78,6 +98,10 @@ public void saveProfile(Profile profile) { Utils.log.error(e); } + saveToLatest(jsonObject); + } + + public synchronized void saveToLatest(JsonObject jsonObject) { deleteProfile("latest"); try (FileWriter fileWriter = new FileWriter(new File(directory, "latest.json"))) { Gson gson = new GsonBuilder().setPrettyPrinting().create(); @@ -219,12 +243,7 @@ public void loadProfile(String name) { } if (!Objects.equals(name, "latest")) { - deleteProfile("latest"); - try (FileWriter fileWriter = new FileWriter(new File(directory, "latest.json"))) { - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - gson.toJson(profileJson, fileWriter); - } catch (Exception ignored) { - } + saveToLatest(profileJson); } } catch (Exception e) { failedMessage("load", name); diff --git a/src/main/java/keystrokesmod/utility/profile/ProfileModule.java b/src/main/java/keystrokesmod/utility/profile/ProfileModule.java index 2f19309f..4478fae7 100644 --- a/src/main/java/keystrokesmod/utility/profile/ProfileModule.java +++ b/src/main/java/keystrokesmod/utility/profile/ProfileModule.java @@ -8,19 +8,18 @@ import keystrokesmod.utility.Utils; public class ProfileModule extends Module { - private ButtonSetting saveProfile, removeProfile; - private Profile profile; + private final Profile profile; public boolean saved = true; public ProfileModule(Profile profile, String name, int bind) { super(name, category.profiles, bind); this.profile = profile; - this.registerSetting(saveProfile = new ButtonSetting("Save profile", () -> { + this.registerSetting(new ButtonSetting("Save profile", () -> { Utils.sendMessage("&7Saved profile: &b" + getName()); Raven.profileManager.saveProfile(this.profile); saved = true; })); - this.registerSetting(removeProfile = new ButtonSetting("Remove profile", () -> { + this.registerSetting(new ButtonSetting("Remove profile", () -> { Utils.sendMessage("&7Removed profile: &b" + getName()); Raven.profileManager.deleteProfile(getName()); }));