From 81a66dd837b5099bf4f0195d7b9e9157d4920893 Mon Sep 17 00:00:00 2001 From: Joseph Garcia Date: Fri, 11 Dec 2020 16:18:09 -0600 Subject: [PATCH 01/12] add proper command submission buttons to bottom of GUI --- .../com/josyf/macrobuttons/ConfigFile.java | 2 +- .../com/josyf/macrobuttons/gui/ButtonGUI.java | 22 +++++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/josyf/macrobuttons/ConfigFile.java b/src/main/java/com/josyf/macrobuttons/ConfigFile.java index 7989748..1440e72 100644 --- a/src/main/java/com/josyf/macrobuttons/ConfigFile.java +++ b/src/main/java/com/josyf/macrobuttons/ConfigFile.java @@ -31,7 +31,7 @@ public class ConfigFile { // CREATE OBJECT CommandObject newCommand = new CommandObject(); newCommand.name = "Say Hello"; - newCommand.command = "Hello"; + newCommand.command = "Hello 2"; // add new object to config appendToFile(newCommand); diff --git a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java index 1054b63..07b6624 100644 --- a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java +++ b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java @@ -23,13 +23,14 @@ public class ButtonGUI extends LightweightGuiDescription { setupBackground(root); + // ######### DEBUG BUTTONS ############ // 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); + root.add(button, xValue, yValue + 9, 4, 1); // example load serialization button WButton button2 = new WButton(new TranslatableText("Load Serialization")); @@ -37,14 +38,15 @@ public class ButtonGUI extends LightweightGuiDescription { MacroButtons.printMessage(); ConfigFile.loadSerialization(); }); - root.add(button2, xValue + 4, yValue, 6, 1); + root.add(button2, xValue + 4, yValue + 9, 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); + root.add(button3, xValue + 10, yValue + 9, 6, 1); + // ######### DEBUG BUTTONS ############ // Text GUI, not needed yet // WLabel label = new WLabel(new LiteralText("Test"), 0xFFFFFF); @@ -60,16 +62,22 @@ public class ButtonGUI extends LightweightGuiDescription { } private void addCommandSection(WGridPanel root) { - // Add text field for command entry + // Add text field for command NAME entry + WTextField nameTextField = new WTextField(); + nameTextField.setSuggestion("Name"); + root.add(nameTextField, 5, 12, 6, 1); + + // Add text field for command / entry WTextField textField = new WTextField(); - root.add(textField, 6, 12, 6, 1); + textField.setSuggestion("/command"); + root.add(textField, 11, 12, 6, 1); // Add button for command entry WButton addCmdBtn = new WButton(new TranslatableText("+")); addCmdBtn.setOnClick(() -> { addGUIButton(root, xValue); }); - root.add(addCmdBtn, 13, 12, 1, 1); + root.add(addCmdBtn, 18, 12, 1, 1); } private void addGUIButton(WGridPanel root, int x) { @@ -105,7 +113,7 @@ public class ButtonGUI extends LightweightGuiDescription { private void setupBackground(WGridPanel root) { setRootPanel(root); - root.setSize(300, 240); + root.setSize(350, 240); } From 4ee594b033c64628a6cce97ead2dfff2d91651c9 Mon Sep 17 00:00:00 2001 From: Joseph Date: Mon, 14 Dec 2020 15:01:11 -0600 Subject: [PATCH 02/12] add button adds name and command to GUI --- .../com/josyf/macrobuttons/MacroButtons.java | 4 +- .../com/josyf/macrobuttons/gui/ButtonGUI.java | 51 ++++++++++++------- 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/josyf/macrobuttons/MacroButtons.java b/src/main/java/com/josyf/macrobuttons/MacroButtons.java index 8cda889..d177e1c 100644 --- a/src/main/java/com/josyf/macrobuttons/MacroButtons.java +++ b/src/main/java/com/josyf/macrobuttons/MacroButtons.java @@ -43,8 +43,8 @@ public class MacroButtons implements ModInitializer { } // player can run a command here - public static void printMessage() { - MinecraftClient.getInstance().player.sendChatMessage("/seed"); + public static void printMessage(String savedCommand) { + MinecraftClient.getInstance().player.sendChatMessage(savedCommand); } public static void sayMessage(String message) { diff --git a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java index 07b6624..f543599 100644 --- a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java +++ b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java @@ -27,7 +27,7 @@ public class ButtonGUI extends LightweightGuiDescription { // example button to create config JSON WButton button = new WButton(new TranslatableText("Serialize")); button.setOnClick(() -> { - MacroButtons.printMessage(); + MacroButtons.printMessage("serializing"); ConfigFile.serializeCommand(); }); root.add(button, xValue, yValue + 9, 4, 1); @@ -35,7 +35,7 @@ public class ButtonGUI extends LightweightGuiDescription { // example load serialization button WButton button2 = new WButton(new TranslatableText("Load Serialization")); button2.setOnClick(() -> { - MacroButtons.printMessage(); + MacroButtons.printMessage("load serialization"); ConfigFile.loadSerialization(); }); root.add(button2, xValue + 4, yValue + 9, 6, 1); @@ -65,33 +65,50 @@ public class ButtonGUI extends LightweightGuiDescription { // Add text field for command NAME entry WTextField nameTextField = new WTextField(); nameTextField.setSuggestion("Name"); - root.add(nameTextField, 5, 12, 6, 1); + root.add(nameTextField, 0, 12, 6, 1); // Add text field for command / entry - WTextField textField = new WTextField(); - textField.setSuggestion("/command"); - root.add(textField, 11, 12, 6, 1); + 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, xValue, nameTextField, commandTextField); }); 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); + 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("")) { - root.add(button, xValue, yValue, 4, 1); + System.out.println("Here, command is " + command.getText()); + String instancedString = command.getText(); - adjustBounds(); + WButton button = new WButton(new TranslatableText(name.getText())); + 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(); + + name.setText(""); + command.setText(""); + + root.validate(this); + + } else { + System.out.println("No name and value entered!"); + } } From 6db63b8f8cc148ed78bc85de3d1a7529e2fb4c8a Mon Sep 17 00:00:00 2001 From: Joseph Date: Tue, 15 Dec 2020 13:28:48 -0600 Subject: [PATCH 03/12] add preliminary pseudocode for loading buttons from json --- .../com/josyf/macrobuttons/CommandObject.java | 4 ++-- .../com/josyf/macrobuttons/ConfigFile.java | 19 ++++++++++++++--- .../com/josyf/macrobuttons/MacroButtons.java | 13 ++++++++++-- .../com/josyf/macrobuttons/gui/ButtonGUI.java | 21 +++++++++++++++++++ 4 files changed, 50 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/josyf/macrobuttons/CommandObject.java b/src/main/java/com/josyf/macrobuttons/CommandObject.java index 91a55de..c0acaf1 100644 --- a/src/main/java/com/josyf/macrobuttons/CommandObject.java +++ b/src/main/java/com/josyf/macrobuttons/CommandObject.java @@ -2,6 +2,6 @@ package com.josyf.macrobuttons; public class CommandObject { - public String name; - public String command; + public CommandObject(String name, String command) { + } } diff --git a/src/main/java/com/josyf/macrobuttons/ConfigFile.java b/src/main/java/com/josyf/macrobuttons/ConfigFile.java index 1440e72..4fa1867 100644 --- a/src/main/java/com/josyf/macrobuttons/ConfigFile.java +++ b/src/main/java/com/josyf/macrobuttons/ConfigFile.java @@ -29,9 +29,7 @@ public class ConfigFile { public static void serializeCommand() { // CREATE OBJECT - CommandObject newCommand = new CommandObject(); - newCommand.name = "Say Hello"; - newCommand.command = "Hello 2"; + CommandObject newCommand = new CommandObject("Say hello", "hello command"); // add new object to config appendToFile(newCommand); @@ -101,6 +99,21 @@ public class ConfigFile { } } + // TODO: create func initArray to assign global JSONArray masterCommList to commands.json + public static JSONArray initArray() { + Object obj = null; + try { + obj = parser.parse(new FileReader("commands.json")); + JSONArray array = (JSONArray) parser.parse(obj.toString()); + return array; + } catch (IOException e) { + e.printStackTrace(); + } catch (ParseException e) { + e.printStackTrace(); + } + return null; + } + private static void writeToFile(String jsonMessage) { try { // these both write to the correct location diff --git a/src/main/java/com/josyf/macrobuttons/MacroButtons.java b/src/main/java/com/josyf/macrobuttons/MacroButtons.java index d177e1c..04e1158 100644 --- a/src/main/java/com/josyf/macrobuttons/MacroButtons.java +++ b/src/main/java/com/josyf/macrobuttons/MacroButtons.java @@ -10,15 +10,19 @@ 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.JSONArray; import org.lwjgl.glfw.GLFW; public class MacroButtons implements ModInitializer { public static final String MOD_ID = "mgbuttons"; + public static JSONArray masterCommList; @Override public void onInitialize() { assignGuiToKey(); + // TODO: call initArray(); here + initArray(); } private void assignGuiToKey() { @@ -37,7 +41,6 @@ public class MacroButtons implements ModInitializer { while (keyBinding.wasPressed()) { // client.player.sendMessage(new LiteralText("Key 1 was pressed!"), false); MinecraftClient.getInstance().openScreen(new ButtonGUIScreen(new ButtonGUI())); - // printMessage(); } }); } @@ -51,7 +54,13 @@ public class MacroButtons implements ModInitializer { MinecraftClient.getInstance().player.sendChatMessage(message); } - + // TODO: create func initArray to assign global JSONArray masterCommList to commands.json + private void initArray() { + masterCommList = ConfigFile.initArray(); + if (masterCommList == null) { + System.out.println("Error! Master Command List is null"); + } + } } diff --git a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java index f543599..3a73cac 100644 --- a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java +++ b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java @@ -8,6 +8,8 @@ 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; public class ButtonGUI extends LightweightGuiDescription { @@ -52,6 +54,9 @@ public class ButtonGUI extends LightweightGuiDescription { // WLabel label = new WLabel(new LiteralText("Test"), 0xFFFFFF); // root.add(label, 0, 4, 2, 1); + // TODO: addSavedButtons(); + addSavedButtons(); + addCommandSection(root); root.validate(this); @@ -112,6 +117,22 @@ public class ButtonGUI extends LightweightGuiDescription { } + // 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; + 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 + } + // Then convert the objects to buttons + } + + private void adjustBounds() { if (xValue % 12 == 0 && xValue != 0) { yValue += 2; From 84105c611b5236794535ae20c6f7c27df298d277 Mon Sep 17 00:00:00 2001 From: Joseph Garcia Date: Tue, 15 Dec 2020 22:52:51 -0600 Subject: [PATCH 04/12] primitive loading buttons from file --- .../com/josyf/macrobuttons/ConfigFile.java | 59 +++++++++++++------ .../com/josyf/macrobuttons/MacroButtons.java | 10 +++- .../com/josyf/macrobuttons/gui/ButtonGUI.java | 43 +++++++++++--- 3 files changed, 85 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/josyf/macrobuttons/ConfigFile.java b/src/main/java/com/josyf/macrobuttons/ConfigFile.java index 4fa1867..3ebec7d 100644 --- a/src/main/java/com/josyf/macrobuttons/ConfigFile.java +++ b/src/main/java/com/josyf/macrobuttons/ConfigFile.java @@ -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 masterCommList + public static ArrayList initArray() { + ArrayList 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) { diff --git a/src/main/java/com/josyf/macrobuttons/MacroButtons.java b/src/main/java/com/josyf/macrobuttons/MacroButtons.java index 04e1158..aebb602 100644 --- a/src/main/java/com/josyf/macrobuttons/MacroButtons.java +++ b/src/main/java/com/josyf/macrobuttons/MacroButtons.java @@ -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 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"); diff --git a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java index 3a73cac..0470284 100644 --- a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java +++ b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java @@ -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); + } } From 98f84809f83d3517860784872a40a374069ec9ad Mon Sep 17 00:00:00 2001 From: Joseph Garcia Date: Thu, 17 Dec 2020 15:42:00 -0600 Subject: [PATCH 05/12] add button adds jsonObject to masterCommList -add getter/setter for masterCommList -addSavedButtons correctly loads buttons from CommList instance --- .../com/josyf/macrobuttons/ConfigFile.java | 1 - .../com/josyf/macrobuttons/MacroButtons.java | 21 +++++--- .../com/josyf/macrobuttons/gui/ButtonGUI.java | 54 ++++++++++++++----- .../macrobuttons/gui/ButtonGUIScreen.java | 1 - 4 files changed, 54 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/josyf/macrobuttons/ConfigFile.java b/src/main/java/com/josyf/macrobuttons/ConfigFile.java index 3ebec7d..43c1c73 100644 --- a/src/main/java/com/josyf/macrobuttons/ConfigFile.java +++ b/src/main/java/com/josyf/macrobuttons/ConfigFile.java @@ -109,7 +109,6 @@ public class ConfigFile { } } - // TODO: create func initArray to assign commands.json to global array masterCommList public static ArrayList initArray() { ArrayList commandObjects = new ArrayList<>(); try { diff --git a/src/main/java/com/josyf/macrobuttons/MacroButtons.java b/src/main/java/com/josyf/macrobuttons/MacroButtons.java index aebb602..c7a3e9a 100644 --- a/src/main/java/com/josyf/macrobuttons/MacroButtons.java +++ b/src/main/java/com/josyf/macrobuttons/MacroButtons.java @@ -10,11 +10,12 @@ 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.JSONArray; 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 { @@ -24,14 +25,10 @@ public class MacroButtons implements ModInitializer { @Override public void onInitialize() { assignGuiToKey(); - // TODO: call initArray(); here initArray(); } - @Override - public void onQuit() { - - } + private void assignGuiToKey() { @@ -47,8 +44,9 @@ public class MacroButtons implements ModInitializer { ClientTickEvents.END_CLIENT_TICK.register(client -> { while (keyBinding.wasPressed()) { - // client.player.sendMessage(new LiteralText("Key 1 was pressed!"), false); + System.out.println("Key 1 was pressed!"); MinecraftClient.getInstance().openScreen(new ButtonGUIScreen(new ButtonGUI())); + //client.player.closeScreen(); } }); } @@ -62,7 +60,6 @@ public class MacroButtons implements ModInitializer { MinecraftClient.getInstance().player.sendChatMessage(message); } - // TODO: create func initArray to assign global JSONArray masterCommList to commands.json private void initArray() { masterCommList = ConfigFile.initArray(); if (masterCommList == null) { @@ -70,5 +67,13 @@ public class MacroButtons implements ModInitializer { } } + public static ArrayList getMasterCommList() { + return masterCommList; + } + + public static void setMasterCommList(ArrayList commList) { + masterCommList = commList; + } + } diff --git a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java index 0470284..4cba58f 100644 --- a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java +++ b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java @@ -1,5 +1,6 @@ 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; @@ -11,6 +12,8 @@ import net.minecraft.text.TranslatableText; import org.json.simple.JSONArray; import org.json.simple.JSONObject; +import java.util.ArrayList; + public class ButtonGUI extends LightweightGuiDescription { int xValue = 0; @@ -48,13 +51,22 @@ public class ButtonGUI extends LightweightGuiDescription { ConfigFile.readFile(); }); root.add(button3, xValue + 10, yValue + 9, 6, 1); + + + // read mastercommlist + // read json file button + WButton button4 = new WButton(new TranslatableText("Read master list")); + button4.setOnClick(() -> { + System.out.println(MacroButtons.getMasterCommList()); + }); + root.add(button4, xValue + 10, yValue + 8, 6, 1); + // ######### DEBUG BUTTONS ############ // Text GUI, not needed yet // WLabel label = new WLabel(new LiteralText("Test"), 0xFFFFFF); // root.add(label, 0, 4, 2, 1); - // TODO: addSavedButtons(); addSavedButtons(root); addCommandSection(root); @@ -90,7 +102,7 @@ public class ButtonGUI extends LightweightGuiDescription { // Only add the button if there are contents in both if (!name.getText().equals("") && !command.getText().equals("")) { - System.out.println("Here, command is " + command.getText()); + //System.out.println("Here, command is " + command.getText()); String instancedString = command.getText(); WButton button = new WButton(new TranslatableText(name.getText())); @@ -99,11 +111,24 @@ public class ButtonGUI extends LightweightGuiDescription { System.out.println("Command was " + instancedString); }); // int newX = incrementNumber(x, 4); - System.out.println("x val: " + xValue); - System.out.println("y val: " + yValue); + //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 + JSONObject newJsonObject = new JSONObject(); + newJsonObject.put("name", name.getText()); + newJsonObject.put("command", command.getText()); + System.out.println(newJsonObject); + + ArrayList commListCopy = MacroButtons.getMasterCommList(); + + commListCopy.add(newJsonObject); + System.out.println(MacroButtons.masterCommList); + + MacroButtons.setMasterCommList(commListCopy); + adjustBounds(); name.setText(""); @@ -117,10 +142,11 @@ public class ButtonGUI extends LightweightGuiDescription { } + // function to load buttons from commands.json private void addGUIButton(WGridPanel root, int x, String name, String command) { if (!name.equals("") && !command.equals("")) { - System.out.println("Here, command is " + command); + //System.out.println("Here, command is " + command); String instancedString = command; WButton button = new WButton(new TranslatableText(name)); @@ -129,11 +155,14 @@ public class ButtonGUI extends LightweightGuiDescription { System.out.println("Command was " + instancedString); }); // int newX = incrementNumber(x, 4); - System.out.println("x val: " + xValue); - System.out.println("y val: " + yValue); + //System.out.println("x val: " + xValue); + //System.out.println("y val: " + yValue); + root.add(button, xValue, yValue, 4, 1); + + adjustBounds(); root.validate(this); @@ -143,18 +172,17 @@ public class ButtonGUI extends LightweightGuiDescription { } } - // TODO: addSavedButtons - // TODO: Iterate over masterCommList to add corresponding buttons + // Array will contain String class types. Convert these to objects. private void addSavedButtons(WGridPanel root) { //JSONArray stringCommList = MacroButtons.masterCommList; - JSONArray objCommList; + ArrayList commListCopy = MacroButtons.getMasterCommList(); // Array will contain String class types. Convert these to objects 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(); + 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, xValue, name, command); } } diff --git a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUIScreen.java b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUIScreen.java index 5a7b01b..be3eb97 100644 --- a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUIScreen.java +++ b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUIScreen.java @@ -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 { From 46bc34af388c475f8d010d593e0adadc1873aaae Mon Sep 17 00:00:00 2001 From: Joseph Garcia Date: Fri, 18 Dec 2020 12:54:44 -0600 Subject: [PATCH 06/12] add updated appendToFile and cleanup comments --- .../com/josyf/macrobuttons/ConfigFile.java | 69 +++++++------------ .../com/josyf/macrobuttons/MacroButtons.java | 16 +---- .../com/josyf/macrobuttons/gui/ButtonGUI.java | 18 ++--- 3 files changed, 34 insertions(+), 69 deletions(-) diff --git a/src/main/java/com/josyf/macrobuttons/ConfigFile.java b/src/main/java/com/josyf/macrobuttons/ConfigFile.java index 43c1c73..5c49a19 100644 --- a/src/main/java/com/josyf/macrobuttons/ConfigFile.java +++ b/src/main/java/com/josyf/macrobuttons/ConfigFile.java @@ -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 initArray() { ArrayList 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() { diff --git a/src/main/java/com/josyf/macrobuttons/MacroButtons.java b/src/main/java/com/josyf/macrobuttons/MacroButtons.java index c7a3e9a..4174ecb 100644 --- a/src/main/java/com/josyf/macrobuttons/MacroButtons.java +++ b/src/main/java/com/josyf/macrobuttons/MacroButtons.java @@ -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) { diff --git a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java index 4cba58f..58af0a5 100644 --- a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java +++ b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java @@ -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 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); From f078d92af76f1a5a9a1df727006b95c699ce7139 Mon Sep 17 00:00:00 2001 From: Joseph Garcia Date: Fri, 18 Dec 2020 13:43:50 -0600 Subject: [PATCH 07/12] re-fix create json when adding initial btns --- .../com/josyf/macrobuttons/ConfigFile.java | 8 +++++- .../com/josyf/macrobuttons/MacroButtons.java | 2 +- .../com/josyf/macrobuttons/gui/ButtonGUI.java | 25 ++++++++++++------- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/josyf/macrobuttons/ConfigFile.java b/src/main/java/com/josyf/macrobuttons/ConfigFile.java index 5c49a19..ec59e58 100644 --- a/src/main/java/com/josyf/macrobuttons/ConfigFile.java +++ b/src/main/java/com/josyf/macrobuttons/ConfigFile.java @@ -65,8 +65,14 @@ public class ConfigFile { JSONArray array = (JSONArray) obj; array.add(jsonObject); writeToFile(array); + MacroButtons.initArray(); } catch (IOException e) { - e.printStackTrace(); + System.out.println("Commands.json doesn't exist. Creating one...!"); + // create json + JSONArray jsonArray = new JSONArray(); + jsonArray.add(jsonObject); + writeToFile(jsonArray); + MacroButtons.initArray(); } catch (ParseException e) { e.printStackTrace(); } diff --git a/src/main/java/com/josyf/macrobuttons/MacroButtons.java b/src/main/java/com/josyf/macrobuttons/MacroButtons.java index 4174ecb..9a3d72d 100644 --- a/src/main/java/com/josyf/macrobuttons/MacroButtons.java +++ b/src/main/java/com/josyf/macrobuttons/MacroButtons.java @@ -50,7 +50,7 @@ public class MacroButtons implements ModInitializer { } // Assign masterCommList to an array of JSON objects (from commands.json) - private void initArray() { + static void initArray() { masterCommList = ConfigFile.initArray(); if (masterCommList == null) { System.out.println("Error! Master Command List is null"); diff --git a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java index 58af0a5..3c31dc0 100644 --- a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java +++ b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java @@ -117,13 +117,17 @@ public class ButtonGUI extends LightweightGuiDescription { ArrayList commListCopy = MacroButtons.getMasterCommList(); - commListCopy.add(newJsonObject); - System.out.println(MacroButtons.masterCommList); + if (commListCopy != null) { + commListCopy.add(newJsonObject); - // Add jsonObject to commands.json - ConfigFile.appendToFile(newJsonObject); + // Add jsonObject to commands.json + ConfigFile.appendToFile(newJsonObject); + + MacroButtons.setMasterCommList(commListCopy); + } else { + ConfigFile.appendToFile(newJsonObject); + } - MacroButtons.setMasterCommList(commListCopy); adjustBounds(); @@ -176,11 +180,14 @@ public class ButtonGUI extends LightweightGuiDescription { // Array will contain String class types. Convert these to objects System.out.println("I be doin my thing here"); // Then convert the objects to buttons - 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, xValue, name, command); + 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, xValue, name, command); + } } + } From 9f674f2d1fc015a9709c04a17ac9d866aee12a6b Mon Sep 17 00:00:00 2001 From: Joseph Garcia Date: Fri, 18 Dec 2020 15:22:53 -0600 Subject: [PATCH 08/12] remove bloat code --- .../com/josyf/macrobuttons/CommandObject.java | 7 -- .../com/josyf/macrobuttons/ConfigFile.java | 81 ------------------- .../com/josyf/macrobuttons/gui/ButtonGUI.java | 42 ---------- 3 files changed, 130 deletions(-) delete mode 100644 src/main/java/com/josyf/macrobuttons/CommandObject.java diff --git a/src/main/java/com/josyf/macrobuttons/CommandObject.java b/src/main/java/com/josyf/macrobuttons/CommandObject.java deleted file mode 100644 index c0acaf1..0000000 --- a/src/main/java/com/josyf/macrobuttons/CommandObject.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.josyf.macrobuttons; - -public class CommandObject { - - public CommandObject(String name, String command) { - } -} diff --git a/src/main/java/com/josyf/macrobuttons/ConfigFile.java b/src/main/java/com/josyf/macrobuttons/ConfigFile.java index ec59e58..8ec5d56 100644 --- a/src/main/java/com/josyf/macrobuttons/ConfigFile.java +++ b/src/main/java/com/josyf/macrobuttons/ConfigFile.java @@ -2,9 +2,6 @@ package com.josyf.macrobuttons; import com.alibaba.fastjson.JSON; import com.cedarsoftware.util.io.JsonWriter; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.josyf.macrobuttons.gui.ButtonGUI; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; @@ -15,49 +12,9 @@ import java.util.ArrayList; public class ConfigFile { - static JSONParser parser = new JSONParser(); - private static FileWriter fileWriter; - public static void serializeCommand() { - // CREATE OBJECT - CommandObject newCommand = new CommandObject("Say hello", "hello command"); - - // add new object to config - appendToFile(newCommand); - } - - public static void loadSerialization() { - String JSONConfig = ButtonGUI.getConfig(); - if (JSONConfig == null) { - MacroButtons.runCommand("GUI Configuration not yet initialized!"); - } else { - 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 { - // if commands.json exists, read it, convert it to an array, and append - Object obj = parser.parse(new FileReader("commands.json")); - - JSONArray array = (JSONArray) obj; - JSONObject obj2 = (JSONObject)array.get(0); - - } 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("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 { @@ -119,42 +76,4 @@ public class ConfigFile { } return null; } - - // DEPRECATED CAN PROBABLY DELETE - 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() { - try { - Object obj = parser.parse(new FileReader("commands.json")); - String jsonString = obj.toString(); - //String jsonString = JSONObject.toJSONString(obj); - System.out.println(jsonString); - return jsonString; - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (ParseException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - return "Not yet initialized yo"; - } } diff --git a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java index 3c31dc0..4dfbf3d 100644 --- a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java +++ b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java @@ -17,63 +17,21 @@ 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); - // ######### DEBUG BUTTONS ############ - // example button to create config JSON - WButton button = new WButton(new TranslatableText("Serialize")); - button.setOnClick(() -> { - ConfigFile.serializeCommand(); - }); - root.add(button, xValue, yValue + 9, 4, 1); - - // example load serialization button - WButton button2 = new WButton(new TranslatableText("Load Serialization")); - button2.setOnClick(() -> { - ConfigFile.loadSerialization(); - }); - root.add(button2, xValue + 4, yValue + 9, 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 + 9, 6, 1); - - - // read mastercommlist - // read json file button - WButton button4 = new WButton(new TranslatableText("Read master list")); - button4.setOnClick(() -> { - System.out.println(MacroButtons.getMasterCommList()); - }); - root.add(button4, xValue + 10, yValue + 8, 6, 1); - - // ######### DEBUG BUTTONS ############ - // 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 NAME entry WTextField nameTextField = new WTextField(); From 5f6ee2b939874bd672b71d6fcd4737f96ed3404d Mon Sep 17 00:00:00 2001 From: Joseph Garcia Date: Fri, 18 Dec 2020 16:28:14 -0600 Subject: [PATCH 09/12] Made masterCommList and commands.json more congruent w/ each other + More code cleanup & light refactor --- .../com/josyf/macrobuttons/ConfigFile.java | 7 ++---- .../com/josyf/macrobuttons/MacroButtons.java | 10 ++++---- .../com/josyf/macrobuttons/gui/ButtonGUI.java | 25 +++---------------- 3 files changed, 10 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/josyf/macrobuttons/ConfigFile.java b/src/main/java/com/josyf/macrobuttons/ConfigFile.java index 8ec5d56..0641748 100644 --- a/src/main/java/com/josyf/macrobuttons/ConfigFile.java +++ b/src/main/java/com/josyf/macrobuttons/ConfigFile.java @@ -15,21 +15,18 @@ public class ConfigFile { static JSONParser parser = new JSONParser(); private static FileWriter fileWriter; - // if command.json exists, read it, convert it to an array, and append A JSON OBJECT + // Read commands.json, 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); - MacroButtons.initArray(); } catch (IOException e) { System.out.println("Commands.json doesn't exist. Creating one...!"); - // create json JSONArray jsonArray = new JSONArray(); jsonArray.add(jsonObject); writeToFile(jsonArray); - MacroButtons.initArray(); } catch (ParseException e) { e.printStackTrace(); } @@ -55,7 +52,7 @@ public class ConfigFile { } // parses commands.json into array, loop through array and add JSONObjects. returns array - public static ArrayList initArray() { + public static ArrayList getArrayFromJsonFile() { ArrayList commandObjects = new ArrayList<>(); try { // assign array to JSONArray using our commands.json as an object diff --git a/src/main/java/com/josyf/macrobuttons/MacroButtons.java b/src/main/java/com/josyf/macrobuttons/MacroButtons.java index 9a3d72d..12e9b6e 100644 --- a/src/main/java/com/josyf/macrobuttons/MacroButtons.java +++ b/src/main/java/com/josyf/macrobuttons/MacroButtons.java @@ -18,7 +18,7 @@ import java.util.ArrayList; public class MacroButtons implements ModInitializer { public static final String MOD_ID = "mgbuttons"; - public static ArrayList masterCommList; + private static ArrayList masterCommList; @Override public void onInitialize() { @@ -38,7 +38,6 @@ public class MacroButtons implements ModInitializer { ClientTickEvents.END_CLIENT_TICK.register(client -> { while (keyBinding.wasPressed()) { - System.out.println("Key 1 was pressed!"); MinecraftClient.getInstance().openScreen(new ButtonGUIScreen(new ButtonGUI())); //client.player.closeScreen(); } @@ -49,11 +48,12 @@ public class MacroButtons implements ModInitializer { MinecraftClient.getInstance().player.sendChatMessage(command); } - // Assign masterCommList to an array of JSON objects (from commands.json) + // Assign masterCommList to JSONArray (from commands.json). Runs once. static void initArray() { - masterCommList = ConfigFile.initArray(); + masterCommList = ConfigFile.getArrayFromJsonFile(); + // If commands.json doesn't exist yet, start a global list variable for future creation if (masterCommList == null) { - System.out.println("Error! Master Command List is null"); + setMasterCommList(new ArrayList<>()); } } diff --git a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java index 4dfbf3d..62c1b3b 100644 --- a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java +++ b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java @@ -62,7 +62,6 @@ public class ButtonGUI extends LightweightGuiDescription { WButton button = new WButton(new TranslatableText(name.getText())); button.setOnClick(() -> { - System.out.println("Command was " + instancedString); MacroButtons.runCommand(instancedString); }); @@ -75,17 +74,9 @@ public class ButtonGUI extends LightweightGuiDescription { ArrayList commListCopy = MacroButtons.getMasterCommList(); - if (commListCopy != null) { - commListCopy.add(newJsonObject); - - // Add jsonObject to commands.json - ConfigFile.appendToFile(newJsonObject); - - MacroButtons.setMasterCommList(commListCopy); - } else { - ConfigFile.appendToFile(newJsonObject); - } - + commListCopy.add(newJsonObject); + MacroButtons.setMasterCommList(commListCopy); + ConfigFile.appendToFile(newJsonObject); adjustBounds(); @@ -109,20 +100,11 @@ public class ButtonGUI extends LightweightGuiDescription { WButton button = new WButton(new TranslatableText(name)); button.setOnClick(() -> { - 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); - - - adjustBounds(); - root.validate(this); } else { @@ -136,7 +118,6 @@ public class ButtonGUI extends LightweightGuiDescription { //JSONArray stringCommList = MacroButtons.masterCommList; ArrayList commListCopy = MacroButtons.getMasterCommList(); // Array will contain String class types. Convert these to objects - System.out.println("I be doin my thing here"); // Then convert the objects to buttons if (commListCopy != null) { for (int i = 0; i < commListCopy.size(); i++) { From 8d711734da991d7f0f7c5328b4fdf505d6d7f43e Mon Sep 17 00:00:00 2001 From: Joseph Garcia Date: Fri, 18 Dec 2020 16:50:33 -0600 Subject: [PATCH 10/12] refactor ButtonGUI Add primitive commList size limit --- .../com/josyf/macrobuttons/ConfigFile.java | 10 ++++++ .../com/josyf/macrobuttons/gui/ButtonGUI.java | 34 ++++++------------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/josyf/macrobuttons/ConfigFile.java b/src/main/java/com/josyf/macrobuttons/ConfigFile.java index 0641748..93a9f17 100644 --- a/src/main/java/com/josyf/macrobuttons/ConfigFile.java +++ b/src/main/java/com/josyf/macrobuttons/ConfigFile.java @@ -64,6 +64,7 @@ public class ConfigFile { JSONObject childObject = (JSONObject)array.get(i); commandObjects.add(childObject); System.out.println(i); + if (i >= 19) break; } return commandObjects; } catch (IOException e) { @@ -73,4 +74,13 @@ public class ConfigFile { } return null; } + + public static void addObjectToCommList(JSONObject jsonObject) { + ArrayList commListCopy = MacroButtons.getMasterCommList(); + if (commListCopy.size() <= 20) { + commListCopy.add(jsonObject); + MacroButtons.setMasterCommList(commListCopy); + } + + } } diff --git a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java index 62c1b3b..7f94089 100644 --- a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java +++ b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java @@ -47,24 +47,21 @@ public class ButtonGUI extends LightweightGuiDescription { // Add button for command entry WButton addCmdBtn = new WButton(new TranslatableText("+")); addCmdBtn.setOnClick(() -> { - addGUIButton(root, xValue, nameTextField, commandTextField); + addGUIButton(root, nameTextField, commandTextField); }); 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) { + // 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("")) { - //System.out.println("Here, command is " + command.getText()); - String instancedString = command.getText(); - + String commandString = command.getText(); WButton button = new WButton(new TranslatableText(name.getText())); button.setOnClick(() -> { - MacroButtons.runCommand(instancedString); + MacroButtons.runCommand(commandString); }); - root.add(button, xValue, yValue, 4, 1); // Create a new Json object & append to masterCommList @@ -72,10 +69,8 @@ public class ButtonGUI extends LightweightGuiDescription { newJsonObject.put("name", name.getText()); newJsonObject.put("command", command.getText()); - ArrayList commListCopy = MacroButtons.getMasterCommList(); - - commListCopy.add(newJsonObject); - MacroButtons.setMasterCommList(commListCopy); + // append the buttons to masterList for future loading + ConfigFile.addObjectToCommList(newJsonObject); ConfigFile.appendToFile(newJsonObject); adjustBounds(); @@ -92,21 +87,15 @@ public class ButtonGUI extends LightweightGuiDescription { } // function to load buttons from commands.json - private void addGUIButton(WGridPanel root, int x, String name, String command) { + private void addGUIButton(WGridPanel root, 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.runCommand(instancedString); + MacroButtons.runCommand(command); }); - root.add(button, xValue, yValue, 4, 1); adjustBounds(); root.validate(this); - } else { System.out.println("No name and value entered!"); } @@ -115,15 +104,14 @@ public class ButtonGUI extends LightweightGuiDescription { // Array will contain String class types. Convert these to objects. private void addSavedButtons(WGridPanel root) { - //JSONArray stringCommList = MacroButtons.masterCommList; ArrayList commListCopy = MacroButtons.getMasterCommList(); - // Array will contain String class types. Convert these to objects // 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, xValue, name, command); + addGUIButton(root, name, command); + if (i >= 19) break; } } From 3674d63d0ce6cac0f9d649a18b22afd1310db9a9 Mon Sep 17 00:00:00 2001 From: Joseph Garcia Date: Fri, 18 Dec 2020 17:12:32 -0600 Subject: [PATCH 11/12] size limit on list --- .../com/josyf/macrobuttons/ConfigFile.java | 9 +++-- .../com/josyf/macrobuttons/gui/ButtonGUI.java | 35 +++++++++++-------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/josyf/macrobuttons/ConfigFile.java b/src/main/java/com/josyf/macrobuttons/ConfigFile.java index 93a9f17..00fa512 100644 --- a/src/main/java/com/josyf/macrobuttons/ConfigFile.java +++ b/src/main/java/com/josyf/macrobuttons/ConfigFile.java @@ -77,10 +77,9 @@ public class ConfigFile { public static void addObjectToCommList(JSONObject jsonObject) { ArrayList commListCopy = MacroButtons.getMasterCommList(); - if (commListCopy.size() <= 20) { - commListCopy.add(jsonObject); - MacroButtons.setMasterCommList(commListCopy); - } - + commListCopy.add(jsonObject); + MacroButtons.setMasterCommList(commListCopy); } + + } diff --git a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java index 7f94089..368c150 100644 --- a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java +++ b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java @@ -35,6 +35,7 @@ public class ButtonGUI extends LightweightGuiDescription { private void addCommandSection(WGridPanel root) { // Add text field for command NAME entry WTextField nameTextField = new WTextField(); + nameTextField.setMaxLength(10); nameTextField.setSuggestion("Name"); root.add(nameTextField, 0, 12, 6, 1); @@ -57,23 +58,25 @@ public class ButtonGUI extends LightweightGuiDescription { // Only add the button if there are contents in both if (!name.getText().equals("") && !command.getText().equals("")) { - String commandString = command.getText(); - WButton button = new WButton(new TranslatableText(name.getText())); - button.setOnClick(() -> { - MacroButtons.runCommand(commandString); - }); - 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); - // Create a new Json object & append to masterCommList - JSONObject newJsonObject = new JSONObject(); - newJsonObject.put("name", name.getText()); - newJsonObject.put("command", command.getText()); + // 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); + // append the buttons to masterList for future loading + ConfigFile.addObjectToCommList(newJsonObject); + ConfigFile.appendToFile(newJsonObject); - adjustBounds(); + adjustBounds(); + } name.setText(""); command.setText(""); @@ -127,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() { From 071c567df6de65311fbbb5e6cf2860b535fe44be Mon Sep 17 00:00:00 2001 From: Joseph Garcia Date: Tue, 22 Dec 2020 08:35:05 -0600 Subject: [PATCH 12/12] gradle changes to build dependencies for production --- META-INF/MANIFEST.MF | 3 +++ build.gradle | 21 +++++++++++-------- gradle/wrapper/gradle-wrapper.properties | 5 +++-- .../com/josyf/macrobuttons/ConfigFile.java | 7 +++++-- .../com/josyf/macrobuttons/MacroButtons.java | 4 ++++ .../com/josyf/macrobuttons/gui/ButtonGUI.java | 2 +- src/main/resources/fabric.mod.json | 2 +- 7 files changed, 29 insertions(+), 15 deletions(-) create mode 100644 META-INF/MANIFEST.MF diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF new file mode 100644 index 0000000..da97939 --- /dev/null +++ b/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: com.josyf.macrobuttons.MacroButtons + diff --git a/build.gradle b/build.gradle index 89a1383..6c01b52 100644 --- a/build.gradle +++ b/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() - } + } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 12d38de..15d48d8 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -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 diff --git a/src/main/java/com/josyf/macrobuttons/ConfigFile.java b/src/main/java/com/josyf/macrobuttons/ConfigFile.java index 00fa512..a605f03 100644 --- a/src/main/java/com/josyf/macrobuttons/ConfigFile.java +++ b/src/main/java/com/josyf/macrobuttons/ConfigFile.java @@ -4,17 +4,18 @@ import com.alibaba.fastjson.JSON; import com.cedarsoftware.util.io.JsonWriter; import org.json.simple.JSONArray; import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; +import org.json.simple.parser.*; import java.io.*; import java.util.ArrayList; public class ConfigFile { - static JSONParser parser = new JSONParser(); + private static final JSONParser parser = new JSONParser(); private static FileWriter fileWriter; + // Read commands.json, convert it to an array, and append A JSON OBJECT public static void appendToFile(JSONObject jsonObject) { try { @@ -82,4 +83,6 @@ public class ConfigFile { } + + } diff --git a/src/main/java/com/josyf/macrobuttons/MacroButtons.java b/src/main/java/com/josyf/macrobuttons/MacroButtons.java index 12e9b6e..e3a796c 100644 --- a/src/main/java/com/josyf/macrobuttons/MacroButtons.java +++ b/src/main/java/com/josyf/macrobuttons/MacroButtons.java @@ -20,6 +20,10 @@ public class MacroButtons implements ModInitializer { public static final String MOD_ID = "mgbuttons"; private static ArrayList masterCommList; + public static void main(String[] args) { + + } + @Override public void onInitialize() { assignGuiToKey(); diff --git a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java index 368c150..46d97a7 100644 --- a/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java +++ b/src/main/java/com/josyf/macrobuttons/gui/ButtonGUI.java @@ -71,7 +71,7 @@ public class ButtonGUI extends LightweightGuiDescription { newJsonObject.put("name", name.getText()); newJsonObject.put("command", command.getText()); - // append the buttons to masterList for future loading +// // append the buttons to masterList for future loading ConfigFile.addObjectToCommList(newJsonObject); ConfigFile.appendToFile(newJsonObject); diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 1aa6fb9..fa67594 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -1,6 +1,6 @@ { "schemaVersion": 1, - "id": "mgbuttons", + "id": "macro-gui-buttons", "version": "${version}", "name": "Macro GUI Buttons",