| 1 | package net.minecraft.world;
|
| 2 |
|
| 3 | import net.minecraft.block.material.Material;
|
| 4 | import net.minecraft.block.state.IBlockState;
|
| 5 | import net.minecraft.init.Blocks;
|
| 6 | import net.minecraft.tileentity.TileEntity;
|
| 7 | import net.minecraft.util.BlockPos;
|
| 8 | import net.minecraft.util.EnumFacing;
|
| 9 | import net.minecraft.world.biome.BiomeGenBase;
|
| 10 | import net.minecraft.world.chunk.Chunk;
|
| 11 |
|
| 12 | public class ChunkCache implements IBlockAccess
|
| 13 | {
|
| 14 | protected int chunkX;
|
| 15 | protected int chunkZ;
|
| 16 | protected Chunk[][] chunkArray;
|
| 17 |
|
| 18 | /** set by !chunk.getAreLevelsEmpty */
|
| 19 | protected boolean hasExtendedLevels;
|
| 20 |
|
| 21 | /** Reference to the World object. */
|
| 22 | protected World worldObj;
|
| 23 |
|
| 24 | public ChunkCache(World worldIn, BlockPos posFromIn, BlockPos posToIn, int subIn)
|
| 25 | {
|
| 26 | this.worldObj = worldIn;
|
| 27 | this.chunkX = posFromIn.getX() - subIn >> 4;
|
| 28 | this.chunkZ = posFromIn.getZ() - subIn >> 4;
|
| 29 | int i = posToIn.getX() + subIn >> 4;
|
| 30 | int j = posToIn.getZ() + subIn >> 4;
|
| 31 | this.chunkArray = new Chunk[i - this.chunkX + 1][j - this.chunkZ + 1];
|
| 32 | this.hasExtendedLevels = true;
|
| 33 |
|
| 34 | for (int k = this.chunkX; k <= i; ++k)
|
| 35 | {
|
| 36 | for (int l = this.chunkZ; l <= j; ++l)
|
| 37 | {
|
| 38 | this.chunkArray[k - this.chunkX][l - this.chunkZ] = worldIn.getChunkFromChunkCoords(k, l);
|
| 39 | }
|
| 40 | }
|
| 41 |
|
| 42 | for (int i1 = posFromIn.getX() >> 4; i1 <= posToIn.getX() >> 4; ++i1)
|
| 43 | {
|
| 44 | for (int j1 = posFromIn.getZ() >> 4; j1 <= posToIn.getZ() >> 4; ++j1)
|
| 45 | {
|
| 46 | Chunk chunk = this.chunkArray[i1 - this.chunkX][j1 - this.chunkZ];
|
| 47 |
|
| 48 | if (chunk != null && !chunk.getAreLevelsEmpty(posFromIn.getY(), posToIn.getY()))
|
| 49 | {
|
| 50 | this.hasExtendedLevels = false;
|
| 51 | }
|
| 52 | }
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | /**
|
| 57 | * set by !chunk.getAreLevelsEmpty
|
| 58 | */
|
| 59 | public boolean extendedLevelsInChunkCache()
|
| 60 | {
|
| 61 | return this.hasExtendedLevels;
|
| 62 | }
|
| 63 |
|
| 64 | public TileEntity getTileEntity(BlockPos pos)
|
| 65 | {
|
| 66 | int i = (pos.getX() >> 4) - this.chunkX;
|
| 67 | int j = (pos.getZ() >> 4) - this.chunkZ;
|
| 68 | return this.chunkArray[i][j].getTileEntity(pos, Chunk.EnumCreateEntityType.IMMEDIATE);
|
| 69 | }
|
| 70 |
|
| 71 | public int getCombinedLight(BlockPos pos, int lightValue)
|
| 72 | {
|
| 73 | int i = this.getLightForExt(EnumSkyBlock.SKY, pos);
|
| 74 | int j = this.getLightForExt(EnumSkyBlock.BLOCK, pos);
|
| 75 |
|
| 76 | if (j < lightValue)
|
| 77 | {
|
| 78 | j = lightValue;
|
| 79 | }
|
| 80 |
|
| 81 | return i << 20 | j << 4;
|
| 82 | }
|
| 83 |
|
| 84 | public IBlockState getBlockState(BlockPos pos)
|
| 85 | {
|
| 86 | if (pos.getY() >= 0 && pos.getY() < 256)
|
| 87 | {
|
| 88 | int i = (pos.getX() >> 4) - this.chunkX;
|
| 89 | int j = (pos.getZ() >> 4) - this.chunkZ;
|
| 90 |
|
| 91 | if (i >= 0 && i < this.chunkArray.length && j >= 0 && j < this.chunkArray[i].length)
|
| 92 | {
|
| 93 | Chunk chunk = this.chunkArray[i][j];
|
| 94 |
|
| 95 | if (chunk != null)
|
| 96 | {
|
| 97 | return chunk.getBlockState(pos);
|
| 98 | }
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | return Blocks.air.getDefaultState();
|
| 103 | }
|
| 104 |
|
| 105 | public BiomeGenBase getBiomeGenForCoords(BlockPos pos)
|
| 106 | {
|
| 107 | return this.worldObj.getBiomeGenForCoords(pos);
|
| 108 | }
|
| 109 |
|
| 110 | private int getLightForExt(EnumSkyBlock p_175629_1_, BlockPos pos)
|
| 111 | {
|
| 112 | if (p_175629_1_ == EnumSkyBlock.SKY && this.worldObj.provider.getHasNoSky())
|
| 113 | {
|
| 114 | return 0;
|
| 115 | }
|
| 116 | else if (pos.getY() >= 0 && pos.getY() < 256)
|
| 117 | {
|
| 118 | if (this.getBlockState(pos).getBlock().getUseNeighborBrightness())
|
| 119 | {
|
| 120 | int l = 0;
|
| 121 |
|
| 122 | for (EnumFacing enumfacing : EnumFacing.values())
|
| 123 | {
|
| 124 | int k = this.getLightFor(p_175629_1_, pos.offset(enumfacing));
|
| 125 |
|
| 126 | if (k > l)
|
| 127 | {
|
| 128 | l = k;
|
| 129 | }
|
| 130 |
|
| 131 | if (l >= 15)
|
| 132 | {
|
| 133 | return l;
|
| 134 | }
|
| 135 | }
|
| 136 |
|
| 137 | return l;
|
| 138 | }
|
| 139 | else
|
| 140 | {
|
| 141 | int i = (pos.getX() >> 4) - this.chunkX;
|
| 142 | int j = (pos.getZ() >> 4) - this.chunkZ;
|
| 143 | return this.chunkArray[i][j].getLightFor(p_175629_1_, pos);
|
| 144 | }
|
| 145 | }
|
| 146 | else
|
| 147 | {
|
| 148 | return p_175629_1_.defaultLightValue;
|
| 149 | }
|
| 150 | }
|
| 151 |
|
| 152 | /**
|
| 153 | * Checks to see if an air block exists at the provided location. Note that this only checks to see if the blocks
|
| 154 | * material is set to air, meaning it is possible for non-vanilla blocks to still pass this check.
|
| 155 | */
|
| 156 | public boolean isAirBlock(BlockPos pos)
|
| 157 | {
|
| 158 | return this.getBlockState(pos).getBlock().getMaterial() == Material.air;
|
| 159 | }
|
| 160 |
|
| 161 | public int getLightFor(EnumSkyBlock p_175628_1_, BlockPos pos)
|
| 162 | {
|
| 163 | if (pos.getY() >= 0 && pos.getY() < 256)
|
| 164 | {
|
| 165 | int i = (pos.getX() >> 4) - this.chunkX;
|
| 166 | int j = (pos.getZ() >> 4) - this.chunkZ;
|
| 167 | return this.chunkArray[i][j].getLightFor(p_175628_1_, pos);
|
| 168 | }
|
| 169 | else
|
| 170 | {
|
| 171 | return p_175628_1_.defaultLightValue;
|
| 172 | }
|
| 173 | }
|
| 174 |
|
| 175 | public int getStrongPower(BlockPos pos, EnumFacing direction)
|
| 176 | {
|
| 177 | IBlockState iblockstate = this.getBlockState(pos);
|
| 178 | return iblockstate.getBlock().getStrongPower(this, pos, iblockstate, direction);
|
| 179 | }
|
| 180 |
|
| 181 | public WorldType getWorldType()
|
| 182 | {
|
| 183 | return this.worldObj.getWorldType();
|
| 184 | }
|
| 185 | }
|
| 186 |
|