package openmods.gui.component;

import java.util.List;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
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.IIcon;
import net.minecraft.util.ResourceLocation;

import org.lwjgl.opengl.GL11;

import com.google.common.base.Objects;
import com.google.common.collect.Lists;

public class GuiComponentCraftingGrid extends GuiComponentSprite {
	protected static RenderItem itemRenderer = new RenderItem();
	private ItemStack[] items;

	public GuiComponentCraftingGrid(int x, int y, ItemStack[] items, IIcon icon, ResourceLocation texture) {
		super(x, y, icon, texture);
		this.items = items;
	}

	@Override
	public void renderOverlay(Minecraft minecraft, int offsetX, int offsetY, int mouseX, int mouseY) {
		super.renderOverlay(minecraft, offsetX, offsetY, mouseX, mouseY);

		int relativeMouseX = mouseX + offsetX - x;
		int relativeMouseY = mouseY + offsetY - y;
		int gridOffsetX = 1;
		int gridOffsetY = 1;
		int itemBoxSize = 19;

		ItemStack tooltip = null;
		int i = 0;
		for (ItemStack input : items) {
			if (input != null) {
				int row = (i % 3);
				int column = i / 3;
				int itemX = offsetX + gridOffsetX + (row * itemBoxSize);
				int itemY = offsetY + gridOffsetY + (column * itemBoxSize);
				drawItemStack(input, x + itemX, y + itemY, "");
				if (relativeMouseX > itemX - 2 && relativeMouseX < itemX - 2 + itemBoxSize &&
						relativeMouseY > itemY - 2 && relativeMouseY < itemY - 2 + itemBoxSize) {
					tooltip = input;
				}
			}
			i++;
		}
		if (tooltip != null) {
			drawItemStackTooltip(tooltip, relativeMouseX + 25, relativeMouseY + 30);
		}
	}

	protected void drawItemStackTooltip(ItemStack stack, int x, int y) {
		final Minecraft mc = Minecraft.getMinecraft();
		FontRenderer font = Objects.firstNonNull(stack.getItem().getFontRenderer(stack), mc.fontRenderer);

		@SuppressWarnings("unchecked")
		List<String> list = stack.getTooltip(mc.thePlayer, mc.gameSettings.advancedItemTooltips);

		List<String> colored = Lists.newArrayListWithCapacity(list.size());
		colored.add(getRarityColor(stack) + list.get(0));
		for (String line : list)
			colored.add(EnumChatFormatting.GRAY + line);

		drawHoveringText(colored, x, y, font);
	}

	protected EnumChatFormatting getRarityColor(ItemStack stack) {
		return stack.getRarity().rarityColor;
	}

	private void drawItemStack(ItemStack par1ItemStack, int par2, int par3, String par4Str)
	{
		GL11.glTranslatef(0.0F, 0.0F, 32.0F);
		this.zLevel = 200.0F;
		itemRenderer.zLevel = 200.0F;
		RenderHelper.enableGUIStandardItemLighting();
		GL11.glColor3f(1f, 1f, 1f);
		GL11.glEnable(GL11.GL_NORMALIZE);
		FontRenderer font = null;
		if (par1ItemStack != null) font = par1ItemStack.getItem().getFontRenderer(par1ItemStack);
		if (font == null) font = Minecraft.getMinecraft().fontRenderer;
		itemRenderer.renderItemAndEffectIntoGUI(font, Minecraft.getMinecraft().getTextureManager(), par1ItemStack, par2, par3);
		itemRenderer.renderItemOverlayIntoGUI(font, Minecraft.getMinecraft().getTextureManager(), par1ItemStack, par2, par3, par4Str);
		this.zLevel = 0.0F;
		itemRenderer.zLevel = 0.0F;
	}
}

@@ -22,24 +22,51 @@
 component;%0A%0A
+import java.util.Iterator;%0A
 import java.
@@ -1230,18 +1230,24 @@
 = null;%0A
+%0A
 %09%09
+for (
 int i = 
@@ -1248,24 +1248,45 @@
 t i = 0;
-%0A%09%09for (
+ i %3C items.length; i++) %7B%0A%09%09%09
 ItemStac
@@ -1297,18 +1297,19 @@
 put 
-:
+=
  items
-) %7B
+%5Bi%5D;
 %0A%09%09%09
@@ -1752,16 +1752,8 @@
 %09%09%7D%0A
-%09%09%09i++;%0A
 %09%09%7D%0A
@@ -2082,16 +2082,79 @@
 erer);%0A%0A
+%09%09GL11.glColor3f(1, 1, 1);%0A%09%09GL11.glDisable(GL11.GL_LIGHTING);%0A
 %09%09@Suppr
@@ -2341,16 +2341,57 @@
 ize());%0A
+%09%09Iterator%3CString%3E it = list.iterator();%0A
 %09%09colore
@@ -2424,47 +2424,42 @@
 ) + 
-list.ge
+it.nex
 t(
-0
 ));%0A
+%0A
 %09%09
-for (String line : list
+while (it.hasNext()
 )%0A%09%09
@@ -2501,12 +2501,17 @@
 Y + 
-line
+it.next()
 );%0A%0A
@@ -2558,23 +2558,28 @@
 %0A%09%7D%0A%0A%09pr
-otected
+ivate static
  EnumCha
@@ -2706,25 +2706,17 @@
 emStack 
-par1ItemS
+s
 tack, in
@@ -2721,22 +2721,16 @@
 int 
-par2
+x
 , int 
-par3
+y
 , St
@@ -2734,23 +2734,27 @@
  String 
-par4Str
+overlayText
 )%0A%09%7B%0A%09%09G
@@ -2994,25 +2994,17 @@
 ;%0A%09%09if (
-par1ItemS
+s
 tack != 
@@ -3016,25 +3016,17 @@
  font = 
-par1ItemS
+s
 tack.get
@@ -3048,25 +3048,17 @@
 enderer(
-par1ItemS
+s
 tack);%0A%09
@@ -3220,33 +3220,19 @@
 (), 
-par1ItemStack, par2, par3
+stack, x, y
 );%0A%09
@@ -3326,42 +3326,32 @@
 (), 
-par1ItemStack, par2, par3, par4Str
+stack, x, y, overlayText
 );%0A%09

Fix recipe rendering issues