From ece71396600206e6053f53143132108e18291657 Mon Sep 17 00:00:00 2001 From: Relentless Date: Tue, 23 Aug 2022 17:41:37 +0200 Subject: [PATCH] config cleanup and refactor --- .../almostreliable/unified/config/Config.java | 36 +++++++++++---- .../unified/config/DebugConfig.java | 21 ++++----- .../unified/config/Defaults.java | 33 +++++--------- .../unified/config/DuplicationConfig.java | 44 ++++++------------- .../unified/config/UnifyConfig.java | 27 +++--------- 5 files changed, 66 insertions(+), 95 deletions(-) diff --git a/Common/src/main/java/com/almostreliable/unified/config/Config.java b/Common/src/main/java/com/almostreliable/unified/config/Config.java index 8d4f925..7127646 100644 --- a/Common/src/main/java/com/almostreliable/unified/config/Config.java +++ b/Common/src/main/java/com/almostreliable/unified/config/Config.java @@ -2,16 +2,21 @@ package com.almostreliable.unified.config; import com.almostreliable.unified.AlmostUnified; import com.almostreliable.unified.AlmostUnifiedPlatform; +import com.almostreliable.unified.utils.JsonUtils; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; +import net.minecraft.resources.ResourceLocation; import java.io.BufferedReader; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; +import java.util.HashSet; +import java.util.Set; import java.util.function.Supplier; +import java.util.stream.Collectors; public class Config { @@ -41,10 +46,9 @@ public class Config { } private static JsonObject safeLoadJson(String file) { - try { - Path p = createConfigDir(); - BufferedReader bufferedReader = Files.newBufferedReader(buildPath(p, file)); - return new Gson().fromJson(bufferedReader, JsonObject.class); + Path p = createConfigDir(); + try (BufferedReader reader = Files.newBufferedReader(buildPath(p, file))) { + return new Gson().fromJson(reader, JsonObject.class); } catch (Exception ignored) { } return new JsonObject(); @@ -64,15 +68,15 @@ public class Config { return p.resolve(name + ".json"); } - public static abstract class Serializer { - private boolean invalid = false; + public abstract static class Serializer { + private boolean valid = true; protected void setInvalid() { - this.invalid = true; + this.valid = false; } public boolean isInvalid() { - return invalid; + return !valid; } public V safeGet(Supplier supplier, V defaultValue) { @@ -84,6 +88,22 @@ public class Config { return defaultValue; } + protected Set 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 resourceLocations) { + json.add(configKey, + JsonUtils.toArray(resourceLocations + .stream() + .map(ResourceLocation::toString) + .toList())); + } + public abstract T deserialize(JsonObject json); public abstract JsonObject serialize(T src); diff --git a/Common/src/main/java/com/almostreliable/unified/config/DebugConfig.java b/Common/src/main/java/com/almostreliable/unified/config/DebugConfig.java index 55a437e..915e5ca 100644 --- a/Common/src/main/java/com/almostreliable/unified/config/DebugConfig.java +++ b/Common/src/main/java/com/almostreliable/unified/config/DebugConfig.java @@ -13,7 +13,7 @@ import java.util.Map; import java.util.stream.Collectors; public class DebugConfig extends Config { - public static String NAME = "debug"; + public static final String NAME = "debug"; public final boolean dumpTagMap; public final boolean dumpDuplicates; @@ -35,20 +35,17 @@ public class DebugConfig extends Config { } FileUtils.write(AlmostUnifiedPlatform.INSTANCE.getLogPath(), "unify_tag_dump.txt", sb -> { - tagMap + sb.append(tagMap .getTags() .stream() .sorted(Comparator.comparing(t -> t.location().toString())) - .forEach(t -> sb - .append(StringUtils.rightPad(t.location().toString(), 40)) - .append(" => ") - .append(tagMap - .getItems(t) - .stream() - .map(ResourceLocation::toString) - .sorted() - .collect(Collectors.joining(", "))) - .append("\n")); + .map(t -> StringUtils.rightPad(t.location().toString(), 40) + " => " + tagMap + .getItems(t) + .stream() + .map(ResourceLocation::toString) + .sorted() + .collect(Collectors.joining(", ")) + "\n") + .collect(Collectors.joining())); }); } diff --git a/Common/src/main/java/com/almostreliable/unified/config/Defaults.java b/Common/src/main/java/com/almostreliable/unified/config/Defaults.java index 0edfa1d..2d1f85b 100644 --- a/Common/src/main/java/com/almostreliable/unified/config/Defaults.java +++ b/Common/src/main/java/com/almostreliable/unified/config/Defaults.java @@ -1,20 +1,11 @@ package com.almostreliable.unified.config; -import com.almostreliable.unified.AlmostUnifiedPlatform; import com.almostreliable.unified.Platform; import java.util.List; - -public class Defaults { - public static final List STONE_STRATA = List.of("stone", - "nether", - "deepslate", - "granite", - "diorite", - "andesite"); - - @SuppressWarnings("SpellCheckingInspection") +@SuppressWarnings("SpellCheckingInspection") +public final class Defaults { public static final List MOD_PRIORITIES = List.of( "minecraft", "kubejs", @@ -23,7 +14,12 @@ public class Defaults { "thermal", "immersiveengineering", "mekanism"); - @SuppressWarnings("SpellCheckingInspection") + public static final List STONE_STRATA = List.of("stone", + "nether", + "deepslate", + "granite", + "diorite", + "andesite"); public static final List MATERIALS = List.of("aeternium", "aluminum", "amber", @@ -62,17 +58,8 @@ public class Defaults { "zinc" ); - public static final List TYPES = List.of( - "nuggets", - "dusts", - "gears", - "gems", - "ingots", - "raw_materials", - "ores", - "plates", - "rods", - "storage_blocks"); + + private Defaults() {} public static List getTags(Platform platform) { return switch (platform) { diff --git a/Common/src/main/java/com/almostreliable/unified/config/DuplicationConfig.java b/Common/src/main/java/com/almostreliable/unified/config/DuplicationConfig.java index 0ebd6dd..eb0210e 100644 --- a/Common/src/main/java/com/almostreliable/unified/config/DuplicationConfig.java +++ b/Common/src/main/java/com/almostreliable/unified/config/DuplicationConfig.java @@ -1,19 +1,17 @@ package com.almostreliable.unified.config; -import com.almostreliable.unified.BuildConfig; import com.almostreliable.unified.recipe.RecipeLink; import com.almostreliable.unified.utils.JsonCompare; -import com.almostreliable.unified.utils.JsonUtils; import com.google.gson.JsonObject; import net.minecraft.resources.ResourceLocation; -import java.util.HashSet; import java.util.LinkedHashMap; import java.util.Set; import java.util.stream.Collectors; public class DuplicationConfig extends Config { public static final String NAME = "duplicates"; + private final JsonCompare.CompareSettings defaultRules; private final LinkedHashMap overrideRules; private final Set ignoreRecipeTypes; @@ -42,28 +40,20 @@ public class DuplicationConfig extends Config { @Override public DuplicationConfig deserialize(JsonObject json) { - Set ignoreRecipeTypes = safeGet(() -> JsonUtils - .toList(json.getAsJsonArray(IGNORED_RECIPE_TYPES)) - .stream() - .map(ResourceLocation::new) - .collect(Collectors.toSet()), new HashSet<>()); - Set ignoreRecipes = safeGet(() -> JsonUtils - .toList(json.getAsJsonArray(IGNORED_RECIPES)) - .stream() - .map(ResourceLocation::new) - .collect(Collectors.toSet()), new HashSet<>()); + Set ignoreRecipeTypes = deserializeResourceLocations(json, IGNORED_RECIPE_TYPES); + Set ignoreRecipes = deserializeResourceLocations(json, IGNORED_RECIPES); JsonCompare.CompareSettings defaultRules = safeGet(() -> createCompareSet(json.getAsJsonObject( DEFAULT_DUPLICATE_RULES)), defaultSet()); - LinkedHashMap overrideRules = safeGet(() -> { - LinkedHashMap overrides = new LinkedHashMap<>(); - json.getAsJsonObject(OVERRIDE_DUPLICATE_RULES).entrySet().forEach(entry -> { - overrides.put(new ResourceLocation(entry.getKey()), - createCompareSet(entry.getValue().getAsJsonObject())); - }); - return overrides; - }, new LinkedHashMap<>()); + LinkedHashMap overrideRules = safeGet(() -> json + .getAsJsonObject(OVERRIDE_DUPLICATE_RULES) + .entrySet() + .stream() + .collect(Collectors.toMap(entry -> new ResourceLocation(entry.getKey()), + entry -> createCompareSet(entry.getValue().getAsJsonObject()), + (a, b) -> b, + LinkedHashMap::new)), new LinkedHashMap<>()); return new DuplicationConfig(defaultRules, overrideRules, ignoreRecipeTypes, ignoreRecipes); } @@ -87,16 +77,8 @@ public class DuplicationConfig extends Config { public JsonObject serialize(DuplicationConfig config) { JsonObject json = new JsonObject(); - json.add(IGNORED_RECIPE_TYPES, - JsonUtils.toArray(config.ignoreRecipeTypes - .stream() - .map(ResourceLocation::toString) - .collect(Collectors.toList()))); - json.add(IGNORED_RECIPES, - JsonUtils.toArray(config.ignoreRecipes - .stream() - .map(ResourceLocation::toString) - .collect(Collectors.toList()))); + serializeResourceLocations(json, IGNORED_RECIPE_TYPES, config.ignoreRecipeTypes); + serializeResourceLocations(json, IGNORED_RECIPES, config.ignoreRecipes); json.add(DEFAULT_DUPLICATE_RULES, config.defaultRules.serialize()); JsonObject overrides = new JsonObject(); config.overrideRules.forEach((rl, compareSettings) -> { diff --git a/Common/src/main/java/com/almostreliable/unified/config/UnifyConfig.java b/Common/src/main/java/com/almostreliable/unified/config/UnifyConfig.java index b7330db..ce4457e 100644 --- a/Common/src/main/java/com/almostreliable/unified/config/UnifyConfig.java +++ b/Common/src/main/java/com/almostreliable/unified/config/UnifyConfig.java @@ -13,7 +13,8 @@ import java.util.*; import java.util.stream.Collectors; public class UnifyConfig extends Config { - public static String NAME = "unify"; + public static final String NAME = "unify"; + private final List stoneStrata; private final List materials; private final List unbakedTags; @@ -102,16 +103,8 @@ public class UnifyConfig extends Config { .stream() .map(s -> UnifyTag.item(new ResourceLocation(s))) .collect(Collectors.toSet()), new HashSet<>()); - Set ignoredRecipeTypes = safeGet(() -> JsonUtils - .toList(json.getAsJsonArray(IGNORED_RECIPE_TYPES)) - .stream() - .map(ResourceLocation::new) - .collect(Collectors.toSet()), new HashSet<>()); - Set ignoredRecipes = safeGet(() -> JsonUtils - .toList(json.getAsJsonArray(IGNORED_RECIPES)) - .stream() - .map(ResourceLocation::new) - .collect(Collectors.toSet()), new HashSet<>()); + Set ignoredRecipeTypes = deserializeResourceLocations(json, IGNORED_RECIPE_TYPES); + Set ignoredRecipes = deserializeResourceLocations(json, IGNORED_RECIPES); boolean hideJeiRei = safeGet(() -> json.getAsJsonPrimitive(HIDE_JEI_REI).getAsBoolean(), true); return new UnifyConfig(stoneStrata, @@ -137,16 +130,8 @@ public class UnifyConfig extends Config { .map(UnifyTag::location) .map(ResourceLocation::toString) .toList())); - json.add(IGNORED_RECIPE_TYPES, - JsonUtils.toArray(config.ignoredRecipeTypes - .stream() - .map(ResourceLocation::toString) - .collect(Collectors.toList()))); - json.add(IGNORED_RECIPES, - JsonUtils.toArray(config.ignoredRecipes - .stream() - .map(ResourceLocation::toString) - .collect(Collectors.toList()))); + serializeResourceLocations(json, IGNORED_RECIPE_TYPES, config.ignoredRecipeTypes); + serializeResourceLocations(json, IGNORED_RECIPES, config.ignoredRecipes); json.add(HIDE_JEI_REI, new JsonPrimitive(config.hideJeiRei)); return json; }