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