2025-06-10 22:14:17 +08:00

106 lines
3.6 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.josyf.commandbuttons;
import com.alibaba.fastjson.JSON;
import com.cedarsoftware.util.io.JsonWriter;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.*;
import java.io.*;
import java.util.ArrayList;
public class ConfigFile {
private static final JSONParser parser = new JSONParser();
private static FileWriter fileWriter;
// Read commands.json, convert it to an array, and append A JSON OBJECT
public static void appendToFile(JSONObject jsonObject) {
try {
Object obj = parser.parse(new FileReader("commands.json"));
JSONArray array = (JSONArray) obj;
array.add(jsonObject);
writeToFile(array);
} catch (IOException e) {
System.out.println("Commands.json 不存在。正在创建...");
JSONArray jsonArray = new JSONArray();
jsonArray.add(jsonObject);
writeToFile(jsonArray);
} catch (ParseException e) {
e.printStackTrace();
}
}
public static void removeFromFile(JSONObject jsonObject) {
try {
Object obj = parser.parse(new FileReader("commands.json"));
JSONArray array = (JSONArray) obj;
array.remove(jsonObject);
writeToFile(array);
} catch (IOException | ParseException e) {
e.printStackTrace();
}
}
// overwrites current commands.json w/ updated jsonArray
private static void writeToFile(JSONArray jsonArray) {
try {
fileWriter = new FileWriter("commands.json");
String jArrayToString = JSON.toJSONString(jsonArray);
String formattedJson = JsonWriter.formatJson(jArrayToString);
fileWriter.write(formattedJson);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// parses commands.json into array, loop through array and add JSONObjects. returns array
public static ArrayList<JSONObject> getArrayFromJsonFile() {
ArrayList<JSONObject> commandObjects = new ArrayList<>();
try {
// assign array to JSONArray using our commands.json as an object
Object obj = parser.parse(new FileReader("commands.json"));
JSONArray array = (JSONArray) obj;
// so "array" is now a JSONArray full of JSONObjects
// now we will iterate through the array and add COs to our local CO array
for (int i = 0; i < array.size(); i++) {
JSONObject childObject = (JSONObject)array.get(i);
commandObjects.add(childObject);
if (i >= 19) break;
}
return commandObjects;
} catch (IOException | ParseException e) {
System.out.println("Commands.json尚未初始化");
}
return null;
}
public static void addObjectToCommList(JSONObject jsonObject) {
ArrayList<JSONObject> commListCopy = CommandButtons.getMasterCommList();
commListCopy.add(jsonObject);
CommandButtons.setMasterCommList(commListCopy);
}
public static void removeObject(JSONObject objToRemove) {
// get masterCommList and remove object from list
ArrayList<JSONObject> commListCopy = CommandButtons.getMasterCommList();
commListCopy.remove(objToRemove);
// get commands.json
// remove corresponding button from json
removeFromFile(objToRemove);
}
}