Skip to content

Commit

Permalink
move file to config/rainglow.json
Browse files Browse the repository at this point in the history
  • Loading branch information
ix0rai committed Jun 2, 2024
1 parent 9ed3c0f commit 6e03486
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/main/java/io/ix0rai/rainglow/config/PerWorldConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<ValueMap<String>> modesByWorld = this.map("").build();

public RainglowMode getMode(World world) {
Expand All @@ -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);
Expand Down Expand Up @@ -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<Path, String> 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) {

}
Expand Down

0 comments on commit 6e03486

Please sign in to comment.