From 49969334031750c2a6b5069fd50839ddcb77d02b Mon Sep 17 00:00:00 2001 From: mezz Date: Tue, 7 Jun 2016 01:09:54 -0700 Subject: [PATCH] Improve error messages and plugin logging --- src/main/java/mezz/jei/ProxyCommonClient.java | 18 +++++---- src/main/java/mezz/jei/RecipeRegistry.java | 26 ++++++++----- src/main/java/mezz/jei/util/ErrorUtil.java | 26 +++++++------ src/main/java/mezz/jei/util/StackHelper.java | 38 +++++++++---------- 4 files changed, 60 insertions(+), 48 deletions(-) diff --git a/src/main/java/mezz/jei/ProxyCommonClient.java b/src/main/java/mezz/jei/ProxyCommonClient.java index 78c7bb8d0..7a5a8a24e 100644 --- a/src/main/java/mezz/jei/ProxyCommonClient.java +++ b/src/main/java/mezz/jei/ProxyCommonClient.java @@ -1,5 +1,12 @@ package mezz.jei; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.concurrent.TimeUnit; + import mezz.jei.api.IModPlugin; import mezz.jei.api.gui.IAdvancedGuiHandler; import mezz.jei.config.Config; @@ -35,12 +42,6 @@ import net.minecraftforge.fml.common.eventhandler.EventPriority; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.util.Arrays; -import java.util.Iterator; -import java.util.List; - @SuppressWarnings("unused") public class ProxyCommonClient extends ProxyCommon { @Nullable @@ -158,8 +159,11 @@ private void startJEI() { while (iterator.hasNext()) { IModPlugin plugin = iterator.next(); try { + long start_time = System.nanoTime(); + Log.info("Registering plugin: {}", plugin.getClass().getName()); plugin.register(modRegistry); - Log.info("Registered plugin: {}", plugin.getClass().getName()); + long timeElapsedSeconds = TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - start_time); + Log.info("Registered plugin: {} in {} seconds", plugin.getClass().getName(), timeElapsedSeconds); } catch (RuntimeException | LinkageError e) { Log.error("Failed to register mod plugin: {}", plugin.getClass(), e); iterator.remove(); diff --git a/src/main/java/mezz/jei/RecipeRegistry.java b/src/main/java/mezz/jei/RecipeRegistry.java index ad354b399..a0574ee5e 100644 --- a/src/main/java/mezz/jei/RecipeRegistry.java +++ b/src/main/java/mezz/jei/RecipeRegistry.java @@ -1,5 +1,14 @@ package mezz.jei; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ImmutableCollection; import com.google.common.collect.ImmutableList; @@ -27,15 +36,6 @@ import net.minecraftforge.fluids.Fluid; import net.minecraftforge.fluids.FluidStack; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - public class RecipeRegistry implements IRecipeRegistry { private final ImmutableMap recipeHandlers; private final ImmutableTable recipeTransferHandlers; @@ -170,7 +170,13 @@ public void addRecipe(@Nullable Object recipe) { addRecipeUnchecked(recipe, recipeCategory, recipeHandler); } catch (RuntimeException | LinkageError e) { String recipeInfo = ErrorUtil.getInfoFromBrokenRecipe(recipe, recipeHandler); - Log.error("Found a broken recipe: {}\n", recipeInfo, e); + + // suppress the null item in stack exception, that information is redundant here. + if (e.getMessage().equals(StackHelper.nullItemInStack)) { + Log.error("Found a broken recipe: {}\n", recipeInfo); + } else { + Log.error("Found a broken recipe: {}\n", recipeInfo, e); + } } } diff --git a/src/main/java/mezz/jei/util/ErrorUtil.java b/src/main/java/mezz/jei/util/ErrorUtil.java index bf8527a75..d44582723 100644 --- a/src/main/java/mezz/jei/util/ErrorUtil.java +++ b/src/main/java/mezz/jei/util/ErrorUtil.java @@ -1,17 +1,18 @@ package mezz.jei.util; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.ArrayList; +import java.util.List; + import mezz.jei.Internal; import mezz.jei.api.recipe.IRecipeHandler; import mezz.jei.api.recipe.IRecipeWrapper; import net.minecraft.item.Item; +import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; import net.minecraft.util.ResourceLocation; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.util.ArrayList; -import java.util.List; - public class ErrorUtil { @Nonnull public static String getInfoFromBrokenRecipe(@Nonnull Object recipe, @Nonnull IRecipeHandler recipeHandler) { @@ -36,7 +37,7 @@ public static String getInfoFromBrokenRecipe(@Nonnull Object recipe, @Nonnull IR recipeInfoBuilder.append("\nOutput ItemStacks: "); try { List outputs = recipeWrapper.getOutputs(); - Object itemStackIngredientsInfo = getItemStackIngredientsInfo(outputs); + List itemStackIngredientsInfo = getItemStackIngredientsInfo(outputs); recipeInfoBuilder.append(itemStackIngredientsInfo); } catch (RuntimeException e) { recipeInfoBuilder.append(e.getMessage()); @@ -52,7 +53,7 @@ public static String getInfoFromBrokenRecipe(@Nonnull Object recipe, @Nonnull IR recipeInfoBuilder.append("\nInput ItemStacks: "); try { List inputs = recipeWrapper.getInputs(); - Object itemStackIngredientsInfo = getItemStackIngredientsInfo(inputs); + List itemStackIngredientsInfo = getItemStackIngredientsInfo(inputs); recipeInfoBuilder.append(itemStackIngredientsInfo); } catch (RuntimeException e) { recipeInfoBuilder.append(e.getMessage()); @@ -68,13 +69,13 @@ public static String getInfoFromBrokenRecipe(@Nonnull Object recipe, @Nonnull IR return recipeInfoBuilder.toString(); } - public static List> getItemStackIngredientsInfo(@Nullable List list) { + public static List getItemStackIngredientsInfo(@Nullable List list) { if (list == null) { return null; } StackHelper stackHelper = Internal.getStackHelper(); - List> ingredientsInfo = new ArrayList<>(); + List ingredientsInfo = new ArrayList<>(); for (Object ingredient : list) { List ingredientInfo = new ArrayList<>(); @@ -88,7 +89,8 @@ public static List> getItemStackIngredientsInfo(@Nullable List list String itemStackInfo = getItemStackInfo(stack); ingredientInfo.add(itemStackInfo); } - ingredientsInfo.add(ingredientInfo); + + ingredientsInfo.add(ingredientInfo.toString() + "\n"); } return ingredientsInfo; } @@ -103,9 +105,11 @@ public static String getItemStackInfo(@Nonnull ItemStack itemStack) { ResourceLocation registryName = item.getRegistryName(); if (registryName != null) { itemName = registryName.toString(); + } else if (item instanceof ItemBlock) { + itemName = "ItemBlock(" + ((ItemBlock) item).getBlock() + ")"; } else { itemName = item.getClass().getName(); } - return itemStack.toString().replace(item.getUnlocalizedName(), itemName); + return itemStack.toString() + " " + itemName; } } diff --git a/src/main/java/mezz/jei/util/StackHelper.java b/src/main/java/mezz/jei/util/StackHelper.java index e6da8c137..7ce9ec42e 100644 --- a/src/main/java/mezz/jei/util/StackHelper.java +++ b/src/main/java/mezz/jei/util/StackHelper.java @@ -1,17 +1,5 @@ package mezz.jei.util; -import mezz.jei.Internal; -import mezz.jei.api.recipe.IStackHelper; -import mezz.jei.gui.ingredients.IGuiIngredient; -import net.minecraft.creativetab.CreativeTabs; -import net.minecraft.inventory.Container; -import net.minecraft.inventory.Slot; -import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.util.ResourceLocation; -import net.minecraftforge.oredict.OreDictionary; - import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.util.ArrayList; @@ -26,7 +14,21 @@ import java.util.SortedSet; import java.util.TreeSet; +import mezz.jei.Internal; +import mezz.jei.api.recipe.IStackHelper; +import mezz.jei.gui.ingredients.IGuiIngredient; +import net.minecraft.creativetab.CreativeTabs; +import net.minecraft.inventory.Container; +import net.minecraft.inventory.Slot; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.oredict.OreDictionary; + public class StackHelper implements IStackHelper { + public static final String nullItemInStack = "Found an itemStack with a null item. This is an error from another mod."; + /** Uids are cached during loading to improve startup performance. */ private final Map> uidCache = new EnumMap<>(UidMode.class); private boolean uidCacheEnabled = true; @@ -312,17 +314,13 @@ private void toItemStackList(@Nonnull UniqueItemStackListBuilder itemStackListBu public String getModId(@Nonnull ItemStack stack) { Item item = stack.getItem(); if (item == null) { - throw new NullPointerException("Found an itemStack with a null item. This is an error from another mod."); + throw new NullPointerException(nullItemInStack); } - return getModId(item); - } - - @Nonnull - public String getModId(@Nonnull Item item) { ResourceLocation itemName = Item.REGISTRY.getNameForObject(item); if (itemName == null) { - throw new NullPointerException("Item.itemRegistry.getNameForObject returned null for: " + item.getClass()); + String stackInfo = ErrorUtil.getItemStackInfo(stack); + throw new NullPointerException("Item.itemRegistry.getNameForObject returned null for: " + stackInfo); } return itemName.getResourceDomain(); @@ -344,7 +342,7 @@ public String getUniqueIdentifierForStack(@Nonnull ItemStack stack, @Nonnull Uid Item item = stack.getItem(); if (item == null) { - throw new NullPointerException("Found an itemStack with a null item. This is an error from another mod."); + throw new NullPointerException(nullItemInStack); } ResourceLocation itemName = Item.REGISTRY.getNameForObject(item);