Skip to content

Commit

Permalink
Remove Duplicate Code between configFiles
Browse files Browse the repository at this point in the history
Signed-off-by: Joseph T. McQuigg <[email protected]>
  • Loading branch information
JT122406 committed Dec 13, 2024
1 parent 9c45547 commit 1257c99
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 97 deletions.
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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<BWGTradesConfig, JsonElement> 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());
}
}
Original file line number Diff line number Diff line change
@@ -1,44 +1,35 @@
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;

public record BWGWorldGenConfig(Map<ResourceKey<Biome>, Boolean> enabledBiomes, int regionWeight,
boolean vanillaAdditions, Map<ResourceKey<PlacedFeature>, 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<BWGWorldGenConfig> INSTANCE = Suppliers.memoize(BWGWorldGenConfig::getOrCreateConfigFromDisk);

public static final Codec<BWGWorldGenConfig> CODEC = RecordCodecBuilder.create(instance ->
private static final Codec<BWGWorldGenConfig> 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),
Expand All @@ -47,7 +38,7 @@ public record BWGWorldGenConfig(Map<ResourceKey<Biome>, Boolean> enabledBiomes,
).apply(instance, BWGWorldGenConfig::new)
);

public static BWGWorldGenConfig createDefault() {
private static BWGWorldGenConfig createDefault() {
return new BWGWorldGenConfig(getDefaultBiomes(), 8, true, getVanillaPlacedFeatureAdditions());
}

Expand All @@ -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<BWGWorldGenConfig, JsonElement> configResult = CODEC.decode(JanksonJsonOps.INSTANCE, load).result().orElseThrow();
BWGWorldGenConfig config = configResult.getFirst();


Map<ResourceKey<Biome>, 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());
}
}

0 comments on commit 1257c99

Please sign in to comment.