read and write json data to/from file
This commit is contained in:
parent
a12de0abf6
commit
952e0e05cb
@ -50,6 +50,10 @@ public class MacroButtons implements ModInitializer {
|
||||
MinecraftClient.getInstance().player.sendChatMessage("/seed");
|
||||
}
|
||||
|
||||
public static void sayMessage(String message) {
|
||||
MinecraftClient.getInstance().player.sendChatMessage(message);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,4 +1,6 @@
|
||||
package com.josyf.macrobuttons;
|
||||
|
||||
public class Message {
|
||||
|
||||
public String message;
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
package com.josyf.macrobuttons.gui;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
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;
|
||||
@ -11,11 +14,18 @@ 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";
|
||||
|
||||
|
||||
|
||||
public ButtonGUI() {
|
||||
@ -27,12 +37,21 @@ public class ButtonGUI extends LightweightGuiDescription {
|
||||
// root.add(icon, 0, 2, 1, 1);
|
||||
|
||||
// example button to play with
|
||||
WButton button = new WButton(new TranslatableText("Button"));
|
||||
WButton button = new WButton(new TranslatableText("Serialize"));
|
||||
button.setOnClick(() -> {
|
||||
MacroButtons.printMessage();
|
||||
createMessageForSerialization();
|
||||
});
|
||||
root.add(button, xValue, yValue, 4, 1);
|
||||
|
||||
// example load serialization button
|
||||
WButton button2 = new WButton(new TranslatableText("Load Serialization"));
|
||||
button2.setOnClick(() -> {
|
||||
MacroButtons.printMessage();
|
||||
loadSerialization();
|
||||
});
|
||||
root.add(button2, xValue + 2, yValue, 4, 1);
|
||||
|
||||
WLabel label = new WLabel(new LiteralText("Test"), 0xFFFFFF);
|
||||
root.add(label, 0, 4, 2, 1);
|
||||
|
||||
@ -41,6 +60,47 @@ public class ButtonGUI extends LightweightGuiDescription {
|
||||
root.validate(this);
|
||||
}
|
||||
|
||||
private void createMessageForSerialization() {
|
||||
Message myMessage = new Message();
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void addCommandSection(WGridPanel root) {
|
||||
// Add text field for command entry
|
||||
WTextField textField = new WTextField();
|
||||
|
Loading…
x
Reference in New Issue
Block a user