Skip to content

Commit

Permalink
Improve error messages and plugin logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Jun 7, 2016
1 parent 03aa795 commit 4996933
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 48 deletions.
18 changes: 11 additions & 7 deletions 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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down
26 changes: 16 additions & 10 deletions 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;
Expand Down Expand Up @@ -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<Class, IRecipeHandler> recipeHandlers;
private final ImmutableTable<Class, String, IRecipeTransferHandler> recipeTransferHandlers;
Expand Down Expand Up @@ -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);
}
}
}

Expand Down
26 changes: 15 additions & 11 deletions 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) {
Expand All @@ -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<String> itemStackIngredientsInfo = getItemStackIngredientsInfo(outputs);
recipeInfoBuilder.append(itemStackIngredientsInfo);
} catch (RuntimeException e) {
recipeInfoBuilder.append(e.getMessage());
Expand All @@ -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<String> itemStackIngredientsInfo = getItemStackIngredientsInfo(inputs);
recipeInfoBuilder.append(itemStackIngredientsInfo);
} catch (RuntimeException e) {
recipeInfoBuilder.append(e.getMessage());
Expand All @@ -68,13 +69,13 @@ public static String getInfoFromBrokenRecipe(@Nonnull Object recipe, @Nonnull IR
return recipeInfoBuilder.toString();
}

public static List<List<String>> getItemStackIngredientsInfo(@Nullable List list) {
public static List<String> getItemStackIngredientsInfo(@Nullable List list) {
if (list == null) {
return null;
}
StackHelper stackHelper = Internal.getStackHelper();

List<List<String>> ingredientsInfo = new ArrayList<>();
List<String> ingredientsInfo = new ArrayList<>();
for (Object ingredient : list) {
List<String> ingredientInfo = new ArrayList<>();

Expand All @@ -88,7 +89,8 @@ public static List<List<String>> getItemStackIngredientsInfo(@Nullable List list
String itemStackInfo = getItemStackInfo(stack);
ingredientInfo.add(itemStackInfo);
}
ingredientsInfo.add(ingredientInfo);

ingredientsInfo.add(ingredientInfo.toString() + "\n");
}
return ingredientsInfo;
}
Expand All @@ -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;
}
}
38 changes: 18 additions & 20 deletions 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;
Expand All @@ -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<UidMode, Map<ItemStack, String>> uidCache = new EnumMap<>(UidMode.class);
private boolean uidCacheEnabled = true;
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down

0 comments on commit 4996933

Please sign in to comment.