    001package net.minecraft.client.renderer.tileentity;
    002
    003import cpw.mods.fml.common.FMLLog;
    004import cpw.mods.fml.relauncher.Side;
    005import cpw.mods.fml.relauncher.SideOnly;
    006import java.util.Calendar;
    007import net.minecraft.block.Block;
    008import net.minecraft.block.BlockChest;
    009import net.minecraft.client.model.ModelChest;
    010import net.minecraft.client.model.ModelLargeChest;
    011import net.minecraft.tileentity.TileEntity;
    012import net.minecraft.tileentity.TileEntityChest;
    013import org.lwjgl.opengl.GL11;
    014import org.lwjgl.opengl.GL12;
    015
    016@SideOnly(Side.CLIENT)
    017public class TileEntityChestRenderer extends TileEntitySpecialRenderer
    018{
    019    /** The normal small chest model. */
    020    private ModelChest chestModel = new ModelChest();
    021
    022    /** The large double chest model. */
    023    private ModelChest largeChestModel = new ModelLargeChest();
    024
    025    /** If true, chests will be rendered with the Christmas present textures. */
    026    private boolean isChristmas;
    027
    028    public TileEntityChestRenderer()
    029    {
    030        Calendar calendar = Calendar.getInstance();
    031
    032        if (calendar.get(2) + 1 == 12 && calendar.get(5) >= 24 && calendar.get(5) <= 26)
    033        {
    034            this.isChristmas = true;
    035        }
    036    }
    037
    038    /**
    039     * Renders the TileEntity for the chest at a position.
    040     */
    041    public void renderTileEntityChestAt(TileEntityChest par1TileEntityChest, double par2, double par4, double par6, float par8)
    042    {
    043        int i;
    044
    045        if (!par1TileEntityChest.func_70309_m())
    046        {
    047            i = 0;
    048        }
    049        else
    050        {
    051            Block block = par1TileEntityChest.getBlockType();
    052            i = par1TileEntityChest.getBlockMetadata();
    053
    054            if (block instanceof BlockChest && i == 0)
    055            {
    056                try
    057                {
    058                    ((BlockChest)block).unifyAdjacentChests(par1TileEntityChest.getWorldObj(), par1TileEntityChest.xCoord, par1TileEntityChest.yCoord, par1TileEntityChest.zCoord);
    059                }
    060                catch (ClassCastException e)
    061                {
    062                    FMLLog.severe("Attempted to render a chest at %d,  %d, %d that was not a chest",
    063                            par1TileEntityChest.xCoord, par1TileEntityChest.yCoord, par1TileEntityChest.zCoord);
    064                }
    065                i = par1TileEntityChest.getBlockMetadata();
    066            }
    067
    068            par1TileEntityChest.checkForAdjacentChests();
    069        }
    070
    071        if (par1TileEntityChest.adjacentChestZNeg == null && par1TileEntityChest.adjacentChestXNeg == null)
    072        {
    073            ModelChest modelchest;
    074
    075            if (par1TileEntityChest.adjacentChestXPos == null && par1TileEntityChest.adjacentChestZPosition == null)
    076            {
    077                modelchest = this.chestModel;
    078
    079                if (par1TileEntityChest.func_98041_l() == 1)
    080                {
    081                    this.bindTextureByName("/item/chests/trap_small.png");
    082                }
    083                else if (this.isChristmas)
    084                {
    085                    this.bindTextureByName("/item/xmaschest.png");
    086                }
    087                else
    088                {
    089                    this.bindTextureByName("/item/chest.png");
    090                }
    091            }
    092            else
    093            {
    094                modelchest = this.largeChestModel;
    095
    096                if (par1TileEntityChest.func_98041_l() == 1)
    097                {
    098                    this.bindTextureByName("/item/chests/trap_large.png");
    099                }
    100                else if (this.isChristmas)
    101                {
    102                    this.bindTextureByName("/item/largexmaschest.png");
    103                }
    104                else
    105                {
    106                    this.bindTextureByName("/item/largechest.png");
    107                }
    108            }
    109
    110            GL11.glPushMatrix();
    111            GL11.glEnable(GL12.GL_RESCALE_NORMAL);
    112            GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
    113            GL11.glTranslatef((float)par2, (float)par4 + 1.0F, (float)par6 + 1.0F);
    114            GL11.glScalef(1.0F, -1.0F, -1.0F);
    115            GL11.glTranslatef(0.5F, 0.5F, 0.5F);
    116            short short1 = 0;
    117
    118            if (i == 2)
    119            {
    120                short1 = 180;
    121            }
    122
    123            if (i == 3)
    124            {
    125                short1 = 0;
    126            }
    127
    128            if (i == 4)
    129            {
    130                short1 = 90;
    131            }
    132
    133            if (i == 5)
    134            {
    135                short1 = -90;
    136            }
    137
    138            if (i == 2 && par1TileEntityChest.adjacentChestXPos != null)
    139            {
    140                GL11.glTranslatef(1.0F, 0.0F, 0.0F);
    141            }
    142
    143            if (i == 5 && par1TileEntityChest.adjacentChestZPosition != null)
    144            {
    145                GL11.glTranslatef(0.0F, 0.0F, -1.0F);
    146            }
    147
    148            GL11.glRotatef((float)short1, 0.0F, 1.0F, 0.0F);
    149            GL11.glTranslatef(-0.5F, -0.5F, -0.5F);
    150            float f1 = par1TileEntityChest.prevLidAngle + (par1TileEntityChest.lidAngle - par1TileEntityChest.prevLidAngle) * par8;
    151            float f2;
    152
    153            if (par1TileEntityChest.adjacentChestZNeg != null)
    154            {
    155                f2 = par1TileEntityChest.adjacentChestZNeg.prevLidAngle + (par1TileEntityChest.adjacentChestZNeg.lidAngle - par1TileEntityChest.adjacentChestZNeg.prevLidAngle) * par8;
    156
    157                if (f2 > f1)
    158                {
    159                    f1 = f2;
    160                }
    161            }
    162
    163            if (par1TileEntityChest.adjacentChestXNeg != null)
    164            {
    165                f2 = par1TileEntityChest.adjacentChestXNeg.prevLidAngle + (par1TileEntityChest.adjacentChestXNeg.lidAngle - par1TileEntityChest.adjacentChestXNeg.prevLidAngle) * par8;
    166
    167                if (f2 > f1)
    168                {
    169                    f1 = f2;
    170                }
    171            }
    172
    173            f1 = 1.0F - f1;
    174            f1 = 1.0F - f1 * f1 * f1;
    175            modelchest.chestLid.rotateAngleX = -(f1 * (float)Math.PI / 2.0F);
    176            modelchest.renderAll();
    177            GL11.glDisable(GL12.GL_RESCALE_NORMAL);
    178            GL11.glPopMatrix();
    179            GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
    180        }
    181    }
    182
    183    public void renderTileEntityAt(TileEntity par1TileEntity, double par2, double par4, double par6, float par8)
    184    {
    185        this.renderTileEntityChestAt((TileEntityChest)par1TileEntity, par2, par4, par6, par8);
    186    }
    187}