Skip to content

Commit

Permalink
add error logging for unified failing recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
rlnt committed Aug 26, 2024
1 parent 3d2673d commit a4036db
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.almostreliable.unified.core.AlmostUnifiedRuntimeImpl;
import com.almostreliable.unified.unification.loot.LootUnification;
import com.almostreliable.unified.utils.CustomLogger;
import com.almostreliable.unified.utils.RecipeErrorHandler;
import com.almostreliable.unified.utils.VanillaTagWrapper;

import com.google.common.base.Preconditions;
Expand Down Expand Up @@ -39,9 +40,18 @@ public static void onTagLoaderReload(VanillaTagWrapper<Item> itemTags, VanillaTa
}

public static void onRecipeManagerReload(Map<ResourceLocation, JsonElement> recipes, HolderLookup.Provider registries) {
Preconditions.checkNotNull(RUNTIME, "AlmostUnifiedRuntime was not loaded correctly");
Preconditions.checkNotNull(RUNTIME, "runtime was not loaded correctly");

RUNTIME.run(recipes);
LootUnification.unifyLoot(RUNTIME, registries);
}

public static void onRecipeManagerError(ResourceLocation recipe) {
assert RUNTIME != null;
RecipeErrorHandler.collect(RUNTIME.getRecipeTransformerResult(), recipe);
}

public static void onRecipeManagerEnd() {
RecipeErrorHandler.finish();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ public final class AlmostUnifiedRuntimeImpl implements AlmostUnifiedRuntime {
private final RecipeUnifierRegistry recipeUnifierRegistry;
private final TagSubstitutions tagSubstitutions;
private final Placeholders placeholders;
private final DebugConfig debugConfig;
private final UnificationLookup compositeUnificationLookup;
private final DebugHandler debugHandler;

private AlmostUnifiedRuntimeImpl(Collection<? extends UnificationSettings> unificationSettings, RecipeUnifierRegistry recipeUnifierRegistry, TagSubstitutions tagSubstitutions, Placeholders placeholders, DebugConfig debugConfig) {
this.unificationSettings = unificationSettings;
this.recipeUnifierRegistry = recipeUnifierRegistry;
this.tagSubstitutions = tagSubstitutions;
this.placeholders = placeholders;
this.debugConfig = debugConfig;
this.compositeUnificationLookup = new CompositeUnificationLookup(unificationSettings);
this.debugHandler = new DebugHandler(debugConfig);
}

public static AlmostUnifiedRuntimeImpl create(VanillaTagWrapper<Item> itemTags, VanillaTagWrapper<Block> blockTags) {
Expand Down Expand Up @@ -188,7 +188,7 @@ private static List<UnificationSettings> createUnificationLookups(VanillaTagWrap
}

public void run(Map<ResourceLocation, JsonElement> recipes) {
DebugHandler debugHandler = DebugHandler.onRunStart(recipes, compositeUnificationLookup, debugConfig);
debugHandler.onRunStart(recipes, compositeUnificationLookup);

debugHandler.measure(() -> {
var transformer = new RecipeTransformer(recipeUnifierRegistry, unificationSettings);
Expand Down Expand Up @@ -230,6 +230,10 @@ public Placeholders getPlaceholders() {
return placeholders;
}

public RecipeTransformer.Result getRecipeTransformerResult() {
return debugHandler.getRecipeTransformerResult();
}

private static final class CompositeUnificationLookup implements UnificationLookup {

private final Iterable<? extends UnificationLookup> unificationLookups;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.almostreliable.unified.AlmostUnifiedCommon;

import com.google.gson.JsonElement;
import com.llamalad7.mixinextras.sugar.Local;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -26,10 +27,16 @@ public class RecipeManagerMixin {

@Inject(method = "apply(Ljava/util/Map;Lnet/minecraft/server/packs/resources/ResourceManager;Lnet/minecraft/util/profiling/ProfilerFiller;)V", at = @At("HEAD"))
private void almostunified$onRecipeReload(Map<ResourceLocation, JsonElement> recipes, ResourceManager resourceManager, ProfilerFiller profiler, CallbackInfo ci) {
try {
AlmostUnifiedCommon.onRecipeManagerReload(recipes, registries);
} catch (Exception e) {
AlmostUnifiedCommon.LOGGER.error(e.getMessage(), e);
}
AlmostUnifiedCommon.onRecipeManagerReload(recipes, registries);
}

@Inject(method = "apply(Ljava/util/Map;Lnet/minecraft/server/packs/resources/ResourceManager;Lnet/minecraft/util/profiling/ProfilerFiller;)V", at = @At(value = "INVOKE", target = "Lorg/slf4j/Logger;error(Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;)V"), remap = false)
private void almostunified$onRecipeError(Map<ResourceLocation, JsonElement> recipes, ResourceManager resourceManager, ProfilerFiller profiler, CallbackInfo ci, @Local ResourceLocation recipe) {
AlmostUnifiedCommon.onRecipeManagerError(recipe);
}

@Inject(method = "apply(Ljava/util/Map;Lnet/minecraft/server/packs/resources/ResourceManager;Lnet/minecraft/util/profiling/ProfilerFiller;)V", at = @At("TAIL"))
private void almostunified$onRecipeReloadEnd(Map<ResourceLocation, JsonElement> recipes, ResourceManager resourceManager, ProfilerFiller profiler, CallbackInfo ci) {
AlmostUnifiedCommon.onRecipeManagerEnd();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,11 @@ public void unifyRecipe(RecipeLink recipe) {
}

public static class Result {

private final Multimap<ResourceLocation, RecipeLink> allRecipesByType = HashMultimap.create();
private final Multimap<ResourceLocation, RecipeLink> unifiedRecipesByType = HashMultimap.create();

private final Multimap<ResourceLocation, RecipeLink.DuplicateLink> duplicatesByType = HashMultimap.create();
@Nullable private Set<ResourceLocation> unifiedRecipeIds;

private void add(RecipeLink link) {
if (allRecipesByType.containsEntry(link.getType(), link)) {
Expand All @@ -218,7 +219,6 @@ private void add(RecipeLink link) {
if (link.isUnified()) {
unifiedRecipesByType.put(link.getType(), link);
}

if (link.hasDuplicateLink()) {
duplicatesByType.put(link.getType(), link.getDuplicateLink());
}
Expand All @@ -228,27 +228,39 @@ private void addAll(Collection<RecipeLink> links) {
links.forEach(this::add);
}

public Collection<RecipeLink> getRecipes(ResourceLocation type) {
public Collection<RecipeLink> getRecipesByType(ResourceLocation type) {
return Collections.unmodifiableCollection(allRecipesByType.get(type));
}

public Collection<RecipeLink> getUnifiedRecipes(ResourceLocation type) {
public Collection<ResourceLocation> getUnifiedRecipes() {
if (unifiedRecipeIds == null) {
unifiedRecipeIds = unifiedRecipesByType
.values()
.stream()
.map(RecipeLink::getId)
.collect(Collectors.toSet());
}

return unifiedRecipeIds;
}

public Collection<RecipeLink> getUnifiedRecipesByType(ResourceLocation type) {
return Collections.unmodifiableCollection(unifiedRecipesByType.get(type));
}

public Collection<RecipeLink.DuplicateLink> getDuplicates(ResourceLocation type) {
public Collection<RecipeLink.DuplicateLink> getDuplicateRecipesByType(ResourceLocation type) {
return Collections.unmodifiableCollection(duplicatesByType.get(type));
}

public int getUnifiedRecipeCount() {
public int getUnifiedRecipesCount() {
return unifiedRecipesByType.size();
}

public int getDuplicatesCount() {
public int getDuplicateRecipesCount() {
return duplicatesByType.size();
}

public int getDuplicateRecipesCount() {
public int getTotalDuplicateRecipesCount() {
return duplicatesByType.values().stream().mapToInt(l -> l.getRecipes().size()).sum();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,21 @@ public final class DebugHandler {

private final DebugConfig config;
private final String lastRun;
private final int recipesBefore;

private int recipesBefore = -1;
private long startTime;
private long endTime;
@Nullable private RecipeTransformer.Result transformerResult;

private DebugHandler(int recipesBefore, DebugConfig config) {
public DebugHandler(DebugConfig config) {
this.config = config;
this.lastRun = "# Last run: " + DATE_FORMAT.format(new Date(System.currentTimeMillis()));
this.recipesBefore = recipesBefore;
}

public static DebugHandler onRunStart(Map<ResourceLocation, JsonElement> recipes, UnificationLookup unificationLookup, DebugConfig config) {
DebugHandler handler = new DebugHandler(recipes.size(), config);
handler.dumpTags(unificationLookup);
handler.dumpRecipes(RECIPES_BEFORE, recipes);
return handler;
public void onRunStart(Map<ResourceLocation, JsonElement> recipes, UnificationLookup unificationLookup) {
dumpTags(unificationLookup);
dumpRecipes(RECIPES_BEFORE, recipes);
recipesBefore = recipes.size();
}

public void measure(Supplier<RecipeTransformer.Result> transformerSupplier) {
Expand All @@ -62,6 +60,7 @@ public void measure(Supplier<RecipeTransformer.Result> transformerSupplier) {
}

public void onRunEnd(Map<ResourceLocation, JsonElement> recipes) {
Preconditions.checkArgument(recipesBefore >= 0, "recipesBefore not set");
Preconditions.checkArgument(startTime > 0, "startTime not set");
Preconditions.checkArgument(endTime > 0, "endTime not set");
Preconditions.checkNotNull(transformerResult, "transformerResult not set");
Expand Down Expand Up @@ -122,12 +121,12 @@ private void dumpOverview(int recipesAfter) {
.append(lastRun).append("\n")
.append("# Statistics:\n")
.append("- Unified Recipes: ")
.append(transformerResult.getUnifiedRecipeCount())
.append(transformerResult.getUnifiedRecipesCount())
.append("\n")
.append("- Duplicate Recipes: ")
.append(transformerResult.getDuplicatesCount())
.append(" (Individual: ")
.append(transformerResult.getDuplicateRecipesCount())
.append(" (Individual: ")
.append(transformerResult.getTotalDuplicateRecipesCount())
.append(")\n")
.append("- Recipes Before: ")
.append(recipesBefore)
Expand All @@ -152,11 +151,11 @@ private void dumpOverview(int recipesAfter) {
.append("\n");

getSortedUnifiedRecipeTypes().forEach(type -> {
int unifiedSize = transformerResult.getUnifiedRecipes(type).size();
int allSize = transformerResult.getRecipes(type).size();
int duplicatesSize = transformerResult.getDuplicates(type).size();
int unifiedSize = transformerResult.getUnifiedRecipesByType(type).size();
int allSize = transformerResult.getRecipesByType(type).size();
int duplicatesSize = transformerResult.getDuplicateRecipesByType(type).size();
int individualDuplicatesSize = transformerResult
.getDuplicates(type)
.getDuplicateRecipesByType(type)
.stream()
.mapToInt(l -> l.getRecipes().size())
.sum();
Expand Down Expand Up @@ -209,7 +208,7 @@ private void dumpDuplicates() {
sb.append(lastRun).append("\n");
getSortedUnifiedRecipeTypes().forEach(type -> {
Collection<RecipeLink.DuplicateLink> duplicates = transformerResult
.getDuplicates(type)
.getDuplicateRecipesByType(type)
.stream()
.sorted(Comparator.comparing(l -> l.getMaster().getId().toString()))
.toList();
Expand All @@ -229,7 +228,7 @@ private String createDuplicatesDump(RecipeLink.DuplicateLink link) {
.stream()
.sorted(Comparator.comparing(r -> r.getId().toString()))
.map(r -> "\t\t- " + r.getId() + "\n")
.collect(Collectors.joining("", String.format("\t%s\n", link.getMaster().getId().toString()), "\n"));
.collect(Collectors.joining("", String.format("\t%s\n", link.getMaster().getId()), "\n"));
}

private static <T> int getMaxLength(Collection<T> collection, ToIntFunction<T> function) {
Expand All @@ -255,8 +254,13 @@ private Stream<ResourceLocation> getSortedUnifiedRecipeTypes() {
private Stream<RecipeLink> getSortedUnifiedRecipes(ResourceLocation type) {
Preconditions.checkNotNull(transformerResult);
return transformerResult
.getUnifiedRecipes(type)
.getUnifiedRecipesByType(type)
.stream()
.sorted(Comparator.comparing(r -> r.getId().toString()));
}

public RecipeTransformer.Result getRecipeTransformerResult() {
Preconditions.checkNotNull(transformerResult, "transformerResult not available yet");
return transformerResult;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.almostreliable.unified.utils;

import net.minecraft.resources.ResourceLocation;

import com.almostreliable.unified.AlmostUnifiedCommon;
import com.almostreliable.unified.unification.recipe.RecipeTransformer;

import java.util.HashSet;
import java.util.Set;

public final class RecipeErrorHandler {

private static final Set<ResourceLocation> RECIPES = new HashSet<>();

private RecipeErrorHandler() {}

public static void collect(RecipeTransformer.Result result, ResourceLocation recipe) {
if (result.getUnifiedRecipes().contains(recipe)) {
RECIPES.add(recipe);
}
}

public static void finish() {
if (!RECIPES.isEmpty()) {
AlmostUnifiedCommon.LOGGER.error(
"The following recipes were unified and threw exceptions when being loaded: {}",
RECIPES
);
AlmostUnifiedCommon.LOGGER.warn(
"Try to add them to the ignore list and if the problem is gone, report it to the Almost Unified developers."
);
}

RECIPES.clear();
}
}

0 comments on commit a4036db

Please sign in to comment.