mirror of
https://github.com/AlmostReliable/almostunified.git
synced 2024-11-14 11:15:23 -05:00
Merge 3dbc624aeb
into 09c6b9895c
This commit is contained in:
commit
0a83fbec10
4 changed files with 68 additions and 35 deletions
|
@ -60,20 +60,35 @@ public final class DuplicateConfig extends Config {
|
||||||
* @return True if the recipe type is ignored, false otherwise
|
* @return True if the recipe type is ignored, false otherwise
|
||||||
*/
|
*/
|
||||||
private boolean isRecipeTypeIgnored(RecipeLink recipe) {
|
private boolean isRecipeTypeIgnored(RecipeLink recipe) {
|
||||||
return ignoredRecipeTypesCache.computeIfAbsent(recipe.getType(), type -> {
|
ResourceLocation type = recipe.getType();
|
||||||
for (Pattern ignorePattern : ignoreRecipeTypes) {
|
Boolean ignored = ignoredRecipeTypesCache.get(type);
|
||||||
if (ignorePattern.matcher(type.toString()).matches()) {
|
if (ignored == null) {
|
||||||
return true;
|
ignored = computeIsRecipeTypeIgnored(recipe);
|
||||||
}
|
ignoredRecipeTypesCache.put(type, ignored);
|
||||||
|
}
|
||||||
|
return ignored;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean computeIsRecipeTypeIgnored(RecipeLink recipe) {
|
||||||
|
ResourceLocation type = recipe.getType();
|
||||||
|
String typeString = type.toString();
|
||||||
|
for (Pattern ignorePattern : ignoreRecipeTypes) {
|
||||||
|
if (ignorePattern.matcher(typeString).matches()) {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
}
|
||||||
});
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public JsonCompare.CompareSettings getCompareSettings(ResourceLocation type) {
|
public JsonCompare.CompareSettings getCompareSettings(ResourceLocation type) {
|
||||||
return overrideRules.getOrDefault(type, defaultRules);
|
return overrideRules.getOrDefault(type, defaultRules);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public JsonCompare.CompareContext getCompareContext(RecipeLink recipe) {
|
||||||
|
JsonCompare.CompareSettings compareSettings = getCompareSettings(recipe.getType());
|
||||||
|
return JsonCompare.CompareContext.create(compareSettings, recipe);
|
||||||
|
}
|
||||||
|
|
||||||
public boolean shouldCompareAll() {
|
public boolean shouldCompareAll() {
|
||||||
return compareAll;
|
return compareAll;
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,11 +12,18 @@ import com.google.gson.JsonObject;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public final class RecipeLink implements RecipeData {
|
public final class RecipeLink implements RecipeData {
|
||||||
|
/**
|
||||||
|
* This cache is an optimization to avoid creating many ResourceLocations for just a few different types.
|
||||||
|
* Having fewer ResourceLocation instances can greatly speed up equality checking when these are used as map keys.
|
||||||
|
*/
|
||||||
|
private static final Map<String, ResourceLocation> PARSED_TYPE_CACHE = new HashMap<>();
|
||||||
|
|
||||||
private final ResourceLocation id;
|
private final ResourceLocation id;
|
||||||
private final ResourceLocation type;
|
private final ResourceLocation type;
|
||||||
|
@ -43,7 +50,8 @@ public final class RecipeLink implements RecipeData {
|
||||||
|
|
||||||
public static RecipeLink ofOrThrow(ResourceLocation id, JsonObject originalRecipe) {
|
public static RecipeLink ofOrThrow(ResourceLocation id, JsonObject originalRecipe) {
|
||||||
try {
|
try {
|
||||||
ResourceLocation type = ResourceLocation.parse(originalRecipe.get("type").getAsString());
|
String typeString = originalRecipe.get("type").getAsString();
|
||||||
|
ResourceLocation type = PARSED_TYPE_CACHE.computeIfAbsent(typeString, ResourceLocation::parse);
|
||||||
return new RecipeLink(id, originalRecipe, type);
|
return new RecipeLink(id, originalRecipe, type);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new IllegalArgumentException("could not detect recipe type for recipe " + id);
|
throw new IllegalArgumentException("could not detect recipe type for recipe " + id);
|
||||||
|
@ -57,19 +65,19 @@ public final class RecipeLink implements RecipeData {
|
||||||
*
|
*
|
||||||
* @param first first recipe to compare
|
* @param first first recipe to compare
|
||||||
* @param second second recipe to compare
|
* @param second second recipe to compare
|
||||||
* @param compareSettings Settings to use for comparison.
|
* @param compareContext Settings and context to use for comparison.
|
||||||
* @return the recipe where rules are applied and the recipes are compared for equality, or null if the recipes are not equal
|
* @return the recipe where rules are applied and the recipes are compared for equality, or null if the recipes are not equal
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public static RecipeLink compare(RecipeLink first, RecipeLink second, JsonCompare.CompareSettings compareSettings) {
|
public static RecipeLink compare(RecipeLink first, RecipeLink second, JsonCompare.CompareContext compareContext) {
|
||||||
JsonObject selfActual = first.getActual();
|
JsonObject selfActual = first.getActual();
|
||||||
JsonObject toCompareActual = second.getActual();
|
JsonObject toCompareActual = second.getActual();
|
||||||
|
|
||||||
JsonObject compare = null;
|
JsonObject compare = null;
|
||||||
if (first.getType().toString().equals("minecraft:crafting_shaped")) {
|
if (first.getType().toString().equals("minecraft:crafting_shaped")) {
|
||||||
compare = JsonCompare.compareShaped(selfActual, toCompareActual, compareSettings);
|
compare = JsonCompare.compareShaped(selfActual, toCompareActual, compareContext);
|
||||||
} else if (JsonCompare.matches(selfActual, toCompareActual, compareSettings)) {
|
} else if (JsonCompare.matches(selfActual, toCompareActual, compareContext)) {
|
||||||
compare = JsonCompare.compare(compareSettings.getRules(), selfActual, toCompareActual);
|
compare = JsonCompare.compare(compareContext.settings().getRules(), selfActual, toCompareActual);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (compare == null) return null;
|
if (compare == null) return null;
|
||||||
|
@ -146,10 +154,10 @@ public final class RecipeLink implements RecipeData {
|
||||||
* the master from the link will be used. Otherwise, we will create a new link if needed.
|
* the master from the link will be used. Otherwise, we will create a new link if needed.
|
||||||
*
|
*
|
||||||
* @param otherRecipe Recipe data to check for duplicate against.
|
* @param otherRecipe Recipe data to check for duplicate against.
|
||||||
* @param compareSettings Settings to use for comparison.
|
* @param compareContext Settings and context to use for comparison.
|
||||||
* @return True if recipe is a duplicate, false otherwise.
|
* @return True if recipe is a duplicate, false otherwise.
|
||||||
*/
|
*/
|
||||||
public boolean handleDuplicate(RecipeLink otherRecipe, JsonCompare.CompareSettings compareSettings) {
|
public boolean handleDuplicate(RecipeLink otherRecipe, JsonCompare.CompareContext compareContext) {
|
||||||
DuplicateLink selfDuplicate = getDuplicateLink();
|
DuplicateLink selfDuplicate = getDuplicateLink();
|
||||||
DuplicateLink otherDuplicate = otherRecipe.getDuplicateLink();
|
DuplicateLink otherDuplicate = otherRecipe.getDuplicateLink();
|
||||||
|
|
||||||
|
@ -158,7 +166,7 @@ public final class RecipeLink implements RecipeData {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (selfDuplicate == null && otherDuplicate == null) {
|
if (selfDuplicate == null && otherDuplicate == null) {
|
||||||
RecipeLink compare = compare(this, otherRecipe, compareSettings);
|
RecipeLink compare = compare(this, otherRecipe, compareContext);
|
||||||
if (compare == null) {
|
if (compare == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -170,7 +178,7 @@ public final class RecipeLink implements RecipeData {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (otherDuplicate != null) {
|
if (otherDuplicate != null) {
|
||||||
RecipeLink compare = compare(this, otherDuplicate.getMaster(), compareSettings);
|
RecipeLink compare = compare(this, otherDuplicate.getMaster(), compareContext);
|
||||||
if (compare == null) {
|
if (compare == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -180,7 +188,7 @@ public final class RecipeLink implements RecipeData {
|
||||||
}
|
}
|
||||||
|
|
||||||
// selfDuplicate != null
|
// selfDuplicate != null
|
||||||
RecipeLink compare = compare(selfDuplicate.getMaster(), otherRecipe, compareSettings);
|
RecipeLink compare = compare(selfDuplicate.getMaster(), otherRecipe, compareContext);
|
||||||
if (compare == null) {
|
if (compare == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -141,7 +141,8 @@ public class RecipeTransformer {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonCompare.CompareSettings compareSettings = duplicateConfig.getCompareSettings(curRecipe.getType());
|
JsonCompare.CompareContext compareContext = duplicateConfig.getCompareContext(curRecipe);
|
||||||
|
|
||||||
boolean foundDuplicate = false;
|
boolean foundDuplicate = false;
|
||||||
for (RecipeLink recipeLink : recipes) {
|
for (RecipeLink recipeLink : recipes) {
|
||||||
if (!curRecipe.getType().equals(recipeLink.getType())) {
|
if (!curRecipe.getType().equals(recipeLink.getType())) {
|
||||||
|
@ -153,7 +154,7 @@ public class RecipeTransformer {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
foundDuplicate |= curRecipe.handleDuplicate(recipeLink, compareSettings);
|
foundDuplicate |= curRecipe.handleDuplicate(recipeLink, compareContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
return foundDuplicate;
|
return foundDuplicate;
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package com.almostreliable.unified.utils;
|
package com.almostreliable.unified.utils;
|
||||||
|
|
||||||
|
import com.almostreliable.unified.unification.recipe.RecipeLink;
|
||||||
|
|
||||||
import com.google.gson.JsonArray;
|
import com.google.gson.JsonArray;
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
@ -8,8 +10,6 @@ import com.google.gson.JsonPrimitive;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
|
@ -47,8 +47,8 @@ public final class JsonCompare {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public static JsonObject compareShaped(JsonObject first, JsonObject second, CompareSettings compareSettings) {
|
public static JsonObject compareShaped(JsonObject first, JsonObject second, CompareContext compareContext) {
|
||||||
if (!matches(first, second, compareSettings)) return null;
|
if (!matches(first, second, compareContext)) return null;
|
||||||
|
|
||||||
JsonArray firstPattern = JsonUtils.arrayOrSelf(first.get("pattern"));
|
JsonArray firstPattern = JsonUtils.arrayOrSelf(first.get("pattern"));
|
||||||
JsonArray secondPattern = JsonUtils.arrayOrSelf(second.get("pattern"));
|
JsonArray secondPattern = JsonUtils.arrayOrSelf(second.get("pattern"));
|
||||||
|
@ -97,20 +97,18 @@ public final class JsonCompare {
|
||||||
return keyMap;
|
return keyMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean matches(JsonObject first, JsonObject second, CompareSettings compareSettings) {
|
public static boolean matches(JsonObject first, JsonObject second, CompareContext compareContext) {
|
||||||
Collection<String> ignoredFields = compareSettings.getIgnoredFields();
|
CompareSettings compareSettings = compareContext.settings;
|
||||||
if (ignoredFields.isEmpty() && first.size() != second.size()) {
|
if (!compareSettings.hasIgnoredFields() && first.size() != second.size()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Map.Entry<String, JsonElement> firstEntry : first.entrySet()) {
|
for (String field : compareContext.compareFields()) {
|
||||||
if (ignoredFields.contains(firstEntry.getKey())) continue;
|
JsonElement secondElem = second.get(field);
|
||||||
|
|
||||||
JsonElement firstElem = firstEntry.getValue();
|
|
||||||
JsonElement secondElem = second.get(firstEntry.getKey());
|
|
||||||
|
|
||||||
if (secondElem == null) return false;
|
if (secondElem == null) return false;
|
||||||
|
|
||||||
|
JsonElement firstElem = first.get(field);
|
||||||
|
|
||||||
// sanitize elements for implicit counts of 1
|
// sanitize elements for implicit counts of 1
|
||||||
if (compareSettings.handleImplicitCounts && needsSanitizing(firstElem, secondElem)) {
|
if (compareSettings.handleImplicitCounts && needsSanitizing(firstElem, secondElem)) {
|
||||||
firstElem = sanitize(firstElem);
|
firstElem = sanitize(firstElem);
|
||||||
|
@ -270,6 +268,17 @@ public final class JsonCompare {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public record CompareContext(CompareSettings settings, List<String> compareFields) {
|
||||||
|
public static CompareContext create(CompareSettings settings, RecipeLink curRecipe) {
|
||||||
|
Set<String> compareFields = curRecipe.getActual().keySet();
|
||||||
|
if (!settings.ignoredFields.isEmpty()) {
|
||||||
|
compareFields = new HashSet<>(compareFields);
|
||||||
|
compareFields.removeAll(settings.ignoredFields);
|
||||||
|
}
|
||||||
|
return new CompareContext(settings, List.copyOf(compareFields));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static class CompareSettings {
|
public static class CompareSettings {
|
||||||
|
|
||||||
public static final String IGNORED_FIELDS = "ignored_fields";
|
public static final String IGNORED_FIELDS = "ignored_fields";
|
||||||
|
@ -292,8 +301,8 @@ public final class JsonCompare {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<String> getIgnoredFields() {
|
public boolean hasIgnoredFields() {
|
||||||
return Collections.unmodifiableSet(ignoredFields);
|
return !ignoredFields.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
public JsonObject serialize() {
|
public JsonObject serialize() {
|
||||||
|
|
Loading…
Reference in a new issue