From f078d92af76f1a5a9a1df727006b95c699ce7139 Mon Sep 17 00:00:00 2001 From: Joseph Garcia Date: Fri, 18 Dec 2020 13:43:50 -0600 Subject: [PATCH] re-fix create json when adding initial btns --- .../com/josyf/macrobuttons/ConfigFile.java | 8 +++++- .../com/josyf/macrobuttons/MacroButtons.java | 2 +- .../com/josyf/macrobuttons/gui/ButtonGUI.java | 25 ++++++++++++------- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/josyf/macrobuttons/ConfigFile.java b/src/main/java/com/josyf/macrobuttons/ConfigFile.java index 5c49a19..ec59e58 100644 --- a/src/main/java/com/josyf/macrobuttons/ConfigFile.java +++ b/src/main/java/com/josyf/macrobuttons/ConfigFile.java @@ -65,8 +65,14 @@ public class ConfigFile { JSONArray array = (JSONArray) obj; array.add(jsonObject); writeToFile(array); + MacroButtons.initArray(); } catch (IOException e) { - e.printStackTrace(); + System.out.println("Commands.json doesn't exist. Creating one...!"); + // create json + JSONArray jsonArray = new JSONArray(); + jsonArray.add(jsonObject); + writeToFile(jsonArray); + MacroButtons.initArray(); } catch (ParseException e) { e.printStackTrace(); } diff --git a/src/main/java/com/josyf/macrobuttons/MacroButtons.java b/src/main/java/com/josyf/macrobuttons/MacroButtons.java index 4174ecb..9a3d72d 100644 --- a/src/main/java/com/josyf/macrobuttons/MacroButtons.java +++ b/src/main/java/com/josyf/macrobuttons/MacroButtons.java @@ -50,7 +50,7 @@ public class MacroButtons implements ModInitializer { } // Assign masterCommList to an array of JSON objects (from commands.json) - private void initArray() { + static void initArray() { masterCommList = ConfigFile.initArray(); if (masterCommList == null) { System.out.println("Error! Master Command List is null"); diff --git a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java index 58af0a5..3c31dc0 100644 --- a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java +++ b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java @@ -117,13 +117,17 @@ public class ButtonGUI extends LightweightGuiDescription { ArrayList commListCopy = MacroButtons.getMasterCommList(); - commListCopy.add(newJsonObject); - System.out.println(MacroButtons.masterCommList); + if (commListCopy != null) { + commListCopy.add(newJsonObject); - // Add jsonObject to commands.json - ConfigFile.appendToFile(newJsonObject); + // Add jsonObject to commands.json + ConfigFile.appendToFile(newJsonObject); + + MacroButtons.setMasterCommList(commListCopy); + } else { + ConfigFile.appendToFile(newJsonObject); + } - MacroButtons.setMasterCommList(commListCopy); adjustBounds(); @@ -176,11 +180,14 @@ public class ButtonGUI extends LightweightGuiDescription { // Array will contain String class types. Convert these to objects System.out.println("I be doin my thing here"); // Then convert the objects to buttons - for (int i = 0; i < commListCopy.size(); i++) { - String name = commListCopy.get(i).get("name").toString(); - String command = commListCopy.get(i).get("command").toString(); - addGUIButton(root, xValue, name, command); + if (commListCopy != null) { + for (int i = 0; i < commListCopy.size(); i++) { + String name = commListCopy.get(i).get("name").toString(); + String command = commListCopy.get(i).get("command").toString(); + addGUIButton(root, xValue, name, command); + } } + }