| 1 | package net.minecraft.block;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.minecraft.block.material.Material;
|
| 5 | import net.minecraft.block.properties.IProperty;
|
| 6 | import net.minecraft.block.properties.PropertyBool;
|
| 7 | import net.minecraft.block.properties.PropertyDirection;
|
| 8 | import net.minecraft.block.state.BlockPistonStructureHelper;
|
| 9 | import net.minecraft.block.state.BlockState;
|
| 10 | import net.minecraft.block.state.IBlockState;
|
| 11 | import net.minecraft.creativetab.CreativeTabs;
|
| 12 | import net.minecraft.entity.Entity;
|
| 13 | import net.minecraft.entity.EntityLivingBase;
|
| 14 | import net.minecraft.init.Blocks;
|
| 15 | import net.minecraft.item.ItemStack;
|
| 16 | import net.minecraft.tileentity.TileEntity;
|
| 17 | import net.minecraft.tileentity.TileEntityPiston;
|
| 18 | import net.minecraft.util.AxisAlignedBB;
|
| 19 | import net.minecraft.util.BlockPos;
|
| 20 | import net.minecraft.util.EnumFacing;
|
| 21 | import net.minecraft.util.MathHelper;
|
| 22 | import net.minecraft.world.IBlockAccess;
|
| 23 | import net.minecraft.world.World;
|
| 24 |
|
| 25 | public class BlockPistonBase extends Block
|
| 26 | {
|
| 27 | public static final PropertyDirection FACING = PropertyDirection.create("facing");
|
| 28 | public static final PropertyBool EXTENDED = PropertyBool.create("extended");
|
| 29 |
|
| 30 | /** This piston is the sticky one? */
|
| 31 | private final boolean isSticky;
|
| 32 |
|
| 33 | public BlockPistonBase(boolean isSticky)
|
| 34 | {
|
| 35 | super(Material.piston);
|
| 36 | this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(EXTENDED, Boolean.valueOf(false)));
|
| 37 | this.isSticky = isSticky;
|
| 38 | this.setStepSound(soundTypePiston);
|
| 39 | this.setHardness(0.5F);
|
| 40 | this.setCreativeTab(CreativeTabs.tabRedstone);
|
| 41 | }
|
| 42 |
|
| 43 | /**
|
| 44 | * Used to determine ambient occlusion and culling when rebuilding chunks for render
|
| 45 | */
|
| 46 | public boolean isOpaqueCube()
|
| 47 | {
|
| 48 | return false;
|
| 49 | }
|
| 50 |
|
| 51 | /**
|
| 52 | * Called by ItemBlocks after a block is set in the world, to allow post-place logic
|
| 53 | */
|
| 54 | public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
|
| 55 | {
|
| 56 | worldIn.setBlockState(pos, state.withProperty(FACING, getFacingFromEntity(worldIn, pos, placer)), 2);
|
| 57 |
|
| 58 | if (!worldIn.isRemote)
|
| 59 | {
|
| 60 | this.checkForMove(worldIn, pos, state);
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | /**
|
| 65 | * Called when a neighboring block changes.
|
| 66 | */
|
| 67 | public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock)
|
| 68 | {
|
| 69 | if (!worldIn.isRemote)
|
| 70 | {
|
| 71 | this.checkForMove(worldIn, pos, state);
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
|
| 76 | {
|
| 77 | if (!worldIn.isRemote && worldIn.getTileEntity(pos) == null)
|
| 78 | {
|
| 79 | this.checkForMove(worldIn, pos, state);
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | /**
|
| 84 | * Called by ItemBlocks just before a block is actually set in the world, to allow for adjustments to the
|
| 85 | * IBlockstate
|
| 86 | */
|
| 87 | public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
|
| 88 | {
|
| 89 | return this.getDefaultState().withProperty(FACING, getFacingFromEntity(worldIn, pos, placer)).withProperty(EXTENDED, Boolean.valueOf(false));
|
| 90 | }
|
| 91 |
|
| 92 | private void checkForMove(World worldIn, BlockPos pos, IBlockState state)
|
| 93 | {
|
| 94 | EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
|
| 95 | boolean flag = this.shouldBeExtended(worldIn, pos, enumfacing);
|
| 96 |
|
| 97 | if (flag && !((Boolean)state.getValue(EXTENDED)).booleanValue())
|
| 98 | {
|
| 99 | if ((new BlockPistonStructureHelper(worldIn, pos, enumfacing, true)).canMove())
|
| 100 | {
|
| 101 | worldIn.addBlockEvent(pos, this, 0, enumfacing.getIndex());
|
| 102 | }
|
| 103 | }
|
| 104 | else if (!flag && ((Boolean)state.getValue(EXTENDED)).booleanValue())
|
| 105 | {
|
| 106 | worldIn.setBlockState(pos, state.withProperty(EXTENDED, Boolean.valueOf(false)), 2);
|
| 107 | worldIn.addBlockEvent(pos, this, 1, enumfacing.getIndex());
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | private boolean shouldBeExtended(World worldIn, BlockPos pos, EnumFacing facing)
|
| 112 | {
|
| 113 | for (EnumFacing enumfacing : EnumFacing.values())
|
| 114 | {
|
| 115 | if (enumfacing != facing && worldIn.isSidePowered(pos.offset(enumfacing), enumfacing))
|
| 116 | {
|
| 117 | return true;
|
| 118 | }
|
| 119 | }
|
| 120 |
|
| 121 | if (worldIn.isSidePowered(pos, EnumFacing.DOWN))
|
| 122 | {
|
| 123 | return true;
|
| 124 | }
|
| 125 | else
|
| 126 | {
|
| 127 | BlockPos blockpos = pos.up();
|
| 128 |
|
| 129 | for (EnumFacing enumfacing1 : EnumFacing.values())
|
| 130 | {
|
| 131 | if (enumfacing1 != EnumFacing.DOWN && worldIn.isSidePowered(blockpos.offset(enumfacing1), enumfacing1))
|
| 132 | {
|
| 133 | return true;
|
| 134 | }
|
| 135 | }
|
| 136 |
|
| 137 | return false;
|
| 138 | }
|
| 139 | }
|
| 140 |
|
| 141 | /**
|
| 142 | * Called on both Client and Server when World#addBlockEvent is called
|
| 143 | */
|
| 144 | public boolean onBlockEventReceived(World worldIn, BlockPos pos, IBlockState state, int eventID, int eventParam)
|
| 145 | {
|
| 146 | EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
|
| 147 |
|
| 148 | if (!worldIn.isRemote)
|
| 149 | {
|
| 150 | boolean flag = this.shouldBeExtended(worldIn, pos, enumfacing);
|
| 151 |
|
| 152 | if (flag && eventID == 1)
|
| 153 | {
|
| 154 | worldIn.setBlockState(pos, state.withProperty(EXTENDED, Boolean.valueOf(true)), 2);
|
| 155 | return false;
|
| 156 | }
|
| 157 |
|
| 158 | if (!flag && eventID == 0)
|
| 159 | {
|
| 160 | return false;
|
| 161 | }
|
| 162 | }
|
| 163 |
|
| 164 | if (eventID == 0)
|
| 165 | {
|
| 166 | if (!this.doMove(worldIn, pos, enumfacing, true))
|
| 167 | {
|
| 168 | return false;
|
| 169 | }
|
| 170 |
|
| 171 | worldIn.setBlockState(pos, state.withProperty(EXTENDED, Boolean.valueOf(true)), 2);
|
| 172 | worldIn.playSoundEffect((double)pos.getX() + 0.5D, (double)pos.getY() + 0.5D, (double)pos.getZ() + 0.5D, "tile.piston.out", 0.5F, worldIn.rand.nextFloat() * 0.25F + 0.6F);
|
| 173 | }
|
| 174 | else if (eventID == 1)
|
| 175 | {
|
| 176 | TileEntity tileentity1 = worldIn.getTileEntity(pos.offset(enumfacing));
|
| 177 |
|
| 178 | if (tileentity1 instanceof TileEntityPiston)
|
| 179 | {
|
| 180 | ((TileEntityPiston)tileentity1).clearPistonTileEntity();
|
| 181 | }
|
| 182 |
|
| 183 | worldIn.setBlockState(pos, Blocks.piston_extension.getDefaultState().withProperty(BlockPistonMoving.FACING, enumfacing).withProperty(BlockPistonMoving.TYPE, this.isSticky ? BlockPistonExtension.EnumPistonType.STICKY : BlockPistonExtension.EnumPistonType.DEFAULT), 3);
|
| 184 | worldIn.setTileEntity(pos, BlockPistonMoving.newTileEntity(this.getStateFromMeta(eventParam), enumfacing, false, true));
|
| 185 |
|
| 186 | if (this.isSticky)
|
| 187 | {
|
| 188 | BlockPos blockpos = pos.add(enumfacing.getFrontOffsetX() * 2, enumfacing.getFrontOffsetY() * 2, enumfacing.getFrontOffsetZ() * 2);
|
| 189 | Block block = worldIn.getBlockState(blockpos).getBlock();
|
| 190 | boolean flag1 = false;
|
| 191 |
|
| 192 | if (block == Blocks.piston_extension)
|
| 193 | {
|
| 194 | TileEntity tileentity = worldIn.getTileEntity(blockpos);
|
| 195 |
|
| 196 | if (tileentity instanceof TileEntityPiston)
|
| 197 | {
|
| 198 | TileEntityPiston tileentitypiston = (TileEntityPiston)tileentity;
|
| 199 |
|
| 200 | if (tileentitypiston.getFacing() == enumfacing && tileentitypiston.isExtending())
|
| 201 | {
|
| 202 | tileentitypiston.clearPistonTileEntity();
|
| 203 | flag1 = true;
|
| 204 | }
|
| 205 | }
|
| 206 | }
|
| 207 |
|
| 208 | if (!flag1 && block.getMaterial() != Material.air && canPush(block, worldIn, blockpos, enumfacing.getOpposite(), false) && (block.getMobilityFlag() == 0 || block == Blocks.piston || block == Blocks.sticky_piston))
|
| 209 | {
|
| 210 | this.doMove(worldIn, pos, enumfacing, false);
|
| 211 | }
|
| 212 | }
|
| 213 | else
|
| 214 | {
|
| 215 | worldIn.setBlockToAir(pos.offset(enumfacing));
|
| 216 | }
|
| 217 |
|
| 218 | worldIn.playSoundEffect((double)pos.getX() + 0.5D, (double)pos.getY() + 0.5D, (double)pos.getZ() + 0.5D, "tile.piston.in", 0.5F, worldIn.rand.nextFloat() * 0.15F + 0.6F);
|
| 219 | }
|
| 220 |
|
| 221 | return true;
|
| 222 | }
|
| 223 |
|
| 224 | public void setBlockBoundsBasedOnState(IBlockAccess worldIn, BlockPos pos)
|
| 225 | {
|
| 226 | IBlockState iblockstate = worldIn.getBlockState(pos);
|
| 227 |
|
| 228 | if (iblockstate.getBlock() == this && ((Boolean)iblockstate.getValue(EXTENDED)).booleanValue())
|
| 229 | {
|
| 230 | float f = 0.25F;
|
| 231 | EnumFacing enumfacing = (EnumFacing)iblockstate.getValue(FACING);
|
| 232 |
|
| 233 | if (enumfacing != null)
|
| 234 | {
|
| 235 | switch (enumfacing)
|
| 236 | {
|
| 237 | case DOWN:
|
| 238 | this.setBlockBounds(0.0F, 0.25F, 0.0F, 1.0F, 1.0F, 1.0F);
|
| 239 | break;
|
| 240 |
|
| 241 | case UP:
|
| 242 | this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.75F, 1.0F);
|
| 243 | break;
|
| 244 |
|
| 245 | case NORTH:
|
| 246 | this.setBlockBounds(0.0F, 0.0F, 0.25F, 1.0F, 1.0F, 1.0F);
|
| 247 | break;
|
| 248 |
|
| 249 | case SOUTH:
|
| 250 | this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 0.75F);
|
| 251 | break;
|
| 252 |
|
| 253 | case WEST:
|
| 254 | this.setBlockBounds(0.25F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
|
| 255 | break;
|
| 256 |
|
| 257 | case EAST:
|
| 258 | this.setBlockBounds(0.0F, 0.0F, 0.0F, 0.75F, 1.0F, 1.0F);
|
| 259 | }
|
| 260 | }
|
| 261 | }
|
| 262 | else
|
| 263 | {
|
| 264 | this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
|
| 265 | }
|
| 266 | }
|
| 267 |
|
| 268 | /**
|
| 269 | * Sets the block's bounds for rendering it as an item
|
| 270 | */
|
| 271 | public void setBlockBoundsForItemRender()
|
| 272 | {
|
| 273 | this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
|
| 274 | }
|
| 275 |
|
| 276 | /**
|
| 277 | * Add all collision boxes of this Block to the list that intersect with the given mask.
|
| 278 | */
|
| 279 | public void addCollisionBoxesToList(World worldIn, BlockPos pos, IBlockState state, AxisAlignedBB mask, List<AxisAlignedBB> list, Entity collidingEntity)
|
| 280 | {
|
| 281 | this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
|
| 282 | super.addCollisionBoxesToList(worldIn, pos, state, mask, list, collidingEntity);
|
| 283 | }
|
| 284 |
|
| 285 | public AxisAlignedBB getCollisionBoundingBox(World worldIn, BlockPos pos, IBlockState state)
|
| 286 | {
|
| 287 | this.setBlockBoundsBasedOnState(worldIn, pos);
|
| 288 | return super.getCollisionBoundingBox(worldIn, pos, state);
|
| 289 | }
|
| 290 |
|
| 291 | public boolean isFullCube()
|
| 292 | {
|
| 293 | return false;
|
| 294 | }
|
| 295 |
|
| 296 | public static EnumFacing getFacing(int meta)
|
| 297 | {
|
| 298 | int i = meta & 7;
|
| 299 | return i > 5 ? null : EnumFacing.getFront(i);
|
| 300 | }
|
| 301 |
|
| 302 | public static EnumFacing getFacingFromEntity(World worldIn, BlockPos clickedBlock, EntityLivingBase entityIn)
|
| 303 | {
|
| 304 | if (MathHelper.abs((float)entityIn.posX - (float)clickedBlock.getX()) < 2.0F && MathHelper.abs((float)entityIn.posZ - (float)clickedBlock.getZ()) < 2.0F)
|
| 305 | {
|
| 306 | double d0 = entityIn.posY + (double)entityIn.getEyeHeight();
|
| 307 |
|
| 308 | if (d0 - (double)clickedBlock.getY() > 2.0D)
|
| 309 | {
|
| 310 | return EnumFacing.UP;
|
| 311 | }
|
| 312 |
|
| 313 | if ((double)clickedBlock.getY() - d0 > 0.0D)
|
| 314 | {
|
| 315 | return EnumFacing.DOWN;
|
| 316 | }
|
| 317 | }
|
| 318 |
|
| 319 | return entityIn.getHorizontalFacing().getOpposite();
|
| 320 | }
|
| 321 |
|
| 322 | public static boolean canPush(Block blockIn, World worldIn, BlockPos pos, EnumFacing direction, boolean allowDestroy)
|
| 323 | {
|
| 324 | if (blockIn == Blocks.obsidian)
|
| 325 | {
|
| 326 | return false;
|
| 327 | }
|
| 328 | else if (!worldIn.getWorldBorder().contains(pos))
|
| 329 | {
|
| 330 | return false;
|
| 331 | }
|
| 332 | else if (pos.getY() >= 0 && (direction != EnumFacing.DOWN || pos.getY() != 0))
|
| 333 | {
|
| 334 | if (pos.getY() <= worldIn.getHeight() - 1 && (direction != EnumFacing.UP || pos.getY() != worldIn.getHeight() - 1))
|
| 335 | {
|
| 336 | if (blockIn != Blocks.piston && blockIn != Blocks.sticky_piston)
|
| 337 | {
|
| 338 | if (blockIn.getBlockHardness(worldIn, pos) == -1.0F)
|
| 339 | {
|
| 340 | return false;
|
| 341 | }
|
| 342 |
|
| 343 | if (blockIn.getMobilityFlag() == 2)
|
| 344 | {
|
| 345 | return false;
|
| 346 | }
|
| 347 |
|
| 348 | if (blockIn.getMobilityFlag() == 1)
|
| 349 | {
|
| 350 | if (!allowDestroy)
|
| 351 | {
|
| 352 | return false;
|
| 353 | }
|
| 354 |
|
| 355 | return true;
|
| 356 | }
|
| 357 | }
|
| 358 | else if (((Boolean)worldIn.getBlockState(pos).getValue(EXTENDED)).booleanValue())
|
| 359 | {
|
| 360 | return false;
|
| 361 | }
|
| 362 |
|
| 363 | return !(blockIn instanceof ITileEntityProvider);
|
| 364 | }
|
| 365 | else
|
| 366 | {
|
| 367 | return false;
|
| 368 | }
|
| 369 | }
|
| 370 | else
|
| 371 | {
|
| 372 | return false;
|
| 373 | }
|
| 374 | }
|
| 375 |
|
| 376 | private boolean doMove(World worldIn, BlockPos pos, EnumFacing direction, boolean extending)
|
| 377 | {
|
| 378 | if (!extending)
|
| 379 | {
|
| 380 | worldIn.setBlockToAir(pos.offset(direction));
|
| 381 | }
|
| 382 |
|
| 383 | BlockPistonStructureHelper blockpistonstructurehelper = new BlockPistonStructureHelper(worldIn, pos, direction, extending);
|
| 384 | List<BlockPos> list = blockpistonstructurehelper.getBlocksToMove();
|
| 385 | List<BlockPos> list1 = blockpistonstructurehelper.getBlocksToDestroy();
|
| 386 |
|
| 387 | if (!blockpistonstructurehelper.canMove())
|
| 388 | {
|
| 389 | return false;
|
| 390 | }
|
| 391 | else
|
| 392 | {
|
| 393 | int i = list.size() + list1.size();
|
| 394 | Block[] ablock = new Block[i];
|
| 395 | EnumFacing enumfacing = extending ? direction : direction.getOpposite();
|
| 396 |
|
| 397 | for (int j = list1.size() - 1; j >= 0; --j)
|
| 398 | {
|
| 399 | BlockPos blockpos = (BlockPos)list1.get(j);
|
| 400 | Block block = worldIn.getBlockState(blockpos).getBlock();
|
| 401 | block.dropBlockAsItem(worldIn, blockpos, worldIn.getBlockState(blockpos), 0);
|
| 402 | worldIn.setBlockToAir(blockpos);
|
| 403 | --i;
|
| 404 | ablock[i] = block;
|
| 405 | }
|
| 406 |
|
| 407 | for (int k = list.size() - 1; k >= 0; --k)
|
| 408 | {
|
| 409 | BlockPos blockpos2 = (BlockPos)list.get(k);
|
| 410 | IBlockState iblockstate = worldIn.getBlockState(blockpos2);
|
| 411 | Block block1 = iblockstate.getBlock();
|
| 412 | block1.getMetaFromState(iblockstate);
|
| 413 | worldIn.setBlockToAir(blockpos2);
|
| 414 | blockpos2 = blockpos2.offset(enumfacing);
|
| 415 | worldIn.setBlockState(blockpos2, Blocks.piston_extension.getDefaultState().withProperty(FACING, direction), 4);
|
| 416 | worldIn.setTileEntity(blockpos2, BlockPistonMoving.newTileEntity(iblockstate, direction, extending, false));
|
| 417 | --i;
|
| 418 | ablock[i] = block1;
|
| 419 | }
|
| 420 |
|
| 421 | BlockPos blockpos1 = pos.offset(direction);
|
| 422 |
|
| 423 | if (extending)
|
| 424 | {
|
| 425 | BlockPistonExtension.EnumPistonType blockpistonextension$enumpistontype = this.isSticky ? BlockPistonExtension.EnumPistonType.STICKY : BlockPistonExtension.EnumPistonType.DEFAULT;
|
| 426 | IBlockState iblockstate1 = Blocks.piston_head.getDefaultState().withProperty(BlockPistonExtension.FACING, direction).withProperty(BlockPistonExtension.TYPE, blockpistonextension$enumpistontype);
|
| 427 | IBlockState iblockstate2 = Blocks.piston_extension.getDefaultState().withProperty(BlockPistonMoving.FACING, direction).withProperty(BlockPistonMoving.TYPE, this.isSticky ? BlockPistonExtension.EnumPistonType.STICKY : BlockPistonExtension.EnumPistonType.DEFAULT);
|
| 428 | worldIn.setBlockState(blockpos1, iblockstate2, 4);
|
| 429 | worldIn.setTileEntity(blockpos1, BlockPistonMoving.newTileEntity(iblockstate1, direction, true, false));
|
| 430 | }
|
| 431 |
|
| 432 | for (int l = list1.size() - 1; l >= 0; --l)
|
| 433 | {
|
| 434 | worldIn.notifyNeighborsOfStateChange((BlockPos)list1.get(l), ablock[i++]);
|
| 435 | }
|
| 436 |
|
| 437 | for (int i1 = list.size() - 1; i1 >= 0; --i1)
|
| 438 | {
|
| 439 | worldIn.notifyNeighborsOfStateChange((BlockPos)list.get(i1), ablock[i++]);
|
| 440 | }
|
| 441 |
|
| 442 | if (extending)
|
| 443 | {
|
| 444 | worldIn.notifyNeighborsOfStateChange(blockpos1, Blocks.piston_head);
|
| 445 | worldIn.notifyNeighborsOfStateChange(pos, this);
|
| 446 | }
|
| 447 |
|
| 448 | return true;
|
| 449 | }
|
| 450 | }
|
| 451 |
|
| 452 | /**
|
| 453 | * Possibly modify the given BlockState before rendering it on an Entity (Minecarts, Endermen, ...)
|
| 454 | */
|
| 455 | public IBlockState getStateForEntityRender(IBlockState state)
|
| 456 | {
|
| 457 | return this.getDefaultState().withProperty(FACING, EnumFacing.UP);
|
| 458 | }
|
| 459 |
|
| 460 | /**
|
| 461 | * Convert the given metadata into a BlockState for this Block
|
| 462 | */
|
| 463 | public IBlockState getStateFromMeta(int meta)
|
| 464 | {
|
| 465 | return this.getDefaultState().withProperty(FACING, getFacing(meta)).withProperty(EXTENDED, Boolean.valueOf((meta & 8) > 0));
|
| 466 | }
|
| 467 |
|
| 468 | /**
|
| 469 | * Convert the BlockState into the correct metadata value
|
| 470 | */
|
| 471 | public int getMetaFromState(IBlockState state)
|
| 472 | {
|
| 473 | int i = 0;
|
| 474 | i = i | ((EnumFacing)state.getValue(FACING)).getIndex();
|
| 475 |
|
| 476 | if (((Boolean)state.getValue(EXTENDED)).booleanValue())
|
| 477 | {
|
| 478 | i |= 8;
|
| 479 | }
|
| 480 |
|
| 481 | return i;
|
| 482 | }
|
| 483 |
|
| 484 | protected BlockState createBlockState()
|
| 485 | {
|
| 486 | return new BlockState(this, new IProperty[] {FACING, EXTENDED});
|
| 487 | }
|
| 488 | }
|
| 489 |
|