primitive loading buttons from file

This commit is contained in:
Joseph Garcia 2020-12-15 22:52:51 -06:00
parent 6db63b8f8c
commit 84105c611b
3 changed files with 85 additions and 27 deletions

View File

@ -1,20 +1,19 @@
package com.josyf.macrobuttons;
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cedarsoftware.util.io.JsonWriter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.josyf.macrobuttons.gui.ButtonGUI;
import io.netty.channel.group.ChannelGroupFuture;
import net.minecraft.client.MinecraftClient;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.awt.*;
import java.io.*;
import java.util.logging.Level;
import java.util.ArrayList;
public class ConfigFile {
@ -49,7 +48,7 @@ public class ConfigFile {
MacroButtons.sayMessage(deserializedMessage);
}
}
// if commands.json exists, read it, convert it to an array, and append
private static void appendToFile(CommandObject commandObject) {
JSONArray jsonArray = new JSONArray();
try {
@ -57,15 +56,26 @@ public class ConfigFile {
// if commands.json exists, read it, convert it to an array, and append
Object obj = parser.parse(new FileReader("commands.json"));
JSONArray array = (JSONArray) parser.parse(obj.toString());
array.add(commandObject);
writeToFile(array);
System.out.println("attempting to append");
//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.getClass());
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
@ -99,13 +109,25 @@ public class ConfigFile {
}
}
// TODO: create func initArray to assign global JSONArray masterCommList to commands.json
public static JSONArray initArray() {
Object obj = null;
// TODO: create func initArray to assign commands.json to global array<CommandObjects> masterCommList
public static ArrayList<JSONObject> initArray() {
ArrayList<JSONObject> commandObjects = new ArrayList<>();
try {
obj = parser.parse(new FileReader("commands.json"));
JSONArray array = (JSONArray) parser.parse(obj.toString());
return array;
// assign array to JSONArray using our commands.json as an object
Object obj = parser.parse(new FileReader("commands.json"));
JSONArray array = (JSONArray) obj;
// so "array" is now a JSONArray full of JSONObjects
// 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);
}
return commandObjects;
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
@ -138,7 +160,8 @@ public class ConfigFile {
public static String readFile() {
try {
Object obj = parser.parse(new FileReader("commands.json"));
String jsonString = JSONObject.toJSONString(obj);
String jsonString = obj.toString();
//String jsonString = JSONObject.toJSONString(obj);
System.out.println(jsonString);
return jsonString;
} catch (FileNotFoundException e) {

View File

@ -11,12 +11,15 @@ import net.minecraft.client.MinecraftClient;
import net.minecraft.client.options.KeyBinding;
import net.minecraft.client.util.InputUtil;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.lwjgl.glfw.GLFW;
import java.util.ArrayList;
public class MacroButtons implements ModInitializer {
public static final String MOD_ID = "mgbuttons";
public static JSONArray masterCommList;
public static ArrayList<JSONObject> masterCommList;
@Override
public void onInitialize() {
@ -25,6 +28,11 @@ public class MacroButtons implements ModInitializer {
initArray();
}
@Override
public void onQuit() {
}
private void assignGuiToKey() {
System.out.println("I'm getting here");

View File

@ -55,7 +55,7 @@ public class ButtonGUI extends LightweightGuiDescription {
// root.add(label, 0, 4, 2, 1);
// TODO: addSavedButtons();
addSavedButtons();
addSavedButtons(root);
addCommandSection(root);
@ -117,19 +117,46 @@ public class ButtonGUI extends LightweightGuiDescription {
}
private void addGUIButton(WGridPanel root, int x, String name, String command) {
if (!name.equals("") && !command.equals("")) {
System.out.println("Here, command is " + command);
String instancedString = command;
WButton button = new WButton(new TranslatableText(name));
button.setOnClick(() -> {
MacroButtons.printMessage(instancedString);
System.out.println("Command was " + 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 {
System.out.println("No name and value entered!");
}
}
// TODO: addSavedButtons
// TODO: Iterate over masterCommList to add corresponding buttons
// Array will contain String class types. Convert these to objects.
private void addSavedButtons() {
JSONArray stringCommList = MacroButtons.masterCommList;
private void addSavedButtons(WGridPanel root) {
//JSONArray stringCommList = MacroButtons.masterCommList;
JSONArray objCommList;
// Array will contain String class types. Convert these to objects
for (int i = 0; i < stringCommList.size(); i++) {
Object anObject = stringCommList.get(i);
//System.out.println(stringCommList.get(i).getClass());
//anObject
}
System.out.println("I be doin my thing here");
// Then convert the objects to buttons
for (int i = 0; i < MacroButtons.masterCommList.size(); i++) {
String name = MacroButtons.masterCommList.get(i).get("name").toString();
String command = MacroButtons.masterCommList.get(i).get("command").toString();
addGUIButton(root, xValue, name, command);
}
}