package net.mcft.copy.core.client.renderer;

import net.mcft.copy.core.base.TileEntityBase;
import net.mcft.copy.core.util.StackUtils;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.client.IItemRenderer;

import org.lwjgl.opengl.GL11;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

@SideOnly(Side.CLIENT)
public class ItemRendererTileEntity implements IItemRenderer {
	
	private final TileEntity tileEntity;
	private final TileEntitySpecialRenderer renderer;
	
	private float scale = 1;
	private float offset = 0;
	private float rotation = 0;
	
	public ItemRendererTileEntity(Class<? extends TileEntity> tileEntityClass,
	                              TileEntitySpecialRenderer renderer) {
		try { tileEntity = tileEntityClass.newInstance(); }
		catch (Exception e) { throw new RuntimeException(); }
		this.renderer = renderer;
	}
	
	public ItemRendererTileEntity setScale(float scale) {
		this.scale = scale;
		return this;
	}
	public ItemRendererTileEntity setOffset(float offset) {
		this.offset = offset;
		return this;
	}
	public ItemRendererTileEntity setRotation(float rotation) {
		this.rotation = rotation;
		return this;
	}
	
	// IItemRenderer implementation
	
	@Override
	public boolean handleRenderType(ItemStack item, ItemRenderType type) { return true; }
	
	@Override
	public boolean shouldUseRenderHelper(ItemRenderType type, ItemStack item,
	                                     ItemRendererHelper helper) { return true; }
	
	@Override
	public void renderItem(ItemRenderType type, ItemStack item, Object... data) {
		
		tileEntity.blockType = Block.getBlockFromItem(item.getItem());
		tileEntity.blockMetadata = StackUtils.getDamage(item);
		
		if (tileEntity instanceof TileEntityBase)
			((TileEntityBase)tileEntity).onRenderAsItem(item);
		
		GL11.glPushMatrix();
		
		if ((type != ItemRenderType.EQUIPPED) &&
		    (type != ItemRenderType.EQUIPPED_FIRST_PERSON))
			GL11.glTranslatef(0, -0.5F, 0);
		
		if (type == ItemRenderType.INVENTORY) {
			GL11.glTranslatef(0, offset, 0);
			GL11.glRotatef(rotation, 0, -1, 0);
		}
		
		if (scale != 1.0F)
			GL11.glScalef(scale, scale, scale);
		
		renderer.renderTileEntityAt(tileEntity, -0.5, 0, -0.5, 1);
		
		GL11.glPopMatrix();
		
	}
	
}

@@ -396,16 +396,46 @@
 gl.GL11;
+%0Aimport org.lwjgl.opengl.GL12;
 %0A%0Aimport
@@ -750,25 +750,114 @@
 ivate float 
-r
+inventoryRotation = 0;%0A%09private float firstPersonRotation = 0;%0A%09private float thirdPersonR
 otation = 0;
@@ -1375,16 +1375,25 @@
 tity set
+Inventory
 Rotation
@@ -1418,17 +1418,284 @@
 %0A%09%09this.
-r
+inventoryRotation = rotation;%0A%09%09return this;%0A%09%7D%0A%09public ItemRendererTileEntity setFirstPersonRotation(float rotation) %7B%0A%09%09this.firstPersonRotation = rotation;%0A%09%09return this;%0A%09%7D%0A%09public ItemRendererTileEntity setThirdPersonRotation(float rotation) %7B%0A%09%09this.thirdPersonR
 otation 
@@ -2382,19 +2382,45 @@
 %09%0A%09%09
-if (
+boolean equippedFirstPerson = 
 (type 
-!
+=
 = It
@@ -2444,26 +2444,53 @@
 PPED
-) &&%0A%09%09    
+_FIRST_PERSON);%0A%09%09boolean equipped = (
 (type 
-!
+=
 = It
@@ -2514,104 +2514,351 @@
 PPED
-_FIRST_PERSON))%0A%09%09%09GL11.glTranslatef(0, -0.5F, 0);%0A%09%09%0A%09%09if (type == ItemRenderType.INVENTORY
+) %7C%7C equippedFirstPerson);%0A%09%09boolean inventory = (type == ItemRenderType.INVENTORY);%0A%09%09%0A%09%09if (equipped) %7B%0A%09%09%09GL11.glTranslatef(0.5F, 0, 0.5F);%0A%09%09%09float rotation = (equippedFirstPerson ? firstPersonRotation : (thirdPersonRotation - 90));%0A%09%09%09GL11.glRotatef(rotation, 0, -1, 0);%0A%09%09%7D else %7B%0A%09%09%09GL11.glTranslatef(0, -0.5F, 0);%0A%09%09%09if (inventory
 ) %7B%0A
+%09
 %09%09%09G
@@ -2888,24 +2888,25 @@
 set, 0);%0A%09%09%09
+%09
 GL11.glRotat
@@ -2900,33 +2900,42 @@
 %09GL11.glRotatef(
-r
+inventoryR
 otation, 0, -1, 
@@ -2938,16 +2938,21 @@
 -1, 0);%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%09%0A%09
@@ -3095,22 +3095,63 @@
 opMatrix();%0A
+%09%09GL11.glEnable(GL12.GL_RESCALE_NORMAL);%0A
 %09%09%0A%09%7D%0A%09%0A%7D%0A

Update ItemRendererTileEntity