001package net.minecraft.world; 002 003import cpw.mods.fml.relauncher.Side; 004import cpw.mods.fml.relauncher.SideOnly; 005import net.minecraft.block.Block; 006import net.minecraft.block.BlockHalfSlab; 007import net.minecraft.block.BlockStairs; 008import net.minecraft.block.material.Material; 009import net.minecraft.tileentity.TileEntity; 010import net.minecraft.util.Vec3Pool; 011import net.minecraft.world.biome.BiomeGenBase; 012import net.minecraft.world.chunk.Chunk; 013 014public class ChunkCache implements IBlockAccess 015{ 016 private int chunkX; 017 private int chunkZ; 018 private Chunk[][] chunkArray; 019 020 /** set by !chunk.getAreLevelsEmpty */ 021 private boolean hasExtendedLevels; 022 023 /** Reference to the World object. */ 024 private World worldObj; 025 026 public ChunkCache(World par1World, int par2, int par3, int par4, int par5, int par6, int par7) 027 { 028 this.worldObj = par1World; 029 this.chunkX = par2 >> 4; 030 this.chunkZ = par4 >> 4; 031 int k1 = par5 >> 4; 032 int l1 = par7 >> 4; 033 this.chunkArray = new Chunk[k1 - this.chunkX + 1][l1 - this.chunkZ + 1]; 034 this.hasExtendedLevels = true; 035 036 for (int i2 = this.chunkX; i2 <= k1; ++i2) 037 { 038 for (int j2 = this.chunkZ; j2 <= l1; ++j2) 039 { 040 Chunk chunk = par1World.getChunkFromChunkCoords(i2, j2); 041 042 if (chunk != null) 043 { 044 this.chunkArray[i2 - this.chunkX][j2 - this.chunkZ] = chunk; 045 046 if (!chunk.getAreLevelsEmpty(par3, par6)) 047 { 048 this.hasExtendedLevels = false; 049 } 050 } 051 } 052 } 053 } 054 055 @SideOnly(Side.CLIENT) 056 057 /** 058 * set by !chunk.getAreLevelsEmpty 059 */ 060 public boolean extendedLevelsInChunkCache() 061 { 062 return this.hasExtendedLevels; 063 } 064 065 /** 066 * Returns the block ID at coords x,y,z 067 */ 068 public int getBlockId(int par1, int par2, int par3) 069 { 070 if (par2 < 0) 071 { 072 return 0; 073 } 074 else if (par2 >= 256) 075 { 076 return 0; 077 } 078 else 079 { 080 int l = (par1 >> 4) - this.chunkX; 081 int i1 = (par3 >> 4) - this.chunkZ; 082 083 if (l >= 0 && l < this.chunkArray.length && i1 >= 0 && i1 < this.chunkArray[l].length) 084 { 085 Chunk chunk = this.chunkArray[l][i1]; 086 return chunk == null ? 0 : chunk.getBlockID(par1 & 15, par2, par3 & 15); 087 } 088 else 089 { 090 return 0; 091 } 092 } 093 } 094 095 /** 096 * Returns the TileEntity associated with a given block in X,Y,Z coordinates, or null if no TileEntity exists 097 */ 098 public TileEntity getBlockTileEntity(int par1, int par2, int par3) 099 { 100 int l = (par1 >> 4) - this.chunkX; 101 int i1 = (par3 >> 4) - this.chunkZ; 102 if (l >= 0 && l < this.chunkArray.length && i1 >= 0 && i1 < this.chunkArray[l].length) 103 { 104 Chunk chunk = this.chunkArray[l][i1]; 105 return chunk == null ? null : chunk.getChunkBlockTileEntity(par1 & 15, par2, par3 & 15); 106 } 107 else 108 { 109 return null; 110 } 111 } 112 113 @SideOnly(Side.CLIENT) 114 public float getBrightness(int par1, int par2, int par3, int par4) 115 { 116 int i1 = this.getLightValue(par1, par2, par3); 117 118 if (i1 < par4) 119 { 120 i1 = par4; 121 } 122 123 return this.worldObj.provider.lightBrightnessTable[i1]; 124 } 125 126 @SideOnly(Side.CLIENT) 127 128 /** 129 * Any Light rendered on a 1.8 Block goes through here 130 */ 131 public int getLightBrightnessForSkyBlocks(int par1, int par2, int par3, int par4) 132 { 133 int i1 = this.getSkyBlockTypeBrightness(EnumSkyBlock.Sky, par1, par2, par3); 134 int j1 = this.getSkyBlockTypeBrightness(EnumSkyBlock.Block, par1, par2, par3); 135 136 if (j1 < par4) 137 { 138 j1 = par4; 139 } 140 141 return i1 << 20 | j1 << 4; 142 } 143 144 /** 145 * Returns the block metadata at coords x,y,z 146 */ 147 public int getBlockMetadata(int par1, int par2, int par3) 148 { 149 if (par2 < 0) 150 { 151 return 0; 152 } 153 else if (par2 >= 256) 154 { 155 return 0; 156 } 157 else 158 { 159 int l = (par1 >> 4) - this.chunkX; 160 int i1 = (par3 >> 4) - this.chunkZ; 161 if (l >= 0 && l < this.chunkArray.length && i1 >= 0 && i1 < this.chunkArray[l].length) 162 { 163 Chunk chunk = this.chunkArray[l][i1]; 164 return chunk == null ? 0 : chunk.getBlockMetadata(par1 & 15, par2, par3 & 15); 165 } 166 return 0; 167 } 168 } 169 170 @SideOnly(Side.CLIENT) 171 172 /** 173 * Returns how bright the block is shown as which is the block's light value looked up in a lookup table (light 174 * values aren't linear for brightness). Args: x, y, z 175 */ 176 public float getLightBrightness(int par1, int par2, int par3) 177 { 178 return this.worldObj.provider.lightBrightnessTable[this.getLightValue(par1, par2, par3)]; 179 } 180 181 @SideOnly(Side.CLIENT) 182 183 /** 184 * Gets the light value of the specified block coords. Args: x, y, z 185 */ 186 public int getLightValue(int par1, int par2, int par3) 187 { 188 return this.getLightValueExt(par1, par2, par3, true); 189 } 190 191 @SideOnly(Side.CLIENT) 192 193 /** 194 * Get light value with flag 195 */ 196 public int getLightValueExt(int par1, int par2, int par3, boolean par4) 197 { 198 if (par1 >= -30000000 && par3 >= -30000000 && par1 < 30000000 && par3 <= 30000000) 199 { 200 int l; 201 int i1; 202 203 if (par4) 204 { 205 l = this.getBlockId(par1, par2, par3); 206 207 if (l == Block.stoneSingleSlab.blockID || l == Block.woodSingleSlab.blockID || l == Block.tilledField.blockID || l == Block.stairCompactPlanks.blockID || l == Block.stairCompactCobblestone.blockID) 208 { 209 i1 = this.getLightValueExt(par1, par2 + 1, par3, false); 210 int j1 = this.getLightValueExt(par1 + 1, par2, par3, false); 211 int k1 = this.getLightValueExt(par1 - 1, par2, par3, false); 212 int l1 = this.getLightValueExt(par1, par2, par3 + 1, false); 213 int i2 = this.getLightValueExt(par1, par2, par3 - 1, false); 214 215 if (j1 > i1) 216 { 217 i1 = j1; 218 } 219 220 if (k1 > i1) 221 { 222 i1 = k1; 223 } 224 225 if (l1 > i1) 226 { 227 i1 = l1; 228 } 229 230 if (i2 > i1) 231 { 232 i1 = i2; 233 } 234 235 return i1; 236 } 237 } 238 239 if (par2 < 0) 240 { 241 return 0; 242 } 243 else if (par2 >= 256) 244 { 245 l = 15 - this.worldObj.skylightSubtracted; 246 247 if (l < 0) 248 { 249 l = 0; 250 } 251 252 return l; 253 } 254 else 255 { 256 l = (par1 >> 4) - this.chunkX; 257 i1 = (par3 >> 4) - this.chunkZ; 258 return this.chunkArray[l][i1].getBlockLightValue(par1 & 15, par2, par3 & 15, this.worldObj.skylightSubtracted); 259 } 260 } 261 else 262 { 263 return 15; 264 } 265 } 266 267 /** 268 * Returns the block's material. 269 */ 270 public Material getBlockMaterial(int par1, int par2, int par3) 271 { 272 int l = this.getBlockId(par1, par2, par3); 273 return l == 0 ? Material.air : Block.blocksList[l].blockMaterial; 274 } 275 276 @SideOnly(Side.CLIENT) 277 278 /** 279 * Gets the biome for a given set of x/z coordinates 280 */ 281 public BiomeGenBase getBiomeGenForCoords(int par1, int par2) 282 { 283 return this.worldObj.getBiomeGenForCoords(par1, par2); 284 } 285 286 @SideOnly(Side.CLIENT) 287 288 /** 289 * Returns true if the block at the specified coordinates is an opaque cube. Args: x, y, z 290 */ 291 public boolean isBlockOpaqueCube(int par1, int par2, int par3) 292 { 293 Block block = Block.blocksList[this.getBlockId(par1, par2, par3)]; 294 return block == null ? false : block.isOpaqueCube(); 295 } 296 297 /** 298 * Indicate if a material is a normal solid opaque cube. 299 */ 300 public boolean isBlockNormalCube(int par1, int par2, int par3) 301 { 302 Block block = Block.blocksList[this.getBlockId(par1, par2, par3)]; 303 return block == null ? false : block.blockMaterial.blocksMovement() && block.renderAsNormalBlock(); 304 } 305 306 @SideOnly(Side.CLIENT) 307 308 /** 309 * Returns true if the block at the given coordinate has a solid (buildable) top surface. 310 */ 311 public boolean doesBlockHaveSolidTopSurface(int par1, int par2, int par3) 312 { 313 Block block = Block.blocksList[this.getBlockId(par1, par2, par3)]; 314 return block == null ? false : (block.blockMaterial.isOpaque() && block.renderAsNormalBlock() ? true : (block instanceof BlockStairs ? (this.getBlockMetadata(par1, par2, par3) & 4) == 4 : (block instanceof BlockHalfSlab ? (this.getBlockMetadata(par1, par2, par3) & 8) == 8 : false))); 315 } 316 317 /** 318 * Return the Vec3Pool object for this world. 319 */ 320 public Vec3Pool getWorldVec3Pool() 321 { 322 return this.worldObj.getWorldVec3Pool(); 323 } 324 325 @SideOnly(Side.CLIENT) 326 327 /** 328 * Returns true if the block at the specified coordinates is empty 329 */ 330 public boolean isAirBlock(int par1, int par2, int par3) 331 { 332 Block block = Block.blocksList[this.getBlockId(par1, par2, par3)]; 333 return block == null; 334 } 335 336 @SideOnly(Side.CLIENT) 337 338 /** 339 * Brightness for SkyBlock.Sky is clear white and (through color computing it is assumed) DEPENDENT ON DAYTIME. 340 * Brightness for SkyBlock.Block is yellowish and independent. 341 */ 342 public int getSkyBlockTypeBrightness(EnumSkyBlock par1EnumSkyBlock, int par2, int par3, int par4) 343 { 344 if (par3 < 0) 345 { 346 par3 = 0; 347 } 348 349 if (par3 >= 256) 350 { 351 par3 = 255; 352 } 353 354 if (par3 >= 0 && par3 < 256 && par2 >= -30000000 && par4 >= -30000000 && par2 < 30000000 && par4 <= 30000000) 355 { 356 if (par1EnumSkyBlock == EnumSkyBlock.Sky && this.worldObj.provider.hasNoSky) 357 { 358 return 0; 359 } 360 else 361 { 362 int l; 363 int i1; 364 365 if (Block.useNeighborBrightness[this.getBlockId(par2, par3, par4)]) 366 { 367 l = this.getSpecialBlockBrightness(par1EnumSkyBlock, par2, par3 + 1, par4); 368 i1 = this.getSpecialBlockBrightness(par1EnumSkyBlock, par2 + 1, par3, par4); 369 int j1 = this.getSpecialBlockBrightness(par1EnumSkyBlock, par2 - 1, par3, par4); 370 int k1 = this.getSpecialBlockBrightness(par1EnumSkyBlock, par2, par3, par4 + 1); 371 int l1 = this.getSpecialBlockBrightness(par1EnumSkyBlock, par2, par3, par4 - 1); 372 373 if (i1 > l) 374 { 375 l = i1; 376 } 377 378 if (j1 > l) 379 { 380 l = j1; 381 } 382 383 if (k1 > l) 384 { 385 l = k1; 386 } 387 388 if (l1 > l) 389 { 390 l = l1; 391 } 392 393 return l; 394 } 395 else 396 { 397 l = (par2 >> 4) - this.chunkX; 398 i1 = (par4 >> 4) - this.chunkZ; 399 return this.chunkArray[l][i1].getSavedLightValue(par1EnumSkyBlock, par2 & 15, par3, par4 & 15); 400 } 401 } 402 } 403 else 404 { 405 return par1EnumSkyBlock.defaultLightValue; 406 } 407 } 408 409 @SideOnly(Side.CLIENT) 410 411 /** 412 * is only used on stairs and tilled fields 413 */ 414 public int getSpecialBlockBrightness(EnumSkyBlock par1EnumSkyBlock, int par2, int par3, int par4) 415 { 416 if (par3 < 0) 417 { 418 par3 = 0; 419 } 420 421 if (par3 >= 256) 422 { 423 par3 = 255; 424 } 425 426 if (par3 >= 0 && par3 < 256 && par2 >= -30000000 && par4 >= -30000000 && par2 < 30000000 && par4 <= 30000000) 427 { 428 int l = (par2 >> 4) - this.chunkX; 429 int i1 = (par4 >> 4) - this.chunkZ; 430 return this.chunkArray[l][i1].getSavedLightValue(par1EnumSkyBlock, par2 & 15, par3, par4 & 15); 431 } 432 else 433 { 434 return par1EnumSkyBlock.defaultLightValue; 435 } 436 } 437 438 @SideOnly(Side.CLIENT) 439 440 /** 441 * Returns current world height. 442 */ 443 public int getHeight() 444 { 445 return 256; 446 } 447 448 /** 449 * Is this block powering in the specified direction Args: x, y, z, direction 450 */ 451 public int isBlockProvidingPowerTo(int par1, int par2, int par3, int par4) 452 { 453 int i1 = this.getBlockId(par1, par2, par3); 454 return i1 == 0 ? 0 : Block.blocksList[i1].isProvidingStrongPower(this, par1, par2, par3, par4); 455 } 456}