From 61f953a2c88047ccc5fb2de0b91e661503d0f06a Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Sat, 27 Feb 2021 16:23:05 +0200 Subject: Move config handling to Path --- .../cotton/gui/impl/client/LibGuiClient.java | 21 ++++++++++++--------- src/main/resources/fabric.mod.json | 4 ++-- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/main/java/io/github/cottonmc/cotton/gui/impl/client/LibGuiClient.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/LibGuiClient.java index d9bc2d5..acd8c5e 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/impl/client/LibGuiClient.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/LibGuiClient.java @@ -14,9 +14,10 @@ import io.github.cottonmc.jankson.JanksonFactory; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import java.io.File; -import java.io.FileOutputStream; +import java.io.InputStream; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; public class LibGuiClient implements ClientModInitializer { public static final Logger logger = LogManager.getLogger(); @@ -37,11 +38,15 @@ public class LibGuiClient implements ClientModInitializer { public static LibGuiConfig loadConfig() { try { - File file = new File(FabricLoader.getInstance().getConfigDirectory(),"libgui.json5"); + Path file = FabricLoader.getInstance().getConfigDir().resolve("libgui.json5"); - if (!file.exists()) saveConfig(new LibGuiConfig()); + if (Files.notExists(file)) saveConfig(new LibGuiConfig()); - JsonObject json = jankson.load(file); + JsonObject json; + try (InputStream in = Files.newInputStream(file)) { + json = jankson.load(in); + } + config = jankson.fromJson(json, LibGuiConfig.class); /* @@ -60,13 +65,11 @@ public class LibGuiClient implements ClientModInitializer { public static void saveConfig(LibGuiConfig config) { try { - File file = new File(FabricLoader.getInstance().getConfigDirectory(),"libgui.json5"); + Path file = FabricLoader.getInstance().getConfigDir().resolve("libgui.json5"); JsonElement json = jankson.toJson(config); String result = json.toJson(true, true); - try (FileOutputStream out = new FileOutputStream(file, false)) { - out.write(result.getBytes(StandardCharsets.UTF_8)); - } + Files.write(file, result.getBytes(StandardCharsets.UTF_8)); } catch (Exception e) { logger.error("[LibGui] Error saving config: {}", e.getMessage()); } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 1030297..a48f2bf 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -26,9 +26,9 @@ "mixins.libgui.accessors.json" ], "depends": { - "fabricloader": ">=0.8.8", + "fabricloader": ">=0.11.0", "fabric": "*", - "minecraft": ">=1.16.1", + "minecraft": ">=1.16.5", "jankson": "^3.0.0", "fabric-networking-api-v1": "^1.0.0" }, -- cgit