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