change writing json file from string to array
This commit is contained in:
parent
1ed4e67237
commit
d5d5196515
@ -2,5 +2,6 @@ package com.josyf.macrobuttons;
|
||||
|
||||
public class CommandObject {
|
||||
|
||||
public String message;
|
||||
public String name;
|
||||
public String command;
|
||||
}
|
||||
|
@ -5,9 +5,11 @@ import com.alibaba.fastjson.JSONObject;
|
||||
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.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.*;
|
||||
import java.util.logging.Level;
|
||||
|
||||
@ -23,9 +25,21 @@ public class ConfigFile {
|
||||
|
||||
public static void serializeCommand() {
|
||||
CommandObject newCommand = new CommandObject();
|
||||
newCommand.message = "hello";
|
||||
ButtonGUI.setConfig(JSON.toJSONString(newCommand));
|
||||
writeToFile(ButtonGUI.getConfig());
|
||||
newCommand.name = "Say Hello";
|
||||
newCommand.command = "Hello";
|
||||
// append new object to JSON file
|
||||
|
||||
|
||||
// ButtonGUI.setConfig(JSON.toJSONString(newCommand));
|
||||
|
||||
// get instance of current config
|
||||
String configInstance = ButtonGUI.getConfig();
|
||||
// add new object to config
|
||||
appendToFile(newCommand);
|
||||
|
||||
// set instance to real config
|
||||
|
||||
//writeToFile(ButtonGUI.getConfig());
|
||||
}
|
||||
|
||||
public static void loadSerialization() {
|
||||
@ -39,11 +53,46 @@ public class ConfigFile {
|
||||
}
|
||||
}
|
||||
|
||||
private static void appendToFile(CommandObject commandObject) {
|
||||
JSONArray jsonArray = null;
|
||||
try {
|
||||
jsonArray = (JSONArray) parser.parse(new FileReader("commands.json"));
|
||||
//JSONArray jsonArray = new JSONArray();
|
||||
jsonArray.add(commandObject);
|
||||
writeToFile(jsonArray);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private static void writeToFile(JSONArray jsonArray) {
|
||||
try {
|
||||
fileWriter = new FileWriter("commands.json");
|
||||
String jArrayToString = JSON.toJSONString(jsonArray);
|
||||
fileWriter.write(jArrayToString);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
fileWriter.flush();
|
||||
fileWriter.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
|
@ -42,7 +42,6 @@ public class MacroButtons implements ModInitializer {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// player can run a command here
|
||||
public static void printMessage() {
|
||||
MinecraftClient.getInstance().player.sendChatMessage("/seed");
|
||||
|
@ -23,7 +23,7 @@ public class ButtonGUI extends LightweightGuiDescription {
|
||||
|
||||
setupBackground(root);
|
||||
|
||||
// example button to play with
|
||||
// example button to create config JSON
|
||||
WButton button = new WButton(new TranslatableText("Serialize"));
|
||||
button.setOnClick(() -> {
|
||||
MacroButtons.printMessage();
|
||||
@ -37,14 +37,14 @@ public class ButtonGUI extends LightweightGuiDescription {
|
||||
MacroButtons.printMessage();
|
||||
ConfigFile.loadSerialization();
|
||||
});
|
||||
root.add(button2, xValue + 2, yValue, 4, 1);
|
||||
root.add(button2, xValue + 4, yValue, 6, 1);
|
||||
|
||||
// read json file button
|
||||
WButton button3 = new WButton(new TranslatableText("Read command json"));
|
||||
button3.setOnClick(() -> {
|
||||
ConfigFile.readFile();
|
||||
});
|
||||
root.add(button3, xValue + 6, yValue + 2, 4, 1);
|
||||
root.add(button3, xValue + 10, yValue, 6, 1);
|
||||
|
||||
// Text GUI, not needed yet
|
||||
// WLabel label = new WLabel(new LiteralText("Test"), 0xFFFFFF);
|
||||
@ -100,10 +100,6 @@ public class ButtonGUI extends LightweightGuiDescription {
|
||||
}
|
||||
}
|
||||
|
||||
private int incrementNumber(int a, int b) {
|
||||
return a+b;
|
||||
}
|
||||
|
||||
// Change background panel color to transparent black
|
||||
@Override
|
||||
public void addPainters() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user