com.sr2610.steampunkd.handbook.pages.PageRecipe.java Source code

Java tutorial

Introduction

Here is the source code for com.sr2610.steampunkd.handbook.pages.PageRecipe.java

Source

/**
 * This class was created by <SR2610>.  It is distributed as
 * part of the Steampunk'd Mod. Get the Source Code on Github:
 * https://github.com/SR2610/Steampunkd.
 *
 * Steampunk'd 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.sr2610.steampunkd.handbook.pages;

import java.util.List;

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

import com.sr2610.steampunkd.client.TooltipHelper;
import com.sr2610.steampunkd.client.gui.handbook.GuiHandbookEntry;
import com.sr2610.steampunkd.client.gui.handbook.interfaces.IGuiHandbookEntry;
import com.sr2610.steampunkd.handbook.core.HandbookPage;
import com.sr2610.steampunkd.handbook.core.HandbookRecipeMappings;
import com.sr2610.steampunkd.handbook.core.HandbookRecipeMappings.EntryData;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.RenderItem;
import net.minecraft.client.resources.I18n;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class PageRecipe extends HandbookPage {

    int relativeMouseX, relativeMouseY;
    ItemStack tooltipStack, tooltipContainerStack;
    boolean tooltipEntry;

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

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

        renderRecipe(gui, mx, my);

        final int width = gui.getWidth() - 30;
        final int height = gui.getHeight();
        final int x = gui.getLeft() + 16;
        final int y = (gui.getTop() + height) - 80;
        PageText.renderText(x, y + 10, width, height, getUnlocalizedName());

        if (tooltipStack != null) {
            final List<String> tooltipData = tooltipStack.getTooltip(Minecraft.getMinecraft().thePlayer, false);

            TooltipHelper.renderTooltip(mx, my, tooltipData);

            if (tooltipEntry)
                tooltipData.add(I18n.format("steampunkd.handbook.clickToRecipe"));

            if (tooltipContainerStack != null)
                tooltipData.add(I18n.format("steampunkd.handbook.craftingContainer"));

            TooltipHelper.renderTooltip(mx, my, tooltipData);

        }

        tooltipStack = tooltipContainerStack = null;
        tooltipEntry = false;
        GL11.glDisable(GL11.GL_BLEND);
    }

    @SideOnly(Side.CLIENT)
    public void renderRecipe(IGuiHandbookEntry gui, int mx, int my) {
    }

    @SideOnly(Side.CLIENT)
    public void renderItemAtGridPos(IGuiHandbookEntry gui, int x, int y, 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 = (gui.getLeft() + (x * 25)) - 1;
        int yPos = ((((gui.getTop() + (y * 26)) - (y * 2)) + 4) - y - (y == 3 ? 0 : 2) - (y == 1 ? 1 : 0)) + 10;

        if ((x == 2) && (y == 0)) {
            xPos = gui.getLeft() + ((x + 2) * 25) + 3;
            yPos = ((gui.getTop() + ((y + 2) * 26)) - 4) + 10;
        }
        final ItemStack stack1 = stack.copy();
        if (stack1.getItemDamage() == -1)
            stack1.setItemDamage(0);

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

    @SideOnly(Side.CLIENT)
    public void renderItem(IGuiHandbookEntry gui, int xPos, int yPos, ItemStack stack,
            boolean accountForContainer) {
        yPos += 10;
        final RenderItem render = Minecraft.getMinecraft().getRenderItem();
        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(stack, xPos, yPos);
        RenderHelper.disableStandardItemLighting();
        GL11.glPopMatrix();

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

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

                if (Mouse.isButtonDown(0)) {
                    final GuiHandbookEntry newGui = new GuiHandbookEntry(data.entry, (GuiScreen) gui);
                    newGui.page = data.page;
                    Minecraft.getMinecraft().displayGuiScreen(newGui);
                }
            }

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

        GL11.glDisable(GL11.GL_LIGHTING);
    }

}