forked from mezz/JustEnoughItems
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Catch and log crashes from IRecipeRegistryPlugins instead of crashing
- Loading branch information
Showing
2 changed files
with
58 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/main/java/mezz/jei/recipes/RecipeRegistryPluginSafeWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |