Merge branch 'jsonToGuiButtons'
This commit is contained in:
commit
72448f9206
3
META-INF/MANIFEST.MF
Normal file
3
META-INF/MANIFEST.MF
Normal file
@ -0,0 +1,3 @@
|
||||
Manifest-Version: 1.0
|
||||
Main-Class: com.josyf.macrobuttons.MacroButtons
|
||||
|
21
build.gradle
21
build.gradle
@ -15,6 +15,7 @@ repositories {
|
||||
name = "CottonMC"
|
||||
url = "https://server.bbkr.space/artifactory/libs-release"
|
||||
}
|
||||
jcenter()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
@ -28,16 +29,22 @@ dependencies {
|
||||
|
||||
// Serialization
|
||||
compile group: 'com.alibaba', name: 'fastjson', version: '1.2.56'
|
||||
compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1'
|
||||
compile 'com.googlecode.json-simple:json-simple:1.1.1'
|
||||
|
||||
|
||||
implementation 'com.cedarsoftware:json-io:4.10.1'
|
||||
|
||||
compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
|
||||
|
||||
// includes
|
||||
include 'com.alibaba:fastjson:1.2.56'
|
||||
include 'com.googlecode.json-simple:json-simple:1.1.1'
|
||||
include 'com.cedarsoftware:json-io:4.10.1'
|
||||
|
||||
|
||||
// PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs.
|
||||
// You may need to force-disable transitiveness on them.
|
||||
dependencies {
|
||||
modImplementation "io.github.cottonmc:LibGui:3.2.2+1.16.3"
|
||||
}
|
||||
modImplementation "io.github.cottonmc:LibGui:3.2.2+1.16.3"
|
||||
}
|
||||
|
||||
processResources {
|
||||
@ -81,9 +88,5 @@ publishing {
|
||||
}
|
||||
}
|
||||
|
||||
// select the repositories you want to publish to
|
||||
repositories {
|
||||
// uncomment to publish to the local maven
|
||||
// mavenLocal()
|
||||
}
|
||||
|
||||
}
|
||||
|
5
gradle/wrapper/gradle-wrapper.properties
vendored
5
gradle/wrapper/gradle-wrapper.properties
vendored
@ -1,5 +1,6 @@
|
||||
#Sun Dec 20 14:49:23 CST 2020
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-all.zip
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-bin.zip
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
|
@ -1,7 +0,0 @@
|
||||
package com.josyf.macrobuttons;
|
||||
|
||||
public class CommandObject {
|
||||
|
||||
public String name;
|
||||
public String command;
|
||||
}
|
@ -1,91 +1,43 @@
|
||||
package com.josyf.macrobuttons;
|
||||
|
||||
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.parser.JSONParser;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.ParseException;
|
||||
import org.json.simple.parser.*;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.*;
|
||||
import java.util.logging.Level;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ConfigFile {
|
||||
|
||||
|
||||
static JSONParser parser = new JSONParser();
|
||||
|
||||
private static final 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();
|
||||
newCommand.name = "Say Hello";
|
||||
newCommand.command = "Hello";
|
||||
|
||||
// add new object to config
|
||||
appendToFile(newCommand);
|
||||
|
||||
// set instance to real config
|
||||
}
|
||||
|
||||
public static void loadSerialization() {
|
||||
String JSONConfig = ButtonGUI.getConfig();
|
||||
if (JSONConfig == null) {
|
||||
MacroButtons.sayMessage("GUI Configuration not yet initialized!");
|
||||
} else {
|
||||
//String deserializedMessage = JSON.parseObject(JSONConfig, String.class);
|
||||
String deserializedMessage = readFile();
|
||||
MacroButtons.sayMessage(deserializedMessage);
|
||||
}
|
||||
}
|
||||
|
||||
private static void appendToFile(CommandObject commandObject) {
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
// Read commands.json, convert it to an array, and append A JSON OBJECT
|
||||
public static void appendToFile(JSONObject jsonObject) {
|
||||
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"));
|
||||
JSONArray array = (JSONArray) parser.parse(obj.toString());
|
||||
array.add(commandObject);
|
||||
JSONArray array = (JSONArray) obj;
|
||||
array.add(jsonObject);
|
||||
writeToFile(array);
|
||||
System.out.println("attempting to append");
|
||||
|
||||
|
||||
//System.out.println(obj.getClass());
|
||||
System.out.println("Array size: " + array.size());
|
||||
|
||||
} catch (IOException e) { // commands.json doesn't exist
|
||||
System.out.println("catch 1");
|
||||
// create json
|
||||
jsonArray.add(commandObject);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Commands.json doesn't exist. Creating one...!");
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
jsonArray.add(jsonObject);
|
||||
writeToFile(jsonArray);
|
||||
} catch (ParseException e) {
|
||||
System.out.println("catch 2");
|
||||
|
||||
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();
|
||||
@ -99,40 +51,37 @@ public class ConfigFile {
|
||||
}
|
||||
}
|
||||
|
||||
private static void writeToFile(String jsonMessage) {
|
||||
try {
|
||||
// these both write to the correct location
|
||||
fileWriter = new FileWriter("commands.json");
|
||||
// file = new FileWriter(MinecraftClient.getInstance().runDirectory + "/command.json");
|
||||
System.out.println("APPENDING!");
|
||||
|
||||
fileWriter.write(jsonMessage);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
fileWriter.flush();
|
||||
fileWriter.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static String readFile() {
|
||||
// parses commands.json into array, loop through array and add JSONObjects. returns array
|
||||
public static ArrayList<JSONObject> getArrayFromJsonFile() {
|
||||
ArrayList<JSONObject> commandObjects = new ArrayList<>();
|
||||
try {
|
||||
// assign array to JSONArray using our commands.json as an object
|
||||
Object obj = parser.parse(new FileReader("commands.json"));
|
||||
String jsonString = JSONObject.toJSONString(obj);
|
||||
System.out.println(jsonString);
|
||||
return jsonString;
|
||||
} catch (FileNotFoundException e) {
|
||||
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);
|
||||
commandObjects.add(childObject);
|
||||
System.out.println(i);
|
||||
if (i >= 19) break;
|
||||
}
|
||||
return commandObjects;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "Not yet initialized yo";
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void addObjectToCommList(JSONObject jsonObject) {
|
||||
ArrayList<JSONObject> commListCopy = MacroButtons.getMasterCommList();
|
||||
commListCopy.add(jsonObject);
|
||||
MacroButtons.setMasterCommList(commListCopy);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -10,21 +10,28 @@ import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.options.KeyBinding;
|
||||
import net.minecraft.client.util.InputUtil;
|
||||
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";
|
||||
private static ArrayList<JSONObject> masterCommList;
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
assignGuiToKey();
|
||||
initArray();
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
@ -35,23 +42,32 @@ public class MacroButtons implements ModInitializer {
|
||||
|
||||
ClientTickEvents.END_CLIENT_TICK.register(client -> {
|
||||
while (keyBinding.wasPressed()) {
|
||||
// client.player.sendMessage(new LiteralText("Key 1 was pressed!"), false);
|
||||
MinecraftClient.getInstance().openScreen(new ButtonGUIScreen(new ButtonGUI()));
|
||||
// printMessage();
|
||||
//client.player.closeScreen();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// player can run a command here
|
||||
public static void printMessage() {
|
||||
MinecraftClient.getInstance().player.sendChatMessage("/seed");
|
||||
public static void runCommand(String command) {
|
||||
MinecraftClient.getInstance().player.sendChatMessage(command);
|
||||
}
|
||||
|
||||
public static void sayMessage(String message) {
|
||||
MinecraftClient.getInstance().player.sendChatMessage(message);
|
||||
// Assign masterCommList to JSONArray<objects> (from commands.json). Runs once.
|
||||
static void initArray() {
|
||||
masterCommList = ConfigFile.getArrayFromJsonFile();
|
||||
// If commands.json doesn't exist yet, start a global list variable for future creation
|
||||
if (masterCommList == null) {
|
||||
setMasterCommList(new ArrayList<>());
|
||||
}
|
||||
}
|
||||
|
||||
public static ArrayList<JSONObject> getMasterCommList() {
|
||||
return masterCommList;
|
||||
}
|
||||
|
||||
public static void setMasterCommList(ArrayList<JSONObject> commList) {
|
||||
masterCommList = commList;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -8,85 +8,119 @@ 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.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ButtonGUI extends LightweightGuiDescription {
|
||||
|
||||
int xValue = 0;
|
||||
int yValue = 1;
|
||||
|
||||
private static String ConfigSettings = ConfigFile.readFile();
|
||||
|
||||
public ButtonGUI() {
|
||||
|
||||
// initialize root panel of GUI
|
||||
WGridPanel root = new WGridPanel();
|
||||
|
||||
setupBackground(root);
|
||||
|
||||
// example button to create config JSON
|
||||
WButton button = new WButton(new TranslatableText("Serialize"));
|
||||
button.setOnClick(() -> {
|
||||
MacroButtons.printMessage();
|
||||
ConfigFile.serializeCommand();
|
||||
});
|
||||
root.add(button, xValue, yValue, 4, 1);
|
||||
|
||||
// example load serialization button
|
||||
WButton button2 = new WButton(new TranslatableText("Load Serialization"));
|
||||
button2.setOnClick(() -> {
|
||||
MacroButtons.printMessage();
|
||||
ConfigFile.loadSerialization();
|
||||
});
|
||||
root.add(button2, xValue + 4, yValue, 6, 1);
|
||||
|
||||
// read json file button
|
||||
WButton button3 = new WButton(new TranslatableText("Read command json"));
|
||||
button3.setOnClick(() -> {
|
||||
ConfigFile.readFile();
|
||||
});
|
||||
root.add(button3, xValue + 10, yValue, 6, 1);
|
||||
|
||||
// Text GUI, not needed yet
|
||||
// WLabel label = new WLabel(new LiteralText("Test"), 0xFFFFFF);
|
||||
// root.add(label, 0, 4, 2, 1);
|
||||
|
||||
addSavedButtons(root);
|
||||
addCommandSection(root);
|
||||
|
||||
root.validate(this);
|
||||
}
|
||||
|
||||
public static String getConfig() {
|
||||
return ConfigSettings;
|
||||
}
|
||||
|
||||
private void addCommandSection(WGridPanel root) {
|
||||
// Add text field for command entry
|
||||
WTextField textField = new WTextField();
|
||||
root.add(textField, 6, 12, 6, 1);
|
||||
// Add text field for command NAME entry
|
||||
WTextField nameTextField = new WTextField();
|
||||
nameTextField.setMaxLength(10);
|
||||
nameTextField.setSuggestion("Name");
|
||||
root.add(nameTextField, 0, 12, 6, 1);
|
||||
|
||||
// Add text field for command / entry
|
||||
WTextField commandTextField = new WTextField();
|
||||
commandTextField.setSuggestion("/command");
|
||||
commandTextField.setMaxLength(100);
|
||||
root.add(commandTextField, 6, 12, 11, 1);
|
||||
|
||||
// Add button for command entry
|
||||
WButton addCmdBtn = new WButton(new TranslatableText("+"));
|
||||
addCmdBtn.setOnClick(() -> {
|
||||
addGUIButton(root, xValue);
|
||||
addGUIButton(root, nameTextField, commandTextField);
|
||||
});
|
||||
root.add(addCmdBtn, 13, 12, 1, 1);
|
||||
root.add(addCmdBtn, 18, 12, 1, 1);
|
||||
}
|
||||
|
||||
private void addGUIButton(WGridPanel root, int x) {
|
||||
WButton button = new WButton(new TranslatableText("Button"));
|
||||
button.setOnClick(() -> {
|
||||
// MacroButtons.printMessage();
|
||||
});
|
||||
// int newX = incrementNumber(x, 4);
|
||||
System.out.println("x val: " + xValue);
|
||||
System.out.println("y val: " + yValue);
|
||||
// Function to save newly added buttons to commands.json
|
||||
private void addGUIButton(WGridPanel root, WTextField name, WTextField command) {
|
||||
// Only add the button if there are contents in both
|
||||
if (!name.getText().equals("") && !command.getText().equals("")) {
|
||||
|
||||
root.add(button, xValue, yValue, 4, 1);
|
||||
if (!isListTooLong()) {
|
||||
String commandString = command.getText();
|
||||
WButton button = new WButton(new TranslatableText(name.getText()));
|
||||
button.setOnClick(() -> {
|
||||
MacroButtons.runCommand(commandString);
|
||||
});
|
||||
root.add(button, xValue, yValue, 4, 1);
|
||||
|
||||
adjustBounds();
|
||||
// Create a new Json object & append to masterCommList
|
||||
JSONObject newJsonObject = new JSONObject();
|
||||
newJsonObject.put("name", name.getText());
|
||||
newJsonObject.put("command", command.getText());
|
||||
|
||||
// // append the buttons to masterList for future loading
|
||||
ConfigFile.addObjectToCommList(newJsonObject);
|
||||
ConfigFile.appendToFile(newJsonObject);
|
||||
|
||||
adjustBounds();
|
||||
}
|
||||
|
||||
name.setText("");
|
||||
command.setText("");
|
||||
|
||||
root.validate(this);
|
||||
|
||||
} else {
|
||||
System.out.println("No name and value entered!");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// function to load buttons from commands.json
|
||||
private void addGUIButton(WGridPanel root, String name, String command) {
|
||||
if (!name.equals("") && !command.equals("")) {
|
||||
WButton button = new WButton(new TranslatableText(name));
|
||||
button.setOnClick(() -> {
|
||||
MacroButtons.runCommand(command);
|
||||
});
|
||||
root.add(button, xValue, yValue, 4, 1);
|
||||
adjustBounds();
|
||||
root.validate(this);
|
||||
} else {
|
||||
System.out.println("No name and value entered!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Array will contain String class types. Convert these to objects.
|
||||
private void addSavedButtons(WGridPanel root) {
|
||||
ArrayList<JSONObject> commListCopy = MacroButtons.getMasterCommList();
|
||||
// Then convert the objects to buttons
|
||||
if (commListCopy != null) {
|
||||
for (int i = 0; i < commListCopy.size(); i++) {
|
||||
String name = commListCopy.get(i).get("name").toString();
|
||||
String command = commListCopy.get(i).get("command").toString();
|
||||
addGUIButton(root, name, command);
|
||||
if (i >= 19) break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void adjustBounds() {
|
||||
if (xValue % 12 == 0 && xValue != 0) {
|
||||
yValue += 2;
|
||||
@ -96,6 +130,10 @@ public class ButtonGUI extends LightweightGuiDescription {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isListTooLong() {
|
||||
return MacroButtons.getMasterCommList().size() > 19;
|
||||
}
|
||||
|
||||
// Change background panel color to transparent black
|
||||
@Override
|
||||
public void addPainters() {
|
||||
@ -105,7 +143,7 @@ public class ButtonGUI extends LightweightGuiDescription {
|
||||
|
||||
private void setupBackground(WGridPanel root) {
|
||||
setRootPanel(root);
|
||||
root.setSize(300, 240);
|
||||
root.setSize(350, 240);
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,7 +1,6 @@
|
||||
package com.josyf.macrobuttons.gui;
|
||||
|
||||
import io.github.cottonmc.cotton.gui.GuiDescription;
|
||||
import io.github.cottonmc.cotton.gui.client.BackgroundPainter;
|
||||
import io.github.cottonmc.cotton.gui.client.CottonClientScreen;
|
||||
|
||||
public class ButtonGUIScreen extends CottonClientScreen {
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"schemaVersion": 1,
|
||||
"id": "mgbuttons",
|
||||
"id": "macro-gui-buttons",
|
||||
"version": "${version}",
|
||||
|
||||
"name": "Macro GUI Buttons",
|
||||
|
Loading…
x
Reference in New Issue
Block a user