Skip to content

Commit

Permalink
Add AutoSave
Browse files Browse the repository at this point in the history
  • Loading branch information
xia-mc committed Dec 6, 2024
1 parent 2a73183 commit e4142cf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/main/java/keystrokesmod/Raven.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
35 changes: 27 additions & 8 deletions src/main/java/keystrokesmod/utility/profile/ProfileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}));
Expand Down

0 comments on commit e4142cf

Please sign in to comment.