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 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 com.josyf.macrobuttons.gui.ButtonGUI;
|
||||||
import io.netty.channel.group.ChannelGroupFuture;
|
import io.netty.channel.group.ChannelGroupFuture;
|
||||||
import net.minecraft.client.MinecraftClient;
|
import net.minecraft.client.MinecraftClient;
|
||||||
|
import org.json.simple.JSONArray;
|
||||||
import org.json.simple.parser.JSONParser;
|
import org.json.simple.parser.JSONParser;
|
||||||
import org.json.simple.parser.ParseException;
|
import org.json.simple.parser.ParseException;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
@ -23,9 +25,21 @@ public class ConfigFile {
|
|||||||
|
|
||||||
public static void serializeCommand() {
|
public static void serializeCommand() {
|
||||||
CommandObject newCommand = new CommandObject();
|
CommandObject newCommand = new CommandObject();
|
||||||
newCommand.message = "hello";
|
newCommand.name = "Say Hello";
|
||||||
ButtonGUI.setConfig(JSON.toJSONString(newCommand));
|
newCommand.command = "Hello";
|
||||||
writeToFile(ButtonGUI.getConfig());
|
// 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() {
|
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) {
|
private static void writeToFile(String jsonMessage) {
|
||||||
try {
|
try {
|
||||||
// these both write to the correct location
|
// these both write to the correct location
|
||||||
fileWriter = new FileWriter("commands.json");
|
fileWriter = new FileWriter("commands.json");
|
||||||
// file = new FileWriter(MinecraftClient.getInstance().runDirectory + "/command.json");
|
// file = new FileWriter(MinecraftClient.getInstance().runDirectory + "/command.json");
|
||||||
|
System.out.println("APPENDING!");
|
||||||
|
|
||||||
fileWriter.write(jsonMessage);
|
fileWriter.write(jsonMessage);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -42,7 +42,6 @@ public class MacroButtons implements ModInitializer {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// player can run a command here
|
// player can run a command here
|
||||||
public static void printMessage() {
|
public static void printMessage() {
|
||||||
MinecraftClient.getInstance().player.sendChatMessage("/seed");
|
MinecraftClient.getInstance().player.sendChatMessage("/seed");
|
||||||
|
@ -23,7 +23,7 @@ public class ButtonGUI extends LightweightGuiDescription {
|
|||||||
|
|
||||||
setupBackground(root);
|
setupBackground(root);
|
||||||
|
|
||||||
// example button to play with
|
// example button to create config JSON
|
||||||
WButton button = new WButton(new TranslatableText("Serialize"));
|
WButton button = new WButton(new TranslatableText("Serialize"));
|
||||||
button.setOnClick(() -> {
|
button.setOnClick(() -> {
|
||||||
MacroButtons.printMessage();
|
MacroButtons.printMessage();
|
||||||
@ -37,14 +37,14 @@ public class ButtonGUI extends LightweightGuiDescription {
|
|||||||
MacroButtons.printMessage();
|
MacroButtons.printMessage();
|
||||||
ConfigFile.loadSerialization();
|
ConfigFile.loadSerialization();
|
||||||
});
|
});
|
||||||
root.add(button2, xValue + 2, yValue, 4, 1);
|
root.add(button2, xValue + 4, yValue, 6, 1);
|
||||||
|
|
||||||
// read json file button
|
// read json file button
|
||||||
WButton button3 = new WButton(new TranslatableText("Read command json"));
|
WButton button3 = new WButton(new TranslatableText("Read command json"));
|
||||||
button3.setOnClick(() -> {
|
button3.setOnClick(() -> {
|
||||||
ConfigFile.readFile();
|
ConfigFile.readFile();
|
||||||
});
|
});
|
||||||
root.add(button3, xValue + 6, yValue + 2, 4, 1);
|
root.add(button3, xValue + 10, yValue, 6, 1);
|
||||||
|
|
||||||
// Text GUI, not needed yet
|
// Text GUI, not needed yet
|
||||||
// WLabel label = new WLabel(new LiteralText("Test"), 0xFFFFFF);
|
// 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
|
// Change background panel color to transparent black
|
||||||
@Override
|
@Override
|
||||||
public void addPainters() {
|
public void addPainters() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user