com.professorvennie.machinerycraft.client.gui.pages.PageCraftingRecipe.java Source code

Java tutorial

Introduction

Here is the source code for com.professorvennie.machinerycraft.client.gui.pages.PageCraftingRecipe.java

Source

/**
 * This class was created by <Vazkii>. It's distributed as
 * part of the Machinery Craft Mod. Get the Source Code in github:
 * https://github.com/Professorvennie/MachineryCraft
 *
 * Machinery Craft 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)
 * */
package com.professorvennie.machinerycraft.client.gui.pages;

import com.professorvennie.machinerycraft.api.book.BookEntry;
import com.professorvennie.machinerycraft.api.book.IGuiBookEntry;
import com.professorvennie.machinerycraft.core.helpers.RenderHelper;
import com.professorvennie.machinerycraft.lib.Reference;
import cpw.mods.fml.relauncher.ReflectionHelper;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.ShapedRecipes;
import net.minecraft.item.crafting.ShapelessRecipes;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StatCollector;
import net.minecraftforge.oredict.ShapedOreRecipe;
import net.minecraftforge.oredict.ShapelessOreRecipe;
import org.lwjgl.opengl.GL11;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PageCraftingRecipe extends PageRecipe {

    private static final ResourceLocation craftingOverlay = new ResourceLocation(Reference.MOD_ID,
            "textures/gui/craftingOverlay.png");

    List<IRecipe> recipes;
    int ticksElapsed = 0;
    int recipeAt = 0;

    boolean oreDictRecipe, shapelessRecipe;

    public PageCraftingRecipe(String unlocalizedName, List<IRecipe> recipes) {
        super(unlocalizedName);
        this.recipes = recipes;
    }

    public PageCraftingRecipe(String unlocalizedName, IRecipe recipe) {
        this(unlocalizedName, Arrays.asList(recipe));
    }

    @Override
    public void onPageAdded(BookEntry entry, int index) {
        for (IRecipe recipe : recipes)
            BookRecipeMappings.map(recipe.getRecipeOutput(), entry, index);
    }

    @Override
    @SideOnly(Side.CLIENT)
    public void renderRecipe(IGuiBookEntry gui, int mx, int my) {
        oreDictRecipe = shapelessRecipe = false;

        IRecipe recipe = recipes.get(recipeAt);
        renderCraftingRecipe(gui, recipe);

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

        GL11.glEnable(GL11.GL_BLEND);
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
        GL11.glColor4f(1F, 1F, 1F, 1F);
        ((GuiScreen) gui).drawTexturedModalRect(gui.getLeft(), gui.getTop(), 0, 0, gui.getWidth(), gui.getHeight());

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

        GL11.glEnable(GL11.GL_BLEND);
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

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

            if (mx >= iconX && my >= iconY && mx < iconX + 16 && my < iconY + 16)
                RenderHelper.renderTooltip(mx, my,
                        Arrays.asList(StatCollector.translateToLocal("mc.book.shapeless")));

            iconY += 20;
        }

        render.bindTexture(craftingOverlay);
        GL11.glEnable(GL11.GL_BLEND);

        if (oreDictRecipe) {
            ((GuiScreen) gui).drawTexturedModalRect(iconX, iconY, 240, 16, 16, 16);

            if (mx >= iconX && my >= iconY && mx < iconX + 16 && my < iconY + 16)
                RenderHelper.renderTooltip(mx, my,
                        Arrays.asList(StatCollector.translateToLocal("mc.book.oredict")));
        }
        GL11.glDisable(GL11.GL_BLEND);
    }

    @Override
    @SideOnly(Side.CLIENT)
    public void updateScreen() {
        if (ticksElapsed % 20 == 0) {
            recipeAt++;

            if (recipeAt == recipes.size())
                recipeAt = 0;
        }
        ticksElapsed++;
    }

    @SideOnly(Side.CLIENT)
    public void renderCraftingRecipe(IGuiBookEntry gui, IRecipe recipe) {
        if (recipe instanceof ShapedRecipes) {
            ShapedRecipes shaped = (ShapedRecipes) recipe;

            for (int y = 0; y < shaped.recipeHeight; y++)
                for (int x = 0; x < shaped.recipeWidth; x++)
                    renderItemAtGridPos(gui, 1 + x, 1 + y, shaped.recipeItems[y * shaped.recipeWidth + x], true);
        } else if (recipe instanceof ShapedOreRecipe) {
            ShapedOreRecipe shaped = (ShapedOreRecipe) recipe;
            int width = ReflectionHelper.getPrivateValue(ShapedOreRecipe.class, shaped, 4);
            int height = ReflectionHelper.getPrivateValue(ShapedOreRecipe.class, shaped, 5);

            for (int y = 0; y < height; y++)
                for (int x = 0; x < width; x++) {
                    Object input = shaped.getInput()[y * width + x];
                    if (input != null)
                        renderItemAtGridPos(gui, 1 + x, 1 + y, input instanceof ItemStack ? (ItemStack) input
                                : ((ArrayList<ItemStack>) input).get(0), true);
                }

            oreDictRecipe = true;
        } else if (recipe instanceof ShapelessRecipes) {
            ShapelessRecipes shapeless = (ShapelessRecipes) recipe;

            drawGrid: {
                for (int y = 0; y < 3; y++)
                    for (int x = 0; x < 3; x++) {
                        int index = y * 3 + x;

                        if (index >= shapeless.recipeItems.size())
                            break drawGrid;

                        renderItemAtGridPos(gui, 1 + x, 1 + y, (ItemStack) shapeless.recipeItems.get(index), true);
                    }
            }

            shapelessRecipe = true;
        } else if (recipe instanceof ShapelessOreRecipe) {
            ShapelessOreRecipe shapeless = (ShapelessOreRecipe) recipe;

            drawGrid: {
                for (int y = 0; y < 3; y++)
                    for (int x = 0; x < 3; x++) {
                        int index = y * 3 + x;

                        if (index >= shapeless.getRecipeSize())
                            break drawGrid;

                        Object input = shapeless.getInput().get(index);
                        if (input != null)
                            renderItemAtGridPos(gui, 1 + x, 1 + y, input instanceof ItemStack ? (ItemStack) input
                                    : ((ArrayList<ItemStack>) input).get(0), true);
                    }
            }

            shapelessRecipe = true;
            oreDictRecipe = true;
        }

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

}