Skip to content

Commit

Permalink
Catch and log crashes from IRecipeRegistryPlugins instead of crashing
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Aug 13, 2018
1 parent 83618ab commit f767af9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 18 deletions.
23 changes: 5 additions & 18 deletions src/main/java/mezz/jei/recipes/RecipeRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class RecipeRegistry implements IRecipeRegistry {
private final ListMultiMap<IRecipeCategory, IRecipeWrapper> recipeWrappersForCategories = new ListMultiMap<>();
private final RecipeMap recipeInputMap;
private final RecipeMap recipeOutputMap;
private final List<IRecipeRegistryPlugin> plugins = new ArrayList<>();
private final List<RecipeRegistryPluginSafeWrapper> plugins = new ArrayList<>();
private final SetMultiMap<String, IRecipeWrapper> hiddenRecipes = new SetMultiMap<>(() -> Collections.newSetFromMap(new IdentityHashMap<>())); // recipe category uid key

public RecipeRegistry(
Expand Down Expand Up @@ -117,8 +117,10 @@ public RecipeRegistry(
ImmutableMultimap<String, String> categoriesForRecipeCatalystKeys = categoriesForRecipeCatalystKeysBuilder.build();

IRecipeRegistryPlugin internalRecipeRegistryPlugin = new InternalRecipeRegistryPlugin(this, categoriesForRecipeCatalystKeys, ingredientRegistry, recipeCategoriesMap, recipeInputMap, recipeOutputMap, recipeWrappersForCategories);
this.plugins.add(internalRecipeRegistryPlugin);
this.plugins.addAll(plugins);
this.plugins.add(new RecipeRegistryPluginSafeWrapper(internalRecipeRegistryPlugin));
for (IRecipeRegistryPlugin plugin : plugins) {
this.plugins.add(new RecipeRegistryPluginSafeWrapper(plugin));
}

this.recipeCategories = ImmutableList.copyOf(recipeCategories);
}
Expand Down Expand Up @@ -568,12 +570,7 @@ public <V> List<IRecipeCategory> getRecipeCategories(IFocus<V> focus) {

List<String> allRecipeCategoryUids = new ArrayList<>();
for (IRecipeRegistryPlugin plugin : this.plugins) {
long start_time = System.currentTimeMillis();
List<String> recipeCategoryUids = plugin.getRecipeCategoryUids(focus);
long timeElapsed = System.currentTimeMillis() - start_time;
if (timeElapsed > 10) {
Log.get().warn("Recipe Category lookup is slow: {} ms. {}", timeElapsed, plugin.getClass());
}
for (String recipeCategoryUid : recipeCategoryUids) {
if (!allRecipeCategoryUids.contains(recipeCategoryUid)) {
if (hiddenRecipes.containsKey(recipeCategoryUid)) {
Expand Down Expand Up @@ -606,12 +603,7 @@ public <T extends IRecipeWrapper, V> List<T> getRecipeWrappers(IRecipeCategory<T

List<T> allRecipeWrappers = new ArrayList<>();
for (IRecipeRegistryPlugin plugin : this.plugins) {
long start_time = System.currentTimeMillis();
List<T> recipeWrappers = plugin.getRecipeWrappers(recipeCategory, focus);
long timeElapsed = System.currentTimeMillis() - start_time;
if (timeElapsed > 10) {
Log.get().warn("Recipe Wrapper lookup is slow: {} ms. {}", timeElapsed, plugin.getClass());
}
allRecipeWrappers.addAll(recipeWrappers);
}

Expand All @@ -628,12 +620,7 @@ public <T extends IRecipeWrapper> List<T> getRecipeWrappers(IRecipeCategory<T> r

List<T> allRecipeWrappers = new ArrayList<>();
for (IRecipeRegistryPlugin plugin : this.plugins) {
long start_time = System.currentTimeMillis();
List<T> recipeWrappers = plugin.getRecipeWrappers(recipeCategory);
long timeElapsed = System.currentTimeMillis() - start_time;
if (timeElapsed > 10) {
Log.get().warn("Recipe Wrapper lookup is slow: {} ms. {}", timeElapsed, plugin.getClass());
}
allRecipeWrappers.addAll(recipeWrappers);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package mezz.jei.recipes;

import com.google.common.base.Stopwatch;
import mezz.jei.api.recipe.IFocus;
import mezz.jei.api.recipe.IRecipeCategory;
import mezz.jei.api.recipe.IRecipeRegistryPlugin;
import mezz.jei.api.recipe.IRecipeWrapper;
import mezz.jei.util.Log;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

public class RecipeRegistryPluginSafeWrapper implements IRecipeRegistryPlugin {
private final IRecipeRegistryPlugin plugin;
private final Stopwatch stopWatch = Stopwatch.createUnstarted();

public RecipeRegistryPluginSafeWrapper(IRecipeRegistryPlugin plugin) {
this.plugin = plugin;
}

@Override
public <V> List<String> getRecipeCategoryUids(IFocus<V> focus) {
return callPluginMethod(() -> plugin.getRecipeCategoryUids(focus), Collections.emptyList());
}

@Override
public <T extends IRecipeWrapper, V> List<T> getRecipeWrappers(IRecipeCategory<T> recipeCategory, IFocus<V> focus) {
return callPluginMethod(() -> plugin.getRecipeWrappers(recipeCategory, focus), Collections.emptyList());
}

@Override
public <T extends IRecipeWrapper> List<T> getRecipeWrappers(IRecipeCategory<T> recipeCategory) {
return callPluginMethod(() -> plugin.getRecipeWrappers(recipeCategory), Collections.emptyList());
}

private <T> T callPluginMethod(Supplier<T> supplier, T defaultValue) {
try {
stopWatch.start();
T result = supplier.get();
stopWatch.stop();
if (stopWatch.elapsed(TimeUnit.MILLISECONDS) > 10) {
Log.get().warn("Recipe registry plugin is slow, took {}. {}", stopWatch, plugin.getClass());
}
return result;
} catch (RuntimeException | LinkageError e) {
stopWatch.reset();
Log.get().error("Recipe registry plugin crashed: {}", plugin.getClass(), e);
return defaultValue;
}
}
}

0 comments on commit f767af9

Please sign in to comment.