package openperipheral.addons.glasses.drawable;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.entity.RenderItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import openperipheral.api.adapter.AdapterSourceName;
import openperipheral.api.adapter.CallbackProperty;
import openperipheral.api.adapter.method.ScriptObject;

import org.lwjgl.opengl.GL11;

import com.google.common.base.Strings;

import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

@ScriptObject
@AdapterSourceName("glasses_icon")
public class ItemIcon extends Drawable {
	@SideOnly(Side.CLIENT)
	private RenderItem renderItem;

	private ItemStack drawStack;

	@CallbackProperty
	public float scale = 1;

	@CallbackProperty
	public String itemId;

	@CallbackProperty
	public int meta;

	ItemIcon() {}

	public ItemIcon(short x, short y, String itemId, int meta) {
		super(x, y);
		this.itemId = itemId;
		this.meta = meta;
	}

	@SideOnly(Side.CLIENT)
	private RenderItem getRenderItem() {
		if (renderItem == null) renderItem = new RenderItem();
		return renderItem;
	}

	@Override
	@SideOnly(Side.CLIENT)
	protected void drawContents(float partialTicks) {
		if (drawStack != null) {
			GL11.glEnable(GL11.GL_DEPTH_TEST);
			GL11.glEnable(GL11.GL_TEXTURE_2D);
			GL11.glScalef(scale, scale, scale);
			getRenderItem().renderItemAndEffectIntoGUI(null, Minecraft.getMinecraft().getTextureManager(), drawStack, 0, 0);
			Drawable.setupFlatRenderState();
		}
	}

	@Override
	public Type getTypeEnum() {
		return Type.ITEM;
	}

	@Override
	public int getWidth() {
		return Math.round(16 * scale);
	}

	@Override
	public int getHeight() {
		return Math.round(16 * scale);
	}

	@Override
	public boolean isVisible() {
		return true;
	}

	@Override
	protected void onUpdate() {
		drawStack = findDrawStack(itemId, meta);
	}

	private static ItemStack findDrawStack(String itemId, int meta) {
		if (Strings.isNullOrEmpty(itemId)) return null;
		String[] itemSplit = itemId.split(":");
		if (itemSplit.length != 2) return null;

		Item item = GameRegistry.findItem(itemSplit[0], itemSplit[1]);
		if (item == null) return null;

		return new ItemStack(item, 1, meta);
	}

}
@@ -133,24 +133,118 @@
 RenderItem;%0A
+import net.minecraft.client.renderer.texture.TextureManager;%0Aimport net.minecraft.init.Items;%0A
 import net.m
@@ -259,24 +259,24 @@
 .item.Item;%0A
-
 import net.m
@@ -751,16 +751,48 @@
 wable %7B%0A
+%09private ItemStack dummyStack;%0A%0A
 %09@SideOn
@@ -997,16 +997,103 @@
  meta;%0A%0A
+%09@CallbackProperty%0A%09public float damageBar;%0A%0A%09@CallbackProperty%0A%09public String label;%0A%0A
 %09ItemIco
@@ -1604,86 +1604,407 @@
 %0A%09%09%09
-getRenderItem().renderItemAndEffectIntoGUI(null, Minecraft.getMinecraft().getT
+final RenderItem renderItem = getRenderItem();%0A%09%09%09final Minecraft minecraft = Minecraft.getMinecraft();%0A%09%09%09final TextureManager textureManager = minecraft.getTextureManager();%0A%0A%09%09%09renderItem.renderItemAndEffectIntoGUI(minecraft.fontRenderer, textureManager, drawStack, 0, 0);%0A%0A%09%09%09if (damageBar %3E 0 %7C%7C !Strings.isNullOrEmpty(label)) %7B%0A%09%09%09%09renderItem.renderItemOverlayIntoGUI(minecraft.fontRenderer, t
 extu
@@ -2016,16 +2016,15 @@
 ager
-()
 , d
-raw
+ummy
 Stac
@@ -2030,18 +2030,31 @@
 ck, 0, 0
-);
+, label);%0A%09%09%09%7D%0A
 %0A%09%09%09Draw
@@ -2403,16 +2403,21 @@
 e() %7B%0A%09%09
+this.
 drawStac
@@ -2444,24 +2444,158 @@
 emId, meta);
+%0A%0A%09%09int damage = (int)(damageBar * Items.diamond_hoe.getMaxDamage());%0A%09%09this.dummyStack = new ItemStack(Items.diamond_hoe, 1, damage);
 %0A%09%7D%0A%0A%09privat

Add option to control damage bar and item counter in drawables