| 1 | package net.minecraft.block;
|
| 2 |
|
| 3 | import java.util.EnumSet;
|
| 4 | import java.util.Random;
|
| 5 | import java.util.Set;
|
| 6 | import net.minecraft.block.material.Material;
|
| 7 | import net.minecraft.block.state.IBlockState;
|
| 8 | import net.minecraft.init.Blocks;
|
| 9 | import net.minecraft.util.BlockPos;
|
| 10 | import net.minecraft.util.EnumFacing;
|
| 11 | import net.minecraft.world.World;
|
| 12 |
|
| 13 | public class BlockDynamicLiquid extends BlockLiquid
|
| 14 | {
|
| 15 | int adjacentSourceBlocks;
|
| 16 |
|
| 17 | protected BlockDynamicLiquid(Material materialIn)
|
| 18 | {
|
| 19 | super(materialIn);
|
| 20 | }
|
| 21 |
|
| 22 | private void placeStaticBlock(World worldIn, BlockPos pos, IBlockState currentState)
|
| 23 | {
|
| 24 | worldIn.setBlockState(pos, getStaticBlock(this.blockMaterial).getDefaultState().withProperty(LEVEL, currentState.getValue(LEVEL)), 2);
|
| 25 | }
|
| 26 |
|
| 27 | public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
|
| 28 | {
|
| 29 | int i = ((Integer)state.getValue(LEVEL)).intValue();
|
| 30 | int j = 1;
|
| 31 |
|
| 32 | if (this.blockMaterial == Material.lava && !worldIn.provider.doesWaterVaporize())
|
| 33 | {
|
| 34 | j = 2;
|
| 35 | }
|
| 36 |
|
| 37 | int k = this.tickRate(worldIn);
|
| 38 |
|
| 39 | if (i > 0)
|
| 40 | {
|
| 41 | int l = -100;
|
| 42 | this.adjacentSourceBlocks = 0;
|
| 43 |
|
| 44 | for (EnumFacing enumfacing : EnumFacing.Plane.HORIZONTAL)
|
| 45 | {
|
| 46 | l = this.checkAdjacentBlock(worldIn, pos.offset(enumfacing), l);
|
| 47 | }
|
| 48 |
|
| 49 | int i1 = l + j;
|
| 50 |
|
| 51 | if (i1 >= 8 || l < 0)
|
| 52 | {
|
| 53 | i1 = -1;
|
| 54 | }
|
| 55 |
|
| 56 | if (this.getLevel(worldIn, pos.up()) >= 0)
|
| 57 | {
|
| 58 | int j1 = this.getLevel(worldIn, pos.up());
|
| 59 |
|
| 60 | if (j1 >= 8)
|
| 61 | {
|
| 62 | i1 = j1;
|
| 63 | }
|
| 64 | else
|
| 65 | {
|
| 66 | i1 = j1 + 8;
|
| 67 | }
|
| 68 | }
|
| 69 |
|
| 70 | if (this.adjacentSourceBlocks >= 2 && this.blockMaterial == Material.water)
|
| 71 | {
|
| 72 | IBlockState iblockstate1 = worldIn.getBlockState(pos.down());
|
| 73 |
|
| 74 | if (iblockstate1.getBlock().getMaterial().isSolid())
|
| 75 | {
|
| 76 | i1 = 0;
|
| 77 | }
|
| 78 | else if (iblockstate1.getBlock().getMaterial() == this.blockMaterial && ((Integer)iblockstate1.getValue(LEVEL)).intValue() == 0)
|
| 79 | {
|
| 80 | i1 = 0;
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | if (this.blockMaterial == Material.lava && i < 8 && i1 < 8 && i1 > i && rand.nextInt(4) != 0)
|
| 85 | {
|
| 86 | k *= 4;
|
| 87 | }
|
| 88 |
|
| 89 | if (i1 == i)
|
| 90 | {
|
| 91 | this.placeStaticBlock(worldIn, pos, state);
|
| 92 | }
|
| 93 | else
|
| 94 | {
|
| 95 | i = i1;
|
| 96 |
|
| 97 | if (i1 < 0)
|
| 98 | {
|
| 99 | worldIn.setBlockToAir(pos);
|
| 100 | }
|
| 101 | else
|
| 102 | {
|
| 103 | state = state.withProperty(LEVEL, Integer.valueOf(i1));
|
| 104 | worldIn.setBlockState(pos, state, 2);
|
| 105 | worldIn.scheduleUpdate(pos, this, k);
|
| 106 | worldIn.notifyNeighborsOfStateChange(pos, this);
|
| 107 | }
|
| 108 | }
|
| 109 | }
|
| 110 | else
|
| 111 | {
|
| 112 | this.placeStaticBlock(worldIn, pos, state);
|
| 113 | }
|
| 114 |
|
| 115 | IBlockState iblockstate = worldIn.getBlockState(pos.down());
|
| 116 |
|
| 117 | if (this.canFlowInto(worldIn, pos.down(), iblockstate))
|
| 118 | {
|
| 119 | if (this.blockMaterial == Material.lava && worldIn.getBlockState(pos.down()).getBlock().getMaterial() == Material.water)
|
| 120 | {
|
| 121 | worldIn.setBlockState(pos.down(), Blocks.stone.getDefaultState());
|
| 122 | this.triggerMixEffects(worldIn, pos.down());
|
| 123 | return;
|
| 124 | }
|
| 125 |
|
| 126 | if (i >= 8)
|
| 127 | {
|
| 128 | this.tryFlowInto(worldIn, pos.down(), iblockstate, i);
|
| 129 | }
|
| 130 | else
|
| 131 | {
|
| 132 | this.tryFlowInto(worldIn, pos.down(), iblockstate, i + 8);
|
| 133 | }
|
| 134 | }
|
| 135 | else if (i >= 0 && (i == 0 || this.isBlocked(worldIn, pos.down(), iblockstate)))
|
| 136 | {
|
| 137 | Set<EnumFacing> set = this.getPossibleFlowDirections(worldIn, pos);
|
| 138 | int k1 = i + j;
|
| 139 |
|
| 140 | if (i >= 8)
|
| 141 | {
|
| 142 | k1 = 1;
|
| 143 | }
|
| 144 |
|
| 145 | if (k1 >= 8)
|
| 146 | {
|
| 147 | return;
|
| 148 | }
|
| 149 |
|
| 150 | for (EnumFacing enumfacing1 : set)
|
| 151 | {
|
| 152 | this.tryFlowInto(worldIn, pos.offset(enumfacing1), worldIn.getBlockState(pos.offset(enumfacing1)), k1);
|
| 153 | }
|
| 154 | }
|
| 155 | }
|
| 156 |
|
| 157 | private void tryFlowInto(World worldIn, BlockPos pos, IBlockState state, int level)
|
| 158 | {
|
| 159 | if (this.canFlowInto(worldIn, pos, state))
|
| 160 | {
|
| 161 | if (state.getBlock() != Blocks.air)
|
| 162 | {
|
| 163 | if (this.blockMaterial == Material.lava)
|
| 164 | {
|
| 165 | this.triggerMixEffects(worldIn, pos);
|
| 166 | }
|
| 167 | else
|
| 168 | {
|
| 169 | state.getBlock().dropBlockAsItem(worldIn, pos, state, 0);
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | worldIn.setBlockState(pos, this.getDefaultState().withProperty(LEVEL, Integer.valueOf(level)), 3);
|
| 174 | }
|
| 175 | }
|
| 176 |
|
| 177 | private int func_176374_a(World worldIn, BlockPos pos, int distance, EnumFacing calculateFlowCost)
|
| 178 | {
|
| 179 | int i = 1000;
|
| 180 |
|
| 181 | for (EnumFacing enumfacing : EnumFacing.Plane.HORIZONTAL)
|
| 182 | {
|
| 183 | if (enumfacing != calculateFlowCost)
|
| 184 | {
|
| 185 | BlockPos blockpos = pos.offset(enumfacing);
|
| 186 | IBlockState iblockstate = worldIn.getBlockState(blockpos);
|
| 187 |
|
| 188 | if (!this.isBlocked(worldIn, blockpos, iblockstate) && (iblockstate.getBlock().getMaterial() != this.blockMaterial || ((Integer)iblockstate.getValue(LEVEL)).intValue() > 0))
|
| 189 | {
|
| 190 | if (!this.isBlocked(worldIn, blockpos.down(), iblockstate))
|
| 191 | {
|
| 192 | return distance;
|
| 193 | }
|
| 194 |
|
| 195 | if (distance < 4)
|
| 196 | {
|
| 197 | int j = this.func_176374_a(worldIn, blockpos, distance + 1, enumfacing.getOpposite());
|
| 198 |
|
| 199 | if (j < i)
|
| 200 | {
|
| 201 | i = j;
|
| 202 | }
|
| 203 | }
|
| 204 | }
|
| 205 | }
|
| 206 | }
|
| 207 |
|
| 208 | return i;
|
| 209 | }
|
| 210 |
|
| 211 | private Set<EnumFacing> getPossibleFlowDirections(World worldIn, BlockPos pos)
|
| 212 | {
|
| 213 | int i = 1000;
|
| 214 | Set<EnumFacing> set = EnumSet.<EnumFacing>noneOf(EnumFacing.class);
|
| 215 |
|
| 216 | for (EnumFacing enumfacing : EnumFacing.Plane.HORIZONTAL)
|
| 217 | {
|
| 218 | BlockPos blockpos = pos.offset(enumfacing);
|
| 219 | IBlockState iblockstate = worldIn.getBlockState(blockpos);
|
| 220 |
|
| 221 | if (!this.isBlocked(worldIn, blockpos, iblockstate) && (iblockstate.getBlock().getMaterial() != this.blockMaterial || ((Integer)iblockstate.getValue(LEVEL)).intValue() > 0))
|
| 222 | {
|
| 223 | int j;
|
| 224 |
|
| 225 | if (this.isBlocked(worldIn, blockpos.down(), worldIn.getBlockState(blockpos.down())))
|
| 226 | {
|
| 227 | j = this.func_176374_a(worldIn, blockpos, 1, enumfacing.getOpposite());
|
| 228 | }
|
| 229 | else
|
| 230 | {
|
| 231 | j = 0;
|
| 232 | }
|
| 233 |
|
| 234 | if (j < i)
|
| 235 | {
|
| 236 | set.clear();
|
| 237 | }
|
| 238 |
|
| 239 | if (j <= i)
|
| 240 | {
|
| 241 | set.add(enumfacing);
|
| 242 | i = j;
|
| 243 | }
|
| 244 | }
|
| 245 | }
|
| 246 |
|
| 247 | return set;
|
| 248 | }
|
| 249 |
|
| 250 | private boolean isBlocked(World worldIn, BlockPos pos, IBlockState state)
|
| 251 | {
|
| 252 | Block block = worldIn.getBlockState(pos).getBlock();
|
| 253 | return !(block instanceof BlockDoor) && block != Blocks.standing_sign && block != Blocks.ladder && block != Blocks.reeds ? (block.blockMaterial == Material.portal ? true : block.blockMaterial.blocksMovement()) : true;
|
| 254 | }
|
| 255 |
|
| 256 | protected int checkAdjacentBlock(World worldIn, BlockPos pos, int currentMinLevel)
|
| 257 | {
|
| 258 | int i = this.getLevel(worldIn, pos);
|
| 259 |
|
| 260 | if (i < 0)
|
| 261 | {
|
| 262 | return currentMinLevel;
|
| 263 | }
|
| 264 | else
|
| 265 | {
|
| 266 | if (i == 0)
|
| 267 | {
|
| 268 | ++this.adjacentSourceBlocks;
|
| 269 | }
|
| 270 |
|
| 271 | if (i >= 8)
|
| 272 | {
|
| 273 | i = 0;
|
| 274 | }
|
| 275 |
|
| 276 | return currentMinLevel >= 0 && i >= currentMinLevel ? currentMinLevel : i;
|
| 277 | }
|
| 278 | }
|
| 279 |
|
| 280 | private boolean canFlowInto(World worldIn, BlockPos pos, IBlockState state)
|
| 281 | {
|
| 282 | Material material = state.getBlock().getMaterial();
|
| 283 | return material != this.blockMaterial && material != Material.lava && !this.isBlocked(worldIn, pos, state);
|
| 284 | }
|
| 285 |
|
| 286 | public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
|
| 287 | {
|
| 288 | if (!this.checkForMixing(worldIn, pos, state))
|
| 289 | {
|
| 290 | worldIn.scheduleUpdate(pos, this, this.tickRate(worldIn));
|
| 291 | }
|
| 292 | }
|
| 293 | }
|
| 294 |
|