From 1257c99ac240070a28db576521ceb71974c19eca Mon Sep 17 00:00:00 2001 From: "Joseph T. McQuigg" Date: Thu, 12 Dec 2024 22:30:46 -0500 Subject: [PATCH] Remove Duplicate Code between configFiles Signed-off-by: Joseph T. McQuigg --- .../biomeswevegone/config/ConfigUtils.java | 46 ++++++++++++++ .../config/configs/BWGTradesConfig.java | 44 +------------ .../config/configs/BWGWorldGenConfig.java | 61 ++----------------- 3 files changed, 54 insertions(+), 97 deletions(-) create mode 100644 Common/src/main/java/net/potionstudios/biomeswevegone/config/ConfigUtils.java diff --git a/Common/src/main/java/net/potionstudios/biomeswevegone/config/ConfigUtils.java b/Common/src/main/java/net/potionstudios/biomeswevegone/config/ConfigUtils.java new file mode 100644 index 000000000..6e790cb33 --- /dev/null +++ b/Common/src/main/java/net/potionstudios/biomeswevegone/config/ConfigUtils.java @@ -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 loadConfig(Path path, Codec 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 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 void createDefaultFile(Path path, Codec 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); + } + } +} diff --git a/Common/src/main/java/net/potionstudios/biomeswevegone/config/configs/BWGTradesConfig.java b/Common/src/main/java/net/potionstudios/biomeswevegone/config/configs/BWGTradesConfig.java index a559e2b77..9273bdb84 100644 --- a/Common/src/main/java/net/potionstudios/biomeswevegone/config/configs/BWGTradesConfig.java +++ b/Common/src/main/java/net/potionstudios/biomeswevegone/config/configs/BWGTradesConfig.java @@ -1,21 +1,13 @@ package net.potionstudios.biomeswevegone.config.configs; import com.google.common.base.Suppliers; -import com.mojang.datafixers.util.Pair; import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; import corgitaco.corgilib.serialization.codec.CommentedCodec; -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 net.potionstudios.biomeswevegone.PlatformHandler; +import net.potionstudios.biomeswevegone.config.ConfigUtils; import org.jetbrains.annotations.NotNull; -import java.io.IOException; -import java.nio.file.Files; import java.nio.file.Path; import java.util.function.Supplier; @@ -36,38 +28,6 @@ private static BWGTradesConfig createDefault() { } private static BWGTradesConfig getOrCreateConfigFromDisk() { - BWGTradesConfig defaultConfig = createDefault(); - - if (!PATH.toFile().exists()) { - createDefaultFile(defaultConfig); - return defaultConfig; - } - - Jankson build = new Jankson.Builder().build(); - try { - String configFile = Files.readString(PATH).stripTrailing().trim().strip().stripLeading(); - JsonObject load = build.load(configFile); - Pair configResult = CODEC.decode(JanksonJsonOps.INSTANCE, load).result().orElseThrow(); - BWGTradesConfig config = configResult.getFirst(); - - BWGTradesConfig toCreate = new BWGTradesConfig(config.enableTrades, config.enableVanillaTradeAdditions); - - createDefaultFile(toCreate); - return toCreate; - - } catch (IOException | SyntaxError e) { - throw new RuntimeException(e); - } - } - - private static void createDefaultFile(BWGTradesConfig tradesConfig) { - JsonElement jsonElement = CODEC.encodeStart(JanksonJsonOps.INSTANCE, tradesConfig).result().orElseThrow(); - String json = jsonElement.toJson(JsonGrammar.JSON5); - try { - Files.createDirectories(PATH.getParent()); - Files.writeString(PATH, json); - } catch (IOException e) { - throw new RuntimeException(e); - } + return ConfigUtils.loadConfig(PATH, CODEC, createDefault()); } } diff --git a/Common/src/main/java/net/potionstudios/biomeswevegone/config/configs/BWGWorldGenConfig.java b/Common/src/main/java/net/potionstudios/biomeswevegone/config/configs/BWGWorldGenConfig.java index 0183584f0..8890a9221 100644 --- a/Common/src/main/java/net/potionstudios/biomeswevegone/config/configs/BWGWorldGenConfig.java +++ b/Common/src/main/java/net/potionstudios/biomeswevegone/config/configs/BWGWorldGenConfig.java @@ -1,31 +1,22 @@ package net.potionstudios.biomeswevegone.config.configs; import com.google.common.base.Suppliers; -import com.mojang.datafixers.util.Pair; import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; import corgitaco.corgilib.serialization.codec.CommentedCodec; -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 it.unimi.dsi.fastutil.objects.Object2BooleanMap; import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap; -import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; import net.minecraft.core.registries.Registries; import net.minecraft.resources.ResourceKey; import net.minecraft.world.level.biome.Biome; import net.minecraft.world.level.levelgen.placement.PlacedFeature; import net.potionstudios.biomeswevegone.PlatformHandler; +import net.potionstudios.biomeswevegone.config.ConfigUtils; import net.potionstudios.biomeswevegone.world.level.levelgen.biome.BWGBiomes; import net.potionstudios.biomeswevegone.world.level.levelgen.feature.placed.BWGOverworldTreePlacedFeatures; import net.potionstudios.biomeswevegone.world.level.levelgen.feature.placed.BWGVanillaPlacedFeatures; import org.jetbrains.annotations.NotNull; -import java.io.IOException; -import java.nio.file.Files; import java.nio.file.Path; import java.util.Map; import java.util.function.Supplier; @@ -33,12 +24,12 @@ public record BWGWorldGenConfig(Map, Boolean> enabledBiomes, int regionWeight, boolean vanillaAdditions, Map, Boolean> vanillaFeatures) { - public static final Path PATH = PlatformHandler.PLATFORM_HANDLER.configPath().resolve("world_generation.json5"); + private static final Path PATH = PlatformHandler.PLATFORM_HANDLER.configPath().resolve("world_generation.json5"); @NotNull public static Supplier INSTANCE = Suppliers.memoize(BWGWorldGenConfig::getOrCreateConfigFromDisk); - public static final Codec CODEC = RecordCodecBuilder.create(instance -> + private static final Codec CODEC = RecordCodecBuilder.create(instance -> instance.group( CommentedCodec.of(Codec.unboundedMap(ResourceKey.codec(Registries.BIOME), Codec.BOOL), "enabled_biomes", "Which biomes are enabled, if disabled the biome will default to its vanilla counterpart for the given region").orElse(getDefaultBiomes()).forGetter(BWGWorldGenConfig::enabledBiomes), CommentedCodec.of(Codec.INT, "region_weight", "How much each BWG region weighs. This weight applies to all 3 BWG Regions").orElse(8).forGetter(BWGWorldGenConfig::regionWeight), @@ -47,7 +38,7 @@ public record BWGWorldGenConfig(Map, Boolean> enabledBiomes, ).apply(instance, BWGWorldGenConfig::new) ); - public static BWGWorldGenConfig createDefault() { + private static BWGWorldGenConfig createDefault() { return new BWGWorldGenConfig(getDefaultBiomes(), 8, true, getVanillaPlacedFeatureAdditions()); } @@ -72,47 +63,7 @@ public static BWGWorldGenConfig createDefault() { return enabledFeatures; } - public static BWGWorldGenConfig getOrCreateConfigFromDisk() { - BWGWorldGenConfig defaultWorldGenConfig = createDefault(); - - if (!PATH.toFile().exists()) { - createDefaultFile(defaultWorldGenConfig); - return defaultWorldGenConfig; - } else { - - Jankson build = new Jankson.Builder().build(); - try { - String configFile = Files.readString(PATH).stripTrailing().trim().strip().stripLeading(); - JsonObject load = build.load(configFile); - Pair configResult = CODEC.decode(JanksonJsonOps.INSTANCE, load).result().orElseThrow(); - BWGWorldGenConfig config = configResult.getFirst(); - - - Map, Boolean> temporary = new Reference2ObjectOpenHashMap<>(); - - temporary.putAll(defaultWorldGenConfig.enabledBiomes); - temporary.putAll(config.enabledBiomes); - - BWGWorldGenConfig toCreate = new BWGWorldGenConfig(temporary, config.regionWeight, config.vanillaAdditions, config.vanillaFeatures); - - createDefaultFile(toCreate); - - return toCreate; - - } catch (IOException | SyntaxError e) { - throw new RuntimeException(e); - } - } - } - - private static void createDefaultFile(BWGWorldGenConfig defaultWorldGenConfig) { - JsonElement jsonElement = CODEC.encodeStart(JanksonJsonOps.INSTANCE, defaultWorldGenConfig).result().orElseThrow(); - String json = jsonElement.toJson(JsonGrammar.JSON5); - try { - Files.createDirectories(PATH.getParent()); - Files.writeString(PATH, json); - } catch (IOException e) { - throw new RuntimeException(e); - } + private static BWGWorldGenConfig getOrCreateConfigFromDisk() { + return ConfigUtils.loadConfig(PATH, CODEC, createDefault()); } }