/**
 * This class was created by <Vazkii>. It's distributed as
 * part of the Totemic Mod. Get the Source Code in github:
 * https://github.com/Vazkii/Botania
 *
 * Botania is Open Source and distributed under a
 * Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License
 * (http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_GB)
 *
 * File Created @ [Jan 19, 2014, 4:58:19 PM (GMT)]
 */
package pokefenn.totemic.totempedia.page;

import java.util.Collections;
import java.util.Objects;

import org.lwjgl.opengl.GL11;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.client.resources.I18n;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.item.crafting.ShapedRecipes;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.oredict.ShapedOreRecipe;
import pokefenn.totemic.api.internal.IGuiLexiconEntry;
import pokefenn.totemic.api.lexicon.LexiconEntry;
import pokefenn.totemic.api.lexicon.LexiconRecipeMappings;
import pokefenn.totemic.client.RenderHelper;
import pokefenn.totemic.lib.Resources;

public class PageCraftingRecipe extends PageRecipe
{
    private static final ResourceLocation craftingOverlay = new ResourceLocation(Resources.GUI_CRAFTING_OVERLAY);

    private final IRecipe recipe;
    private final int recipeWidth;
    private final boolean shapelessRecipe;

    private int ticksElapsed = 0;
    private int recipeIndex = 0;

    public PageCraftingRecipe(String unlocalizedName, IRecipe recipe)
    {
        super(unlocalizedName);
        this.recipe = Objects.requireNonNull(recipe);

        if(recipe instanceof ShapedRecipes)
        {
            recipeWidth = ((ShapedRecipes) recipe).getWidth();
            shapelessRecipe = false;
        }
        else if(recipe instanceof ShapedOreRecipe)
        {
            recipeWidth = ((ShapedOreRecipe) recipe).getWidth();
            shapelessRecipe = false;
        }
        else
        {
            recipeWidth = 3;
            shapelessRecipe = true;
        }
    }

    public PageCraftingRecipe(String unlocalizedName, String recipe)
    {
        this(unlocalizedName, CraftingManager.getRecipe(new ResourceLocation(recipe)));
    }

    @Override
    public void onPageAdded(LexiconEntry entry, int index)
    {
        LexiconRecipeMappings.map(recipe.getRecipeOutput(), entry, index);
    }

    @Override
    @SideOnly(Side.CLIENT)
    public void renderRecipe(IGuiLexiconEntry gui, int mx, int my)
    {
        renderCraftingRecipe(gui, recipe);

        TextureManager render = Minecraft.getMinecraft().renderEngine;
        render.bindTexture(craftingOverlay);

        GlStateManager.enableBlend();
        GlStateManager.blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
        GlStateManager.color(1F, 1F, 1F, 1F);
        ((GuiScreen) gui).drawTexturedModalRect(gui.getLeft(), gui.getTop(), 0, 0, gui.getWidth(), gui.getHeight());

        if(shapelessRecipe)
        {
            int iconX = gui.getLeft() + 115;
            int iconY = gui.getTop() + 12;

            ((GuiScreen) gui).drawTexturedModalRect(iconX, iconY, 240, 0, 16, 16);

            if(mx >= iconX && my >= iconY && mx < iconX + 16 && my < iconY + 16)
                RenderHelper.renderTooltip(mx, my, Collections.singletonList(I18n.format("totemicmisc.shapeless")));
        }

        GlStateManager.disableBlend();
    }

    @Override
    @SideOnly(Side.CLIENT)
    public void updateScreen()
    {
        if(ticksElapsed % 30 == 0)
        {
            recipeIndex++;
        }
        ticksElapsed++;
    }

    @SideOnly(Side.CLIENT)
    public void renderCraftingRecipe(IGuiLexiconEntry gui, IRecipe recipe)
    {
        int x = 0;
        int y = 0;
        for(Ingredient ingredient: recipe.getIngredients())
        {
            if(ingredient != Ingredient.EMPTY)
            {
                ItemStack[] stacks = ingredient.getMatchingStacks();
                renderItemAtGridPos(gui, 1 + x, 1 + y, stacks[recipeIndex % stacks.length], true);
            }

            x++;
            if(x >= recipeWidth)
            {
                x = 0;
                y++;
            }
        }

        renderItemAtGridPos(gui, 2, 0, recipe.getRecipeOutput(), false);
    }
}

@@ -973,79 +973,86 @@
 aft.
-item.crafting.ShapedRecipes;%0Aimport net.minecraft.util.ResourceLocation
+util.ResourceLocation;%0Aimport net.minecraftforge.common.crafting.IShapedRecipe
 ;%0Aim
@@ -1151,59 +1151,8 @@
 ly;%0A
-import net.minecraftforge.oredict.ShapedOreRecipe;%0A
 impo
@@ -1933,24 +1933,25 @@
  instanceof 
+I
 ShapedRecipe
@@ -1950,17 +1950,16 @@
 edRecipe
-s
 )%0A      
@@ -1986,24 +1986,25 @@
 peWidth = ((
+I
 ShapedRecipe
@@ -2003,17 +2003,16 @@
 edRecipe
-s
 ) recipe
@@ -2020,181 +2020,14 @@
 .get
-Width();%0A            shapelessRecipe = false;%0A        %7D%0A        else if(recipe instanceof ShapedOreRecipe)%0A        %7B%0A            recipeWidth = ((ShapedOreRecipe) recipe).get
+Recipe
 Widt
@@ -2660,24 +2660,16 @@
 cipe(gui
-, recipe
 );%0A%0A    
@@ -3746,37 +3746,38 @@
 de.CLIENT)%0A    p
-ublic
+rivate
  void renderCraf
@@ -3807,32 +3807,16 @@
 ntry gui
-, IRecipe recipe
 )%0A    %7B%0A

Fix deprecation warning from shaped recipes