re-fix create json when adding initial btns

This commit is contained in:
Joseph Garcia 2020-12-18 13:43:50 -06:00
parent 46bc34af38
commit f078d92af7
3 changed files with 24 additions and 11 deletions

View File

@ -65,8 +65,14 @@ public class ConfigFile {
JSONArray array = (JSONArray) obj; JSONArray array = (JSONArray) obj;
array.add(jsonObject); array.add(jsonObject);
writeToFile(array); writeToFile(array);
MacroButtons.initArray();
} catch (IOException e) { } 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) { } catch (ParseException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -50,7 +50,7 @@ public class MacroButtons implements ModInitializer {
} }
// Assign masterCommList to an array of JSON objects (from commands.json) // Assign masterCommList to an array of JSON objects (from commands.json)
private void initArray() { static void initArray() {
masterCommList = ConfigFile.initArray(); masterCommList = ConfigFile.initArray();
if (masterCommList == null) { if (masterCommList == null) {
System.out.println("Error! Master Command List is null"); System.out.println("Error! Master Command List is null");

View File

@ -117,13 +117,17 @@ public class ButtonGUI extends LightweightGuiDescription {
ArrayList<JSONObject> commListCopy = MacroButtons.getMasterCommList(); ArrayList<JSONObject> commListCopy = MacroButtons.getMasterCommList();
commListCopy.add(newJsonObject); if (commListCopy != null) {
System.out.println(MacroButtons.masterCommList); commListCopy.add(newJsonObject);
// Add jsonObject to commands.json // Add jsonObject to commands.json
ConfigFile.appendToFile(newJsonObject); ConfigFile.appendToFile(newJsonObject);
MacroButtons.setMasterCommList(commListCopy);
} else {
ConfigFile.appendToFile(newJsonObject);
}
MacroButtons.setMasterCommList(commListCopy);
adjustBounds(); adjustBounds();
@ -176,11 +180,14 @@ public class ButtonGUI extends LightweightGuiDescription {
// Array will contain String class types. Convert these to objects // Array will contain String class types. Convert these to objects
System.out.println("I be doin my thing here"); System.out.println("I be doin my thing here");
// Then convert the objects to buttons // Then convert the objects to buttons
for (int i = 0; i < commListCopy.size(); i++) { if (commListCopy != null) {
String name = commListCopy.get(i).get("name").toString(); for (int i = 0; i < commListCopy.size(); i++) {
String command = commListCopy.get(i).get("command").toString(); String name = commListCopy.get(i).get("name").toString();
addGUIButton(root, xValue, name, command); String command = commListCopy.get(i).get("command").toString();
addGUIButton(root, xValue, name, command);
}
} }
} }