mirror of
https://github.com/AlmostReliable/almostunified.git
synced 2024-11-24 16:48:00 -05:00
config cleanup and refactor
This commit is contained in:
parent
f534c61637
commit
ece7139660
5 changed files with 66 additions and 95 deletions
|
@ -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);
|
||||
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<T extends Config> {
|
||||
private boolean invalid = false;
|
||||
public abstract static class Serializer<T extends Config> {
|
||||
private boolean valid = true;
|
||||
|
||||
protected void setInvalid() {
|
||||
this.invalid = true;
|
||||
this.valid = false;
|
||||
}
|
||||
|
||||
public boolean isInvalid() {
|
||||
return invalid;
|
||||
return !valid;
|
||||
}
|
||||
|
||||
public <V> V safeGet(Supplier<V> supplier, V defaultValue) {
|
||||
|
@ -84,6 +88,22 @@ public class Config {
|
|||
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 JsonObject serialize(T src);
|
||||
|
|
|
@ -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
|
||||
.map(t -> StringUtils.rightPad(t.location().toString(), 40) + " => " + tagMap
|
||||
.getItems(t)
|
||||
.stream()
|
||||
.map(ResourceLocation::toString)
|
||||
.sorted()
|
||||
.collect(Collectors.joining(", ")))
|
||||
.append("\n"));
|
||||
.collect(Collectors.joining(", ")) + "\n")
|
||||
.collect(Collectors.joining()));
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -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<String> STONE_STRATA = List.of("stone",
|
||||
"nether",
|
||||
"deepslate",
|
||||
"granite",
|
||||
"diorite",
|
||||
"andesite");
|
||||
|
||||
@SuppressWarnings("SpellCheckingInspection")
|
||||
@SuppressWarnings("SpellCheckingInspection")
|
||||
public final class Defaults {
|
||||
public static final List<String> MOD_PRIORITIES = List.of(
|
||||
"minecraft",
|
||||
"kubejs",
|
||||
|
@ -23,7 +14,12 @@ public class Defaults {
|
|||
"thermal",
|
||||
"immersiveengineering",
|
||||
"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",
|
||||
"aluminum",
|
||||
"amber",
|
||||
|
@ -62,17 +58,8 @@ public class Defaults {
|
|||
"zinc"
|
||||
|
||||
);
|
||||
public static final List<String> TYPES = List.of(
|
||||
"nuggets",
|
||||
"dusts",
|
||||
"gears",
|
||||
"gems",
|
||||
"ingots",
|
||||
"raw_materials",
|
||||
"ores",
|
||||
"plates",
|
||||
"rods",
|
||||
"storage_blocks");
|
||||
|
||||
private Defaults() {}
|
||||
|
||||
public static List<String> getTags(Platform platform) {
|
||||
return switch (platform) {
|
||||
|
|
|
@ -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<ResourceLocation, JsonCompare.CompareSettings> overrideRules;
|
||||
private final Set<ResourceLocation> ignoreRecipeTypes;
|
||||
|
@ -42,28 +40,20 @@ public class DuplicationConfig extends Config {
|
|||
|
||||
@Override
|
||||
public DuplicationConfig deserialize(JsonObject json) {
|
||||
Set<ResourceLocation> ignoreRecipeTypes = safeGet(() -> JsonUtils
|
||||
.toList(json.getAsJsonArray(IGNORED_RECIPE_TYPES))
|
||||
.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<>());
|
||||
Set<ResourceLocation> ignoreRecipeTypes = deserializeResourceLocations(json, IGNORED_RECIPE_TYPES);
|
||||
Set<ResourceLocation> ignoreRecipes = deserializeResourceLocations(json, IGNORED_RECIPES);
|
||||
|
||||
JsonCompare.CompareSettings defaultRules = safeGet(() -> createCompareSet(json.getAsJsonObject(
|
||||
DEFAULT_DUPLICATE_RULES)),
|
||||
defaultSet());
|
||||
LinkedHashMap<ResourceLocation, JsonCompare.CompareSettings> overrideRules = safeGet(() -> {
|
||||
LinkedHashMap<ResourceLocation, JsonCompare.CompareSettings> 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<ResourceLocation, JsonCompare.CompareSettings> 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) -> {
|
||||
|
|
|
@ -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<String> stoneStrata;
|
||||
private final List<String> materials;
|
||||
private final List<String> unbakedTags;
|
||||
|
@ -102,16 +103,8 @@ public class UnifyConfig extends Config {
|
|||
.stream()
|
||||
.map(s -> UnifyTag.item(new ResourceLocation(s)))
|
||||
.collect(Collectors.toSet()), new HashSet<>());
|
||||
Set<ResourceLocation> ignoredRecipeTypes = safeGet(() -> JsonUtils
|
||||
.toList(json.getAsJsonArray(IGNORED_RECIPE_TYPES))
|
||||
.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<>());
|
||||
Set<ResourceLocation> ignoredRecipeTypes = deserializeResourceLocations(json, IGNORED_RECIPE_TYPES);
|
||||
Set<ResourceLocation> 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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue