Skip to content

Commit

Permalink
Improve null checks
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed May 3, 2017
1 parent bc2877f commit 3d48c60
Show file tree
Hide file tree
Showing 22 changed files with 126 additions and 145 deletions.
7 changes: 3 additions & 4 deletions src/main/java/mezz/jei/gui/Focus.java
@@ -1,6 +1,5 @@
package mezz.jei.gui;

import com.google.common.base.Preconditions;
import mezz.jei.Internal;
import mezz.jei.api.ingredients.IIngredientHelper;
import mezz.jei.api.recipe.IFocus;
Expand Down Expand Up @@ -38,7 +37,7 @@ public Mode getMode() {
* Make sure any IFocus coming in through API calls is validated and turned into JEI's Focus.
*/
public static <V> Focus<V> check(IFocus<V> focus) {
Preconditions.checkNotNull(focus, "focus must not be null");
ErrorUtil.checkNotNull(focus, "focus");
if (focus instanceof Focus) {
checkInternal(focus);
return (Focus<V>) focus;
Expand All @@ -47,9 +46,9 @@ public static <V> Focus<V> check(IFocus<V> focus) {
}

private static void checkInternal(IFocus<?> focus) {
Preconditions.checkNotNull(focus.getMode(), "mode must not be null");
ErrorUtil.checkNotNull(focus.getMode(), "focus mode");
Object value = focus.getValue();
Preconditions.checkNotNull(value, "value must not be null");
ErrorUtil.checkNotNull(value, "focus value");
if (value instanceof ItemStack) {
ItemStack itemStack = (ItemStack) value;
ErrorUtil.checkNotEmpty(itemStack);
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/mezz/jei/gui/GuiHelper.java
@@ -1,6 +1,5 @@
package mezz.jei.gui;

import com.google.common.base.Preconditions;
import mezz.jei.api.IGuiHelper;
import mezz.jei.api.gui.ICraftingGridHelper;
import mezz.jei.api.gui.IDrawableAnimated;
Expand All @@ -10,6 +9,7 @@
import mezz.jei.gui.elements.DrawableAnimated;
import mezz.jei.gui.elements.DrawableBlank;
import mezz.jei.gui.elements.DrawableResource;
import mezz.jei.util.ErrorUtil;
import net.minecraft.util.ResourceLocation;

public class GuiHelper implements IGuiHelper {
Expand Down Expand Up @@ -43,29 +43,29 @@ public GuiHelper() {

@Override
public IDrawableStatic createDrawable(ResourceLocation resourceLocation, int u, int v, int width, int height) {
Preconditions.checkNotNull(resourceLocation, "resourceLocation cannot be null");
ErrorUtil.checkNotNull(resourceLocation, "resourceLocation");

return new DrawableResource(resourceLocation, u, v, width, height, 0, 0, 0, 0, 256, 256);
}

@Override
public IDrawableStatic createDrawable(ResourceLocation resourceLocation, int u, int v, int width, int height, int textureWidth, int textureHeight) {
Preconditions.checkNotNull(resourceLocation, "resourceLocation cannot be null");
ErrorUtil.checkNotNull(resourceLocation, "resourceLocation");

return new DrawableResource(resourceLocation, u, v, width, height, 0, 0, 0, 0, textureWidth, textureHeight);
}

@Override
public IDrawableStatic createDrawable(ResourceLocation resourceLocation, int u, int v, int width, int height, int paddingTop, int paddingBottom, int paddingLeft, int paddingRight) {
Preconditions.checkNotNull(resourceLocation, "resourceLocation cannot be null");
ErrorUtil.checkNotNull(resourceLocation, "resourceLocation");

return new DrawableResource(resourceLocation, u, v, width, height, paddingTop, paddingBottom, paddingLeft, paddingRight, 256, 256);
}

@Override
public IDrawableAnimated createAnimatedDrawable(IDrawableStatic drawable, int ticksPerCycle, IDrawableAnimated.StartDirection startDirection, boolean inverted) {
Preconditions.checkNotNull(drawable, "drawable cannot be null");
Preconditions.checkNotNull(startDirection, "startDirection cannot be null");
ErrorUtil.checkNotNull(drawable, "drawable");
ErrorUtil.checkNotNull(startDirection, "startDirection");

IDrawableAnimated.StartDirection animationStartDirection = startDirection;
if (inverted) {
Expand Down
Expand Up @@ -10,7 +10,6 @@
import java.util.Map;
import java.util.Set;

import com.google.common.base.Preconditions;
import mezz.jei.Internal;
import mezz.jei.api.gui.IDrawable;
import mezz.jei.api.gui.IGuiIngredientGroup;
Expand All @@ -21,6 +20,7 @@
import mezz.jei.api.recipe.IFocus;
import mezz.jei.gui.Focus;
import mezz.jei.ingredients.IngredientRegistry;
import mezz.jei.util.ErrorUtil;
import mezz.jei.util.Log;
import net.minecraft.client.Minecraft;

Expand All @@ -44,7 +44,7 @@ public class GuiIngredientGroup<T> implements IGuiIngredientGroup<T> {
private ITooltipCallback<T> tooltipCallback;

public GuiIngredientGroup(Class<T> ingredientClass, @Nullable IFocus<T> focus, int cycleOffset) {
Preconditions.checkNotNull(ingredientClass);
ErrorUtil.checkNotNull(ingredientClass, "ingredientClass");
this.ingredientClass = ingredientClass;
if (focus == null) {
this.inputFocus = null;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/mezz/jei/gui/overlay/IngredientGrid.java
Expand Up @@ -34,6 +34,7 @@
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;

/**
* An ingredient grid displays a rectangular area of clickable recipe ingredients.
Expand Down Expand Up @@ -142,7 +143,7 @@ public void draw(Minecraft minecraft, int mouseX, int mouseY) {

JeiRuntime runtime = Internal.getRuntime();
if (runtime != null) {
Set<ItemStack> highlightedStacks = runtime.getItemListOverlay().getHighlightedStacks();
NonNullList<ItemStack> highlightedStacks = runtime.getItemListOverlay().getHighlightedStacks();
if (!highlightedStacks.isEmpty()) {
StackHelper helper = Internal.getHelpers().getStackHelper();
for (GuiIngredientFast guiItemStack : guiIngredientList.getAllGuiIngredients()) {
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/mezz/jei/gui/overlay/ItemListOverlay.java
Expand Up @@ -3,10 +3,7 @@
import javax.annotation.Nullable;
import java.awt.Rectangle;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import mezz.jei.api.IItemListOverlay;
import mezz.jei.api.ingredients.IIngredientRegistry;
Expand All @@ -20,10 +17,12 @@
import mezz.jei.input.IMouseHandler;
import mezz.jei.input.IPaged;
import mezz.jei.input.IShowsRecipeFocuses;
import mezz.jei.util.ErrorUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;

public class ItemListOverlay implements IItemListOverlay, IPaged, IMouseHandler, IShowsRecipeFocuses {
private static final int BORDER_PADDING = 2;
Expand All @@ -44,7 +43,7 @@ private static boolean isSearchBarCentered(GuiProperties guiProperties) {
}

private final IngredientFilter ingredientFilter;
private final Set<ItemStack> highlightedStacks = new HashSet<ItemStack>();
private final NonNullList<ItemStack> highlightedStacks = NonNullList.create();
private final ConfigButton configButton;
private final PageNavigation navigation;
private final IngredientGrid contents;
Expand Down Expand Up @@ -86,7 +85,7 @@ public void highlightStacks(Collection<ItemStack> stacks) {
highlightedStacks.addAll(stacks);
}

public Set<ItemStack> getHighlightedStacks() {
public NonNullList<ItemStack> getHighlightedStacks() {
return highlightedStacks;
}

Expand Down Expand Up @@ -303,7 +302,7 @@ public ItemStack getStackUnderMouse() {

@Override
public void setFilterText(String filterText) {
Preconditions.checkNotNull(filterText, "filterText cannot be null");
ErrorUtil.checkNotNull(filterText, "filterText");
if (Config.setFilterText(filterText)) {
this.searchField.setText(filterText);
SessionData.setFirstItemIndex(0);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/mezz/jei/gui/recipes/RecipeLayout.java
Expand Up @@ -7,7 +7,6 @@
import java.util.List;
import java.util.Map;

import com.google.common.base.Preconditions;
import mezz.jei.Internal;
import mezz.jei.api.gui.IDrawable;
import mezz.jei.api.gui.IGuiFluidStackGroup;
Expand All @@ -24,6 +23,7 @@
import mezz.jei.gui.ingredients.GuiIngredientGroup;
import mezz.jei.gui.ingredients.GuiItemStackGroup;
import mezz.jei.ingredients.Ingredients;
import mezz.jei.util.ErrorUtil;
import mezz.jei.util.Log;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
Expand Down Expand Up @@ -51,8 +51,8 @@ public class RecipeLayout implements IRecipeLayoutDrawable {
private int posY;

public <T extends IRecipeWrapper> RecipeLayout(int index, IRecipeCategory<T> recipeCategory, T recipeWrapper, @Nullable IFocus focus, int posX, int posY) {
Preconditions.checkNotNull(recipeCategory);
Preconditions.checkNotNull(recipeWrapper);
ErrorUtil.checkNotNull(recipeCategory, "recipeCategory");
ErrorUtil.checkNotNull(recipeWrapper, "recipeWrapper");
if (focus != null) {
focus = Focus.check(focus);
}
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/mezz/jei/ingredients/IngredientBlacklist.java
Expand Up @@ -4,7 +4,6 @@
import java.util.List;
import java.util.Set;

import com.google.common.base.Preconditions;
import mezz.jei.api.IItemBlacklist;
import mezz.jei.api.ingredients.IIngredientBlacklist;
import mezz.jei.api.ingredients.IIngredientHelper;
Expand All @@ -23,7 +22,7 @@ public IngredientBlacklist(IIngredientRegistry ingredientRegistry) {

@Override
public <V> void addIngredientToBlacklist(V ingredient) {
Preconditions.checkNotNull(ingredient, "Null ingredient");
ErrorUtil.checkNotNull(ingredient, "ingredient");

IIngredientHelper<V> ingredientHelper = ingredientRegistry.getIngredientHelper(ingredient);
String uniqueName = ingredientHelper.getUniqueId(ingredient);
Expand All @@ -32,7 +31,7 @@ public <V> void addIngredientToBlacklist(V ingredient) {

@Override
public <V> void removeIngredientFromBlacklist(V ingredient) {
Preconditions.checkNotNull(ingredient, "Null ingredient");
ErrorUtil.checkNotNull(ingredient, "ingredient");

IIngredientHelper<V> ingredientHelper = ingredientRegistry.getIngredientHelper(ingredient);
String uniqueName = ingredientHelper.getUniqueId(ingredient);
Expand All @@ -41,7 +40,7 @@ public <V> void removeIngredientFromBlacklist(V ingredient) {

@Override
public <V> boolean isIngredientBlacklisted(V ingredient) {
Preconditions.checkNotNull(ingredient, "Null ingredient");
ErrorUtil.checkNotNull(ingredient, "ingredient");

if (isIngredientBlacklistedByApi(ingredient)) {
return true;
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/mezz/jei/ingredients/IngredientRegistry.java
Expand Up @@ -6,7 +6,6 @@
import java.util.List;
import java.util.Map;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import mezz.jei.Internal;
Expand Down Expand Up @@ -70,7 +69,7 @@ private void getStackProperties(ItemStack itemStack) {

@Override
public <V> List<V> getIngredients(Class<V> ingredientClass) {
Preconditions.checkNotNull(ingredientClass, "ingredientClass cannot be null");
ErrorUtil.checkNotNull(ingredientClass, "ingredientClass");

//noinspection unchecked
List<V> ingredients = ingredientsMap.get(ingredientClass);
Expand All @@ -83,15 +82,15 @@ public <V> List<V> getIngredients(Class<V> ingredientClass) {

@Override
public <V> IIngredientHelper<V> getIngredientHelper(V ingredient) {
Preconditions.checkNotNull(ingredient, "ingredient cannot be null");
ErrorUtil.checkNotNull(ingredient, "ingredient");

//noinspection unchecked
return (IIngredientHelper<V>) getIngredientHelper(ingredient.getClass());
}

@Override
public <V> IIngredientHelper<V> getIngredientHelper(Class<V> ingredientClass) {
Preconditions.checkNotNull(ingredientClass, "ingredientClass cannot be null");
ErrorUtil.checkNotNull(ingredientClass, "ingredientClass");

//noinspection unchecked
IIngredientHelper<V> ingredientHelper = ingredientHelperMap.get(ingredientClass);
Expand All @@ -103,7 +102,7 @@ public <V> IIngredientHelper<V> getIngredientHelper(Class<V> ingredientClass) {

@Override
public <V> IIngredientRenderer<V> getIngredientRenderer(V ingredient) {
Preconditions.checkNotNull(ingredient, "ingredient cannot be null");
ErrorUtil.checkNotNull(ingredient, "ingredient");

//noinspection unchecked
Class<V> ingredientClass = (Class<V>) ingredient.getClass();
Expand All @@ -112,7 +111,7 @@ public <V> IIngredientRenderer<V> getIngredientRenderer(V ingredient) {

@Override
public <V> IIngredientRenderer<V> getIngredientRenderer(Class<V> ingredientClass) {
Preconditions.checkNotNull(ingredientClass, "ingredientClass cannot be null");
ErrorUtil.checkNotNull(ingredientClass, "ingredientClass");

//noinspection unchecked
IIngredientRenderer<V> ingredientRenderer = ingredientRendererMap.get(ingredientClass);
Expand All @@ -139,7 +138,7 @@ public List<ItemStack> getPotionIngredients() {

@Override
public <V> void addIngredientsAtRuntime(Class<V> ingredientClass, List<V> ingredients) {
Preconditions.checkNotNull(ingredientClass, "ingredientClass cannot be null");
ErrorUtil.checkNotNull(ingredientClass, "ingredientClass");
ErrorUtil.checkNotEmpty(ingredients, "ingredients");

//noinspection unchecked
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/mezz/jei/input/ClickedIngredient.java
@@ -1,6 +1,5 @@
package mezz.jei.input;

import com.google.common.base.Preconditions;
import mezz.jei.util.ErrorUtil;
import net.minecraft.item.ItemStack;

Expand All @@ -9,7 +8,7 @@ public class ClickedIngredient<V> implements IClickedIngredient<V> {
private boolean allowsCheating;

public ClickedIngredient(V value) {
Preconditions.checkNotNull(value, "value must not be null");
ErrorUtil.checkNotNull(value, "value");
if (value instanceof ItemStack) {
ItemStack itemStack = (ItemStack) value;
ErrorUtil.checkNotEmpty(itemStack);
Expand Down
Expand Up @@ -11,7 +11,6 @@
import mezz.jei.Internal;
import mezz.jei.api.ingredients.IIngredientRegistry;
import mezz.jei.config.Config;
import mezz.jei.util.ItemStackUtil;
import mezz.jei.util.Java6Util;
import mezz.jei.util.Log;
import net.minecraft.init.PotionTypes;
Expand Down Expand Up @@ -97,7 +96,7 @@ private List<ItemStack> getNewPotions(List<ItemStack> knownPotions, List<ItemSta
}
}

BrewingRecipeWrapper recipe = new BrewingRecipeWrapper(ItemStackUtil.singletonList(potionIngredient), potionInput.copy(), potionOutput);
BrewingRecipeWrapper recipe = new BrewingRecipeWrapper(Collections.singletonList(potionIngredient), potionInput.copy(), potionOutput);
if (!recipes.contains(recipe)) {
recipes.add(recipe);
newPotions.add(potionOutput);
Expand Down
Expand Up @@ -14,20 +14,19 @@
import net.minecraft.item.ItemStack;
import net.minecraft.potion.PotionType;
import net.minecraft.potion.PotionUtils;
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.registry.ForgeRegistries;

public class BrewingRecipeWrapper extends BlankRecipeWrapper {
private static final BrewingRecipeUtil UTIL = new BrewingRecipeUtil();

private final NonNullList<ItemStack> ingredients;
private final List<ItemStack> ingredients;
private final ItemStack potionInput;
private final ItemStack potionOutput;
private final List<List<ItemStack>> inputs;
private final int hashCode;

public BrewingRecipeWrapper(NonNullList<ItemStack> ingredients, ItemStack potionInput, ItemStack potionOutput) {
public BrewingRecipeWrapper(List<ItemStack> ingredients, ItemStack potionInput, ItemStack potionOutput) {
this.ingredients = ingredients;
this.potionInput = potionInput;
this.potionOutput = potionOutput;
Expand Down
Expand Up @@ -4,7 +4,6 @@
import java.awt.Color;
import java.util.List;

import com.google.common.base.Preconditions;
import mezz.jei.api.ingredients.IIngredientHelper;
import mezz.jei.color.ColorGetter;
import mezz.jei.startup.StackHelper;
Expand Down Expand Up @@ -33,7 +32,7 @@ public ItemStack getMatch(Iterable<ItemStack> ingredients, ItemStack toMatch) {

@Override
public String getDisplayName(ItemStack ingredient) {
return Preconditions.checkNotNull(ingredient.getDisplayName(), "No display name for ItemStack.");
return ErrorUtil.checkNotNull(ingredient.getDisplayName(), "itemStack.getDisplayName()");
}

@Override
Expand Down

0 comments on commit 3d48c60

Please sign in to comment.