| 1 | package net.minecraft.block;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.Random;
|
| 5 | import net.minecraft.block.material.Material;
|
| 6 | import net.minecraft.block.properties.IProperty;
|
| 7 | import net.minecraft.block.properties.PropertyBool;
|
| 8 | import net.minecraft.block.properties.PropertyDirection;
|
| 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.entity.player.EntityPlayer;
|
| 15 | import net.minecraft.entity.projectile.EntityArrow;
|
| 16 | import net.minecraft.util.AxisAlignedBB;
|
| 17 | import net.minecraft.util.BlockPos;
|
| 18 | import net.minecraft.util.EnumFacing;
|
| 19 | import net.minecraft.world.IBlockAccess;
|
| 20 | import net.minecraft.world.World;
|
| 21 |
|
| 22 | public abstract class BlockButton extends Block
|
| 23 | {
|
| 24 | public static final PropertyDirection FACING = PropertyDirection.create("facing");
|
| 25 | public static final PropertyBool POWERED = PropertyBool.create("powered");
|
| 26 | private final boolean wooden;
|
| 27 |
|
| 28 | protected BlockButton(boolean wooden)
|
| 29 | {
|
| 30 | super(Material.circuits);
|
| 31 | this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(POWERED, Boolean.valueOf(false)));
|
| 32 | this.setTickRandomly(true);
|
| 33 | this.setCreativeTab(CreativeTabs.tabRedstone);
|
| 34 | this.wooden = wooden;
|
| 35 | }
|
| 36 |
|
| 37 | public AxisAlignedBB getCollisionBoundingBox(World worldIn, BlockPos pos, IBlockState state)
|
| 38 | {
|
| 39 | return null;
|
| 40 | }
|
| 41 |
|
| 42 | /**
|
| 43 | * How many world ticks before ticking
|
| 44 | */
|
| 45 | public int tickRate(World worldIn)
|
| 46 | {
|
| 47 | return this.wooden ? 30 : 20;
|
| 48 | }
|
| 49 |
|
| 50 | /**
|
| 51 | * Used to determine ambient occlusion and culling when rebuilding chunks for render
|
| 52 | */
|
| 53 | public boolean isOpaqueCube()
|
| 54 | {
|
| 55 | return false;
|
| 56 | }
|
| 57 |
|
| 58 | public boolean isFullCube()
|
| 59 | {
|
| 60 | return false;
|
| 61 | }
|
| 62 |
|
| 63 | /**
|
| 64 | * Check whether this Block can be placed on the given side
|
| 65 | */
|
| 66 | public boolean canPlaceBlockOnSide(World worldIn, BlockPos pos, EnumFacing side)
|
| 67 | {
|
| 68 | return func_181088_a(worldIn, pos, side.getOpposite());
|
| 69 | }
|
| 70 |
|
| 71 | public boolean canPlaceBlockAt(World worldIn, BlockPos pos)
|
| 72 | {
|
| 73 | for (EnumFacing enumfacing : EnumFacing.values())
|
| 74 | {
|
| 75 | if (func_181088_a(worldIn, pos, enumfacing))
|
| 76 | {
|
| 77 | return true;
|
| 78 | }
|
| 79 | }
|
| 80 |
|
| 81 | return false;
|
| 82 | }
|
| 83 |
|
| 84 | protected static boolean func_181088_a(World p_181088_0_, BlockPos p_181088_1_, EnumFacing p_181088_2_)
|
| 85 | {
|
| 86 | BlockPos blockpos = p_181088_1_.offset(p_181088_2_);
|
| 87 | return p_181088_2_ == EnumFacing.DOWN ? World.doesBlockHaveSolidTopSurface(p_181088_0_, blockpos) : p_181088_0_.getBlockState(blockpos).getBlock().isNormalCube();
|
| 88 | }
|
| 89 |
|
| 90 | /**
|
| 91 | * Called by ItemBlocks just before a block is actually set in the world, to allow for adjustments to the
|
| 92 | * IBlockstate
|
| 93 | */
|
| 94 | public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
|
| 95 | {
|
| 96 | return func_181088_a(worldIn, pos, facing.getOpposite()) ? this.getDefaultState().withProperty(FACING, facing).withProperty(POWERED, Boolean.valueOf(false)) : this.getDefaultState().withProperty(FACING, EnumFacing.DOWN).withProperty(POWERED, Boolean.valueOf(false));
|
| 97 | }
|
| 98 |
|
| 99 | /**
|
| 100 | * Called when a neighboring block changes.
|
| 101 | */
|
| 102 | public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock)
|
| 103 | {
|
| 104 | if (this.checkForDrop(worldIn, pos, state) && !func_181088_a(worldIn, pos, ((EnumFacing)state.getValue(FACING)).getOpposite()))
|
| 105 | {
|
| 106 | this.dropBlockAsItem(worldIn, pos, state, 0);
|
| 107 | worldIn.setBlockToAir(pos);
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | private boolean checkForDrop(World worldIn, BlockPos pos, IBlockState state)
|
| 112 | {
|
| 113 | if (this.canPlaceBlockAt(worldIn, pos))
|
| 114 | {
|
| 115 | return true;
|
| 116 | }
|
| 117 | else
|
| 118 | {
|
| 119 | this.dropBlockAsItem(worldIn, pos, state, 0);
|
| 120 | worldIn.setBlockToAir(pos);
|
| 121 | return false;
|
| 122 | }
|
| 123 | }
|
| 124 |
|
| 125 | public void setBlockBoundsBasedOnState(IBlockAccess worldIn, BlockPos pos)
|
| 126 | {
|
| 127 | this.updateBlockBounds(worldIn.getBlockState(pos));
|
| 128 | }
|
| 129 |
|
| 130 | private void updateBlockBounds(IBlockState state)
|
| 131 | {
|
| 132 | EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
|
| 133 | boolean flag = ((Boolean)state.getValue(POWERED)).booleanValue();
|
| 134 | float f = 0.25F;
|
| 135 | float f1 = 0.375F;
|
| 136 | float f2 = (float)(flag ? 1 : 2) / 16.0F;
|
| 137 | float f3 = 0.125F;
|
| 138 | float f4 = 0.1875F;
|
| 139 |
|
| 140 | switch (enumfacing)
|
| 141 | {
|
| 142 | case EAST:
|
| 143 | this.setBlockBounds(0.0F, 0.375F, 0.3125F, f2, 0.625F, 0.6875F);
|
| 144 | break;
|
| 145 |
|
| 146 | case WEST:
|
| 147 | this.setBlockBounds(1.0F - f2, 0.375F, 0.3125F, 1.0F, 0.625F, 0.6875F);
|
| 148 | break;
|
| 149 |
|
| 150 | case SOUTH:
|
| 151 | this.setBlockBounds(0.3125F, 0.375F, 0.0F, 0.6875F, 0.625F, f2);
|
| 152 | break;
|
| 153 |
|
| 154 | case NORTH:
|
| 155 | this.setBlockBounds(0.3125F, 0.375F, 1.0F - f2, 0.6875F, 0.625F, 1.0F);
|
| 156 | break;
|
| 157 |
|
| 158 | case UP:
|
| 159 | this.setBlockBounds(0.3125F, 0.0F, 0.375F, 0.6875F, 0.0F + f2, 0.625F);
|
| 160 | break;
|
| 161 |
|
| 162 | case DOWN:
|
| 163 | this.setBlockBounds(0.3125F, 1.0F - f2, 0.375F, 0.6875F, 1.0F, 0.625F);
|
| 164 | }
|
| 165 | }
|
| 166 |
|
| 167 | public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
|
| 168 | {
|
| 169 | if (((Boolean)state.getValue(POWERED)).booleanValue())
|
| 170 | {
|
| 171 | return true;
|
| 172 | }
|
| 173 | else
|
| 174 | {
|
| 175 | worldIn.setBlockState(pos, state.withProperty(POWERED, Boolean.valueOf(true)), 3);
|
| 176 | worldIn.markBlockRangeForRenderUpdate(pos, pos);
|
| 177 | worldIn.playSoundEffect((double)pos.getX() + 0.5D, (double)pos.getY() + 0.5D, (double)pos.getZ() + 0.5D, "random.click", 0.3F, 0.6F);
|
| 178 | this.notifyNeighbors(worldIn, pos, (EnumFacing)state.getValue(FACING));
|
| 179 | worldIn.scheduleUpdate(pos, this, this.tickRate(worldIn));
|
| 180 | return true;
|
| 181 | }
|
| 182 | }
|
| 183 |
|
| 184 | public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
|
| 185 | {
|
| 186 | if (((Boolean)state.getValue(POWERED)).booleanValue())
|
| 187 | {
|
| 188 | this.notifyNeighbors(worldIn, pos, (EnumFacing)state.getValue(FACING));
|
| 189 | }
|
| 190 |
|
| 191 | super.breakBlock(worldIn, pos, state);
|
| 192 | }
|
| 193 |
|
| 194 | public int getWeakPower(IBlockAccess worldIn, BlockPos pos, IBlockState state, EnumFacing side)
|
| 195 | {
|
| 196 | return ((Boolean)state.getValue(POWERED)).booleanValue() ? 15 : 0;
|
| 197 | }
|
| 198 |
|
| 199 | public int getStrongPower(IBlockAccess worldIn, BlockPos pos, IBlockState state, EnumFacing side)
|
| 200 | {
|
| 201 | return !((Boolean)state.getValue(POWERED)).booleanValue() ? 0 : (state.getValue(FACING) == side ? 15 : 0);
|
| 202 | }
|
| 203 |
|
| 204 | /**
|
| 205 | * Can this block provide power. Only wire currently seems to have this change based on its state.
|
| 206 | */
|
| 207 | public boolean canProvidePower()
|
| 208 | {
|
| 209 | return true;
|
| 210 | }
|
| 211 |
|
| 212 | /**
|
| 213 | * Called randomly when setTickRandomly is set to true (used by e.g. crops to grow, etc.)
|
| 214 | */
|
| 215 | public void randomTick(World worldIn, BlockPos pos, IBlockState state, Random random)
|
| 216 | {
|
| 217 | }
|
| 218 |
|
| 219 | public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
|
| 220 | {
|
| 221 | if (!worldIn.isRemote)
|
| 222 | {
|
| 223 | if (((Boolean)state.getValue(POWERED)).booleanValue())
|
| 224 | {
|
| 225 | if (this.wooden)
|
| 226 | {
|
| 227 | this.checkForArrows(worldIn, pos, state);
|
| 228 | }
|
| 229 | else
|
| 230 | {
|
| 231 | worldIn.setBlockState(pos, state.withProperty(POWERED, Boolean.valueOf(false)));
|
| 232 | this.notifyNeighbors(worldIn, pos, (EnumFacing)state.getValue(FACING));
|
| 233 | worldIn.playSoundEffect((double)pos.getX() + 0.5D, (double)pos.getY() + 0.5D, (double)pos.getZ() + 0.5D, "random.click", 0.3F, 0.5F);
|
| 234 | worldIn.markBlockRangeForRenderUpdate(pos, pos);
|
| 235 | }
|
| 236 | }
|
| 237 | }
|
| 238 | }
|
| 239 |
|
| 240 | /**
|
| 241 | * Sets the block's bounds for rendering it as an item
|
| 242 | */
|
| 243 | public void setBlockBoundsForItemRender()
|
| 244 | {
|
| 245 | float f = 0.1875F;
|
| 246 | float f1 = 0.125F;
|
| 247 | float f2 = 0.125F;
|
| 248 | this.setBlockBounds(0.5F - f, 0.5F - f1, 0.5F - f2, 0.5F + f, 0.5F + f1, 0.5F + f2);
|
| 249 | }
|
| 250 |
|
| 251 | /**
|
| 252 | * Called When an Entity Collided with the Block
|
| 253 | */
|
| 254 | public void onEntityCollidedWithBlock(World worldIn, BlockPos pos, IBlockState state, Entity entityIn)
|
| 255 | {
|
| 256 | if (!worldIn.isRemote)
|
| 257 | {
|
| 258 | if (this.wooden)
|
| 259 | {
|
| 260 | if (!((Boolean)state.getValue(POWERED)).booleanValue())
|
| 261 | {
|
| 262 | this.checkForArrows(worldIn, pos, state);
|
| 263 | }
|
| 264 | }
|
| 265 | }
|
| 266 | }
|
| 267 |
|
| 268 | private void checkForArrows(World worldIn, BlockPos pos, IBlockState state)
|
| 269 | {
|
| 270 | this.updateBlockBounds(state);
|
| 271 | List <? extends Entity > list = worldIn.<Entity>getEntitiesWithinAABB(EntityArrow.class, new AxisAlignedBB((double)pos.getX() + this.minX, (double)pos.getY() + this.minY, (double)pos.getZ() + this.minZ, (double)pos.getX() + this.maxX, (double)pos.getY() + this.maxY, (double)pos.getZ() + this.maxZ));
|
| 272 | boolean flag = !list.isEmpty();
|
| 273 | boolean flag1 = ((Boolean)state.getValue(POWERED)).booleanValue();
|
| 274 |
|
| 275 | if (flag && !flag1)
|
| 276 | {
|
| 277 | worldIn.setBlockState(pos, state.withProperty(POWERED, Boolean.valueOf(true)));
|
| 278 | this.notifyNeighbors(worldIn, pos, (EnumFacing)state.getValue(FACING));
|
| 279 | worldIn.markBlockRangeForRenderUpdate(pos, pos);
|
| 280 | worldIn.playSoundEffect((double)pos.getX() + 0.5D, (double)pos.getY() + 0.5D, (double)pos.getZ() + 0.5D, "random.click", 0.3F, 0.6F);
|
| 281 | }
|
| 282 |
|
| 283 | if (!flag && flag1)
|
| 284 | {
|
| 285 | worldIn.setBlockState(pos, state.withProperty(POWERED, Boolean.valueOf(false)));
|
| 286 | this.notifyNeighbors(worldIn, pos, (EnumFacing)state.getValue(FACING));
|
| 287 | worldIn.markBlockRangeForRenderUpdate(pos, pos);
|
| 288 | worldIn.playSoundEffect((double)pos.getX() + 0.5D, (double)pos.getY() + 0.5D, (double)pos.getZ() + 0.5D, "random.click", 0.3F, 0.5F);
|
| 289 | }
|
| 290 |
|
| 291 | if (flag)
|
| 292 | {
|
| 293 | worldIn.scheduleUpdate(pos, this, this.tickRate(worldIn));
|
| 294 | }
|
| 295 | }
|
| 296 |
|
| 297 | private void notifyNeighbors(World worldIn, BlockPos pos, EnumFacing facing)
|
| 298 | {
|
| 299 | worldIn.notifyNeighborsOfStateChange(pos, this);
|
| 300 | worldIn.notifyNeighborsOfStateChange(pos.offset(facing.getOpposite()), this);
|
| 301 | }
|
| 302 |
|
| 303 | /**
|
| 304 | * Convert the given metadata into a BlockState for this Block
|
| 305 | */
|
| 306 | public IBlockState getStateFromMeta(int meta)
|
| 307 | {
|
| 308 | EnumFacing enumfacing;
|
| 309 |
|
| 310 | switch (meta & 7)
|
| 311 | {
|
| 312 | case 0:
|
| 313 | enumfacing = EnumFacing.DOWN;
|
| 314 | break;
|
| 315 |
|
| 316 | case 1:
|
| 317 | enumfacing = EnumFacing.EAST;
|
| 318 | break;
|
| 319 |
|
| 320 | case 2:
|
| 321 | enumfacing = EnumFacing.WEST;
|
| 322 | break;
|
| 323 |
|
| 324 | case 3:
|
| 325 | enumfacing = EnumFacing.SOUTH;
|
| 326 | break;
|
| 327 |
|
| 328 | case 4:
|
| 329 | enumfacing = EnumFacing.NORTH;
|
| 330 | break;
|
| 331 |
|
| 332 | case 5:
|
| 333 | default:
|
| 334 | enumfacing = EnumFacing.UP;
|
| 335 | }
|
| 336 |
|
| 337 | return this.getDefaultState().withProperty(FACING, enumfacing).withProperty(POWERED, Boolean.valueOf((meta & 8) > 0));
|
| 338 | }
|
| 339 |
|
| 340 | /**
|
| 341 | * Convert the BlockState into the correct metadata value
|
| 342 | */
|
| 343 | public int getMetaFromState(IBlockState state)
|
| 344 | {
|
| 345 | int i;
|
| 346 |
|
| 347 | switch ((EnumFacing)state.getValue(FACING))
|
| 348 | {
|
| 349 | case EAST:
|
| 350 | i = 1;
|
| 351 | break;
|
| 352 |
|
| 353 | case WEST:
|
| 354 | i = 2;
|
| 355 | break;
|
| 356 |
|
| 357 | case SOUTH:
|
| 358 | i = 3;
|
| 359 | break;
|
| 360 |
|
| 361 | case NORTH:
|
| 362 | i = 4;
|
| 363 | break;
|
| 364 |
|
| 365 | case UP:
|
| 366 | default:
|
| 367 | i = 5;
|
| 368 | break;
|
| 369 |
|
| 370 | case DOWN:
|
| 371 | i = 0;
|
| 372 | }
|
| 373 |
|
| 374 | if (((Boolean)state.getValue(POWERED)).booleanValue())
|
| 375 | {
|
| 376 | i |= 8;
|
| 377 | }
|
| 378 |
|
| 379 | return i;
|
| 380 | }
|
| 381 |
|
| 382 | protected BlockState createBlockState()
|
| 383 | {
|
| 384 | return new BlockState(this, new IProperty[] {FACING, POWERED});
|
| 385 | }
|
| 386 | }
|
| 387 |
|