refactor serializations

This commit is contained in:
Joseph Garcia 2020-12-10 14:27:55 -06:00
parent 952e0e05cb
commit 5c08c03a11
5 changed files with 68 additions and 67 deletions

View File

@ -1,6 +1,6 @@
package com.josyf.macrobuttons; package com.josyf.macrobuttons;
public class Message { public class CommandObject {
public String message; public String message;
} }

View File

@ -1,4 +1,49 @@
package com.josyf.macrobuttons; package com.josyf.macrobuttons;
import com.alibaba.fastjson.JSON;
import com.josyf.macrobuttons.gui.ButtonGUI;
import java.io.FileWriter;
import java.io.IOException;
public class ConfigFile { public class ConfigFile {
private static FileWriter file;
//private static final String configSettings = ButtonGUI.getConfig();
public static void serializeCommand() {
CommandObject newCommand = new CommandObject();
newCommand.message = "hello";
ButtonGUI.setConfig(JSON.toJSONString(newCommand));
writeToFile(ButtonGUI.getConfig());
}
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);
MacroButtons.sayMessage(deserializedMessage);
}
}
private static void writeToFile(String jsonMessage) {
try {
// these both write to the correct location
file = new FileWriter("commands.json");
// file = new FileWriter(MinecraftClient.getInstance().runDirectory + "/command.json");
file.write(jsonMessage);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
file.flush();
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} }

View File

@ -16,9 +16,6 @@ public class MacroButtons implements ModInitializer {
public static final String MOD_ID = "mgbuttons"; public static final String MOD_ID = "mgbuttons";
@Override @Override
public void onInitialize() { public void onInitialize() {
assignGuiToKey(); assignGuiToKey();
@ -28,6 +25,7 @@ public class MacroButtons implements ModInitializer {
System.out.println("I'm getting here"); System.out.println("I'm getting here");
// Currently assigns to the G key
KeyBinding keyBinding = KeyBindingHelper.registerKeyBinding(new KeyBinding( KeyBinding keyBinding = KeyBindingHelper.registerKeyBinding(new KeyBinding(
"key.macrobuttons.opengui", // The translation key of the keybinding's name "key.macrobuttons.opengui", // The translation key of the keybinding's name
InputUtil.Type.KEYSYM, // The type of the keybinding, KEYSYM for keyboard, MOUSE for mouse. InputUtil.Type.KEYSYM, // The type of the keybinding, KEYSYM for keyboard, MOUSE for mouse.
@ -39,7 +37,7 @@ public class MacroButtons implements ModInitializer {
while (keyBinding.wasPressed()) { while (keyBinding.wasPressed()) {
// client.player.sendMessage(new LiteralText("Key 1 was pressed!"), false); // client.player.sendMessage(new LiteralText("Key 1 was pressed!"), false);
MinecraftClient.getInstance().openScreen(new ButtonGUIScreen(new ButtonGUI())); MinecraftClient.getInstance().openScreen(new ButtonGUIScreen(new ButtonGUI()));
//printMessage(); // printMessage();
} }
}); });
} }

View File

@ -1,46 +1,33 @@
package com.josyf.macrobuttons.gui; package com.josyf.macrobuttons.gui;
import com.alibaba.fastjson.JSON; import com.josyf.macrobuttons.ConfigFile;
import com.alibaba.fastjson.JSONObject;
import com.josyf.macrobuttons.MacroButtons; import com.josyf.macrobuttons.MacroButtons;
import com.josyf.macrobuttons.Message;
import io.github.cottonmc.cotton.gui.client.BackgroundPainter; import io.github.cottonmc.cotton.gui.client.BackgroundPainter;
import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription; import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription;
import io.github.cottonmc.cotton.gui.widget.WButton; import io.github.cottonmc.cotton.gui.widget.WButton;
import io.github.cottonmc.cotton.gui.widget.WGridPanel; import io.github.cottonmc.cotton.gui.widget.WGridPanel;
import io.github.cottonmc.cotton.gui.widget.WLabel;
import io.github.cottonmc.cotton.gui.widget.WTextField; import io.github.cottonmc.cotton.gui.widget.WTextField;
import net.minecraft.client.MinecraftClient;
import net.minecraft.text.LiteralText;
import net.minecraft.text.TranslatableText; import net.minecraft.text.TranslatableText;
import java.io.FileWriter;
import java.io.IOException;
public class ButtonGUI extends LightweightGuiDescription { public class ButtonGUI extends LightweightGuiDescription {
int xValue = 0; int xValue = 0;
int yValue = 1; int yValue = 1;
private static FileWriter file; private static String ConfigSettings;
String JSONMessage = "not yet set";
public ButtonGUI() { public ButtonGUI() {
WGridPanel root = new WGridPanel();
setRootPanel(root);
root.setSize(300, 240);
// WSprite icon = new WSprite(new Identifier("minecraft:textures/item/redstone.png")); // initialize root panel of GUI
// root.add(icon, 0, 2, 1, 1); WGridPanel root = new WGridPanel();
setupBackground(root);
// example button to play with // example button to play with
WButton button = new WButton(new TranslatableText("Serialize")); WButton button = new WButton(new TranslatableText("Serialize"));
button.setOnClick(() -> { button.setOnClick(() -> {
MacroButtons.printMessage(); MacroButtons.printMessage();
createMessageForSerialization(); ConfigFile.serializeCommand();
}); });
root.add(button, xValue, yValue, 4, 1); root.add(button, xValue, yValue, 4, 1);
@ -48,57 +35,25 @@ public class ButtonGUI extends LightweightGuiDescription {
WButton button2 = new WButton(new TranslatableText("Load Serialization")); WButton button2 = new WButton(new TranslatableText("Load Serialization"));
button2.setOnClick(() -> { button2.setOnClick(() -> {
MacroButtons.printMessage(); MacroButtons.printMessage();
loadSerialization(); ConfigFile.loadSerialization();
}); });
root.add(button2, xValue + 2, yValue, 4, 1); root.add(button2, xValue + 2, yValue, 4, 1);
WLabel label = new WLabel(new LiteralText("Test"), 0xFFFFFF); // Text GUI, not needed yet
root.add(label, 0, 4, 2, 1); // WLabel label = new WLabel(new LiteralText("Test"), 0xFFFFFF);
// root.add(label, 0, 4, 2, 1);
addCommandSection(root); addCommandSection(root);
root.validate(this); root.validate(this);
} }
private void createMessageForSerialization() { public static void setConfig(String configSettings) {
Message myMessage = new Message(); ConfigSettings = configSettings;
myMessage.message = "hello";
JSONMessage = JSON.toJSONString(myMessage);
writeToFile(JSONMessage);
}
private String getJsonMessage() {
return JSONMessage;
}
private void loadSerialization() {
String JsonMessage = getJsonMessage();
if (JsonMessage.equals("not yet set")) {
MacroButtons.sayMessage("not yet set!");
} else {
String deserializedMessage = JSON.parseObject(JsonMessage, String.class);
MacroButtons.sayMessage(deserializedMessage);
}
}
private void writeToFile(String jsonMessage) {
try {
// these both write to the correct location
file = new FileWriter("commands.json");
// file = new FileWriter(MinecraftClient.getInstance().runDirectory + "/command.json");
file.write(jsonMessage);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
file.flush();
file.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
public static String getConfig() {
return ConfigSettings;
} }
private void addCommandSection(WGridPanel root) { private void addCommandSection(WGridPanel root) {
@ -149,5 +104,10 @@ public class ButtonGUI extends LightweightGuiDescription {
this.rootPanel.setBackgroundPainter(BackgroundPainter.createColorful(0x4D000000)); this.rootPanel.setBackgroundPainter(BackgroundPainter.createColorful(0x4D000000));
} }
private void setupBackground(WGridPanel root) {
setRootPanel(root);
root.setSize(300, 240);
}
} }

View File

@ -7,8 +7,6 @@ import io.github.cottonmc.cotton.gui.client.CottonClientScreen;
public class ButtonGUIScreen extends CottonClientScreen { public class ButtonGUIScreen extends CottonClientScreen {
public ButtonGUIScreen(GuiDescription description) { public ButtonGUIScreen(GuiDescription description) {
super(description); super(description);
} }