package openblocks.client.renderer.tileentity;

import net.minecraft.client.renderer.entity.Render;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.entity.Entity;
import net.minecraft.tileentity.TileEntity;
import openblocks.common.TrophyHandler.Trophy;
import openblocks.common.tileentity.TileEntityTrophy;
import openmods.utils.BlockUtils;
import openmods.utils.render.RenderUtils;

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

public class TileEntityTrophyRenderer extends TileEntitySpecialRenderer {

	public static boolean renderWithLighting = true;

	@Override
	public void renderTileEntityAt(TileEntity tileentity, double d0, double d1, double d2, float f) {
		TileEntityTrophy trophy = (TileEntityTrophy)tileentity;

		Trophy type = trophy.getTrophy();
		if (type != null) {
			Entity entity = type.getEntity();
			if (entity != null) {
				double ratio = type.getScale();
				GL11.glPushMatrix();
				GL11.glTranslated(d0, d1, d2);
				GL11.glTranslated(0.5, 0.2 + type.getVerticalOffset(), 0.5);
				GL11.glRotatef(BlockUtils.getRotationFromDirection(trophy.getRotation()), 0, 1, 0);

				GL11.glScaled(ratio, ratio, ratio);
				entity.worldObj = RenderUtils.getRenderWorld();
				if (entity.worldObj != null) {
					Render renderer = RenderManager.instance.getEntityRenderObject(entity);
					// yeah we don't care about fonts, but we do care that the
					// renderManager is available
					if (renderer != null && renderer.getFontRendererFromRenderManager() != null) {
						if (renderWithLighting) {
							GL11.glDisable(GL12.GL_RESCALE_NORMAL);
							RenderUtils.enableLightmap();
						}

						renderer.doRender(entity, 0, 0, 0, 0, 0);
					}
				}
				entity.worldObj = null;

				/*
				 * Fix render issues caused by rendering an entity. Yes that was
				 * a pain to fix :P
				 * - NC
				 */
				if (renderWithLighting) {
					GL11.glEnable(GL12.GL_RESCALE_NORMAL);
					RenderUtils.disableLightmap();
				}

				/* <EOF> End of Fix */
				bindTexture(TextureMap.locationBlocksTexture);
				GL11.glPopMatrix();

			}
		}
	}

}

@@ -156,65 +156,8 @@
 er;%0A
-import net.minecraft.client.renderer.texture.TextureMap;%0A
 impo
@@ -518,38 +518,8 @@
 L11;
-%0Aimport org.lwjgl.opengl.GL12;
 %0A%0Apu
@@ -1578,88 +1578,82 @@
 %09%09%09%09
-if (renderWithLighting) %7B%0A%09%09%09%09%09%09%09GL11.glDisable
+GL11.glPushAttrib
 (GL1
-2
+1
 .GL_
-RESCALE_NORMAL);%0A%09%09%09%09%09%09%09
+ALL_ATTRIB_BITS);%0A%09%09%09%09%09%09if (renderWithLighting) 
 Rend
@@ -1682,17 +1682,8 @@
 ();%0A
-%09%09%09%09%09%09%7D%0A%0A
 %09%09%09%09
@@ -1727,24 +1727,50 @@
 0, 0);%0A%09%09%09%09%09
+%09GL11.glPopAttrib();%0A%09%09%09%09%09
 %7D%0A%09%09%09%09%7D%0A%09%09%09%09
@@ -1795,324 +1795,8 @@
 null
-;%0A%0A%09%09%09%09/*%0A%09%09%09%09 * Fix render issues caused by rendering an entity. Yes that was%0A%09%09%09%09 * a pain to fix :P%0A%09%09%09%09 * - NC%0A%09%09%09%09 */%0A%09%09%09%09if (renderWithLighting) %7B%0A%09%09%09%09%09GL11.glEnable(GL12.GL_RESCALE_NORMAL);%0A%09%09%09%09%09RenderUtils.disableLightmap();%0A%09%09%09%09%7D%0A%0A%09%09%09%09/* %3CEOF%3E End of Fix */%0A%09%09%09%09bindTexture(TextureMap.locationBlocksTexture)
 ;%0A%09%09

Fix some status leaking on trophy rendering