From 6e034866812d36493ab33de6d0902ee9841ed07d Mon Sep 17 00:00:00 2001 From: ix0rai Date: Sat, 1 Jun 2024 22:11:53 -0500 Subject: [PATCH] move file to config/rainglow.json --- .../rainglow/config/PerWorldConfig.java | 30 +++++++++++++++---- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/ix0rai/rainglow/config/PerWorldConfig.java b/src/main/java/io/ix0rai/rainglow/config/PerWorldConfig.java index a89d30c..47ca576 100644 --- a/src/main/java/io/ix0rai/rainglow/config/PerWorldConfig.java +++ b/src/main/java/io/ix0rai/rainglow/config/PerWorldConfig.java @@ -11,8 +11,8 @@ import io.ix0rai.rainglow.data.RainglowMode; import io.ix0rai.rainglow.mixin.MinecraftServerAccessor; import net.minecraft.client.MinecraftClient; +import net.minecraft.server.MinecraftServer; import net.minecraft.server.dedicated.DedicatedServer; -import net.minecraft.util.WorldSavePath; import net.minecraft.world.World; import java.nio.file.Files; @@ -21,7 +21,7 @@ @SerializedNameConvention(NamingSchemes.SNAKE_CASE) public class PerWorldConfig extends ReflectiveConfig { @Comment("The mode used for each non-local world.") - @Comment("Note that for singleplayer worlds, the mode is saved in the world folder in the file \"rainglow.json\".") + @Comment("Note that for singleplayer worlds, the mode is saved in the world folder in the file \"config/rainglow.json\".") public final TrackedValue> modesByWorld = this.map("").build(); public RainglowMode getMode(World world) { @@ -31,7 +31,7 @@ public RainglowMode getMode(World world) { if (saveName.right().isPresent()) { mode = modesByWorld.value().get(saveName.right().get()); } else if (saveName.left().isPresent()) { - Path path = saveName.left().get().resolve("rainglow.json"); + Path path = getJsonPath(saveName.left().get()); if (Files.exists(path)) { try { var data = Rainglow.GSON.fromJson(Files.readString(path), RainglowJson.class); @@ -63,33 +63,51 @@ public void setMode(World world, RainglowMode mode) { } private static void save(Path worldPath, RainglowMode mode) { - Path path = worldPath.resolve("rainglow.json"); + Path path = getJsonPath(worldPath); var data = new RainglowJson(mode.getId()); try { + Path configPath = getConfigFolderPath(worldPath); + if (!Files.exists(configPath)) { + Files.createDirectories(configPath); + } + Files.writeString(path, Rainglow.GSON.toJson(data)); } catch (Exception e) { Rainglow.LOGGER.error("Failed to save Rainglow config for world " + worldPath, e); } } + private static Path getJsonPath(Path worldPath) { + return getConfigFolderPath(worldPath).resolve("rainglow.json"); + } + + private static Path getConfigFolderPath(Path worldPath) { + return worldPath.resolve("config"); + } + @SuppressWarnings("ConstantConditions") private static Either getSaveName(World world) { if (!world.isClient) { if (world.getServer() instanceof DedicatedServer dedicatedServer) { return Either.right(dedicatedServer.getLevelName()); // "world" or something } else { - return Either.left(((MinecraftServerAccessor) world.getServer()).getSession().getDirectory(WorldSavePath.ROOT)); + return Either.left(getWorldPath(world.getServer())); } } else { if (MinecraftClient.getInstance().isInSingleplayer()) { - return Either.left(((MinecraftServerAccessor) MinecraftClient.getInstance().getServer()).getSession().getDirectory(WorldSavePath.ROOT)); + return Either.left(getWorldPath(MinecraftClient.getInstance().getServer())); } else { return Either.right(MinecraftClient.getInstance().getCurrentServerEntry().address); } } } + @SuppressWarnings("ConstantConditions") + private static Path getWorldPath(MinecraftServer server) { + return ((MinecraftServerAccessor) server).getSession().method_54543().path(); + } + private record RainglowJson(String mode) { }