aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/dev/isxander/yacl3/config/v3/JsonFileCodecConfig.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/dev/isxander/yacl3/config/v3/JsonFileCodecConfig.java')
-rw-r--r--src/main/java/dev/isxander/yacl3/config/v3/JsonFileCodecConfig.java90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/main/java/dev/isxander/yacl3/config/v3/JsonFileCodecConfig.java b/src/main/java/dev/isxander/yacl3/config/v3/JsonFileCodecConfig.java
new file mode 100644
index 0000000..49a0dac
--- /dev/null
+++ b/src/main/java/dev/isxander/yacl3/config/v3/JsonFileCodecConfig.java
@@ -0,0 +1,90 @@
+package dev.isxander.yacl3.config.v3;
+
+import com.google.gson.*;
+import com.mojang.serialization.DataResult;
+import com.mojang.serialization.JsonOps;
+import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.Nullable;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardOpenOption;
+
+@ApiStatus.Experimental
+public abstract class JsonFileCodecConfig extends CodecConfig {
+ private final Path configPath;
+ private final Gson gson;
+
+ public JsonFileCodecConfig(Path configPath) {
+ this.configPath = configPath;
+ this.gson = createGson();
+ }
+
+ public void saveToFile() {
+ DataResult<JsonElement> jsonTreeResult = this.encodeStart(JsonOps.INSTANCE);
+ if (jsonTreeResult.error().isPresent()) {
+ onSaveError(
+ SaveError.ENCODING,
+ new IllegalStateException("Failed to encode: " + jsonTreeResult.error().get().message())
+ );
+ return;
+ }
+
+ JsonElement jsonTree = jsonTreeResult.result().orElseThrow();
+ String json = gson.toJson(jsonTree);
+
+ try {
+ Files.writeString(configPath, json, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
+ } catch (IOException e) {
+ onSaveError(SaveError.WRITING, e);
+ }
+ }
+
+ public boolean loadFromFile() {
+ if (Files.notExists(configPath)) {
+ return false;
+ }
+
+ String json;
+ try {
+ json = Files.readString(configPath);
+ } catch (IOException e) {
+ onLoadError(LoadError.READING, e);
+ return false;
+ }
+
+ JsonElement jsonTree;
+ try {
+ jsonTree = JsonParser.parseString(json);
+ } catch (JsonParseException e) {
+ onLoadError(LoadError.JSON_PARSING, e);
+ return false;
+ }
+
+ return this.decode(jsonTree, JsonOps.INSTANCE);
+ }
+
+ protected Gson createGson() {
+ return new GsonBuilder().setPrettyPrinting().create();
+ }
+
+ protected void onSaveError(SaveError error, @Nullable Throwable e) {
+ throw new IllegalStateException("Error whilst " + error.name().toLowerCase(), e);
+ }
+
+ protected void onLoadError(LoadError error, @Nullable Throwable e) {
+ throw new IllegalStateException("Error whilst " + error.name().toLowerCase(), e);
+ }
+
+ protected enum SaveError {
+ WRITING,
+ ENCODING,
+ }
+
+ protected enum LoadError {
+ READING,
+ JSON_PARSING,
+ DECODING,
+ }
+}