From 1ed4e67237a2f3d680ccca7609b4c445ee1d6afa Mon Sep 17 00:00:00 2001 From: Joseph Garcia <46433893+free-range-chicken@users.noreply.github.com> Date: Thu, 10 Dec 2020 17:52:42 -0600 Subject: [PATCH] read from file persists over client restarts --- build.gradle | 2 + .../com/josyf/macrobuttons/ConfigFile.java | 43 +++++++++++++++---- .../com/josyf/macrobuttons/gui/ButtonGUI.java | 9 +++- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/build.gradle b/build.gradle index 2cf470c..09245a0 100644 --- a/build.gradle +++ b/build.gradle @@ -28,6 +28,8 @@ dependencies { // Serialization compile group: 'com.alibaba', name: 'fastjson', version: '1.2.56' + compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1' + // PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs. // You may need to force-disable transitiveness on them. diff --git a/src/main/java/com/josyf/macrobuttons/ConfigFile.java b/src/main/java/com/josyf/macrobuttons/ConfigFile.java index 21240ac..dc70552 100644 --- a/src/main/java/com/josyf/macrobuttons/ConfigFile.java +++ b/src/main/java/com/josyf/macrobuttons/ConfigFile.java @@ -1,14 +1,24 @@ package com.josyf.macrobuttons; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import com.josyf.macrobuttons.gui.ButtonGUI; +import io.netty.channel.group.ChannelGroupFuture; +import net.minecraft.client.MinecraftClient; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; -import java.io.FileWriter; -import java.io.IOException; +import java.io.*; +import java.util.logging.Level; public class ConfigFile { - private static FileWriter file; + + static JSONParser parser = new JSONParser(); + + private static FileWriter fileWriter; + private static FileReader fileReader; + private static File file; //private static final String configSettings = ButtonGUI.getConfig(); public static void serializeCommand() { @@ -23,7 +33,8 @@ public class ConfigFile { if (JSONConfig == null) { MacroButtons.sayMessage("GUI Configuration not yet initialized!"); } else { - String deserializedMessage = JSON.parseObject(JSONConfig, String.class); + //String deserializedMessage = JSON.parseObject(JSONConfig, String.class); + String deserializedMessage = readFile(); MacroButtons.sayMessage(deserializedMessage); } } @@ -31,19 +42,35 @@ public class ConfigFile { private static void writeToFile(String jsonMessage) { try { // these both write to the correct location - file = new FileWriter("commands.json"); + fileWriter = new FileWriter("commands.json"); // file = new FileWriter(MinecraftClient.getInstance().runDirectory + "/command.json"); - file.write(jsonMessage); + fileWriter.write(jsonMessage); } catch (IOException e) { e.printStackTrace(); } finally { try { - file.flush(); - file.close(); + fileWriter.flush(); + fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } + + public static String readFile() { + try { + Object obj = parser.parse(new FileReader("commands.json")); + String jsonString = JSONObject.toJSONString(obj); + System.out.println(jsonString); + return jsonString; + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (ParseException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return "Not yet initialized yo"; + } } diff --git a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java index 46aa3f0..5ae4dca 100644 --- a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java +++ b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java @@ -14,7 +14,7 @@ public class ButtonGUI extends LightweightGuiDescription { int xValue = 0; int yValue = 1; - private static String ConfigSettings; + private static String ConfigSettings = ConfigFile.readFile(); public ButtonGUI() { @@ -39,6 +39,13 @@ public class ButtonGUI extends LightweightGuiDescription { }); root.add(button2, xValue + 2, yValue, 4, 1); + // read json file button + WButton button3 = new WButton(new TranslatableText("Read command json")); + button3.setOnClick(() -> { + ConfigFile.readFile(); + }); + root.add(button3, xValue + 6, yValue + 2, 4, 1); + // Text GUI, not needed yet // WLabel label = new WLabel(new LiteralText("Test"), 0xFFFFFF); // root.add(label, 0, 4, 2, 1);