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 Iterator var1 = this.specialRendererMap.values().iterator(); 061 062 while (var1.hasNext()) 063 { 064 TileEntitySpecialRenderer var2 = (TileEntitySpecialRenderer)var1.next(); 065 var2.setTileEntityRenderer(this); 066 } 067 } 068 069 /** 070 * Returns the TileEntitySpecialRenderer used to render this TileEntity class, or null if it has no special renderer 071 */ 072 public TileEntitySpecialRenderer getSpecialRendererForClass(Class par1Class) 073 { 074 TileEntitySpecialRenderer var2 = (TileEntitySpecialRenderer)this.specialRendererMap.get(par1Class); 075 076 if (var2 == null && par1Class != TileEntity.class) 077 { 078 var2 = this.getSpecialRendererForClass(par1Class.getSuperclass()); 079 this.specialRendererMap.put(par1Class, var2); 080 } 081 082 return var2; 083 } 084 085 /** 086 * Returns true if this TileEntity instance has a TileEntitySpecialRenderer associated with it, false otherwise. 087 */ 088 public boolean hasSpecialRenderer(TileEntity par1TileEntity) 089 { 090 return this.getSpecialRendererForEntity(par1TileEntity) != null; 091 } 092 093 /** 094 * Returns the TileEntitySpecialRenderer used to render this TileEntity instance, or null if it has no special 095 * renderer 096 */ 097 public TileEntitySpecialRenderer getSpecialRendererForEntity(TileEntity par1TileEntity) 098 { 099 return par1TileEntity == null ? null : this.getSpecialRendererForClass(par1TileEntity.getClass()); 100 } 101 102 /** 103 * Caches several render-related references, including the active World, RenderEngine, FontRenderer, and the camera- 104 * bound EntityLiving's interpolated pitch, yaw and position. Args: world, renderengine, fontrenderer, entityliving, 105 * partialTickTime 106 */ 107 public void cacheActiveRenderInfo(World par1World, RenderEngine par2RenderEngine, FontRenderer par3FontRenderer, EntityLiving par4EntityLiving, float par5) 108 { 109 if (this.worldObj != par1World) 110 { 111 this.setWorld(par1World); 112 } 113 114 this.renderEngine = par2RenderEngine; 115 this.entityLivingPlayer = par4EntityLiving; 116 this.fontRenderer = par3FontRenderer; 117 this.playerYaw = par4EntityLiving.prevRotationYaw + (par4EntityLiving.rotationYaw - par4EntityLiving.prevRotationYaw) * par5; 118 this.playerPitch = par4EntityLiving.prevRotationPitch + (par4EntityLiving.rotationPitch - par4EntityLiving.prevRotationPitch) * par5; 119 this.playerX = par4EntityLiving.lastTickPosX + (par4EntityLiving.posX - par4EntityLiving.lastTickPosX) * (double)par5; 120 this.playerY = par4EntityLiving.lastTickPosY + (par4EntityLiving.posY - par4EntityLiving.lastTickPosY) * (double)par5; 121 this.playerZ = par4EntityLiving.lastTickPosZ + (par4EntityLiving.posZ - par4EntityLiving.lastTickPosZ) * (double)par5; 122 } 123 124 /** 125 * Render this TileEntity at its current position from the player 126 */ 127 public void renderTileEntity(TileEntity par1TileEntity, float par2) 128 { 129 double dist = par1TileEntity.getRenderDistance(); 130 dist *= dist; 131 if (par1TileEntity.getDistanceFrom(this.playerX, this.playerY, this.playerZ) < dist) 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 }