From 952e0e05cb497702345841b2c4004e20dbdb8936 Mon Sep 17 00:00:00 2001 From: Joseph Garcia <46433893+free-range-chicken@users.noreply.github.com> Date: Thu, 10 Dec 2020 13:20:55 -0600 Subject: [PATCH] read and write json data to/from file --- .../com/josyf/macrobuttons/MacroButtons.java | 4 ++ .../java/com/josyf/macrobuttons/Message.java | 2 + .../com/josyf/macrobuttons/gui/ButtonGUI.java | 62 ++++++++++++++++++- 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/josyf/macrobuttons/MacroButtons.java b/src/main/java/com/josyf/macrobuttons/MacroButtons.java index c57910f..da48fc6 100644 --- a/src/main/java/com/josyf/macrobuttons/MacroButtons.java +++ b/src/main/java/com/josyf/macrobuttons/MacroButtons.java @@ -50,6 +50,10 @@ public class MacroButtons implements ModInitializer { MinecraftClient.getInstance().player.sendChatMessage("/seed"); } + public static void sayMessage(String message) { + MinecraftClient.getInstance().player.sendChatMessage(message); + } + } diff --git a/src/main/java/com/josyf/macrobuttons/Message.java b/src/main/java/com/josyf/macrobuttons/Message.java index fd77e85..e7f5342 100644 --- a/src/main/java/com/josyf/macrobuttons/Message.java +++ b/src/main/java/com/josyf/macrobuttons/Message.java @@ -1,4 +1,6 @@ package com.josyf.macrobuttons; public class Message { + + public String message; } diff --git a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java index a039fa4..64cdfa4 100644 --- a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java +++ b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java @@ -1,6 +1,9 @@ package com.josyf.macrobuttons.gui; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import com.josyf.macrobuttons.MacroButtons; +import com.josyf.macrobuttons.Message; import io.github.cottonmc.cotton.gui.client.BackgroundPainter; import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription; import io.github.cottonmc.cotton.gui.widget.WButton; @@ -11,11 +14,18 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.text.LiteralText; import net.minecraft.text.TranslatableText; +import java.io.FileWriter; +import java.io.IOException; + public class ButtonGUI extends LightweightGuiDescription { int xValue = 0; int yValue = 1; + private static FileWriter file; + + String JSONMessage = "not yet set"; + public ButtonGUI() { @@ -27,12 +37,21 @@ public class ButtonGUI extends LightweightGuiDescription { // root.add(icon, 0, 2, 1, 1); // example button to play with - WButton button = new WButton(new TranslatableText("Button")); + WButton button = new WButton(new TranslatableText("Serialize")); button.setOnClick(() -> { MacroButtons.printMessage(); + createMessageForSerialization(); }); root.add(button, xValue, yValue, 4, 1); + // example load serialization button + WButton button2 = new WButton(new TranslatableText("Load Serialization")); + button2.setOnClick(() -> { + MacroButtons.printMessage(); + loadSerialization(); + }); + root.add(button2, xValue + 2, yValue, 4, 1); + WLabel label = new WLabel(new LiteralText("Test"), 0xFFFFFF); root.add(label, 0, 4, 2, 1); @@ -41,6 +60,47 @@ public class ButtonGUI extends LightweightGuiDescription { root.validate(this); } + private void createMessageForSerialization() { + Message myMessage = new Message(); + myMessage.message = "hello"; + JSONMessage = JSON.toJSONString(myMessage); + writeToFile(JSONMessage); + } + + private String getJsonMessage() { + return JSONMessage; + } + + private void loadSerialization() { + String JsonMessage = getJsonMessage(); + if (JsonMessage.equals("not yet set")) { + MacroButtons.sayMessage("not yet set!"); + } else { + String deserializedMessage = JSON.parseObject(JsonMessage, String.class); + MacroButtons.sayMessage(deserializedMessage); + } + + } + + private void writeToFile(String jsonMessage) { + try { + // these both write to the correct location + file = new FileWriter("commands.json"); + // file = new FileWriter(MinecraftClient.getInstance().runDirectory + "/command.json"); + file.write(jsonMessage); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + file.flush(); + file.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + } + private void addCommandSection(WGridPanel root) { // Add text field for command entry WTextField textField = new WTextField();