Skip to content

Commit

Permalink
Get better error information from ErrorUtil#checkIsValidIngredient an…
Browse files Browse the repository at this point in the history
…d ClickedIngredient creation
  • Loading branch information
mezz committed Nov 18, 2018
1 parent e20c3a5 commit bd478c6
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 11 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mcversion=1.12.2
forgeversion=14.23.4.2749
forgeversion=14.23.5.2775
mcp_mappings=stable_nodoc_39
curse_project_id=238222

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/mezz/jei/gui/recipes/RecipeCatalysts.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public IClickedIngredient<?> getIngredientUnderMouse(int mouseX, int mouseY) {
if (hovered != null) {
Object ingredientUnderMouse = hovered.getDisplayedIngredient();
if (ingredientUnderMouse != null) {
return new ClickedIngredient<>(ingredientUnderMouse, hovered.getRect());
return ClickedIngredient.create(ingredientUnderMouse, hovered.getRect());
}
}
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/mezz/jei/gui/recipes/RecipesGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public IClickedIngredient<?> getIngredientUnderMouse(int mouseX, int mouseY) {
if (clicked != null) {
Object displayedIngredient = clicked.getDisplayedIngredient();
if (displayedIngredient != null) {
return new ClickedIngredient<>(displayedIngredient, clicked.getRect());
return ClickedIngredient.create(displayedIngredient, clicked.getRect());
}
}
}
Expand Down
23 changes: 21 additions & 2 deletions src/main/java/mezz/jei/input/ClickedIngredient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import mezz.jei.Internal;
import mezz.jei.api.ingredients.IIngredientHelper;
import mezz.jei.api.ingredients.IIngredientRegistry;
import mezz.jei.ingredients.IngredientRegistry;
import mezz.jei.util.ErrorUtil;
import mezz.jei.util.Log;
import net.minecraft.item.ItemStack;

import javax.annotation.Nullable;
Expand All @@ -17,8 +19,25 @@ public class ClickedIngredient<V> implements IClickedIngredient<V> {
private IOnClickHandler onClickHandler;
private boolean allowsCheating;

public ClickedIngredient(V value, @Nullable Rectangle area) {
ErrorUtil.checkIsValidIngredient(value, "value");
@Nullable
public static <V> ClickedIngredient<V> create(V value, @Nullable Rectangle area) {
ErrorUtil.checkNotNull(value, "value");
IngredientRegistry ingredientRegistry = Internal.getIngredientRegistry();
IIngredientHelper<V> ingredientHelper = ingredientRegistry.getIngredientHelper(value);
try {
if (ingredientHelper.isValidIngredient(value)) {
return new ClickedIngredient<>(value, area);
}
String ingredientInfo = ingredientHelper.getErrorInfo(value);
Log.get().error("Clicked invalid ingredient. Ingredient Info: {}", ingredientInfo);
} catch (RuntimeException e) {
String ingredientInfo = ingredientHelper.getErrorInfo(value);
Log.get().error("Clicked invalid ingredient. Ingredient Info: {}", ingredientInfo, e);
}
return null;
}

private ClickedIngredient(V value, @Nullable Rectangle area) {
this.value = value;
this.area = area;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/mezz/jei/input/GuiContainerWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public IClickedIngredient<?> getIngredientUnderMouse(int mouseX, int mouseY) {
ItemStack stack = slotUnderMouse.getStack();
if (!stack.isEmpty()) {
Rectangle slotArea = new Rectangle(slotUnderMouse.xPos, slotUnderMouse.yPos, 16, 16);
return new ClickedIngredient<>(stack, slotArea);
return ClickedIngredient.create(stack, slotArea);
}
}
return getAdvancedGuiHandlerIngredientUnderMouse(guiContainer, mouseX, mouseY);
Expand All @@ -46,7 +46,7 @@ private <T extends GuiContainer> IClickedIngredient<?> getAdvancedGuiHandlerIngr
if (clicked instanceof ItemStack && slotUnderMouse != null && ItemStack.areItemStacksEqual(slotUnderMouse.getStack(), (ItemStack) clicked)) {
area = new Rectangle(slotUnderMouse.xPos, slotUnderMouse.yPos, 16, 16);
}
return new ClickedIngredient<>(clicked, area);
return ClickedIngredient.create(clicked, area);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public ClickedIngredient<?> getIngredientUnderMouse(int mouseX, int mouseY) {
IngredientRenderer hovered = getHovered(mouseX, mouseY);
if (hovered != null) {
IIngredientListElement element = hovered.getElement();
return new ClickedIngredient<>(element.getIngredient(), hovered.getArea());
return ClickedIngredient.create(element.getIngredient(), hovered.getArea());
}
return null;
}
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/mezz/jei/util/ErrorUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import mezz.jei.api.ingredients.VanillaTypes;
import mezz.jei.api.recipe.IIngredientType;
import mezz.jei.api.recipe.IRecipeWrapper;
import mezz.jei.ingredients.IngredientRegistry;
import mezz.jei.ingredients.Ingredients;
import mezz.jei.startup.ForgeModIdHelper;
import net.minecraft.block.Block;
Expand Down Expand Up @@ -220,10 +221,13 @@ public static <T> T checkNotNull(@Nullable T object, String name) {
return object;
}

public static void checkIsValidIngredient(@Nullable Object ingredient, String name) {
public static <T> void checkIsValidIngredient(@Nullable T ingredient, String name) {
checkNotNull(ingredient, name);
if (!Internal.getIngredientRegistry().isValidIngredient(ingredient)) {
throw new IllegalArgumentException("Invalid ingredient found. Parameter Name: " + name + " Class: " + ingredient.getClass() + " Object: " + ingredient);
IngredientRegistry ingredientRegistry = Internal.getIngredientRegistry();
IIngredientHelper<T> ingredientHelper = ingredientRegistry.getIngredientHelper(ingredient);
if (!ingredientHelper.isValidIngredient(ingredient)) {
String ingredientInfo = ingredientHelper.getErrorInfo(ingredient);
throw new IllegalArgumentException("Invalid ingredient found. Parameter Name: " + name + " Ingredient Info: " + ingredientInfo);
}
}

Expand Down

0 comments on commit bd478c6

Please sign in to comment.