package client.renderer;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;

import client.Client;
import client.renderer.entity.RenderItem;
import client.renderer.entity.RenderManager;
import client.renderer.entity.RenderNpc;
import client.renderer.texture.EntityTexManager;
import client.renderer.texture.Sprite;
import client.renderer.texture.TextureMap;
import common.block.Block;
import common.entity.npc.EntityNPC;
import common.entity.types.EntityLiving;
import common.init.Blocks;
import common.item.Item;
import common.item.ItemAction;
import common.item.ItemStack;
import common.util.BlockPos;
import common.util.ExtMath;
import common.util.Vec3;
import common.world.State;


public class ItemRenderer
{
	private static final FloatBuffer BUFFER = ByteBuffer.allocateDirect(16 << 2).order(ByteOrder.nativeOrder()).asFloatBuffer();
	private static final Vec3 LIGHT0_POS = (new Vec3(0.20000000298023224D, 1.0D, -0.699999988079071D)).normalize();
	private static final Vec3 LIGHT1_POS = (new Vec3(-0.20000000298023224D, 1.0D, 0.699999988079071D)).normalize();
	
    private final Client gm;
    private final RenderManager renderManager;
    private final RenderItem itemRenderer;
    
    private ItemStack itemToRender;
    private float equippedProgress;
    private float prevEquippedProgress;

    private static FloatBuffer setColorBuffer(float r, float g, float b, float a)
	{
	    BUFFER.clear();
	    BUFFER.put(r).put(g).put(b).put(a);
	    BUFFER.flip();
	    return BUFFER;
	}

	public static void enableGUIStandardItemLighting()
	{
	    GL11.glPushMatrix();
	    GL11.glRotatef(-30.0F, 0.0F, 1.0F, 0.0F);
	    GL11.glRotatef(165.0F, 1.0F, 0.0F, 0.0F);
	    enableStandardItemLighting();
	    GL11.glPopMatrix();
	}

	public static void enableStandardItemLighting()
	{
	    GlState.enableLighting();
	    GlState.enableLight(0);
	    GlState.enableLight(1);
	    GlState.enableColorMaterial();
	    GlState.colorMaterial(GL11.GL_FRONT_AND_BACK, GL11.GL_AMBIENT_AND_DIFFUSE);
	    float f = 0.4F;
	    float f1 = 0.6F;
	    float f2 = 0.0F;
	    GL11.glLightfv(GL11.GL_LIGHT0, GL11.GL_POSITION, setColorBuffer(
		(float)LIGHT0_POS.xCoord, (float)LIGHT0_POS.yCoord, (float)LIGHT0_POS.zCoord, 0.0f));
	    GL11.glLightfv(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, setColorBuffer(f1, f1, f1, 1.0F));
	    GL11.glLightfv(GL11.GL_LIGHT0, GL11.GL_AMBIENT, setColorBuffer(0.0F, 0.0F, 0.0F, 1.0F));
	    GL11.glLightfv(GL11.GL_LIGHT0, GL11.GL_SPECULAR, setColorBuffer(f2, f2, f2, 1.0F));
	    GL11.glLightfv(GL11.GL_LIGHT1, GL11.GL_POSITION, setColorBuffer(
		(float)LIGHT1_POS.xCoord, (float)LIGHT1_POS.yCoord, (float)LIGHT1_POS.zCoord, 0.0f));
	    GL11.glLightfv(GL11.GL_LIGHT1, GL11.GL_DIFFUSE, setColorBuffer(f1, f1, f1, 1.0F));
	    GL11.glLightfv(GL11.GL_LIGHT1, GL11.GL_AMBIENT, setColorBuffer(0.0F, 0.0F, 0.0F, 1.0F));
	    GL11.glLightfv(GL11.GL_LIGHT1, GL11.GL_SPECULAR, setColorBuffer(f2, f2, f2, 1.0F));
	    GlState.shadeModel(GL11.GL_FLAT);
	    GL11.glLightModelfv(GL11.GL_LIGHT_MODEL_AMBIENT, setColorBuffer(f, f, f, 1.0F));
	}

	public static void disableStandardItemLighting()
	{
	    GlState.disableLighting();
	    GlState.disableLight(0);
	    GlState.disableLight(1);
	    GlState.disableColorMaterial();
	}

    public ItemRenderer(Client gmIn)
    {
        this.gm = gmIn;
        this.renderManager = gmIn.getRenderManager();
        this.itemRenderer = gmIn.getRenderItem();
    }

    public void renderItem(EntityLiving entityIn, ItemStack heldStack, boolean third)
    {
        if (heldStack != null)
        {
            Item item = heldStack.getItem();
            Block block = item.getBlock();
            GL11.glPushMatrix();

            if (this.itemRenderer.shouldRenderItemIn3D(heldStack))
            {
                GL11.glScalef(2.0F, 2.0F, 2.0F);
            }

            this.itemRenderer.renderItemForEntity(heldStack, entityIn, third);

            GL11.glPopMatrix();
        }
    }

    private void rotateArroundXAndY(float angle, float angleY)
    {
        GL11.glPushMatrix();
        GL11.glRotatef(angle, 1.0F, 0.0F, 0.0F);
        GL11.glRotatef(angleY, 0.0F, 1.0F, 0.0F);
        ItemRenderer.enableStandardItemLighting();
        GL11.glPopMatrix();
    }

    private void setLightMapFromPlayer(EntityNPC clientPlayer)
    {
        int i = this.gm.world.getCombinedLight(new BlockPos(clientPlayer.posX, clientPlayer.posY + (double)clientPlayer.getEyeHeight(), clientPlayer.posZ), 0);
        float f = (float)(i & 65535);
        float f1 = (float)(i >> 16);
        GL13.glMultiTexCoord2f(GL13.GL_TEXTURE1, f, f1);
    }

    private void rotateWithPlayerRotations(EntityNPC entityplayerspIn, float partialTicks)
    {
        float f = entityplayerspIn.prevRenderArmPitch + (entityplayerspIn.renderArmPitch - entityplayerspIn.prevRenderArmPitch) * partialTicks;
        float f1 = entityplayerspIn.prevRenderArmYaw + (entityplayerspIn.renderArmYaw - entityplayerspIn.prevRenderArmYaw) * partialTicks;
        GL11.glRotatef((entityplayerspIn.rotPitch - f) * 0.1F, 1.0F, 0.0F, 0.0F);
        GL11.glRotatef((entityplayerspIn.rotYaw - f1) * 0.1F, 0.0F, 1.0F, 0.0F);
    }

    private float getMapAngleFromPitch(float pitch)
    {
        float f = 1.0F - pitch / 45.0F + 0.1F;
        f = ExtMath.clampf(f, 0.0F, 1.0F);
        f = -ExtMath.cos(f * (float)Math.PI) * 0.5F + 0.5F;
        return f;
    }

//    private void renderRightArm(RenderPlayer renderPlayerIn)
//    {
//        SKC.glPushMatrix();
//        SKC.glRotatef(54.0F, 0.0F, 1.0F, 0.0F);
//        SKC.glRotatef(64.0F, 1.0F, 0.0F, 0.0F);
//        SKC.glRotatef(-62.0F, 0.0F, 0.0F, 1.0F);
//        SKC.glTranslatef(0.25F, -0.85F, 0.75F);
//        renderPlayerIn.renderRightArm(this.gm.thePlayer);
//        SKC.glPopMatrix();
//    }
//
//    private void renderLeftArm(RenderPlayer renderPlayerIn)
//    {
//        SKC.glPushMatrix();
//        SKC.glRotatef(92.0F, 0.0F, 1.0F, 0.0F);
//        SKC.glRotatef(45.0F, 1.0F, 0.0F, 0.0F);
//        SKC.glRotatef(41.0F, 0.0F, 0.0F, 1.0F);
//        SKC.glTranslatef(-0.3F, -1.1F, 0.45F);
//        renderPlayerIn.renderLeftArm(this.gm.thePlayer);
//        SKC.glPopMatrix();
//    }

//    private void renderPlayerArms(EntityNPCClient clientPlayer)
//    {
//        this.gm.getTextureManager().bindTexture(clientPlayer.getLocationSkin());
//        Render<EntityNPCClient> render = this.renderManager.<EntityNPCClient>getEntityRenderObject(this.gm.thePlayer);
//        RenderPlayer renderplayer = (RenderPlayer)render;
//
//        if (!clientPlayer.isInvisible())
//        {
//            GlState.disableCull();
//            this.renderRightArm(renderplayer);
//            this.renderLeftArm(renderplayer);
//            GlState.enableCull();
//        }
//    }

//    private void renderItemMap(EntityNPCClient clientPlayer, float pitch, float equipmentProgress, float swingProgress)
//    {
//        float f = -0.4F * MathHelper.sin(MathHelper.sqrt_float(swingProgress) * (float)Math.PI);
//        float f1 = 0.2F * MathHelper.sin(MathHelper.sqrt_float(swingProgress) * (float)Math.PI * 2.0F);
//        float f2 = -0.2F * MathHelper.sin(swingProgress * (float)Math.PI);
//        SKC.glTranslatef(f, f1, f2);
//        float f3 = this.getMapAngleFromPitch(pitch);
//        SKC.glTranslatef(0.0F, 0.04F, -0.72F);
//        SKC.glTranslatef(0.0F, equipmentProgress * -1.2F, 0.0F);
//        SKC.glTranslatef(0.0F, f3 * -0.5F, 0.0F);
//        SKC.glRotatef(90.0F, 0.0F, 1.0F, 0.0F);
//        SKC.glRotatef(f3 * -85.0F, 0.0F, 0.0F, 1.0F);
//        SKC.glRotatef(0.0F, 1.0F, 0.0F, 0.0F);
//        this.renderPlayerArms(clientPlayer);
//        float f4 = MathHelper.sin(swingProgress * swingProgress * (float)Math.PI);
//        float f5 = MathHelper.sin(MathHelper.sqrt_float(swingProgress) * (float)Math.PI);
//        SKC.glRotatef(f4 * -20.0F, 0.0F, 1.0F, 0.0F);
//        SKC.glRotatef(f5 * -20.0F, 0.0F, 0.0F, 1.0F);
//        SKC.glRotatef(f5 * -80.0F, 1.0F, 0.0F, 0.0F);
//        SKC.glScalef(0.38F, 0.38F, 0.38F);
//        SKC.glRotatef(90.0F, 0.0F, 1.0F, 0.0F);
//        SKC.glRotatef(180.0F, 0.0F, 0.0F, 1.0F);
//        SKC.glRotatef(0.0F, 1.0F, 0.0F, 0.0F);
//        SKC.glTranslatef(-1.0F, -1.0F, 0.0F);
//        SKC.glScalef(0.015625F, 0.015625F, 0.015625F);
//        this.gm.getTextureManager().bindTexture(RES_MAP_BACKGROUND);
////        Tessellator tessellator = Tessellator.getInstance();
//        RenderBuffer worldrenderer = Tessellator.getBuffer();
//        SKC.glNormal3f(0.0F, 0.0F, -1.0F);
//        worldrenderer.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX);
//        worldrenderer.pos(-7.0D, 135.0D, 0.0D).tex(0.0D, 1.0D).endVertex();
//        worldrenderer.pos(135.0D, 135.0D, 0.0D).tex(1.0D, 1.0D).endVertex();
//        worldrenderer.pos(135.0D, -7.0D, 0.0D).tex(1.0D, 0.0D).endVertex();
//        worldrenderer.pos(-7.0D, -7.0D, 0.0D).tex(0.0D, 0.0D).endVertex();
//        Tessellator.draw();
//        MapData mapdata = Items.filled_map.getMapData(this.itemToRender, this.gm.theWorld);
//
//        if (mapdata != null)
//        {
//            this.gm.entityRenderer.getMapItemRenderer().renderMap(mapdata, false);
//        }
//    }

    private void renderPlayerArm(EntityNPC clientPlayer, float equipProgress, float swingProgress)
    {
        float f = -0.3F * ExtMath.sin(ExtMath.sqrtf(swingProgress) * (float)Math.PI);
        float f1 = 0.4F * ExtMath.sin(ExtMath.sqrtf(swingProgress) * (float)Math.PI * 2.0F);
        float f2 = -0.4F * ExtMath.sin(swingProgress * (float)Math.PI);
        GL11.glTranslatef(f, f1, f2);
        GL11.glTranslatef(0.64000005F, -0.6F, -0.71999997F);
        GL11.glTranslatef(0.0F, equipProgress * -0.6F, 0.0F);
        GL11.glRotatef(45.0F, 0.0F, 1.0F, 0.0F);
        float f3 = ExtMath.sin(swingProgress * swingProgress * (float)Math.PI);
        float f4 = ExtMath.sin(ExtMath.sqrtf(swingProgress) * (float)Math.PI);
        GL11.glRotatef(f4 * 70.0F, 0.0F, 1.0F, 0.0F);
        GL11.glRotatef(f3 * -20.0F, 0.0F, 0.0F, 1.0F);
        this.gm.getTextureManager().bindTexture(EntityTexManager.getSkin(clientPlayer));
        GL11.glTranslatef(-1.0F, 3.6F, 3.5F);
        GL11.glRotatef(120.0F, 0.0F, 0.0F, 1.0F);
        GL11.glRotatef(200.0F, 1.0F, 0.0F, 0.0F);
        GL11.glRotatef(-135.0F, 0.0F, 1.0F, 0.0F);
        GL11.glScalef(1.0F, 1.0F, 1.0F);
        GL11.glTranslatef(5.6F, 0.0F, 0.0F);
        RenderNpc render = this.renderManager.getRenderObject(this.gm.player.getModel());
        GlState.disableCull();
        render.renderPlayerArm(this.gm.player);
        GlState.enableCull();
    }

    private void doItemUsedTransformations(float swingProgress)
    {
        float f = -0.4F * ExtMath.sin(ExtMath.sqrtf(swingProgress) * (float)Math.PI);
        float f1 = 0.2F * ExtMath.sin(ExtMath.sqrtf(swingProgress) * (float)Math.PI * 2.0F);
        float f2 = -0.2F * ExtMath.sin(swingProgress * (float)Math.PI);
        GL11.glTranslatef(f, f1, f2);
    }

    private void performDrinking(EntityNPC clientPlayer, float partialTicks)
    {
        float f = (float)clientPlayer.getItemInUseCount() - partialTicks + 1.0F;
        float f1 = f / (float)this.itemToRender.getMaxItemUseDuration();
        float f2 = ExtMath.absf(ExtMath.cos(f / 4.0F * (float)Math.PI) * 0.1F);

        if (f1 >= 0.8F)
        {
            f2 = 0.0F;
        }

        GL11.glTranslatef(0.0F, f2, 0.0F);
        float f3 = 1.0F - (float)Math.pow((double)f1, 27.0D);
        GL11.glTranslatef(f3 * 0.6F, f3 * -0.5F, f3 * 0.0F);
        GL11.glRotatef(f3 * 90.0F, 0.0F, 1.0F, 0.0F);
        GL11.glRotatef(f3 * 10.0F, 1.0F, 0.0F, 0.0F);
        GL11.glRotatef(f3 * 30.0F, 0.0F, 0.0F, 1.0F);
    }

    private void transformFirstPersonItem(float equipProgress, float swingProgress)
    {
        GL11.glTranslatef(0.56F, -0.52F, -0.71999997F);
        GL11.glTranslatef(0.0F, equipProgress * -0.6F, 0.0F);
        GL11.glRotatef(45.0F, 0.0F, 1.0F, 0.0F);
        float f = ExtMath.sin(swingProgress * swingProgress * (float)Math.PI);
        float f1 = ExtMath.sin(ExtMath.sqrtf(swingProgress) * (float)Math.PI);
        GL11.glRotatef(f * -20.0F, 0.0F, 1.0F, 0.0F);
        GL11.glRotatef(f1 * -20.0F, 0.0F, 0.0F, 1.0F);
        GL11.glRotatef(f1 * -80.0F, 1.0F, 0.0F, 0.0F);
        GL11.glScalef(0.4F, 0.4F, 0.4F);
    }

    private void doBowTransformations(float partialTicks, EntityNPC clientPlayer)
    {
        GL11.glRotatef(-18.0F, 0.0F, 0.0F, 1.0F);
        GL11.glRotatef(-12.0F, 0.0F, 1.0F, 0.0F);
        GL11.glRotatef(-8.0F, 1.0F, 0.0F, 0.0F);
        GL11.glTranslatef(-0.9F, 0.2F, 0.0F);
        float f = (float)this.itemToRender.getMaxItemUseDuration() - ((float)clientPlayer.getItemInUseCount() - partialTicks + 1.0F);
        float f1 = f / 20.0F;
        f1 = (f1 * f1 + f1 * 2.0F) / 3.0F;

        if (f1 > 1.0F)
        {
            f1 = 1.0F;
        }

        if (f1 > 0.1F)
        {
            float f2 = ExtMath.sin((f - 0.1F) * 1.3F);
            float f3 = f1 - 0.1F;
            float f4 = f2 * f3;
            GL11.glTranslatef(f4 * 0.0F, f4 * 0.01F, f4 * 0.0F);
        }

        GL11.glTranslatef(f1 * 0.0F, f1 * 0.0F, f1 * 0.1F);
        GL11.glScalef(1.0F, 1.0F, 1.0F + f1 * 0.2F);
    }

    private void doBlockTransformations()
    {
        GL11.glTranslatef(-0.5F, 0.2F, 0.0F);
        GL11.glRotatef(30.0F, 0.0F, 1.0F, 0.0F);
        GL11.glRotatef(-80.0F, 1.0F, 0.0F, 0.0F);
        GL11.glRotatef(60.0F, 0.0F, 1.0F, 0.0F);
    }

    public void renderItemInFirstPerson(float partialTicks)
    {
        float f = 1.0F - (this.prevEquippedProgress + (this.equippedProgress - this.prevEquippedProgress) * partialTicks);
        EntityNPC clientplayer = this.gm.player;
        float f1 = clientplayer.getSwingProgress(partialTicks);
        float f2 = clientplayer.prevPitch + (clientplayer.rotPitch - clientplayer.prevPitch) * partialTicks;
        float f3 = clientplayer.prevYaw + (clientplayer.rotYaw - clientplayer.prevYaw) * partialTicks;
        this.rotateArroundXAndY(f2, f3);
        this.setLightMapFromPlayer(clientplayer);
        this.rotateWithPlayerRotations(clientplayer, partialTicks);
        GlState.enableRescaleNormal();
        GL11.glPushMatrix();

        if (this.itemToRender != null && (this.itemToRender.getItem().getWieldType() != null || (clientplayer.getItemInUseCount() > 0 && this.itemToRender.getItemUseAction() != ItemAction.NONE)))
        {
            if (clientplayer.getItemInUseCount() > 0)
            {
                ItemAction enumaction = this.itemToRender.getItemUseAction();

                switch (enumaction)
                {
                    case NONE:
                        this.transformFirstPersonItem(f, 0.0F);
                        break;

                    case EAT:
                    case DRINK:
                    	this.performDrinking(clientplayer, partialTicks);
                        this.transformFirstPersonItem(f, 0.0F);
                        break;

                    case BLOCK:
                        this.transformFirstPersonItem(f, 0.0F);
                        this.doBlockTransformations();
                        break;

                    case AIM:
                        this.transformFirstPersonItem(f, 0.0F);
                        this.doBowTransformations(partialTicks, clientplayer);
                }
            }
            else
            {
                this.doItemUsedTransformations(f1);
                this.transformFirstPersonItem(f, f1);
            }

            this.renderItem(clientplayer, this.itemToRender, false);
        }
        else if(this.itemToRender == null || this.itemToRender.getItem().canRenderHand())
        {
            this.renderPlayerArm(clientplayer, f, f1);
        }

        GL11.glPopMatrix();
        GlState.disableRescaleNormal();
        ItemRenderer.disableStandardItemLighting();
    }

    public void renderOverlays(float partialTicks)
    {
        GlState.disableAlpha();

        if (this.gm.player.isEntityInsideOpaqueBlock())
        {
            State iblockstate = this.gm.world.getState(new BlockPos(this.gm.player));
            EntityNPC entityplayer = this.gm.player;

            for (int i = 0; i < 8; ++i)
            {
                double d0 = entityplayer.posX + (double)(((float)((i >> 0) % 2) - 0.5F) * entityplayer.width * 0.8F);
                double d1 = entityplayer.posY + (double)(((float)((i >> 1) % 2) - 0.5F) * 0.1F);
                double d2 = entityplayer.posZ + (double)(((float)((i >> 2) % 2) - 0.5F) * entityplayer.width * 0.8F);
                BlockPos blockpos = new BlockPos(d0, d1 + (double)entityplayer.getEyeHeight(), d2);
                State iblockstate1 = this.gm.world.getState(blockpos);

                if (iblockstate1.getBlock().isVisuallyOpaque())
                {
                    iblockstate = iblockstate1;
                }
            }

            if (iblockstate.getBlock() != Blocks.air)
            {
                this.renderBlockInside(partialTicks, this.gm.renderer.getModelManager().getTexture(iblockstate));
            }
        }

        if(this.gm.player.isBurning()) {
            this.renderFireOverlay(partialTicks);
        }

        GlState.enableAlpha();
    }

    private void renderBlockInside(float partialTicks, Sprite atlas)
    {
        this.gm.getTextureManager().bindTexture(TextureMap.BLOCKS);
        RenderBuffer worldrenderer = Tessellator.getBuffer();
        float f = 0.1F;
        GlState.color(0.1F, 0.1F, 0.1F, 0.5F);
        GL11.glPushMatrix();
        float f1 = -1.0F;
        float f2 = 1.0F;
        float f3 = -1.0F;
        float f4 = 1.0F;
        float f5 = -0.5F;
        float f6 = atlas.getMinU();
        float f7 = atlas.getMaxU();
        float f8 = atlas.getMinV();
        float f9 = atlas.getMaxV();
        worldrenderer.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX);
        worldrenderer.pos(-1.0D, -1.0D, -0.5D).tex((double)f7, (double)f9).endVertex();
        worldrenderer.pos(1.0D, -1.0D, -0.5D).tex((double)f6, (double)f9).endVertex();
        worldrenderer.pos(1.0D, 1.0D, -0.5D).tex((double)f6, (double)f8).endVertex();
        worldrenderer.pos(-1.0D, 1.0D, -0.5D).tex((double)f7, (double)f8).endVertex();
        Tessellator.draw();
        GL11.glPopMatrix();
        GlState.color(1.0F, 1.0F, 1.0F, 1.0F);
    }

    private void renderFireOverlay(float partialTicks)
    {
        RenderBuffer worldrenderer = Tessellator.getBuffer();
        GlState.color(1.0F, 1.0F, 1.0F, 0.9F);
        GlState.depthFunc(GL11.GL_ALWAYS);
        GlState.depthMask(false);
        GlState.enableBlend();
        GlState.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ZERO);
        float f = 1.0F;

        for (int i = 0; i < 2; ++i)
        {
            GL11.glPushMatrix();
            Sprite textureatlassprite = this.gm.getTextureMapBlocks().getAtlasSprite("blocks/fire_layer_1");
            this.gm.getTextureManager().bindTexture(TextureMap.BLOCKS);
            float f1 = textureatlassprite.getMinU();
            float f2 = textureatlassprite.getMaxU();
            float f3 = textureatlassprite.getMinV();
            float f4 = textureatlassprite.getMaxV();
            float f5 = (0.0F - f) / 2.0F;
            float f6 = f5 + f;
            float f7 = 0.0F - f / 2.0F;
            float f8 = f7 + f;
            float f9 = -0.5F;
            GL11.glTranslatef((float)(-(i * 2 - 1)) * 0.24F, -0.3F, 0.0F);
            GL11.glRotatef((float)(i * 2 - 1) * 10.0F, 0.0F, 1.0F, 0.0F);
            worldrenderer.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX);
            worldrenderer.pos((double)f5, (double)f7, (double)f9).tex((double)f2, (double)f4).endVertex();
            worldrenderer.pos((double)f6, (double)f7, (double)f9).tex((double)f1, (double)f4).endVertex();
            worldrenderer.pos((double)f6, (double)f8, (double)f9).tex((double)f1, (double)f3).endVertex();
            worldrenderer.pos((double)f5, (double)f8, (double)f9).tex((double)f2, (double)f3).endVertex();
            Tessellator.draw();
            GL11.glPopMatrix();
        }

        GlState.color(1.0F, 1.0F, 1.0F, 1.0F);
        GlState.disableBlend();
        GlState.depthMask(true);
        GlState.depthFunc(GL11.GL_LEQUAL);
    }

    public void update()
    {
        this.prevEquippedProgress = this.equippedProgress;
        EntityNPC entityplayer = this.gm.player;
        ItemStack itemstack = entityplayer.getHeldItem();
        boolean flag = false;

        if (this.itemToRender != null && itemstack != null)
        {
            if (!this.itemToRender.allEquals(itemstack))
            {
                flag = true;
            }
        }
        else if (this.itemToRender == null && itemstack == null)
        {
            flag = false;
        }
        else
        {
            flag = true;
        }

        float f = 0.4F;
        float f1 = flag ? 0.0F : 1.0F;
        float f2 = ExtMath.clampf(f1 - this.equippedProgress, -f, f);
        this.equippedProgress += f2;

        if (this.equippedProgress < 0.1F)
        {
            this.itemToRender = itemstack;
        }
    }

    public void resetProgress()
    {
        this.equippedProgress = 0.0F;
    }
}
