diff --git a/src/main/java/com/josyf/macrobuttons/ConfigFile.java b/src/main/java/com/josyf/macrobuttons/ConfigFile.java index 4fa1867..3ebec7d 100644 --- a/src/main/java/com/josyf/macrobuttons/ConfigFile.java +++ b/src/main/java/com/josyf/macrobuttons/ConfigFile.java @@ -1,20 +1,19 @@ package com.josyf.macrobuttons; +//import com.alibaba.fastjson.JSON; +//import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; import com.cedarsoftware.util.io.JsonWriter; import com.google.gson.Gson; import com.google.gson.GsonBuilder; 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.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; -import java.awt.*; import java.io.*; -import java.util.logging.Level; +import java.util.ArrayList; public class ConfigFile { @@ -49,7 +48,7 @@ public class ConfigFile { MacroButtons.sayMessage(deserializedMessage); } } - + // if commands.json exists, read it, convert it to an array, and append private static void appendToFile(CommandObject commandObject) { JSONArray jsonArray = new JSONArray(); try { @@ -57,15 +56,26 @@ public class ConfigFile { // if commands.json exists, read it, convert it to an array, and append Object obj = parser.parse(new FileReader("commands.json")); - JSONArray array = (JSONArray) parser.parse(obj.toString()); - array.add(commandObject); - writeToFile(array); - System.out.println("attempting to append"); + //ArrayList array = (ArrayList) parser.parse(obj.toString()); + //array.add(commandObject); + //writeToFile(array); + System.out.println("Here is the object: " + obj); + System.out.println(obj.getClass()); + + JSONArray array = (JSONArray) obj; + System.out.println("length of array: " + array.size()); + JSONObject obj2 = (JSONObject)array.get(0); + System.out.println("Qu'est-ce que c'est?"); + System.out.println(obj2.get("name")); - //System.out.println(obj.getClass()); + + System.out.println("obj's class is: " + obj.getClass()); + System.out.println("obj2's class is: " + obj2.getClass()); System.out.println("Array size: " + array.size()); + System.out.println(MacroButtons.masterCommList); + } catch (IOException e) { // commands.json doesn't exist System.out.println("catch 1"); // create json @@ -99,13 +109,25 @@ public class ConfigFile { } } - // TODO: create func initArray to assign global JSONArray masterCommList to commands.json - public static JSONArray initArray() { - Object obj = null; + // TODO: create func initArray to assign commands.json to global array masterCommList + public static ArrayList initArray() { + ArrayList commandObjects = new ArrayList<>(); try { - obj = parser.parse(new FileReader("commands.json")); - JSONArray array = (JSONArray) parser.parse(obj.toString()); - return array; + // assign array to JSONArray using our commands.json as an object + Object obj = parser.parse(new FileReader("commands.json")); + JSONArray array = (JSONArray) obj; + // so "array" is now a JSONArray full of JSONObjects + // now we will iterate through the array and add COs to our local CO array + for (int i = 0; i < array.size(); i++) { + JSONObject childObject = (JSONObject)array.get(i); + //String name = childObject.get("name").toString(); + //String command = childObject.get("command").toString(); + //CommandObject commandObject = new CommandObject(name, command); + //commandObjects.add(commandObject); + commandObjects.add(childObject); + System.out.println(i); + } + return commandObjects; } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { @@ -138,7 +160,8 @@ public class ConfigFile { public static String readFile() { try { Object obj = parser.parse(new FileReader("commands.json")); - String jsonString = JSONObject.toJSONString(obj); + String jsonString = obj.toString(); + //String jsonString = JSONObject.toJSONString(obj); System.out.println(jsonString); return jsonString; } catch (FileNotFoundException e) { diff --git a/src/main/java/com/josyf/macrobuttons/MacroButtons.java b/src/main/java/com/josyf/macrobuttons/MacroButtons.java index 04e1158..aebb602 100644 --- a/src/main/java/com/josyf/macrobuttons/MacroButtons.java +++ b/src/main/java/com/josyf/macrobuttons/MacroButtons.java @@ -11,12 +11,15 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.client.options.KeyBinding; import net.minecraft.client.util.InputUtil; import org.json.simple.JSONArray; +import org.json.simple.JSONObject; import org.lwjgl.glfw.GLFW; +import java.util.ArrayList; + public class MacroButtons implements ModInitializer { public static final String MOD_ID = "mgbuttons"; - public static JSONArray masterCommList; + public static ArrayList masterCommList; @Override public void onInitialize() { @@ -25,6 +28,11 @@ public class MacroButtons implements ModInitializer { initArray(); } + @Override + public void onQuit() { + + } + private void assignGuiToKey() { System.out.println("I'm getting here"); diff --git a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java index 3a73cac..0470284 100644 --- a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java +++ b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java @@ -55,7 +55,7 @@ public class ButtonGUI extends LightweightGuiDescription { // root.add(label, 0, 4, 2, 1); // TODO: addSavedButtons(); - addSavedButtons(); + addSavedButtons(root); addCommandSection(root); @@ -117,19 +117,46 @@ public class ButtonGUI extends LightweightGuiDescription { } + private void addGUIButton(WGridPanel root, int x, String name, String command) { + if (!name.equals("") && !command.equals("")) { + + System.out.println("Here, command is " + command); + String instancedString = command; + + WButton button = new WButton(new TranslatableText(name)); + button.setOnClick(() -> { + MacroButtons.printMessage(instancedString); + System.out.println("Command was " + instancedString); + }); + // int newX = incrementNumber(x, 4); + System.out.println("x val: " + xValue); + System.out.println("y val: " + yValue); + + root.add(button, xValue, yValue, 4, 1); + + adjustBounds(); + + root.validate(this); + + } else { + System.out.println("No name and value entered!"); + } + } + // TODO: addSavedButtons // TODO: Iterate over masterCommList to add corresponding buttons // Array will contain String class types. Convert these to objects. - private void addSavedButtons() { - JSONArray stringCommList = MacroButtons.masterCommList; + private void addSavedButtons(WGridPanel root) { + //JSONArray stringCommList = MacroButtons.masterCommList; JSONArray objCommList; // Array will contain String class types. Convert these to objects - for (int i = 0; i < stringCommList.size(); i++) { - Object anObject = stringCommList.get(i); - //System.out.println(stringCommList.get(i).getClass()); - //anObject - } + System.out.println("I be doin my thing here"); // Then convert the objects to buttons + for (int i = 0; i < MacroButtons.masterCommList.size(); i++) { + String name = MacroButtons.masterCommList.get(i).get("name").toString(); + String command = MacroButtons.masterCommList.get(i).get("command").toString(); + addGUIButton(root, xValue, name, command); + } }