Skip to content

Commit

Permalink
Add NotebookData
Browse files Browse the repository at this point in the history
  • Loading branch information
JunePrimavera committed Apr 24, 2024
1 parent d94c7ac commit 70244bb
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
14 changes: 6 additions & 8 deletions src/main/java/xyz/sillyjune/notebook/Notebook.java
Original file line number Diff line number Diff line change
Expand Up @@ -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."); }

Expand Down
7 changes: 3 additions & 4 deletions src/main/java/xyz/sillyjune/notebook/NotebookConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
}
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/xyz/sillyjune/notebook/NotebookData.java
Original file line number Diff line number Diff line change
@@ -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}");
}
}
}

0 comments on commit 70244bb

Please sign in to comment.