From bc34b57ef5167b5396f501bda7d6f0e7305337cf Mon Sep 17 00:00:00 2001 From: PinkGoosik Date: Wed, 11 Jan 2023 00:43:49 +0500 Subject: [PATCH] json config --- gradle.properties | 2 +- .../java/skylands/config/SkylandsConfig.java | 49 +++++++++++++++++++ .../config/SkylandsConfigCommands.java | 10 ++++ src/main/java/skylands/logic/Skylands.java | 6 +-- 4 files changed, 63 insertions(+), 4 deletions(-) diff --git a/gradle.properties b/gradle.properties index 60bee82..84d8757 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,7 +5,7 @@ org.gradle.parallel = true # Mod Properties maven_group = ru.pinkgoosik archives_base_name = skylands -mod_version = 0.1.1 +mod_version = 0.2.0 # Dependencies | Check these on https://fabricmc.net/develop minecraft_version = 1.19.2 diff --git a/src/main/java/skylands/config/SkylandsConfig.java b/src/main/java/skylands/config/SkylandsConfig.java index 876421f..7e7655d 100644 --- a/src/main/java/skylands/config/SkylandsConfig.java +++ b/src/main/java/skylands/config/SkylandsConfig.java @@ -1,9 +1,22 @@ package skylands.config; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import net.fabricmc.loader.api.FabricLoader; import net.minecraft.nbt.NbtCompound; import net.minecraft.util.math.Vec3d; +import skylands.SkylandsMod; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; public class SkylandsConfig { + public static final Gson GSON = new GsonBuilder().setLenient().setPrettyPrinting().create(); + @SuppressWarnings("unused") + public String readDocs = "https://github.com/tyap-lyap/skylands/wiki"; + public String configFormat = "json"; public Vec3d defaultSpawnPos = new Vec3d(0.5D, 75D, 0.5D); public Vec3d defaultVisitsPos = new Vec3d(0.5D, 75D, 0.5D); @@ -56,4 +69,40 @@ public void writeToNbt(NbtCompound nbt) { nbt.put("config", configNbt); } + + public static SkylandsConfig read() { +// String filePath = path.isBlank() ? this.file : this.path + "/" + this.file; + + String filePath = FabricLoader.getInstance().getConfigDir().resolve("skylands.json").toString(); + try { + BufferedReader reader = new BufferedReader(new FileReader(filePath)); + return GSON.fromJson(reader, SkylandsConfig.class); + } + catch(FileNotFoundException e) { + SkylandsMod.LOGGER.info("File " + filePath + " is not found! Setting to default."); + var conf = new SkylandsConfig(); + conf.save(); + return conf; + } + catch(Exception e) { + SkylandsMod.LOGGER.info("Failed to read skylands config due to an exception. " + + "Please delete skylands.json to regenerate config or fix the issue:\n" + e); + e.printStackTrace(); + System.exit(0); + return new SkylandsConfig(); + } + } + + public void save() { + try { + String filePath = FabricLoader.getInstance().getConfigDir().resolve("skylands.json").toString(); + try(FileWriter writer = new FileWriter(filePath)) { + writer.write(GSON.toJson(this)); + } + } + catch(Exception e) { + SkylandsMod.LOGGER.info("Failed to save skylands config due to an exception:\n" + e); + } + } + } diff --git a/src/main/java/skylands/config/SkylandsConfigCommands.java b/src/main/java/skylands/config/SkylandsConfigCommands.java index ea4b3ea..d0f080d 100644 --- a/src/main/java/skylands/config/SkylandsConfigCommands.java +++ b/src/main/java/skylands/config/SkylandsConfigCommands.java @@ -20,6 +20,7 @@ public static void init(CommandDispatcher dispatcher) { dispatcher.register(literal("force-sl").requires(source -> source.hasPermissionLevel(4)).then(literal("config").then(literal("default-spawn-pos").then(argument("position", blockPos()).executes(context -> { var pos = BlockPosArgumentType.getBlockPos(context, "position"); Skylands.instance.config.defaultSpawnPos = new Vec3d(pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5); + Skylands.instance.config.save(); String posText = pos.getX() + " " + pos.getY() + " " + pos.getZ(); context.getSource().sendFeedback(Text.of("config.defaultSpawnPos has changed to: " + posText), true); return 1; @@ -27,6 +28,7 @@ public static void init(CommandDispatcher dispatcher) { }))).then(literal("default-visits-pos").then(argument("position", blockPos()).executes(context -> { var pos = BlockPosArgumentType.getBlockPos(context, "position"); Skylands.instance.config.defaultVisitsPos = new Vec3d(pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5); + Skylands.instance.config.save(); String posText = pos.getX() + " " + pos.getY() + " " + pos.getZ(); context.getSource().sendFeedback(Text.of("config.defaultVisitsPos has changed to: " + posText), true); return 1; @@ -34,26 +36,34 @@ public static void init(CommandDispatcher dispatcher) { }))).then(literal("teleport-after-island-creation").executes(context -> { var config = Skylands.instance.config; config.teleportAfterIslandCreation = !config.teleportAfterIslandCreation; + config.save(); context.getSource().sendFeedback(Text.of("config.teleportAfterIslandCreation has changed to: " + config.teleportAfterIslandCreation), true); return 1; })).then(literal("create-island-on-player-join").executes(context -> { var config = Skylands.instance.config; config.createIslandOnPlayerJoin = !config.createIslandOnPlayerJoin; + config.save(); context.getSource().sendFeedback(Text.of("config.createIslandOnPlayerJoin has changed to: " + config.createIslandOnPlayerJoin), true); return 1; })).then(literal("toggle-update-checker").executes(context -> { var config = Skylands.instance.config; config.updateCheckerEnabled = !config.updateCheckerEnabled; + config.save(); context.getSource().sendFeedback(Text.of("config.updateCheckerEnabled has changed to: " + config.updateCheckerEnabled), true); return 1; })).then(literal("toggle-right-click-harvest").executes(context -> { var config = Skylands.instance.config; config.rightClickHarvestEnabled = !config.rightClickHarvestEnabled; + config.save(); context.getSource().sendFeedback(Text.of("config.rightClickHarvestEnabled has changed to: " + config.rightClickHarvestEnabled), true); return 1; + })).then(literal("reload").executes(context -> { + Skylands.instance.config = SkylandsConfig.read(); + context.getSource().sendFeedback(Text.of("Config successfully reloaded!"), true); + return 1; })))); } diff --git a/src/main/java/skylands/logic/Skylands.java b/src/main/java/skylands/logic/Skylands.java index 684be2f..e5f731e 100644 --- a/src/main/java/skylands/logic/Skylands.java +++ b/src/main/java/skylands/logic/Skylands.java @@ -22,7 +22,7 @@ public Skylands(MinecraftServer server) { this.islands = new IslandStuck(); this.hub = new Hub(); this.invites = new Invites(); - this.config = new SkylandsConfig(); + this.config = SkylandsConfig.read(); } public void readFromNbt(NbtCompound nbt) { @@ -32,7 +32,7 @@ public void readFromNbt(NbtCompound nbt) { NbtMigrator.update(skylandsNbt); this.format = skylandsNbt.getInt("format"); - this.config.readFromNbt(skylandsNbt); +// this.config.readFromNbt(skylandsNbt); this.islands.readFromNbt(skylandsNbt); this.hub.readFromNbt(skylandsNbt); } @@ -41,7 +41,7 @@ public void writeToNbt(NbtCompound nbt) { NbtCompound skylandsNbt = new NbtCompound(); skylandsNbt.putInt("format", this.format); - this.config.writeToNbt(skylandsNbt); +// this.config.writeToNbt(skylandsNbt); this.islands.writeToNbt(skylandsNbt); this.hub.writeToNbt(skylandsNbt);