From d5d5196515bc96dd4790a62284be0d95db6d38e7 Mon Sep 17 00:00:00 2001 From: Joseph Garcia <46433893+free-range-chicken@users.noreply.github.com> Date: Thu, 10 Dec 2020 19:23:07 -0600 Subject: [PATCH] change writing json file from string to array --- .../com/josyf/macrobuttons/CommandObject.java | 3 +- .../com/josyf/macrobuttons/ConfigFile.java | 55 ++++++++++++++++++- .../com/josyf/macrobuttons/MacroButtons.java | 1 - .../com/josyf/macrobuttons/gui/ButtonGUI.java | 10 +--- 4 files changed, 57 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/josyf/macrobuttons/CommandObject.java b/src/main/java/com/josyf/macrobuttons/CommandObject.java index a7f958c..91a55de 100644 --- a/src/main/java/com/josyf/macrobuttons/CommandObject.java +++ b/src/main/java/com/josyf/macrobuttons/CommandObject.java @@ -2,5 +2,6 @@ package com.josyf.macrobuttons; public class CommandObject { - public String message; + public String name; + public String command; } diff --git a/src/main/java/com/josyf/macrobuttons/ConfigFile.java b/src/main/java/com/josyf/macrobuttons/ConfigFile.java index dc70552..3c37576 100644 --- a/src/main/java/com/josyf/macrobuttons/ConfigFile.java +++ b/src/main/java/com/josyf/macrobuttons/ConfigFile.java @@ -5,9 +5,11 @@ 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.JSONArray; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; +import java.awt.*; import java.io.*; import java.util.logging.Level; @@ -23,9 +25,21 @@ public class ConfigFile { public static void serializeCommand() { CommandObject newCommand = new CommandObject(); - newCommand.message = "hello"; - ButtonGUI.setConfig(JSON.toJSONString(newCommand)); - writeToFile(ButtonGUI.getConfig()); + newCommand.name = "Say Hello"; + newCommand.command = "Hello"; + // append new object to JSON file + + + // ButtonGUI.setConfig(JSON.toJSONString(newCommand)); + + // get instance of current config + String configInstance = ButtonGUI.getConfig(); + // add new object to config + appendToFile(newCommand); + + // set instance to real config + + //writeToFile(ButtonGUI.getConfig()); } public static void loadSerialization() { @@ -39,11 +53,46 @@ public class ConfigFile { } } + private static void appendToFile(CommandObject commandObject) { + JSONArray jsonArray = null; + try { + jsonArray = (JSONArray) parser.parse(new FileReader("commands.json")); + //JSONArray jsonArray = new JSONArray(); + jsonArray.add(commandObject); + writeToFile(jsonArray); + } catch (IOException e) { + e.printStackTrace(); + } catch (ParseException e) { + e.printStackTrace(); + } + + + } + + private static void writeToFile(JSONArray jsonArray) { + try { + fileWriter = new FileWriter("commands.json"); + String jArrayToString = JSON.toJSONString(jsonArray); + fileWriter.write(jArrayToString); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + fileWriter.flush(); + fileWriter.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + private static void writeToFile(String jsonMessage) { try { // these both write to the correct location fileWriter = new FileWriter("commands.json"); // file = new FileWriter(MinecraftClient.getInstance().runDirectory + "/command.json"); + System.out.println("APPENDING!"); + fileWriter.write(jsonMessage); } catch (IOException e) { e.printStackTrace(); diff --git a/src/main/java/com/josyf/macrobuttons/MacroButtons.java b/src/main/java/com/josyf/macrobuttons/MacroButtons.java index f1b41cd..8cda889 100644 --- a/src/main/java/com/josyf/macrobuttons/MacroButtons.java +++ b/src/main/java/com/josyf/macrobuttons/MacroButtons.java @@ -42,7 +42,6 @@ public class MacroButtons implements ModInitializer { }); } - // player can run a command here public static void printMessage() { MinecraftClient.getInstance().player.sendChatMessage("/seed"); diff --git a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java index 5ae4dca..d00ba41 100644 --- a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java +++ b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java @@ -23,7 +23,7 @@ public class ButtonGUI extends LightweightGuiDescription { setupBackground(root); - // example button to play with + // example button to create config JSON WButton button = new WButton(new TranslatableText("Serialize")); button.setOnClick(() -> { MacroButtons.printMessage(); @@ -37,14 +37,14 @@ public class ButtonGUI extends LightweightGuiDescription { MacroButtons.printMessage(); ConfigFile.loadSerialization(); }); - root.add(button2, xValue + 2, yValue, 4, 1); + root.add(button2, xValue + 4, yValue, 6, 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); + root.add(button3, xValue + 10, yValue, 6, 1); // Text GUI, not needed yet // WLabel label = new WLabel(new LiteralText("Test"), 0xFFFFFF); @@ -100,10 +100,6 @@ public class ButtonGUI extends LightweightGuiDescription { } } - private int incrementNumber(int a, int b) { - return a+b; - } - // Change background panel color to transparent black @Override public void addPainters() {