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;
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();
}

View File

@ -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");

View File

@ -117,13 +117,17 @@ public class ButtonGUI extends LightweightGuiDescription {
ArrayList<JSONObject> 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);
}
}
}