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();
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) {
try {
Object obj = parser.parse(new FileReader("commands.json"));
JSONArray array = (JSONArray) obj;
array.add(jsonObject);
writeToFile(array);
MacroButtons.initArray();
} catch (IOException e) {
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();
}
@ -55,7 +52,7 @@ public class ConfigFile {
}
// 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<>();
try {
// 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 static final String MOD_ID = "mgbuttons";
public static ArrayList<JSONObject> masterCommList;
private static ArrayList<JSONObject> masterCommList;
@Override
public void onInitialize() {
@ -38,7 +38,6 @@ public class MacroButtons implements ModInitializer {
ClientTickEvents.END_CLIENT_TICK.register(client -> {
while (keyBinding.wasPressed()) {
System.out.println("Key 1 was pressed!");
MinecraftClient.getInstance().openScreen(new ButtonGUIScreen(new ButtonGUI()));
//client.player.closeScreen();
}
@ -49,11 +48,12 @@ public class MacroButtons implements ModInitializer {
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() {
masterCommList = ConfigFile.initArray();
masterCommList = ConfigFile.getArrayFromJsonFile();
// If commands.json doesn't exist yet, start a global list variable for future creation
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()));
button.setOnClick(() -> {
System.out.println("Command was " + instancedString);
MacroButtons.runCommand(instancedString);
});
@ -75,17 +74,9 @@ public class ButtonGUI extends LightweightGuiDescription {
ArrayList<JSONObject> commListCopy = MacroButtons.getMasterCommList();
if (commListCopy != null) {
commListCopy.add(newJsonObject);
// Add jsonObject to commands.json
ConfigFile.appendToFile(newJsonObject);
MacroButtons.setMasterCommList(commListCopy);
} else {
ConfigFile.appendToFile(newJsonObject);
}
commListCopy.add(newJsonObject);
MacroButtons.setMasterCommList(commListCopy);
ConfigFile.appendToFile(newJsonObject);
adjustBounds();
@ -109,20 +100,11 @@ public class ButtonGUI extends LightweightGuiDescription {
WButton button = new WButton(new TranslatableText(name));
button.setOnClick(() -> {
System.out.println("Command was " + 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);
adjustBounds();
root.validate(this);
} else {
@ -136,7 +118,6 @@ public class ButtonGUI extends LightweightGuiDescription {
//JSONArray stringCommList = MacroButtons.masterCommList;
ArrayList<JSONObject> commListCopy = MacroButtons.getMasterCommList();
// 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
if (commListCopy != null) {
for (int i = 0; i < commListCopy.size(); i++) {