add updated appendToFile
and cleanup comments
This commit is contained in:
parent
98f84809f8
commit
46bc34af38
@ -1,7 +1,5 @@
|
||||
package com.josyf.macrobuttons;
|
||||
|
||||
//import com.alibaba.fastjson.JSON;
|
||||
//import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.cedarsoftware.util.io.JsonWriter;
|
||||
import com.google.gson.Gson;
|
||||
@ -21,81 +19,65 @@ public class ConfigFile {
|
||||
static JSONParser parser = new JSONParser();
|
||||
|
||||
private static FileWriter fileWriter;
|
||||
private static FileReader fileReader;
|
||||
private static File file;
|
||||
//private static final String configSettings = ButtonGUI.getConfig();
|
||||
|
||||
public static void serializeCommand() {
|
||||
|
||||
// CREATE OBJECT
|
||||
CommandObject newCommand = new CommandObject("Say hello", "hello command");
|
||||
|
||||
// add new object to config
|
||||
appendToFile(newCommand);
|
||||
|
||||
// set instance to real config
|
||||
|
||||
//writeToFile(ButtonGUI.getConfig());
|
||||
}
|
||||
|
||||
public static void loadSerialization() {
|
||||
String JSONConfig = ButtonGUI.getConfig();
|
||||
if (JSONConfig == null) {
|
||||
MacroButtons.sayMessage("GUI Configuration not yet initialized!");
|
||||
MacroButtons.runCommand("GUI Configuration not yet initialized!");
|
||||
} else {
|
||||
//String deserializedMessage = JSON.parseObject(JSONConfig, String.class);
|
||||
String deserializedMessage = readFile();
|
||||
MacroButtons.sayMessage(deserializedMessage);
|
||||
System.out.println(readFile());
|
||||
}
|
||||
}
|
||||
|
||||
// if commands.json exists, read it, convert it to an array, and append
|
||||
// DEPRECATED PROBABLY DELETABLE
|
||||
private static void appendToFile(CommandObject commandObject) {
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
try {
|
||||
//jsonArray = (JSONArray) parser.parse(new FileReader("commands.json"));
|
||||
|
||||
// if commands.json exists, read it, convert it to an array, and append
|
||||
Object obj = parser.parse(new FileReader("commands.json"));
|
||||
//ArrayList array = (ArrayList) parser.parse(obj.toString());
|
||||
//array.add(commandObject);
|
||||
//writeToFile(array);
|
||||
System.out.println("Here is the object: " + obj);
|
||||
System.out.println(obj.getClass());
|
||||
|
||||
JSONArray array = (JSONArray) obj;
|
||||
System.out.println("length of array: " + array.size());
|
||||
JSONObject obj2 = (JSONObject)array.get(0);
|
||||
System.out.println("Qu'est-ce que c'est?");
|
||||
System.out.println(obj2.get("name"));
|
||||
|
||||
|
||||
|
||||
System.out.println("obj's class is: " + obj.getClass());
|
||||
System.out.println("obj2's class is: " + obj2.getClass());
|
||||
System.out.println("Array size: " + array.size());
|
||||
|
||||
System.out.println(MacroButtons.masterCommList);
|
||||
|
||||
} catch (IOException e) { // commands.json doesn't exist
|
||||
System.out.println("catch 1");
|
||||
} catch (IOException e) {
|
||||
System.out.println("Commands.json doesn't exist. Creating one...");
|
||||
// create json
|
||||
jsonArray.add(commandObject);
|
||||
writeToFile(jsonArray);
|
||||
} catch (ParseException e) {
|
||||
System.out.println("catch 2");
|
||||
|
||||
System.out.println("Something went wrong |o|");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
// if command.json exists, read it, 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);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// overwrites current commands.json w/ updated jsonArray
|
||||
private static void writeToFile(JSONArray jsonArray) {
|
||||
try {
|
||||
fileWriter = new FileWriter("commands.json");
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
String jArrayToString = JSON.toJSONString(jsonArray);
|
||||
String formattedJson = JsonWriter.formatJson(jArrayToString);
|
||||
//fileWriter.write(jArrayToString);
|
||||
fileWriter.write(formattedJson);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
@ -109,6 +91,7 @@ public class ConfigFile {
|
||||
}
|
||||
}
|
||||
|
||||
// parses commands.json into array, loop through array and add JSONObjects. returns array
|
||||
public static ArrayList<JSONObject> initArray() {
|
||||
ArrayList<JSONObject> commandObjects = new ArrayList<>();
|
||||
try {
|
||||
@ -119,10 +102,6 @@ public class ConfigFile {
|
||||
// now we will iterate through the array and add COs to our local CO array
|
||||
for (int i = 0; i < array.size(); i++) {
|
||||
JSONObject childObject = (JSONObject)array.get(i);
|
||||
//String name = childObject.get("name").toString();
|
||||
//String command = childObject.get("command").toString();
|
||||
//CommandObject commandObject = new CommandObject(name, command);
|
||||
//commandObjects.add(commandObject);
|
||||
commandObjects.add(childObject);
|
||||
System.out.println(i);
|
||||
}
|
||||
@ -135,6 +114,7 @@ public class ConfigFile {
|
||||
return null;
|
||||
}
|
||||
|
||||
// DEPRECATED CAN PROBABLY DELETE
|
||||
private static void writeToFile(String jsonMessage) {
|
||||
try {
|
||||
// these both write to the correct location
|
||||
@ -153,7 +133,6 @@ public class ConfigFile {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static String readFile() {
|
||||
|
@ -14,8 +14,6 @@ import org.json.simple.JSONObject;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
public class MacroButtons implements ModInitializer {
|
||||
|
||||
@ -29,11 +27,7 @@ public class MacroButtons implements ModInitializer {
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void assignGuiToKey() {
|
||||
|
||||
System.out.println("I'm getting here");
|
||||
|
||||
// Currently assigns to the G key
|
||||
KeyBinding keyBinding = KeyBindingHelper.registerKeyBinding(new KeyBinding(
|
||||
"key.macrobuttons.opengui", // The translation key of the keybinding's name
|
||||
@ -51,15 +45,11 @@ public class MacroButtons implements ModInitializer {
|
||||
});
|
||||
}
|
||||
|
||||
// player can run a command here
|
||||
public static void printMessage(String savedCommand) {
|
||||
MinecraftClient.getInstance().player.sendChatMessage(savedCommand);
|
||||
}
|
||||
|
||||
public static void sayMessage(String message) {
|
||||
MinecraftClient.getInstance().player.sendChatMessage(message);
|
||||
public static void runCommand(String command) {
|
||||
MinecraftClient.getInstance().player.sendChatMessage(command);
|
||||
}
|
||||
|
||||
// Assign masterCommList to an array of JSON objects (from commands.json)
|
||||
private void initArray() {
|
||||
masterCommList = ConfigFile.initArray();
|
||||
if (masterCommList == null) {
|
||||
|
@ -1,6 +1,5 @@
|
||||
package com.josyf.macrobuttons.gui;
|
||||
|
||||
import com.josyf.macrobuttons.CommandObject;
|
||||
import com.josyf.macrobuttons.ConfigFile;
|
||||
import com.josyf.macrobuttons.MacroButtons;
|
||||
import io.github.cottonmc.cotton.gui.client.BackgroundPainter;
|
||||
@ -9,7 +8,6 @@ import io.github.cottonmc.cotton.gui.widget.WButton;
|
||||
import io.github.cottonmc.cotton.gui.widget.WGridPanel;
|
||||
import io.github.cottonmc.cotton.gui.widget.WTextField;
|
||||
import net.minecraft.text.TranslatableText;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -32,7 +30,6 @@ public class ButtonGUI extends LightweightGuiDescription {
|
||||
// example button to create config JSON
|
||||
WButton button = new WButton(new TranslatableText("Serialize"));
|
||||
button.setOnClick(() -> {
|
||||
MacroButtons.printMessage("serializing");
|
||||
ConfigFile.serializeCommand();
|
||||
});
|
||||
root.add(button, xValue, yValue + 9, 4, 1);
|
||||
@ -40,7 +37,6 @@ public class ButtonGUI extends LightweightGuiDescription {
|
||||
// example load serialization button
|
||||
WButton button2 = new WButton(new TranslatableText("Load Serialization"));
|
||||
button2.setOnClick(() -> {
|
||||
MacroButtons.printMessage("load serialization");
|
||||
ConfigFile.loadSerialization();
|
||||
});
|
||||
root.add(button2, xValue + 4, yValue + 9, 6, 1);
|
||||
@ -98,6 +94,7 @@ public class ButtonGUI extends LightweightGuiDescription {
|
||||
root.add(addCmdBtn, 18, 12, 1, 1);
|
||||
}
|
||||
|
||||
// function to save newly added buttons to commands.json
|
||||
private void addGUIButton(WGridPanel root, int x, WTextField name, WTextField command) {
|
||||
// Only add the button if there are contents in both
|
||||
if (!name.getText().equals("") && !command.getText().equals("")) {
|
||||
@ -107,26 +104,25 @@ public class ButtonGUI extends LightweightGuiDescription {
|
||||
|
||||
WButton button = new WButton(new TranslatableText(name.getText()));
|
||||
button.setOnClick(() -> {
|
||||
MacroButtons.printMessage(instancedString);
|
||||
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);
|
||||
|
||||
// Create a new Json command object & append to masterCommList
|
||||
// Create a new Json object & append to masterCommList
|
||||
JSONObject newJsonObject = new JSONObject();
|
||||
newJsonObject.put("name", name.getText());
|
||||
newJsonObject.put("command", command.getText());
|
||||
System.out.println(newJsonObject);
|
||||
|
||||
ArrayList<JSONObject> commListCopy = MacroButtons.getMasterCommList();
|
||||
|
||||
commListCopy.add(newJsonObject);
|
||||
System.out.println(MacroButtons.masterCommList);
|
||||
|
||||
// Add jsonObject to commands.json
|
||||
ConfigFile.appendToFile(newJsonObject);
|
||||
|
||||
MacroButtons.setMasterCommList(commListCopy);
|
||||
|
||||
adjustBounds();
|
||||
@ -151,8 +147,8 @@ public class ButtonGUI extends LightweightGuiDescription {
|
||||
|
||||
WButton button = new WButton(new TranslatableText(name));
|
||||
button.setOnClick(() -> {
|
||||
MacroButtons.printMessage(instancedString);
|
||||
System.out.println("Command was " + instancedString);
|
||||
MacroButtons.runCommand(instancedString);
|
||||
});
|
||||
// int newX = incrementNumber(x, 4);
|
||||
//System.out.println("x val: " + xValue);
|
||||
|
Loading…
x
Reference in New Issue
Block a user