From 70244bb99eb2dd90e59cb9c918512ab80c41ffd0 Mon Sep 17 00:00:00 2001 From: Juniper Primavera Date: Wed, 24 Apr 2024 18:49:59 +0100 Subject: [PATCH] Add NotebookData --- .../java/xyz/sillyjune/notebook/Notebook.java | 14 +++-- .../sillyjune/notebook/NotebookConfig.java | 7 ++- .../xyz/sillyjune/notebook/NotebookData.java | 51 +++++++++++++++++++ 3 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 src/main/java/xyz/sillyjune/notebook/NotebookData.java diff --git a/src/main/java/xyz/sillyjune/notebook/Notebook.java b/src/main/java/xyz/sillyjune/notebook/Notebook.java index 8f07260..37895cd 100644 --- a/src/main/java/xyz/sillyjune/notebook/Notebook.java +++ b/src/main/java/xyz/sillyjune/notebook/Notebook.java @@ -48,20 +48,18 @@ public void onInitialize() { } } CONFIG = cfg; // Set the config - openBookKeybindRegister(); // Register keybind for opening the notebook, ";" by default + openBookKeybindRegister(); // Register keybind for opening the notebook, ";" by default - // TODO: Rewrite this - if (!new File(BOOK_FOLDER).exists() || !new File(BOOK_FOLDER + "/Default").exists()) { + if (!new File(BOOK_FOLDER).exists() || !new File(STR."\{BOOK_FOLDER}/default.json").exists()) { try { - Path path = Paths.get(BOOK_FOLDER + "/Default"); - Files.createDirectories(path); + Files.createDirectories(Paths.get(BOOK_FOLDER)); + NotebookData data = new NotebookData(new String[0], "default.json"); + NotebookData.write(data); } catch (IOException e) { - System.err.println("Failed to create directory! Make a bug report if you see this\n " + e.getMessage()); + LOGGER.error(STR."failed to create \{BOOK_FOLDER}"); } } - // - if (CONFIG.debug()) { LOGGER.error("June is very silly. Continue with extreme caution."); } diff --git a/src/main/java/xyz/sillyjune/notebook/NotebookConfig.java b/src/main/java/xyz/sillyjune/notebook/NotebookConfig.java index 2946c33..3d44436 100644 --- a/src/main/java/xyz/sillyjune/notebook/NotebookConfig.java +++ b/src/main/java/xyz/sillyjune/notebook/NotebookConfig.java @@ -5,14 +5,14 @@ import java.util.Scanner; public record NotebookConfig(boolean debug, int button_offset) { - public boolean debug() { + public boolean debug() { // Is debug mode enabled? return debug; } - public int button_offset() { + public int button_offset() { // Button offset config option for compatibility with mods with buttons in the same place (e.g. create) return button_offset; } - static String read_config() { + static String read_config() { // Read config from file try { File config = new File("config/notebook.json"); Scanner reader = new Scanner(config); @@ -24,7 +24,6 @@ static String read_config() { reader.close(); return d.toString(); } catch (FileNotFoundException e) { - System.out.println("Couldn't read config!"); return null; } } diff --git a/src/main/java/xyz/sillyjune/notebook/NotebookData.java b/src/main/java/xyz/sillyjune/notebook/NotebookData.java new file mode 100644 index 0000000..210b76f --- /dev/null +++ b/src/main/java/xyz/sillyjune/notebook/NotebookData.java @@ -0,0 +1,51 @@ +package xyz.sillyjune.notebook; + +import com.google.gson.Gson; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Scanner; + +import static xyz.sillyjune.notebook.Notebook.BOOK_FOLDER; +import static xyz.sillyjune.notebook.Notebook.LOGGER; + +public record NotebookData(String[] content, String location) { + public String[] content() { + return content; + } + public String location() { + return location; + } + + public NotebookData read() { // Read the record from a file + File config = new File(STR."notebook/\{location}.json"); + StringBuilder d = new StringBuilder(); + try { + Scanner reader = new Scanner(config); + while (reader.hasNextLine()) { + String data = reader.nextLine(); + d.append(data); + } + reader.close(); + } catch (FileNotFoundException e) { // If it fails, create a new one + LOGGER.error("Failed to read book!"); + NotebookData data = new NotebookData(new String[0], STR."notebook/\{location}"); + write(data); // Write it to file + } + String json = d.toString(); + return new Gson().fromJson(json, NotebookData.class); + } + + public static void write(NotebookData data) { + String json = new Gson().toJson(data); + try { // Write the record to a file + FileWriter writer = new FileWriter(BOOK_FOLDER + STR."/\{data.location}"); + writer.write(json); + writer.close(); + } catch (IOException e) { + Notebook.LOGGER.error(STR."Failed to write book!\n\{e}"); + } + } +}