-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove Duplicate Code between configFiles
Signed-off-by: Joseph T. McQuigg <[email protected]>
- Loading branch information
Showing
3 changed files
with
54 additions
and
97 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
Common/src/main/java/net/potionstudios/biomeswevegone/config/ConfigUtils.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,46 @@ | ||
package net.potionstudios.biomeswevegone.config; | ||
|
||
import com.mojang.datafixers.util.Pair; | ||
import com.mojang.serialization.Codec; | ||
import corgitaco.corgilib.serialization.jankson.JanksonJsonOps; | ||
import corgitaco.corgilib.shadow.blue.endless.jankson.Jankson; | ||
import corgitaco.corgilib.shadow.blue.endless.jankson.JsonElement; | ||
import corgitaco.corgilib.shadow.blue.endless.jankson.JsonGrammar; | ||
import corgitaco.corgilib.shadow.blue.endless.jankson.JsonObject; | ||
import corgitaco.corgilib.shadow.blue.endless.jankson.api.SyntaxError; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
public class ConfigUtils { | ||
public static <T> T loadConfig(Path path, Codec<T> codec, T defaultConfig) { | ||
if (!path.toFile().exists()) { | ||
createDefaultFile(path, codec, defaultConfig); | ||
return defaultConfig; | ||
} | ||
|
||
Jankson build = new Jankson.Builder().build(); | ||
try { | ||
String configFile = Files.readString(path).strip(); | ||
JsonObject load = build.load(configFile); | ||
Pair<T, JsonElement> configResult = codec.decode(JanksonJsonOps.INSTANCE, load).result().orElseThrow(); | ||
T config = configResult.getFirst(); | ||
createDefaultFile(path, codec, config); | ||
return config; | ||
} catch (IOException | SyntaxError e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public static <T> void createDefaultFile(Path path, Codec<T> codec, T config) { | ||
JsonElement jsonElement = codec.encodeStart(JanksonJsonOps.INSTANCE, config).result().orElseThrow(); | ||
String json = jsonElement.toJson(JsonGrammar.JSON5); | ||
try { | ||
Files.createDirectories(path.getParent()); | ||
Files.writeString(path, json); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
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