diff --git a/Common/src/main/java/com/almostreliable/unified/AlmostUnifiedRuntimeImpl.java b/Common/src/main/java/com/almostreliable/unified/AlmostUnifiedRuntimeImpl.java index 02ec99b..7ca8e3d 100644 --- a/Common/src/main/java/com/almostreliable/unified/AlmostUnifiedRuntimeImpl.java +++ b/Common/src/main/java/com/almostreliable/unified/AlmostUnifiedRuntimeImpl.java @@ -7,6 +7,7 @@ import com.almostreliable.unified.config.UnifyConfig; import com.almostreliable.unified.recipe.RecipeDumper; import com.almostreliable.unified.recipe.RecipeTransformer; import com.almostreliable.unified.recipe.unifier.RecipeHandlerFactory; +import com.almostreliable.unified.utils.MissingRecipesDumper; import com.almostreliable.unified.utils.ReplacementMap; import com.almostreliable.unified.utils.TagMap; import com.google.gson.JsonElement; @@ -55,6 +56,7 @@ public final class AlmostUnifiedRuntimeImpl implements AlmostUnifiedRuntime { dumper.dump(debugConfig.dumpOverview, debugConfig.dumpUnification, debugConfig.dumpDuplicates); debugConfig.logRecipes(recipes, "recipes_after_unification.txt"); + MissingRecipesDumper.write(this, debugConfig.dumpPotentialMissingRecipes, recipes); } @Override 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 4e2e6e7..82b275e 100644 --- a/Common/src/main/java/com/almostreliable/unified/config/DebugConfig.java +++ b/Common/src/main/java/com/almostreliable/unified/config/DebugConfig.java @@ -21,13 +21,15 @@ public class DebugConfig extends Config { public final boolean dumpUnification; public final boolean dumpOverview; public final boolean dumpRecipes; + public final boolean dumpPotentialMissingRecipes; - public DebugConfig(boolean dumpTagMap, boolean dumpDuplicates, boolean dumpUnification, boolean dumpOverview, boolean dumpRecipes) { + public DebugConfig(boolean dumpTagMap, boolean dumpDuplicates, boolean dumpUnification, boolean dumpOverview, boolean dumpRecipes, boolean dumpPotentialMissingRecipes) { this.dumpTagMap = dumpTagMap; this.dumpDuplicates = dumpDuplicates; this.dumpUnification = dumpUnification; this.dumpOverview = dumpOverview; this.dumpRecipes = dumpRecipes; + this.dumpPotentialMissingRecipes = dumpPotentialMissingRecipes; } public void logUnifyTagDump(TagMap tagMap) { @@ -71,6 +73,7 @@ public class DebugConfig extends Config { public static final String DUMP_UNIFICATION = "dumpUnification"; public static final String DUMP_OVERVIEW = "dumpOverview"; public static final String DUMP_RECIPES = "dumpRecipes"; + public static final String DUMP_POTENTIAL_MISSING_RECIPES = "dumpPotentialMissingRecipes"; @Override public DebugConfig deserialize(JsonObject json) { @@ -79,7 +82,8 @@ public class DebugConfig extends Config { safeGet(() -> json.get(DUMP_DUPLICATES).getAsBoolean(), false), safeGet(() -> json.get(DUMP_UNIFICATION).getAsBoolean(), false), safeGet(() -> json.get(DUMP_OVERVIEW).getAsBoolean(), false), - safeGet(() -> json.get(DUMP_RECIPES).getAsBoolean(), false) + safeGet(() -> json.get(DUMP_RECIPES).getAsBoolean(), false), + safeGet(() -> json.get(DUMP_POTENTIAL_MISSING_RECIPES).getAsBoolean(), false) ); } @@ -91,6 +95,7 @@ public class DebugConfig extends Config { json.addProperty(DUMP_UNIFICATION, src.dumpUnification); json.addProperty(DUMP_OVERVIEW, src.dumpOverview); json.addProperty(DUMP_RECIPES, src.dumpRecipes); + json.addProperty(DUMP_POTENTIAL_MISSING_RECIPES, src.dumpPotentialMissingRecipes); return json; } } diff --git a/Common/src/main/java/com/almostreliable/unified/utils/MissingRecipesDumper.java b/Common/src/main/java/com/almostreliable/unified/utils/MissingRecipesDumper.java new file mode 100644 index 0000000..1b19892 --- /dev/null +++ b/Common/src/main/java/com/almostreliable/unified/utils/MissingRecipesDumper.java @@ -0,0 +1,95 @@ +package com.almostreliable.unified.utils; + +import com.almostreliable.unified.AlmostUnifiedPlatform; +import com.almostreliable.unified.AlmostUnifiedRuntime; +import com.google.common.collect.HashMultimap; +import com.google.common.collect.Multimap; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.Item; + +import java.util.HashSet; +import java.util.Map; +import java.util.Optional; +import java.util.Set; + +public class MissingRecipesDumper { + + public static void write(AlmostUnifiedRuntime runtime, boolean dumpPotentialMissingRecipes, Map recipes) { + if (!dumpPotentialMissingRecipes) return; + + var itemsPerTag = getHidingIds(runtime); + FileUtils.write(AlmostUnifiedPlatform.INSTANCE.getLogPath(), "recipe_hiding_check.txt", sb -> { + recipes.forEach(((recipe, json) -> { + if (json instanceof JsonObject obj) { + write(recipe, obj, itemsPerTag, sb); + } + })); + }); + } + + private static void write(ResourceLocation recipe, JsonObject json, Multimap, ResourceLocation> itemsPerTag, StringBuilder sb) { + String jsonStr = json.toString(); + Multimap, ResourceLocation> found = HashMultimap.create(); + itemsPerTag.asMap().forEach((tag, items) -> { + for (ResourceLocation item : items) { + if (jsonStr.contains(item.toString())) { + found.put(tag, item); + } + } + }); + + if (found.isEmpty()) return; + + String type = Optional.ofNullable(json.get("type")).map(JsonElement::getAsString).orElse(""); + sb + .append("Recipe ") + .append(recipe) + .append(" (") + .append(type) + .append(") contains potentially hiding items:") + .append("\n") + .append("Json: ") + .append(jsonStr) + .append("\n") + .append("Items: ") + .append("\n"); + found.asMap().forEach((tag, items) -> { + for (ResourceLocation item : items) { + sb.append("\t").append(item).append(" (#").append(tag.location()).append(")").append("\n"); + } + }); + + sb.append("\n"); + } + + + public static Multimap, ResourceLocation> getHidingIds(AlmostUnifiedRuntime runtime) { + ReplacementMap repMap = runtime.getReplacementMap().orElse(null); + var tagMap = runtime.getFilteredTagMap().orElse(null); + + Multimap, ResourceLocation> hidings = HashMultimap.create(); + if (repMap == null || tagMap == null) return hidings; + + for (var unifyTag : tagMap.getTags()) { + var itemsByTag = tagMap.getEntriesByTag(unifyTag); + + if (Utils.allSameNamespace(itemsByTag)) continue; + + ResourceLocation kingItem = repMap.getPreferredItemForTag(unifyTag, $ -> true); + if (kingItem == null) continue; + + Set r = new HashSet<>(); + for (ResourceLocation item : itemsByTag) { + if (!item.equals(kingItem)) { + r.add(item); + } + } + + hidings.putAll(unifyTag, r); + } + + return hidings; + } +}