001    package net.minecraft.src;
002    
003    import cpw.mods.fml.common.Side;
004    import cpw.mods.fml.common.asm.SideOnly;
005    import java.util.HashMap;
006    import java.util.Iterator;
007    import java.util.Map;
008    import org.lwjgl.opengl.GL11;
009    
010    @SideOnly(Side.CLIENT)
011    public class TileEntityRenderer
012    {
013        /**
014         * A mapping of TileEntitySpecialRenderers used for each TileEntity that has one
015         */
016        public Map specialRendererMap = new HashMap();
017    
018        /** The static instance of TileEntityRenderer */
019        public static TileEntityRenderer instance = new TileEntityRenderer();
020    
021        /** The FontRenderer instance used by the TileEntityRenderer */
022        private FontRenderer fontRenderer;
023    
024        /** The player's current X position (same as playerX) */
025        public static double staticPlayerX;
026    
027        /** The player's current Y position (same as playerY) */
028        public static double staticPlayerY;
029    
030        /** The player's current Z position (same as playerZ) */
031        public static double staticPlayerZ;
032    
033        /** The RenderEngine instance used by the TileEntityRenderer */
034        public RenderEngine renderEngine;
035    
036        /** Reference to the World object. */
037        public World worldObj;
038        public EntityLiving entityLivingPlayer;
039        public float playerYaw;
040        public float playerPitch;
041    
042        /** The player's X position in this rendering context */
043        public double playerX;
044    
045        /** The player's Y position in this rendering context */
046        public double playerY;
047    
048        /** The player's Z position in this rendering context */
049        public double playerZ;
050    
051        private TileEntityRenderer()
052        {
053            this.specialRendererMap.put(TileEntitySign.class, new TileEntitySignRenderer());
054            this.specialRendererMap.put(TileEntityMobSpawner.class, new TileEntityMobSpawnerRenderer());
055            this.specialRendererMap.put(TileEntityPiston.class, new TileEntityRendererPiston());
056            this.specialRendererMap.put(TileEntityChest.class, new TileEntityChestRenderer());
057            this.specialRendererMap.put(TileEntityEnderChest.class, new TileEntityEnderChestRenderer());
058            this.specialRendererMap.put(TileEntityEnchantmentTable.class, new RenderEnchantmentTable());
059            this.specialRendererMap.put(TileEntityEndPortal.class, new RenderEndPortal());
060            this.specialRendererMap.put(TileEntityBeacon.class, new TileEntityBeaconRenderer());
061            this.specialRendererMap.put(TileEntitySkull.class, new TileEntitySkullRenderer());
062            Iterator var1 = this.specialRendererMap.values().iterator();
063    
064            while (var1.hasNext())
065            {
066                TileEntitySpecialRenderer var2 = (TileEntitySpecialRenderer)var1.next();
067                var2.setTileEntityRenderer(this);
068            }
069        }
070    
071        /**
072         * Returns the TileEntitySpecialRenderer used to render this TileEntity class, or null if it has no special renderer
073         */
074        public TileEntitySpecialRenderer getSpecialRendererForClass(Class par1Class)
075        {
076            TileEntitySpecialRenderer var2 = (TileEntitySpecialRenderer)this.specialRendererMap.get(par1Class);
077    
078            if (var2 == null && par1Class != TileEntity.class)
079            {
080                var2 = this.getSpecialRendererForClass(par1Class.getSuperclass());
081                this.specialRendererMap.put(par1Class, var2);
082            }
083    
084            return var2;
085        }
086    
087        /**
088         * Returns true if this TileEntity instance has a TileEntitySpecialRenderer associated with it, false otherwise.
089         */
090        public boolean hasSpecialRenderer(TileEntity par1TileEntity)
091        {
092            return this.getSpecialRendererForEntity(par1TileEntity) != null;
093        }
094    
095        /**
096         * Returns the TileEntitySpecialRenderer used to render this TileEntity instance, or null if it has no special
097         * renderer
098         */
099        public TileEntitySpecialRenderer getSpecialRendererForEntity(TileEntity par1TileEntity)
100        {
101            return par1TileEntity == null ? null : this.getSpecialRendererForClass(par1TileEntity.getClass());
102        }
103    
104        /**
105         * Caches several render-related references, including the active World, RenderEngine, FontRenderer, and the camera-
106         * bound EntityLiving's interpolated pitch, yaw and position. Args: world, renderengine, fontrenderer, entityliving,
107         * partialTickTime
108         */
109        public void cacheActiveRenderInfo(World par1World, RenderEngine par2RenderEngine, FontRenderer par3FontRenderer, EntityLiving par4EntityLiving, float par5)
110        {
111            if (this.worldObj != par1World)
112            {
113                this.setWorld(par1World);
114            }
115    
116            this.renderEngine = par2RenderEngine;
117            this.entityLivingPlayer = par4EntityLiving;
118            this.fontRenderer = par3FontRenderer;
119            this.playerYaw = par4EntityLiving.prevRotationYaw + (par4EntityLiving.rotationYaw - par4EntityLiving.prevRotationYaw) * par5;
120            this.playerPitch = par4EntityLiving.prevRotationPitch + (par4EntityLiving.rotationPitch - par4EntityLiving.prevRotationPitch) * par5;
121            this.playerX = par4EntityLiving.lastTickPosX + (par4EntityLiving.posX - par4EntityLiving.lastTickPosX) * (double)par5;
122            this.playerY = par4EntityLiving.lastTickPosY + (par4EntityLiving.posY - par4EntityLiving.lastTickPosY) * (double)par5;
123            this.playerZ = par4EntityLiving.lastTickPosZ + (par4EntityLiving.posZ - par4EntityLiving.lastTickPosZ) * (double)par5;
124        }
125    
126        /**
127         * Render this TileEntity at its current position from the player
128         */
129        public void renderTileEntity(TileEntity par1TileEntity, float par2)
130        {
131            if (par1TileEntity.getDistanceFrom(this.playerX, this.playerY, this.playerZ) < par1TileEntity.func_82115_m())
132            {
133                int var3 = this.worldObj.getLightBrightnessForSkyBlocks(par1TileEntity.xCoord, par1TileEntity.yCoord, par1TileEntity.zCoord, 0);
134                int var4 = var3 % 65536;
135                int var5 = var3 / 65536;
136                OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, (float)var4 / 1.0F, (float)var5 / 1.0F);
137                GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
138                this.renderTileEntityAt(par1TileEntity, (double)par1TileEntity.xCoord - staticPlayerX, (double)par1TileEntity.yCoord - staticPlayerY, (double)par1TileEntity.zCoord - staticPlayerZ, par2);
139            }
140        }
141    
142        /**
143         * Render this TileEntity at a given set of coordinates
144         */
145        public void renderTileEntityAt(TileEntity par1TileEntity, double par2, double par4, double par6, float par8)
146        {
147            TileEntitySpecialRenderer var9 = this.getSpecialRendererForEntity(par1TileEntity);
148    
149            if (var9 != null)
150            {
151                var9.renderTileEntityAt(par1TileEntity, par2, par4, par6, par8);
152            }
153        }
154    
155        /**
156         * Sets the world used by all TileEntitySpecialRender instances and notifies them of this change.
157         */
158        public void setWorld(World par1World)
159        {
160            this.worldObj = par1World;
161            Iterator var2 = this.specialRendererMap.values().iterator();
162    
163            while (var2.hasNext())
164            {
165                TileEntitySpecialRenderer var3 = (TileEntitySpecialRenderer)var2.next();
166    
167                if (var3 != null)
168                {
169                    var3.onWorldChange(par1World);
170                }
171            }
172        }
173    
174        public FontRenderer getFontRenderer()
175        {
176            return this.fontRenderer;
177        }
178    }