config cleanup and refactor

This commit is contained in:
Relentless 2022-08-23 17:41:37 +02:00
parent f534c61637
commit ece7139660
No known key found for this signature in database
GPG key ID: 759D97B8C6F25265
5 changed files with 66 additions and 95 deletions

View file

@ -2,16 +2,21 @@ package com.almostreliable.unified.config;
import com.almostreliable.unified.AlmostUnified; import com.almostreliable.unified.AlmostUnified;
import com.almostreliable.unified.AlmostUnifiedPlatform; import com.almostreliable.unified.AlmostUnifiedPlatform;
import com.almostreliable.unified.utils.JsonUtils;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardOpenOption; import java.nio.file.StandardOpenOption;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Collectors;
public class Config { public class Config {
@ -41,10 +46,9 @@ public class Config {
} }
private static JsonObject safeLoadJson(String file) { private static JsonObject safeLoadJson(String file) {
try {
Path p = createConfigDir(); Path p = createConfigDir();
BufferedReader bufferedReader = Files.newBufferedReader(buildPath(p, file)); try (BufferedReader reader = Files.newBufferedReader(buildPath(p, file))) {
return new Gson().fromJson(bufferedReader, JsonObject.class); return new Gson().fromJson(reader, JsonObject.class);
} catch (Exception ignored) { } catch (Exception ignored) {
} }
return new JsonObject(); return new JsonObject();
@ -64,15 +68,15 @@ public class Config {
return p.resolve(name + ".json"); return p.resolve(name + ".json");
} }
public static abstract class Serializer<T extends Config> { public abstract static class Serializer<T extends Config> {
private boolean invalid = false; private boolean valid = true;
protected void setInvalid() { protected void setInvalid() {
this.invalid = true; this.valid = false;
} }
public boolean isInvalid() { public boolean isInvalid() {
return invalid; return !valid;
} }
public <V> V safeGet(Supplier<V> supplier, V defaultValue) { public <V> V safeGet(Supplier<V> supplier, V defaultValue) {
@ -84,6 +88,22 @@ public class Config {
return defaultValue; return defaultValue;
} }
protected Set<ResourceLocation> deserializeResourceLocations(JsonObject json, String configKey) {
return safeGet(() -> JsonUtils
.toList(json.getAsJsonArray(configKey))
.stream()
.map(ResourceLocation::new)
.collect(Collectors.toSet()), new HashSet<>());
}
protected void serializeResourceLocations(JsonObject json, String configKey, Set<ResourceLocation> resourceLocations) {
json.add(configKey,
JsonUtils.toArray(resourceLocations
.stream()
.map(ResourceLocation::toString)
.toList()));
}
public abstract T deserialize(JsonObject json); public abstract T deserialize(JsonObject json);
public abstract JsonObject serialize(T src); public abstract JsonObject serialize(T src);

View file

@ -13,7 +13,7 @@ import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class DebugConfig extends Config { public class DebugConfig extends Config {
public static String NAME = "debug"; public static final String NAME = "debug";
public final boolean dumpTagMap; public final boolean dumpTagMap;
public final boolean dumpDuplicates; public final boolean dumpDuplicates;
@ -35,20 +35,17 @@ public class DebugConfig extends Config {
} }
FileUtils.write(AlmostUnifiedPlatform.INSTANCE.getLogPath(), "unify_tag_dump.txt", sb -> { FileUtils.write(AlmostUnifiedPlatform.INSTANCE.getLogPath(), "unify_tag_dump.txt", sb -> {
tagMap sb.append(tagMap
.getTags() .getTags()
.stream() .stream()
.sorted(Comparator.comparing(t -> t.location().toString())) .sorted(Comparator.comparing(t -> t.location().toString()))
.forEach(t -> sb .map(t -> StringUtils.rightPad(t.location().toString(), 40) + " => " + tagMap
.append(StringUtils.rightPad(t.location().toString(), 40))
.append(" => ")
.append(tagMap
.getItems(t) .getItems(t)
.stream() .stream()
.map(ResourceLocation::toString) .map(ResourceLocation::toString)
.sorted() .sorted()
.collect(Collectors.joining(", "))) .collect(Collectors.joining(", ")) + "\n")
.append("\n")); .collect(Collectors.joining()));
}); });
} }

View file

@ -1,20 +1,11 @@
package com.almostreliable.unified.config; package com.almostreliable.unified.config;
import com.almostreliable.unified.AlmostUnifiedPlatform;
import com.almostreliable.unified.Platform; import com.almostreliable.unified.Platform;
import java.util.List; import java.util.List;
@SuppressWarnings("SpellCheckingInspection")
public class Defaults { public final class Defaults {
public static final List<String> STONE_STRATA = List.of("stone",
"nether",
"deepslate",
"granite",
"diorite",
"andesite");
@SuppressWarnings("SpellCheckingInspection")
public static final List<String> MOD_PRIORITIES = List.of( public static final List<String> MOD_PRIORITIES = List.of(
"minecraft", "minecraft",
"kubejs", "kubejs",
@ -23,7 +14,12 @@ public class Defaults {
"thermal", "thermal",
"immersiveengineering", "immersiveengineering",
"mekanism"); "mekanism");
@SuppressWarnings("SpellCheckingInspection") public static final List<String> STONE_STRATA = List.of("stone",
"nether",
"deepslate",
"granite",
"diorite",
"andesite");
public static final List<String> MATERIALS = List.of("aeternium", public static final List<String> MATERIALS = List.of("aeternium",
"aluminum", "aluminum",
"amber", "amber",
@ -62,17 +58,8 @@ public class Defaults {
"zinc" "zinc"
); );
public static final List<String> TYPES = List.of(
"nuggets", private Defaults() {}
"dusts",
"gears",
"gems",
"ingots",
"raw_materials",
"ores",
"plates",
"rods",
"storage_blocks");
public static List<String> getTags(Platform platform) { public static List<String> getTags(Platform platform) {
return switch (platform) { return switch (platform) {

View file

@ -1,19 +1,17 @@
package com.almostreliable.unified.config; package com.almostreliable.unified.config;
import com.almostreliable.unified.BuildConfig;
import com.almostreliable.unified.recipe.RecipeLink; import com.almostreliable.unified.recipe.RecipeLink;
import com.almostreliable.unified.utils.JsonCompare; import com.almostreliable.unified.utils.JsonCompare;
import com.almostreliable.unified.utils.JsonUtils;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import java.util.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class DuplicationConfig extends Config { public class DuplicationConfig extends Config {
public static final String NAME = "duplicates"; public static final String NAME = "duplicates";
private final JsonCompare.CompareSettings defaultRules; private final JsonCompare.CompareSettings defaultRules;
private final LinkedHashMap<ResourceLocation, JsonCompare.CompareSettings> overrideRules; private final LinkedHashMap<ResourceLocation, JsonCompare.CompareSettings> overrideRules;
private final Set<ResourceLocation> ignoreRecipeTypes; private final Set<ResourceLocation> ignoreRecipeTypes;
@ -42,28 +40,20 @@ public class DuplicationConfig extends Config {
@Override @Override
public DuplicationConfig deserialize(JsonObject json) { public DuplicationConfig deserialize(JsonObject json) {
Set<ResourceLocation> ignoreRecipeTypes = safeGet(() -> JsonUtils Set<ResourceLocation> ignoreRecipeTypes = deserializeResourceLocations(json, IGNORED_RECIPE_TYPES);
.toList(json.getAsJsonArray(IGNORED_RECIPE_TYPES)) Set<ResourceLocation> ignoreRecipes = deserializeResourceLocations(json, IGNORED_RECIPES);
.stream()
.map(ResourceLocation::new)
.collect(Collectors.toSet()), new HashSet<>());
Set<ResourceLocation> ignoreRecipes = safeGet(() -> JsonUtils
.toList(json.getAsJsonArray(IGNORED_RECIPES))
.stream()
.map(ResourceLocation::new)
.collect(Collectors.toSet()), new HashSet<>());
JsonCompare.CompareSettings defaultRules = safeGet(() -> createCompareSet(json.getAsJsonObject( JsonCompare.CompareSettings defaultRules = safeGet(() -> createCompareSet(json.getAsJsonObject(
DEFAULT_DUPLICATE_RULES)), DEFAULT_DUPLICATE_RULES)),
defaultSet()); defaultSet());
LinkedHashMap<ResourceLocation, JsonCompare.CompareSettings> overrideRules = safeGet(() -> { LinkedHashMap<ResourceLocation, JsonCompare.CompareSettings> overrideRules = safeGet(() -> json
LinkedHashMap<ResourceLocation, JsonCompare.CompareSettings> overrides = new LinkedHashMap<>(); .getAsJsonObject(OVERRIDE_DUPLICATE_RULES)
json.getAsJsonObject(OVERRIDE_DUPLICATE_RULES).entrySet().forEach(entry -> { .entrySet()
overrides.put(new ResourceLocation(entry.getKey()), .stream()
createCompareSet(entry.getValue().getAsJsonObject())); .collect(Collectors.toMap(entry -> new ResourceLocation(entry.getKey()),
}); entry -> createCompareSet(entry.getValue().getAsJsonObject()),
return overrides; (a, b) -> b,
}, new LinkedHashMap<>()); LinkedHashMap::new)), new LinkedHashMap<>());
return new DuplicationConfig(defaultRules, overrideRules, ignoreRecipeTypes, ignoreRecipes); return new DuplicationConfig(defaultRules, overrideRules, ignoreRecipeTypes, ignoreRecipes);
} }
@ -87,16 +77,8 @@ public class DuplicationConfig extends Config {
public JsonObject serialize(DuplicationConfig config) { public JsonObject serialize(DuplicationConfig config) {
JsonObject json = new JsonObject(); JsonObject json = new JsonObject();
json.add(IGNORED_RECIPE_TYPES, serializeResourceLocations(json, IGNORED_RECIPE_TYPES, config.ignoreRecipeTypes);
JsonUtils.toArray(config.ignoreRecipeTypes serializeResourceLocations(json, IGNORED_RECIPES, config.ignoreRecipes);
.stream()
.map(ResourceLocation::toString)
.collect(Collectors.toList())));
json.add(IGNORED_RECIPES,
JsonUtils.toArray(config.ignoreRecipes
.stream()
.map(ResourceLocation::toString)
.collect(Collectors.toList())));
json.add(DEFAULT_DUPLICATE_RULES, config.defaultRules.serialize()); json.add(DEFAULT_DUPLICATE_RULES, config.defaultRules.serialize());
JsonObject overrides = new JsonObject(); JsonObject overrides = new JsonObject();
config.overrideRules.forEach((rl, compareSettings) -> { config.overrideRules.forEach((rl, compareSettings) -> {

View file

@ -13,7 +13,8 @@ import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class UnifyConfig extends Config { public class UnifyConfig extends Config {
public static String NAME = "unify"; public static final String NAME = "unify";
private final List<String> stoneStrata; private final List<String> stoneStrata;
private final List<String> materials; private final List<String> materials;
private final List<String> unbakedTags; private final List<String> unbakedTags;
@ -102,16 +103,8 @@ public class UnifyConfig extends Config {
.stream() .stream()
.map(s -> UnifyTag.item(new ResourceLocation(s))) .map(s -> UnifyTag.item(new ResourceLocation(s)))
.collect(Collectors.toSet()), new HashSet<>()); .collect(Collectors.toSet()), new HashSet<>());
Set<ResourceLocation> ignoredRecipeTypes = safeGet(() -> JsonUtils Set<ResourceLocation> ignoredRecipeTypes = deserializeResourceLocations(json, IGNORED_RECIPE_TYPES);
.toList(json.getAsJsonArray(IGNORED_RECIPE_TYPES)) Set<ResourceLocation> ignoredRecipes = deserializeResourceLocations(json, IGNORED_RECIPES);
.stream()
.map(ResourceLocation::new)
.collect(Collectors.toSet()), new HashSet<>());
Set<ResourceLocation> ignoredRecipes = safeGet(() -> JsonUtils
.toList(json.getAsJsonArray(IGNORED_RECIPES))
.stream()
.map(ResourceLocation::new)
.collect(Collectors.toSet()), new HashSet<>());
boolean hideJeiRei = safeGet(() -> json.getAsJsonPrimitive(HIDE_JEI_REI).getAsBoolean(), true); boolean hideJeiRei = safeGet(() -> json.getAsJsonPrimitive(HIDE_JEI_REI).getAsBoolean(), true);
return new UnifyConfig(stoneStrata, return new UnifyConfig(stoneStrata,
@ -137,16 +130,8 @@ public class UnifyConfig extends Config {
.map(UnifyTag::location) .map(UnifyTag::location)
.map(ResourceLocation::toString) .map(ResourceLocation::toString)
.toList())); .toList()));
json.add(IGNORED_RECIPE_TYPES, serializeResourceLocations(json, IGNORED_RECIPE_TYPES, config.ignoredRecipeTypes);
JsonUtils.toArray(config.ignoredRecipeTypes serializeResourceLocations(json, IGNORED_RECIPES, config.ignoredRecipes);
.stream()
.map(ResourceLocation::toString)
.collect(Collectors.toList())));
json.add(IGNORED_RECIPES,
JsonUtils.toArray(config.ignoredRecipes
.stream()
.map(ResourceLocation::toString)
.collect(Collectors.toList())));
json.add(HIDE_JEI_REI, new JsonPrimitive(config.hideJeiRei)); json.add(HIDE_JEI_REI, new JsonPrimitive(config.hideJeiRei));
return json; return json;
} }