regular expression support for config ignore lists

This commit is contained in:
Relentless 2022-09-16 20:48:49 +02:00
parent 75ba0e72db
commit f730fa0929
No known key found for this signature in database
GPG key ID: 759D97B8C6F25265
4 changed files with 39 additions and 24 deletions

View file

@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog], The format is based on [Keep a Changelog],
and this project adheres to [Semantic Versioning]. and this project adheres to [Semantic Versioning].
## Unreleased
### Added
- more materials and another tag to defaults
- back up your `unify.json` config and let it regenerate to get the new default or add it yourself
### Changed
- ignore lists in `unify.json` and `duplicates.json` now support regular expressions
### Fixed
- a compat issue on Fabric when REI is present
## [0.0.6] - 2022-09-02 ## [0.0.6] - 2022-09-02
### Added ### Added

View file

@ -6,7 +6,6 @@ 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;
@ -17,6 +16,7 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class Config { public class Config {
@ -89,20 +89,20 @@ public class Config {
return defaultValue; return defaultValue;
} }
protected Set<ResourceLocation> deserializeResourceLocations(JsonObject json, String configKey, List<String> defaultValue) { protected Set<Pattern> deserializePatterns(JsonObject json, String configKey, List<String> defaultValue) {
return safeGet(() -> JsonUtils return safeGet(() -> JsonUtils
.toList(json.getAsJsonArray(configKey)) .toList(json.getAsJsonArray(configKey))
.stream() .stream()
.map(ResourceLocation::new) .map(Pattern::compile)
.collect(Collectors.toSet()), .collect(Collectors.toSet()),
new HashSet<>(defaultValue.stream().map(ResourceLocation::new).toList())); new HashSet<>(defaultValue.stream().map(Pattern::compile).toList()));
} }
protected void serializeResourceLocations(JsonObject json, String configKey, Set<ResourceLocation> resourceLocations) { protected void serializePatterns(JsonObject json, String configKey, Set<Pattern> patterns) {
json.add(configKey, json.add(configKey,
JsonUtils.toArray(resourceLocations JsonUtils.toArray(patterns
.stream() .stream()
.map(ResourceLocation::toString) .map(Pattern::pattern)
.toList())); .toList()));
} }

View file

@ -8,6 +8,7 @@ import net.minecraft.resources.ResourceLocation;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class DuplicationConfig extends Config { public class DuplicationConfig extends Config {
@ -15,11 +16,11 @@ public class DuplicationConfig extends Config {
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<Pattern> ignoreRecipeTypes;
private final Set<ResourceLocation> ignoreRecipes; private final Set<Pattern> ignoreRecipes;
private final boolean strictMode; private final boolean strictMode;
public DuplicationConfig(JsonCompare.CompareSettings defaultRules, LinkedHashMap<ResourceLocation, JsonCompare.CompareSettings> overrideRules, Set<ResourceLocation> ignoreRecipeTypes, Set<ResourceLocation> ignoreRecipes, boolean strictMode) { public DuplicationConfig(JsonCompare.CompareSettings defaultRules, LinkedHashMap<ResourceLocation, JsonCompare.CompareSettings> overrideRules, Set<Pattern> ignoreRecipeTypes, Set<Pattern> ignoreRecipes, boolean strictMode) {
this.defaultRules = defaultRules; this.defaultRules = defaultRules;
this.overrideRules = overrideRules; this.overrideRules = overrideRules;
this.ignoreRecipeTypes = ignoreRecipeTypes; this.ignoreRecipeTypes = ignoreRecipeTypes;
@ -28,7 +29,8 @@ public class DuplicationConfig extends Config {
} }
public boolean shouldIgnoreRecipe(RecipeLink recipe) { public boolean shouldIgnoreRecipe(RecipeLink recipe) {
return ignoreRecipeTypes.contains(recipe.getType()) || ignoreRecipes.contains(recipe.getId()); return ignoreRecipeTypes.stream().anyMatch(pattern -> pattern.matcher(recipe.getType().toString()).matches()) ||
ignoreRecipes.stream().anyMatch(pattern -> pattern.matcher(recipe.getId().toString()).matches());
} }
public JsonCompare.CompareSettings getCompareSettings(ResourceLocation type) { public JsonCompare.CompareSettings getCompareSettings(ResourceLocation type) {
@ -48,10 +50,10 @@ public class DuplicationConfig extends Config {
@Override @Override
public DuplicationConfig deserialize(JsonObject json) { public DuplicationConfig deserialize(JsonObject json) {
Set<ResourceLocation> ignoreRecipeTypes = deserializeResourceLocations(json, Set<Pattern> ignoreRecipeTypes = deserializePatterns(json,
IGNORED_RECIPE_TYPES, IGNORED_RECIPE_TYPES,
Defaults.IGNORED_RECIPE_TYPES); Defaults.IGNORED_RECIPE_TYPES);
Set<ResourceLocation> ignoreRecipes = deserializeResourceLocations(json, IGNORED_RECIPES, List.of()); Set<Pattern> ignoreRecipes = deserializePatterns(json, IGNORED_RECIPES, List.of());
JsonCompare.CompareSettings defaultRules = safeGet(() -> createCompareSet(json.getAsJsonObject( JsonCompare.CompareSettings defaultRules = safeGet(() -> createCompareSet(json.getAsJsonObject(
DEFAULT_DUPLICATE_RULES)), DEFAULT_DUPLICATE_RULES)),
@ -100,8 +102,8 @@ public class DuplicationConfig extends Config {
public JsonObject serialize(DuplicationConfig config) { public JsonObject serialize(DuplicationConfig config) {
JsonObject json = new JsonObject(); JsonObject json = new JsonObject();
serializeResourceLocations(json, IGNORED_RECIPE_TYPES, config.ignoreRecipeTypes); serializePatterns(json, IGNORED_RECIPE_TYPES, config.ignoreRecipeTypes);
serializeResourceLocations(json, IGNORED_RECIPES, config.ignoreRecipes); serializePatterns(json, IGNORED_RECIPES, config.ignoreRecipes);
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

@ -10,6 +10,7 @@ import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item; import net.minecraft.world.item.Item;
import java.util.*; import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class UnifyConfig extends Config { public class UnifyConfig extends Config {
@ -20,11 +21,11 @@ public class UnifyConfig extends Config {
private final List<String> unbakedTags; private final List<String> unbakedTags;
private final List<String> modPriorities; private final List<String> modPriorities;
private final Set<UnifyTag<Item>> ignoredTags; private final Set<UnifyTag<Item>> ignoredTags;
private final Set<ResourceLocation> ignoredRecipeTypes; private final Set<Pattern> ignoredRecipeTypes;
private final Set<ResourceLocation> ignoredRecipes; private final Set<Pattern> ignoredRecipes;
private final boolean hideJeiRei; private final boolean hideJeiRei;
public UnifyConfig(List<String> stoneStrata, List<String> materials, List<String> unbakedTags, List<String> modPriorities, Set<UnifyTag<Item>> ignoredTags, Set<ResourceLocation> ignoredRecipeTypes, Set<ResourceLocation> ignoredRecipes, boolean hideJeiRei) { public UnifyConfig(List<String> stoneStrata, List<String> materials, List<String> unbakedTags, List<String> modPriorities, Set<UnifyTag<Item>> ignoredTags, Set<Pattern> ignoredRecipeTypes, Set<Pattern> ignoredRecipes, boolean hideJeiRei) {
this.stoneStrata = stoneStrata; this.stoneStrata = stoneStrata;
this.materials = materials; this.materials = materials;
this.unbakedTags = unbakedTags; this.unbakedTags = unbakedTags;
@ -44,11 +45,11 @@ public class UnifyConfig extends Config {
} }
public boolean includeRecipe(ResourceLocation recipe) { public boolean includeRecipe(ResourceLocation recipe) {
return !ignoredRecipes.contains(recipe); return ignoredRecipes.stream().noneMatch(pattern -> pattern.matcher(recipe.toString()).matches());
} }
public boolean includeRecipeType(ResourceLocation type) { public boolean includeRecipeType(ResourceLocation type) {
return !ignoredRecipeTypes.contains(type); return ignoredRecipeTypes.stream().noneMatch(pattern -> pattern.matcher(type.toString()).matches());
} }
public List<UnifyTag<Item>> bakeTags() { public List<UnifyTag<Item>> bakeTags() {
@ -102,10 +103,10 @@ 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 = deserializeResourceLocations(json, Set<Pattern> ignoredRecipeTypes = deserializePatterns(json,
IGNORED_RECIPE_TYPES, IGNORED_RECIPE_TYPES,
Defaults.IGNORED_RECIPE_TYPES); Defaults.IGNORED_RECIPE_TYPES);
Set<ResourceLocation> ignoredRecipes = deserializeResourceLocations(json, IGNORED_RECIPES, List.of()); Set<Pattern> ignoredRecipes = deserializePatterns(json, IGNORED_RECIPES, List.of());
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,
@ -131,8 +132,8 @@ public class UnifyConfig extends Config {
.map(UnifyTag::location) .map(UnifyTag::location)
.map(ResourceLocation::toString) .map(ResourceLocation::toString)
.toList())); .toList()));
serializeResourceLocations(json, IGNORED_RECIPE_TYPES, config.ignoredRecipeTypes); serializePatterns(json, IGNORED_RECIPE_TYPES, config.ignoredRecipeTypes);
serializeResourceLocations(json, IGNORED_RECIPES, config.ignoredRecipes); serializePatterns(json, IGNORED_RECIPES, config.ignoredRecipes);
json.add(HIDE_JEI_REI, new JsonPrimitive(config.hideJeiRei)); json.add(HIDE_JEI_REI, new JsonPrimitive(config.hideJeiRei));
return json; return json;
} }