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;
public class Message {
public class CommandObject {
public String message;
}

View File

@ -1,4 +1,49 @@
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 {
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";
@Override
public void onInitialize() {
assignGuiToKey();
@ -28,6 +25,7 @@ public class MacroButtons implements ModInitializer {
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
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()) {
// client.player.sendMessage(new LiteralText("Key 1 was pressed!"), false);
MinecraftClient.getInstance().openScreen(new ButtonGUIScreen(new ButtonGUI()));
//printMessage();
// printMessage();
}
});
}

View File

@ -1,46 +1,33 @@
package com.josyf.macrobuttons.gui;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.josyf.macrobuttons.ConfigFile;
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.LightweightGuiDescription;
import io.github.cottonmc.cotton.gui.widget.WButton;
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 net.minecraft.client.MinecraftClient;
import net.minecraft.text.LiteralText;
import net.minecraft.text.TranslatableText;
import java.io.FileWriter;
import java.io.IOException;
public class ButtonGUI extends LightweightGuiDescription {
int xValue = 0;
int yValue = 1;
private static FileWriter file;
String JSONMessage = "not yet set";
private static String ConfigSettings;
public ButtonGUI() {
WGridPanel root = new WGridPanel();
setRootPanel(root);
root.setSize(300, 240);
// WSprite icon = new WSprite(new Identifier("minecraft:textures/item/redstone.png"));
// root.add(icon, 0, 2, 1, 1);
// initialize root panel of GUI
WGridPanel root = new WGridPanel();
setupBackground(root);
// example button to play with
WButton button = new WButton(new TranslatableText("Serialize"));
button.setOnClick(() -> {
MacroButtons.printMessage();
createMessageForSerialization();
ConfigFile.serializeCommand();
});
root.add(button, xValue, yValue, 4, 1);
@ -48,57 +35,25 @@ public class ButtonGUI extends LightweightGuiDescription {
WButton button2 = new WButton(new TranslatableText("Load Serialization"));
button2.setOnClick(() -> {
MacroButtons.printMessage();
loadSerialization();
ConfigFile.loadSerialization();
});
root.add(button2, xValue + 2, yValue, 4, 1);
WLabel label = new WLabel(new LiteralText("Test"), 0xFFFFFF);
root.add(label, 0, 4, 2, 1);
// Text GUI, not needed yet
// WLabel label = new WLabel(new LiteralText("Test"), 0xFFFFFF);
// root.add(label, 0, 4, 2, 1);
addCommandSection(root);
root.validate(this);
}
private void createMessageForSerialization() {
Message myMessage = new Message();
myMessage.message = "hello";
JSONMessage = JSON.toJSONString(myMessage);
writeToFile(JSONMessage);
public static void setConfig(String configSettings) {
ConfigSettings = configSettings;
}
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) {
@ -149,5 +104,10 @@ public class ButtonGUI extends LightweightGuiDescription {
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 ButtonGUIScreen(GuiDescription description) {
super(description);
}