Skip to content

Commit

Permalink
Add MissingRecipesDumper
Browse files Browse the repository at this point in the history
  • Loading branch information
LLytho committed Mar 13, 2024
1 parent ddc29a2 commit 2047276
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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;
Expand Down Expand Up @@ -55,6 +56,7 @@ public void run(Map<ResourceLocation, JsonElement> recipes, boolean skipClientTr
dumper.dump(debugConfig.dumpOverview, debugConfig.dumpUnification, debugConfig.dumpDuplicates);

debugConfig.logRecipes(recipes, "recipes_after_unification.txt");
MissingRecipesDumper.write(this, debugConfig.dumpPotentialMissingRecipes, recipes);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item> tagMap) {
Expand Down Expand Up @@ -71,6 +73,7 @@ public static class Serializer extends Config.Serializer<DebugConfig> {
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) {
Expand All @@ -79,7 +82,8 @@ public DebugConfig deserialize(JsonObject json) {
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)
);
}

Expand All @@ -91,6 +95,7 @@ public JsonObject serialize(DebugConfig src) {
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;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ResourceLocation, JsonElement> 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<UnifyTag<Item>, ResourceLocation> itemsPerTag, StringBuilder sb) {
String jsonStr = json.toString();
Multimap<UnifyTag<Item>, 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<UnifyTag<Item>, ResourceLocation> getHidingIds(AlmostUnifiedRuntime runtime) {
ReplacementMap repMap = runtime.getReplacementMap().orElse(null);
var tagMap = runtime.getFilteredTagMap().orElse(null);

Multimap<UnifyTag<Item>, 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<ResourceLocation> r = new HashSet<>();
for (ResourceLocation item : itemsByTag) {
if (!item.equals(kingItem)) {
r.add(item);
}
}

hidings.putAll(unifyTag, r);
}

return hidings;
}
}

0 comments on commit 2047276

Please sign in to comment.