| 1 | package net.minecraft.block;
|
| 2 |
|
| 3 | import net.minecraft.block.material.Material;
|
| 4 | import net.minecraft.block.properties.IProperty;
|
| 5 | import net.minecraft.block.properties.PropertyBool;
|
| 6 | import net.minecraft.block.properties.PropertyEnum;
|
| 7 | import net.minecraft.block.state.BlockState;
|
| 8 | import net.minecraft.block.state.IBlockState;
|
| 9 | import net.minecraft.creativetab.CreativeTabs;
|
| 10 | import net.minecraft.entity.EntityLivingBase;
|
| 11 | import net.minecraft.entity.player.EntityPlayer;
|
| 12 | import net.minecraft.util.AxisAlignedBB;
|
| 13 | import net.minecraft.util.BlockPos;
|
| 14 | import net.minecraft.util.EnumFacing;
|
| 15 | import net.minecraft.util.IStringSerializable;
|
| 16 | import net.minecraft.world.IBlockAccess;
|
| 17 | import net.minecraft.world.World;
|
| 18 |
|
| 19 | public class BlockLever extends Block
|
| 20 | {
|
| 21 | public static final PropertyEnum<BlockLever.EnumOrientation> FACING = PropertyEnum.<BlockLever.EnumOrientation>create("facing", BlockLever.EnumOrientation.class);
|
| 22 | public static final PropertyBool POWERED = PropertyBool.create("powered");
|
| 23 |
|
| 24 | protected BlockLever()
|
| 25 | {
|
| 26 | super(Material.circuits);
|
| 27 | this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, BlockLever.EnumOrientation.NORTH).withProperty(POWERED, Boolean.valueOf(false)));
|
| 28 | this.setCreativeTab(CreativeTabs.tabRedstone);
|
| 29 | }
|
| 30 |
|
| 31 | public AxisAlignedBB getCollisionBoundingBox(World worldIn, BlockPos pos, IBlockState state)
|
| 32 | {
|
| 33 | return null;
|
| 34 | }
|
| 35 |
|
| 36 | /**
|
| 37 | * Used to determine ambient occlusion and culling when rebuilding chunks for render
|
| 38 | */
|
| 39 | public boolean isOpaqueCube()
|
| 40 | {
|
| 41 | return false;
|
| 42 | }
|
| 43 |
|
| 44 | public boolean isFullCube()
|
| 45 | {
|
| 46 | return false;
|
| 47 | }
|
| 48 |
|
| 49 | /**
|
| 50 | * Check whether this Block can be placed on the given side
|
| 51 | */
|
| 52 | public boolean canPlaceBlockOnSide(World worldIn, BlockPos pos, EnumFacing side)
|
| 53 | {
|
| 54 | return func_181090_a(worldIn, pos, side.getOpposite());
|
| 55 | }
|
| 56 |
|
| 57 | public boolean canPlaceBlockAt(World worldIn, BlockPos pos)
|
| 58 | {
|
| 59 | for (EnumFacing enumfacing : EnumFacing.values())
|
| 60 | {
|
| 61 | if (func_181090_a(worldIn, pos, enumfacing))
|
| 62 | {
|
| 63 | return true;
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | return false;
|
| 68 | }
|
| 69 |
|
| 70 | protected static boolean func_181090_a(World p_181090_0_, BlockPos p_181090_1_, EnumFacing p_181090_2_)
|
| 71 | {
|
| 72 | return BlockButton.func_181088_a(p_181090_0_, p_181090_1_, p_181090_2_);
|
| 73 | }
|
| 74 |
|
| 75 | /**
|
| 76 | * Called by ItemBlocks just before a block is actually set in the world, to allow for adjustments to the
|
| 77 | * IBlockstate
|
| 78 | */
|
| 79 | public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
|
| 80 | {
|
| 81 | IBlockState iblockstate = this.getDefaultState().withProperty(POWERED, Boolean.valueOf(false));
|
| 82 |
|
| 83 | if (func_181090_a(worldIn, pos, facing.getOpposite()))
|
| 84 | {
|
| 85 | return iblockstate.withProperty(FACING, BlockLever.EnumOrientation.forFacings(facing, placer.getHorizontalFacing()));
|
| 86 | }
|
| 87 | else
|
| 88 | {
|
| 89 | for (EnumFacing enumfacing : EnumFacing.Plane.HORIZONTAL)
|
| 90 | {
|
| 91 | if (enumfacing != facing && func_181090_a(worldIn, pos, enumfacing.getOpposite()))
|
| 92 | {
|
| 93 | return iblockstate.withProperty(FACING, BlockLever.EnumOrientation.forFacings(enumfacing, placer.getHorizontalFacing()));
|
| 94 | }
|
| 95 | }
|
| 96 |
|
| 97 | if (World.doesBlockHaveSolidTopSurface(worldIn, pos.down()))
|
| 98 | {
|
| 99 | return iblockstate.withProperty(FACING, BlockLever.EnumOrientation.forFacings(EnumFacing.UP, placer.getHorizontalFacing()));
|
| 100 | }
|
| 101 | else
|
| 102 | {
|
| 103 | return iblockstate;
|
| 104 | }
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | public static int getMetadataForFacing(EnumFacing facing)
|
| 109 | {
|
| 110 | switch (facing)
|
| 111 | {
|
| 112 | case DOWN:
|
| 113 | return 0;
|
| 114 |
|
| 115 | case UP:
|
| 116 | return 5;
|
| 117 |
|
| 118 | case NORTH:
|
| 119 | return 4;
|
| 120 |
|
| 121 | case SOUTH:
|
| 122 | return 3;
|
| 123 |
|
| 124 | case WEST:
|
| 125 | return 2;
|
| 126 |
|
| 127 | case EAST:
|
| 128 | return 1;
|
| 129 |
|
| 130 | default:
|
| 131 | return -1;
|
| 132 | }
|
| 133 | }
|
| 134 |
|
| 135 | /**
|
| 136 | * Called when a neighboring block changes.
|
| 137 | */
|
| 138 | public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock)
|
| 139 | {
|
| 140 | if (this.func_181091_e(worldIn, pos, state) && !func_181090_a(worldIn, pos, ((BlockLever.EnumOrientation)state.getValue(FACING)).getFacing().getOpposite()))
|
| 141 | {
|
| 142 | this.dropBlockAsItem(worldIn, pos, state, 0);
|
| 143 | worldIn.setBlockToAir(pos);
|
| 144 | }
|
| 145 | }
|
| 146 |
|
| 147 | private boolean func_181091_e(World p_181091_1_, BlockPos p_181091_2_, IBlockState p_181091_3_)
|
| 148 | {
|
| 149 | if (this.canPlaceBlockAt(p_181091_1_, p_181091_2_))
|
| 150 | {
|
| 151 | return true;
|
| 152 | }
|
| 153 | else
|
| 154 | {
|
| 155 | this.dropBlockAsItem(p_181091_1_, p_181091_2_, p_181091_3_, 0);
|
| 156 | p_181091_1_.setBlockToAir(p_181091_2_);
|
| 157 | return false;
|
| 158 | }
|
| 159 | }
|
| 160 |
|
| 161 | public void setBlockBoundsBasedOnState(IBlockAccess worldIn, BlockPos pos)
|
| 162 | {
|
| 163 | float f = 0.1875F;
|
| 164 |
|
| 165 | switch ((BlockLever.EnumOrientation)worldIn.getBlockState(pos).getValue(FACING))
|
| 166 | {
|
| 167 | case EAST:
|
| 168 | this.setBlockBounds(0.0F, 0.2F, 0.5F - f, f * 2.0F, 0.8F, 0.5F + f);
|
| 169 | break;
|
| 170 |
|
| 171 | case WEST:
|
| 172 | this.setBlockBounds(1.0F - f * 2.0F, 0.2F, 0.5F - f, 1.0F, 0.8F, 0.5F + f);
|
| 173 | break;
|
| 174 |
|
| 175 | case SOUTH:
|
| 176 | this.setBlockBounds(0.5F - f, 0.2F, 0.0F, 0.5F + f, 0.8F, f * 2.0F);
|
| 177 | break;
|
| 178 |
|
| 179 | case NORTH:
|
| 180 | this.setBlockBounds(0.5F - f, 0.2F, 1.0F - f * 2.0F, 0.5F + f, 0.8F, 1.0F);
|
| 181 | break;
|
| 182 |
|
| 183 | case UP_Z:
|
| 184 | case UP_X:
|
| 185 | f = 0.25F;
|
| 186 | this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, 0.6F, 0.5F + f);
|
| 187 | break;
|
| 188 |
|
| 189 | case DOWN_X:
|
| 190 | case DOWN_Z:
|
| 191 | f = 0.25F;
|
| 192 | this.setBlockBounds(0.5F - f, 0.4F, 0.5F - f, 0.5F + f, 1.0F, 0.5F + f);
|
| 193 | }
|
| 194 | }
|
| 195 |
|
| 196 | public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
|
| 197 | {
|
| 198 | if (worldIn.isRemote)
|
| 199 | {
|
| 200 | return true;
|
| 201 | }
|
| 202 | else
|
| 203 | {
|
| 204 | state = state.cycleProperty(POWERED);
|
| 205 | worldIn.setBlockState(pos, state, 3);
|
| 206 | worldIn.playSoundEffect((double)pos.getX() + 0.5D, (double)pos.getY() + 0.5D, (double)pos.getZ() + 0.5D, "random.click", 0.3F, ((Boolean)state.getValue(POWERED)).booleanValue() ? 0.6F : 0.5F);
|
| 207 | worldIn.notifyNeighborsOfStateChange(pos, this);
|
| 208 | EnumFacing enumfacing = ((BlockLever.EnumOrientation)state.getValue(FACING)).getFacing();
|
| 209 | worldIn.notifyNeighborsOfStateChange(pos.offset(enumfacing.getOpposite()), this);
|
| 210 | return true;
|
| 211 | }
|
| 212 | }
|
| 213 |
|
| 214 | public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
|
| 215 | {
|
| 216 | if (((Boolean)state.getValue(POWERED)).booleanValue())
|
| 217 | {
|
| 218 | worldIn.notifyNeighborsOfStateChange(pos, this);
|
| 219 | EnumFacing enumfacing = ((BlockLever.EnumOrientation)state.getValue(FACING)).getFacing();
|
| 220 | worldIn.notifyNeighborsOfStateChange(pos.offset(enumfacing.getOpposite()), this);
|
| 221 | }
|
| 222 |
|
| 223 | super.breakBlock(worldIn, pos, state);
|
| 224 | }
|
| 225 |
|
| 226 | public int getWeakPower(IBlockAccess worldIn, BlockPos pos, IBlockState state, EnumFacing side)
|
| 227 | {
|
| 228 | return ((Boolean)state.getValue(POWERED)).booleanValue() ? 15 : 0;
|
| 229 | }
|
| 230 |
|
| 231 | public int getStrongPower(IBlockAccess worldIn, BlockPos pos, IBlockState state, EnumFacing side)
|
| 232 | {
|
| 233 | return !((Boolean)state.getValue(POWERED)).booleanValue() ? 0 : (((BlockLever.EnumOrientation)state.getValue(FACING)).getFacing() == side ? 15 : 0);
|
| 234 | }
|
| 235 |
|
| 236 | /**
|
| 237 | * Can this block provide power. Only wire currently seems to have this change based on its state.
|
| 238 | */
|
| 239 | public boolean canProvidePower()
|
| 240 | {
|
| 241 | return true;
|
| 242 | }
|
| 243 |
|
| 244 | /**
|
| 245 | * Convert the given metadata into a BlockState for this Block
|
| 246 | */
|
| 247 | public IBlockState getStateFromMeta(int meta)
|
| 248 | {
|
| 249 | return this.getDefaultState().withProperty(FACING, BlockLever.EnumOrientation.byMetadata(meta & 7)).withProperty(POWERED, Boolean.valueOf((meta & 8) > 0));
|
| 250 | }
|
| 251 |
|
| 252 | /**
|
| 253 | * Convert the BlockState into the correct metadata value
|
| 254 | */
|
| 255 | public int getMetaFromState(IBlockState state)
|
| 256 | {
|
| 257 | int i = 0;
|
| 258 | i = i | ((BlockLever.EnumOrientation)state.getValue(FACING)).getMetadata();
|
| 259 |
|
| 260 | if (((Boolean)state.getValue(POWERED)).booleanValue())
|
| 261 | {
|
| 262 | i |= 8;
|
| 263 | }
|
| 264 |
|
| 265 | return i;
|
| 266 | }
|
| 267 |
|
| 268 | protected BlockState createBlockState()
|
| 269 | {
|
| 270 | return new BlockState(this, new IProperty[] {FACING, POWERED});
|
| 271 | }
|
| 272 |
|
| 273 | public static enum EnumOrientation implements IStringSerializable
|
| 274 | {
|
| 275 | DOWN_X(0, "down_x", EnumFacing.DOWN),
|
| 276 | EAST(1, "east", EnumFacing.EAST),
|
| 277 | WEST(2, "west", EnumFacing.WEST),
|
| 278 | SOUTH(3, "south", EnumFacing.SOUTH),
|
| 279 | NORTH(4, "north", EnumFacing.NORTH),
|
| 280 | UP_Z(5, "up_z", EnumFacing.UP),
|
| 281 | UP_X(6, "up_x", EnumFacing.UP),
|
| 282 | DOWN_Z(7, "down_z", EnumFacing.DOWN);
|
| 283 |
|
| 284 | private static final BlockLever.EnumOrientation[] META_LOOKUP = new BlockLever.EnumOrientation[values().length];
|
| 285 | private final int meta;
|
| 286 | private final String name;
|
| 287 | private final EnumFacing facing;
|
| 288 |
|
| 289 | private EnumOrientation(int meta, String name, EnumFacing facing)
|
| 290 | {
|
| 291 | this.meta = meta;
|
| 292 | this.name = name;
|
| 293 | this.facing = facing;
|
| 294 | }
|
| 295 |
|
| 296 | public int getMetadata()
|
| 297 | {
|
| 298 | return this.meta;
|
| 299 | }
|
| 300 |
|
| 301 | public EnumFacing getFacing()
|
| 302 | {
|
| 303 | return this.facing;
|
| 304 | }
|
| 305 |
|
| 306 | public String toString()
|
| 307 | {
|
| 308 | return this.name;
|
| 309 | }
|
| 310 |
|
| 311 | public static BlockLever.EnumOrientation byMetadata(int meta)
|
| 312 | {
|
| 313 | if (meta < 0 || meta >= META_LOOKUP.length)
|
| 314 | {
|
| 315 | meta = 0;
|
| 316 | }
|
| 317 |
|
| 318 | return META_LOOKUP[meta];
|
| 319 | }
|
| 320 |
|
| 321 | public static BlockLever.EnumOrientation forFacings(EnumFacing clickedSide, EnumFacing entityFacing)
|
| 322 | {
|
| 323 | switch (clickedSide)
|
| 324 | {
|
| 325 | case DOWN:
|
| 326 | switch (entityFacing.getAxis())
|
| 327 | {
|
| 328 | case X:
|
| 329 | return DOWN_X;
|
| 330 |
|
| 331 | case Z:
|
| 332 | return DOWN_Z;
|
| 333 |
|
| 334 | default:
|
| 335 | throw new IllegalArgumentException("Invalid entityFacing " + entityFacing + " for facing " + clickedSide);
|
| 336 | }
|
| 337 |
|
| 338 | case UP:
|
| 339 | switch (entityFacing.getAxis())
|
| 340 | {
|
| 341 | case X:
|
| 342 | return UP_X;
|
| 343 |
|
| 344 | case Z:
|
| 345 | return UP_Z;
|
| 346 |
|
| 347 | default:
|
| 348 | throw new IllegalArgumentException("Invalid entityFacing " + entityFacing + " for facing " + clickedSide);
|
| 349 | }
|
| 350 |
|
| 351 | case NORTH:
|
| 352 | return NORTH;
|
| 353 |
|
| 354 | case SOUTH:
|
| 355 | return SOUTH;
|
| 356 |
|
| 357 | case WEST:
|
| 358 | return WEST;
|
| 359 |
|
| 360 | case EAST:
|
| 361 | return EAST;
|
| 362 |
|
| 363 | default:
|
| 364 | throw new IllegalArgumentException("Invalid facing: " + clickedSide);
|
| 365 | }
|
| 366 | }
|
| 367 |
|
| 368 | public String getName()
|
| 369 | {
|
| 370 | return this.name;
|
| 371 | }
|
| 372 |
|
| 373 | static {
|
| 374 | for (BlockLever.EnumOrientation blocklever$enumorientation : values())
|
| 375 | {
|
| 376 | META_LOOKUP[blocklever$enumorientation.getMetadata()] = blocklever$enumorientation;
|
| 377 | }
|
| 378 | }
|
| 379 | }
|
| 380 | }
|
| 381 |
|