dracocore.handbook.pages.PageRecipe.java Source code

Java tutorial

Introduction

Here is the source code for dracocore.handbook.pages.PageRecipe.java

Source

/**
 * This class was created by <Vazkii>. It's distributed as
 * part of the Botania 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 @ [Feb 8, 2014, 2:46:36 PM (GMT)]
 */
package dracocore.handbook.pages;

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

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.entity.RenderItem;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.StatCollector;

import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import dracocore.handbook.core.LexiconPage;
import dracocore.handbook.core.LexiconRecipeMappings;
import dracocore.handbook.gui.GuiLexiconEntry;
import dracocore.handbook.gui.IGuiLexiconEntry;
import dracocore.handbook.helpers.VazkiiRenderHelper;

public class PageRecipe extends LexiconPage {

    static boolean mouseDownLastTick = false;
    int relativeMouseX, relativeMouseY;
    ItemStack tooltipStack, tooltipContainerStack;
    boolean tooltipEntry;

    public PageRecipe(String unlocalizedName) {
        super(unlocalizedName);
    }

    @Override
    @SideOnly(Side.CLIENT)
    public void renderScreen(IGuiLexiconEntry gui, int mx, int my) {
        relativeMouseX = mx;
        relativeMouseY = my;

        renderRecipe(gui, mx, my);

        int width = gui.getWidth() - 30;
        int height = gui.getHeight();
        int x = gui.getLeft() + 16;
        int y = gui.getTop() + height - 40;
        if (!StatCollector.translateToLocal(getUnlocalizedName()).equals(getUnlocalizedName())) {
            PageText.renderText(x, y, width, height, getUnlocalizedName());
        }

        if (tooltipStack != null) {
            List<String> tooltipData = tooltipStack.getTooltip(Minecraft.getMinecraft().thePlayer, false);
            List<String> parsedTooltip = new ArrayList();
            boolean first = true;

            for (String s : tooltipData) {
                String s_ = s;
                if (!first)
                    s_ = EnumChatFormatting.GRAY + s;
                parsedTooltip.add(s_);
                first = false;
            }

            VazkiiRenderHelper.renderTooltip(mx, my, parsedTooltip);

            int tooltipY = 8 + tooltipData.size() * 11;

            if (tooltipEntry) {
                VazkiiRenderHelper.renderTooltipOrange(mx, my + tooltipY, Arrays.asList(
                        EnumChatFormatting.GRAY + StatCollector.translateToLocal("dracocoremisc.clickToRecipe")));
                tooltipY += 18;
            }

            if (tooltipContainerStack != null)
                VazkiiRenderHelper.renderTooltipGreen(mx, my + tooltipY,
                        Arrays.asList(
                                EnumChatFormatting.AQUA
                                        + StatCollector.translateToLocal("dracocoremisc.craftingContainer"),
                                tooltipContainerStack.getDisplayName()));
        }

        tooltipStack = tooltipContainerStack = null;
        tooltipEntry = false;
        GL11.glDisable(GL11.GL_BLEND);
        mouseDownLastTick = Mouse.isButtonDown(0);
    }

    @SideOnly(Side.CLIENT)
    public void renderRecipe(IGuiLexiconEntry gui, int mx, int my) {
        // NO-OP
    }

    @SideOnly(Side.CLIENT)
    public void renderItemAtAngle(IGuiLexiconEntry gui, int angle, ItemStack stack) {
        if (stack == null || stack.getItem() == null)
            return;

        ItemStack workStack = stack.copy();

        if (workStack.getItemDamage() == Short.MAX_VALUE || workStack.getItemDamage() == -1)
            workStack.setItemDamage(0);

        angle -= 90;
        int radius = 32;
        double xPos = gui.getLeft() + Math.cos(angle * Math.PI / 180D) * radius + gui.getWidth() / 2 - 8;
        double yPos = gui.getTop() + Math.sin(angle * Math.PI / 180D) * radius + 53;

        renderItem(gui, (int) xPos, (int) yPos, workStack, false);
    }

    @SideOnly(Side.CLIENT)
    public void renderItemAtGridPos(IGuiLexiconEntry gui, double x, double d, ItemStack stack,
            boolean accountForContainer) {
        if (stack == null || stack.getItem() == null)
            return;
        stack = stack.copy();

        if (stack.getItemDamage() == Short.MAX_VALUE)
            stack.setItemDamage(0);

        int xPos = (int) (gui.getLeft() + x * 29 + 7 + (d == 0 && x == 3 ? 10 : 0));
        int yPos = (int) (gui.getTop() + d * 29 + 24 - (d == 0 ? 7 : 0));
        ItemStack stack1 = stack.copy();
        if (stack1.getItemDamage() == -1)
            stack1.setItemDamage(0);

        renderItem(gui, xPos, yPos, stack1, accountForContainer);
    }

    @SideOnly(Side.CLIENT)
    public void renderItem(IGuiLexiconEntry gui, int xPos, int yPos, ItemStack stack, boolean accountForContainer) {
        RenderItem render = new RenderItem();
        boolean mouseDown = Mouse.isButtonDown(0);

        GL11.glPushMatrix();
        GL11.glEnable(GL11.GL_BLEND);
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
        RenderHelper.enableGUIStandardItemLighting();
        GL11.glEnable(GL12.GL_RESCALE_NORMAL);
        GL11.glEnable(GL11.GL_DEPTH_TEST);
        render.renderItemAndEffectIntoGUI(Minecraft.getMinecraft().fontRenderer,
                Minecraft.getMinecraft().getTextureManager(), stack, xPos, yPos);
        render.renderItemOverlayIntoGUI(Minecraft.getMinecraft().fontRenderer,
                Minecraft.getMinecraft().getTextureManager(), stack, xPos, yPos);
        RenderHelper.disableStandardItemLighting();
        GL11.glPopMatrix();

        if (relativeMouseX >= xPos && relativeMouseY >= yPos && relativeMouseX <= xPos + 16
                && relativeMouseY <= yPos + 16) {
            tooltipStack = stack;

            LexiconRecipeMappings.EntryData data = LexiconRecipeMappings.getDataForStack(tooltipStack);
            if (data != null && (data.entry != gui.getEntry() || data.page != gui.getPageOn())) {
                tooltipEntry = true;

                if (!mouseDownLastTick && mouseDown && GuiScreen.isShiftKeyDown()) {
                    GuiLexiconEntry newGui = new GuiLexiconEntry(data.entry, (GuiScreen) gui);
                    newGui.page = data.page;
                    Minecraft.getMinecraft().displayGuiScreen(newGui);
                }
            } else
                tooltipEntry = false;

            if (accountForContainer) {
                ItemStack containerStack = stack.getItem().getContainerItem(stack);
                if (containerStack != null && containerStack.getItem() != null)
                    tooltipContainerStack = containerStack;
            }
        }

        GL11.glDisable(GL11.GL_LIGHTING);
    }

}