Made masterCommList and commands.json more congruent w/ each other

+ More code cleanup & light refactor
This commit is contained in:
Joseph Garcia 2020-12-18 16:28:14 -06:00
parent 9f674f2d1f
commit 5f6ee2b939
3 changed files with 10 additions and 32 deletions

View File

@ -15,21 +15,18 @@ public class ConfigFile {
static JSONParser parser = new JSONParser(); static JSONParser parser = new JSONParser();
private static FileWriter fileWriter; private static FileWriter fileWriter;
// if command.json exists, read it, convert it to an array, and append A JSON OBJECT // Read commands.json, convert it to an array, and append A JSON OBJECT
public static void appendToFile(JSONObject jsonObject) { public static void appendToFile(JSONObject jsonObject) {
try { try {
Object obj = parser.parse(new FileReader("commands.json")); Object obj = parser.parse(new FileReader("commands.json"));
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) {
System.out.println("Commands.json doesn't exist. Creating one...!"); System.out.println("Commands.json doesn't exist. Creating one...!");
// create json
JSONArray jsonArray = new JSONArray(); JSONArray jsonArray = new JSONArray();
jsonArray.add(jsonObject); jsonArray.add(jsonObject);
writeToFile(jsonArray); writeToFile(jsonArray);
MacroButtons.initArray();
} catch (ParseException e) { } catch (ParseException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -55,7 +52,7 @@ public class ConfigFile {
} }
// parses commands.json into array, loop through array and add JSONObjects. returns array // parses commands.json into array, loop through array and add JSONObjects. returns array
public static ArrayList<JSONObject> initArray() { public static ArrayList<JSONObject> getArrayFromJsonFile() {
ArrayList<JSONObject> commandObjects = new ArrayList<>(); ArrayList<JSONObject> commandObjects = new ArrayList<>();
try { try {
// assign array to JSONArray using our commands.json as an object // assign array to JSONArray using our commands.json as an object

View File

@ -18,7 +18,7 @@ import java.util.ArrayList;
public class MacroButtons implements ModInitializer { public class MacroButtons implements ModInitializer {
public static final String MOD_ID = "mgbuttons"; public static final String MOD_ID = "mgbuttons";
public static ArrayList<JSONObject> masterCommList; private static ArrayList<JSONObject> masterCommList;
@Override @Override
public void onInitialize() { public void onInitialize() {
@ -38,7 +38,6 @@ public class MacroButtons implements ModInitializer {
ClientTickEvents.END_CLIENT_TICK.register(client -> { ClientTickEvents.END_CLIENT_TICK.register(client -> {
while (keyBinding.wasPressed()) { while (keyBinding.wasPressed()) {
System.out.println("Key 1 was pressed!");
MinecraftClient.getInstance().openScreen(new ButtonGUIScreen(new ButtonGUI())); MinecraftClient.getInstance().openScreen(new ButtonGUIScreen(new ButtonGUI()));
//client.player.closeScreen(); //client.player.closeScreen();
} }
@ -49,11 +48,12 @@ public class MacroButtons implements ModInitializer {
MinecraftClient.getInstance().player.sendChatMessage(command); MinecraftClient.getInstance().player.sendChatMessage(command);
} }
// Assign masterCommList to an array of JSON objects (from commands.json) // Assign masterCommList to JSONArray<objects> (from commands.json). Runs once.
static void initArray() { static void initArray() {
masterCommList = ConfigFile.initArray(); masterCommList = ConfigFile.getArrayFromJsonFile();
// If commands.json doesn't exist yet, start a global list variable for future creation
if (masterCommList == null) { if (masterCommList == null) {
System.out.println("Error! Master Command List is null"); setMasterCommList(new ArrayList<>());
} }
} }

View File

@ -62,7 +62,6 @@ public class ButtonGUI extends LightweightGuiDescription {
WButton button = new WButton(new TranslatableText(name.getText())); WButton button = new WButton(new TranslatableText(name.getText()));
button.setOnClick(() -> { button.setOnClick(() -> {
System.out.println("Command was " + instancedString);
MacroButtons.runCommand(instancedString); MacroButtons.runCommand(instancedString);
}); });
@ -75,17 +74,9 @@ public class ButtonGUI extends LightweightGuiDescription {
ArrayList<JSONObject> commListCopy = MacroButtons.getMasterCommList(); ArrayList<JSONObject> commListCopy = MacroButtons.getMasterCommList();
if (commListCopy != null) {
commListCopy.add(newJsonObject); commListCopy.add(newJsonObject);
// Add jsonObject to commands.json
ConfigFile.appendToFile(newJsonObject);
MacroButtons.setMasterCommList(commListCopy); MacroButtons.setMasterCommList(commListCopy);
} else {
ConfigFile.appendToFile(newJsonObject); ConfigFile.appendToFile(newJsonObject);
}
adjustBounds(); adjustBounds();
@ -109,20 +100,11 @@ public class ButtonGUI extends LightweightGuiDescription {
WButton button = new WButton(new TranslatableText(name)); WButton button = new WButton(new TranslatableText(name));
button.setOnClick(() -> { button.setOnClick(() -> {
System.out.println("Command was " + instancedString);
MacroButtons.runCommand(instancedString); MacroButtons.runCommand(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); root.add(button, xValue, yValue, 4, 1);
adjustBounds(); adjustBounds();
root.validate(this); root.validate(this);
} else { } else {
@ -136,7 +118,6 @@ public class ButtonGUI extends LightweightGuiDescription {
//JSONArray stringCommList = MacroButtons.masterCommList; //JSONArray stringCommList = MacroButtons.masterCommList;
ArrayList<JSONObject> commListCopy = MacroButtons.getMasterCommList(); ArrayList<JSONObject> commListCopy = MacroButtons.getMasterCommList();
// 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");
// Then convert the objects to buttons // Then convert the objects to buttons
if (commListCopy != null) { if (commListCopy != null) {
for (int i = 0; i < commListCopy.size(); i++) { for (int i = 0; i < commListCopy.size(); i++) {