primitive loading buttons from file
This commit is contained in:
parent
6db63b8f8c
commit
84105c611b
@ -1,20 +1,19 @@
|
|||||||
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.alibaba.fastjson.JSONObject;
|
|
||||||
import com.cedarsoftware.util.io.JsonWriter;
|
import com.cedarsoftware.util.io.JsonWriter;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
import com.josyf.macrobuttons.gui.ButtonGUI;
|
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.JSONArray;
|
||||||
|
import org.json.simple.JSONObject;
|
||||||
import org.json.simple.parser.JSONParser;
|
import org.json.simple.parser.JSONParser;
|
||||||
import org.json.simple.parser.ParseException;
|
import org.json.simple.parser.ParseException;
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.logging.Level;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class ConfigFile {
|
public class ConfigFile {
|
||||||
|
|
||||||
@ -49,7 +48,7 @@ public class ConfigFile {
|
|||||||
MacroButtons.sayMessage(deserializedMessage);
|
MacroButtons.sayMessage(deserializedMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// if commands.json exists, read it, convert it to an array, and append
|
||||||
private static void appendToFile(CommandObject commandObject) {
|
private static void appendToFile(CommandObject commandObject) {
|
||||||
JSONArray jsonArray = new JSONArray();
|
JSONArray jsonArray = new JSONArray();
|
||||||
try {
|
try {
|
||||||
@ -57,15 +56,26 @@ public class ConfigFile {
|
|||||||
|
|
||||||
// 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"));
|
||||||
JSONArray array = (JSONArray) parser.parse(obj.toString());
|
//ArrayList array = (ArrayList) parser.parse(obj.toString());
|
||||||
array.add(commandObject);
|
//array.add(commandObject);
|
||||||
writeToFile(array);
|
//writeToFile(array);
|
||||||
System.out.println("attempting to append");
|
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("Array size: " + array.size());
|
||||||
|
|
||||||
|
System.out.println(MacroButtons.masterCommList);
|
||||||
|
|
||||||
} catch (IOException e) { // commands.json doesn't exist
|
} catch (IOException e) { // commands.json doesn't exist
|
||||||
System.out.println("catch 1");
|
System.out.println("catch 1");
|
||||||
// create json
|
// create json
|
||||||
@ -99,13 +109,25 @@ public class ConfigFile {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: create func initArray to assign global JSONArray masterCommList to commands.json
|
// TODO: create func initArray to assign commands.json to global array<CommandObjects> masterCommList
|
||||||
public static JSONArray initArray() {
|
public static ArrayList<JSONObject> initArray() {
|
||||||
Object obj = null;
|
ArrayList<JSONObject> commandObjects = new ArrayList<>();
|
||||||
try {
|
try {
|
||||||
obj = parser.parse(new FileReader("commands.json"));
|
// assign array to JSONArray using our commands.json as an object
|
||||||
JSONArray array = (JSONArray) parser.parse(obj.toString());
|
Object obj = parser.parse(new FileReader("commands.json"));
|
||||||
return array;
|
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) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (ParseException e) {
|
} catch (ParseException e) {
|
||||||
@ -138,7 +160,8 @@ public class ConfigFile {
|
|||||||
public static String readFile() {
|
public static String readFile() {
|
||||||
try {
|
try {
|
||||||
Object obj = parser.parse(new FileReader("commands.json"));
|
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);
|
System.out.println(jsonString);
|
||||||
return jsonString;
|
return jsonString;
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
|
@ -11,12 +11,15 @@ import net.minecraft.client.MinecraftClient;
|
|||||||
import net.minecraft.client.options.KeyBinding;
|
import net.minecraft.client.options.KeyBinding;
|
||||||
import net.minecraft.client.util.InputUtil;
|
import net.minecraft.client.util.InputUtil;
|
||||||
import org.json.simple.JSONArray;
|
import org.json.simple.JSONArray;
|
||||||
|
import org.json.simple.JSONObject;
|
||||||
import org.lwjgl.glfw.GLFW;
|
import org.lwjgl.glfw.GLFW;
|
||||||
|
|
||||||
|
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 JSONArray masterCommList;
|
public static ArrayList<JSONObject> masterCommList;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onInitialize() {
|
public void onInitialize() {
|
||||||
@ -25,6 +28,11 @@ public class MacroButtons implements ModInitializer {
|
|||||||
initArray();
|
initArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onQuit() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private void assignGuiToKey() {
|
private void assignGuiToKey() {
|
||||||
|
|
||||||
System.out.println("I'm getting here");
|
System.out.println("I'm getting here");
|
||||||
|
@ -55,7 +55,7 @@ public class ButtonGUI extends LightweightGuiDescription {
|
|||||||
// root.add(label, 0, 4, 2, 1);
|
// root.add(label, 0, 4, 2, 1);
|
||||||
|
|
||||||
// TODO: addSavedButtons();
|
// TODO: addSavedButtons();
|
||||||
addSavedButtons();
|
addSavedButtons(root);
|
||||||
|
|
||||||
addCommandSection(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: addSavedButtons
|
||||||
// TODO: Iterate over masterCommList to add corresponding buttons
|
// TODO: Iterate over masterCommList to add corresponding buttons
|
||||||
// Array will contain String class types. Convert these to objects.
|
// Array will contain String class types. Convert these to objects.
|
||||||
private void addSavedButtons() {
|
private void addSavedButtons(WGridPanel root) {
|
||||||
JSONArray stringCommList = MacroButtons.masterCommList;
|
//JSONArray stringCommList = MacroButtons.masterCommList;
|
||||||
JSONArray objCommList;
|
JSONArray objCommList;
|
||||||
// Array will contain String class types. Convert these to objects
|
// Array will contain String class types. Convert these to objects
|
||||||
for (int i = 0; i < stringCommList.size(); i++) {
|
System.out.println("I be doin my thing here");
|
||||||
Object anObject = stringCommList.get(i);
|
|
||||||
//System.out.println(stringCommList.get(i).getClass());
|
|
||||||
//anObject
|
|
||||||
}
|
|
||||||
// Then convert the objects to buttons
|
// 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user