Skip to content

Commit

Permalink
Throw exception if some JEI API methods are called from the wrong thread
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Nov 5, 2017
1 parent c7a3627 commit c6e4329
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gradle.properties
@@ -1,5 +1,5 @@
mcversion=1.12.2
forgeversion=14.23.0.2501
forgeversion=14.23.0.2529
mcp_mappings=snapshot_20170918
curse_project_id=238222

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/mezz/jei/ingredients/IngredientRegistry.java
Expand Up @@ -155,6 +155,7 @@ public List<ItemStack> getPotionIngredients() {

@Override
public <V> void addIngredientsAtRuntime(Class<V> ingredientClass, List<V> ingredients) {
ErrorUtil.assertMainThread();
addIngredientsAtRuntime(ingredientClass, ingredients, Internal.getIngredientFilter());
}

Expand Down Expand Up @@ -183,6 +184,7 @@ public <V> void addIngredientsAtRuntime(Class<V> ingredientClass, List<V> ingred

@Override
public <V> void removeIngredientsAtRuntime(Class<V> ingredientClass, List<V> ingredients) {
ErrorUtil.assertMainThread();
removeIngredientsAtRuntime(ingredientClass, ingredients, Internal.getIngredientFilter());
}

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/mezz/jei/recipes/RecipeRegistry.java
Expand Up @@ -178,6 +178,7 @@ public <V> IFocus<V> createFocus(IFocus.Mode mode, V ingredient) {
@Override
public void addRecipe(Object recipe) {
ErrorUtil.checkNotNull(recipe, "recipe");
ErrorUtil.assertMainThread();

addRecipe(recipe, recipe.getClass(), null);
}
Expand All @@ -186,6 +187,7 @@ public void addRecipe(Object recipe) {
public void addRecipe(IRecipeWrapper recipe, String recipeCategoryUid) {
ErrorUtil.checkNotNull(recipe, "recipe");
ErrorUtil.checkNotNull(recipeCategoryUid, "recipeCategoryUid");
ErrorUtil.assertMainThread();

IRecipeCategory recipeCategory = getRecipeCategory(recipeCategoryUid);
if (recipeCategory == null) {
Expand Down Expand Up @@ -282,6 +284,7 @@ public Ingredients getIngredients(IRecipeWrapper recipeWrapper) {
@Override
public void removeRecipe(Object recipe) {
ErrorUtil.checkNotNull(recipe, "recipe");
ErrorUtil.assertMainThread();

List<IRecipeHandler<Object>> recipeHandlers1 = getRecipeHandlers(recipe.getClass());
for (IRecipeHandler<Object> recipeHandler : recipeHandlers1) {
Expand Down Expand Up @@ -312,6 +315,7 @@ private <T> void removeRecipe(T recipe, String recipeCategoryUid) {
public void removeRecipe(IRecipeWrapper recipe, String recipeCategoryUid) {
ErrorUtil.checkNotNull(recipe, "recipe");
ErrorUtil.checkNotNull(recipeCategoryUid, "recipeCategoryUid");
ErrorUtil.assertMainThread();

hideRecipe(recipe);
}
Expand Down Expand Up @@ -670,13 +674,15 @@ public <T extends IRecipeWrapper> IRecipeLayoutDrawable createRecipeLayoutDrawab
@Override
public void hideRecipe(IRecipeWrapper recipe) {
ErrorUtil.checkNotNull(recipe, "recipe");
ErrorUtil.assertMainThread();
hiddenRecipes.add(recipe);
recipeCategoriesVisibleCache.clear();
}

@Override
public void unhideRecipe(IRecipeWrapper recipe) {
ErrorUtil.checkNotNull(recipe, "recipe");
ErrorUtil.assertMainThread();
hiddenRecipes.remove(recipe);
recipeCategoriesVisibleCache.clear();
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/mezz/jei/util/ErrorUtil.java
Expand Up @@ -13,6 +13,7 @@
import mezz.jei.api.recipe.IRecipeWrapper;
import mezz.jei.ingredients.Ingredients;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
Expand Down Expand Up @@ -203,4 +204,12 @@ public static void checkIsValidIngredient(@Nullable Object ingredient, String na
throw new IllegalArgumentException("Invalid ingredient found. Parameter Name: " + name + " Class: " + ingredient.getClass() + " Object: " + ingredient);
}
}

@SuppressWarnings("ConstantConditions")
public static void assertMainThread() {
Minecraft minecraft = Minecraft.getMinecraft();
if (minecraft != null && !minecraft.isCallingFromMinecraftThread()) {
throw new IllegalStateException("A JEI API method is being called by another mod from the wrong thread. It must be called on the main thread.");
}
}
}

3 comments on commit c6e4329

@PhillipPruett
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Mezz, do you have have a suggested workaround for calling unhide() from an eventHandler since this change?

@mezz
Copy link
Owner Author

@mezz mezz commented on c6e4329 Jul 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use this method to schedule some code to run on the main thread: Minecraft#addScheduledTask()

@PhillipPruett
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, thanks.

Please sign in to comment.