-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2e5264
commit af8bf17
Showing
4 changed files
with
67 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/main/java/me/senseiwells/chunkdebug/server/config/ChunkDebugServerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package me.senseiwells.chunkdebug.server.config; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.JsonElement; | ||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.JsonOps; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
import me.senseiwells.chunkdebug.ChunkDebug; | ||
import net.fabricmc.loader.api.FabricLoader; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.BufferedWriter; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Optional; | ||
|
||
public record ChunkDebugServerConfig( | ||
boolean requirePermissions | ||
) { | ||
private static final Path PATH = FabricLoader.getInstance().getConfigDir().resolve("chunk-debug-server.json"); | ||
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create(); | ||
|
||
public static final Codec<ChunkDebugServerConfig> CODEC = RecordCodecBuilder.create(instance -> { | ||
return instance.group( | ||
Codec.BOOL.fieldOf("require_permissions").forGetter(ChunkDebugServerConfig::requirePermissions) | ||
).apply(instance, ChunkDebugServerConfig::new); | ||
}); | ||
|
||
public static ChunkDebugServerConfig read() { | ||
if (!Files.exists(PATH)) { | ||
ChunkDebug.LOGGER.info("Generating default config"); | ||
return write(new ChunkDebugServerConfig(true)); | ||
} | ||
try (BufferedReader reader = Files.newBufferedReader(PATH)) { | ||
JsonElement element = GSON.fromJson(reader, JsonElement.class); | ||
Optional<ChunkDebugServerConfig> result = CODEC.parse(JsonOps.INSTANCE, element).resultOrPartial(); | ||
if (result.isPresent()) { | ||
return result.get(); | ||
} | ||
} catch (IOException e) { | ||
ChunkDebug.LOGGER.error("Failed to read chunk-debug-server config", e); | ||
} | ||
return write(new ChunkDebugServerConfig(true)); | ||
} | ||
|
||
private static ChunkDebugServerConfig write(ChunkDebugServerConfig config) { | ||
try { | ||
Files.createDirectories(PATH.getParent()); | ||
Optional<JsonElement> result = CODEC.encodeStart(JsonOps.INSTANCE, config).resultOrPartial(); | ||
if (result.isPresent()) { | ||
try (BufferedWriter writer = Files.newBufferedWriter(PATH)) { | ||
GSON.toJson(result.get(), writer); | ||
} | ||
} | ||
} catch (IOException e) { | ||
ChunkDebug.LOGGER.error("Failed to write chunk-debug-server config", e); | ||
} | ||
return config; | ||
} | ||
} |