add button adds jsonObject to masterCommList

-add getter/setter for masterCommList
-addSavedButtons correctly loads buttons from CommList instance
This commit is contained in:
Joseph Garcia 2020-12-17 15:42:00 -06:00
parent 84105c611b
commit 98f84809f8
4 changed files with 54 additions and 23 deletions

View File

@ -109,7 +109,6 @@ public class ConfigFile {
}
}
// TODO: create func initArray to assign commands.json to global array<CommandObjects> masterCommList
public static ArrayList<JSONObject> initArray() {
ArrayList<JSONObject> commandObjects = new ArrayList<>();
try {

View File

@ -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<JSONObject> getMasterCommList() {
return masterCommList;
}
public static void setMasterCommList(ArrayList<JSONObject> commList) {
masterCommList = commList;
}
}

View File

@ -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<JSONObject> 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<JSONObject> 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);
}
}

View File

@ -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 {