| 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.PropertyDirection;
|
| 7 | import net.minecraft.block.properties.PropertyEnum;
|
| 8 | import net.minecraft.block.state.BlockState;
|
| 9 | import net.minecraft.block.state.IBlockState;
|
| 10 | import net.minecraft.creativetab.CreativeTabs;
|
| 11 | import net.minecraft.entity.EntityLivingBase;
|
| 12 | import net.minecraft.entity.player.EntityPlayer;
|
| 13 | import net.minecraft.init.Blocks;
|
| 14 | import net.minecraft.util.AxisAlignedBB;
|
| 15 | import net.minecraft.util.BlockPos;
|
| 16 | import net.minecraft.util.EnumFacing;
|
| 17 | import net.minecraft.util.EnumWorldBlockLayer;
|
| 18 | import net.minecraft.util.IStringSerializable;
|
| 19 | import net.minecraft.util.MovingObjectPosition;
|
| 20 | import net.minecraft.util.Vec3;
|
| 21 | import net.minecraft.world.IBlockAccess;
|
| 22 | import net.minecraft.world.World;
|
| 23 |
|
| 24 | public class BlockTrapDoor extends Block
|
| 25 | {
|
| 26 | public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
|
| 27 | public static final PropertyBool OPEN = PropertyBool.create("open");
|
| 28 | public static final PropertyEnum<BlockTrapDoor.DoorHalf> HALF = PropertyEnum.<BlockTrapDoor.DoorHalf>create("half", BlockTrapDoor.DoorHalf.class);
|
| 29 |
|
| 30 | protected BlockTrapDoor(Material materialIn)
|
| 31 | {
|
| 32 | super(materialIn);
|
| 33 | this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(OPEN, Boolean.valueOf(false)).withProperty(HALF, BlockTrapDoor.DoorHalf.BOTTOM));
|
| 34 | float f = 0.5F;
|
| 35 | float f1 = 1.0F;
|
| 36 | this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
|
| 37 | this.setCreativeTab(CreativeTabs.tabRedstone);
|
| 38 | }
|
| 39 |
|
| 40 | /**
|
| 41 | * Used to determine ambient occlusion and culling when rebuilding chunks for render
|
| 42 | */
|
| 43 | public boolean isOpaqueCube()
|
| 44 | {
|
| 45 | return false;
|
| 46 | }
|
| 47 |
|
| 48 | public boolean isFullCube()
|
| 49 | {
|
| 50 | return false;
|
| 51 | }
|
| 52 |
|
| 53 | public boolean isPassable(IBlockAccess worldIn, BlockPos pos)
|
| 54 | {
|
| 55 | return !((Boolean)worldIn.getBlockState(pos).getValue(OPEN)).booleanValue();
|
| 56 | }
|
| 57 |
|
| 58 | public AxisAlignedBB getSelectedBoundingBox(World worldIn, BlockPos pos)
|
| 59 | {
|
| 60 | this.setBlockBoundsBasedOnState(worldIn, pos);
|
| 61 | return super.getSelectedBoundingBox(worldIn, pos);
|
| 62 | }
|
| 63 |
|
| 64 | public AxisAlignedBB getCollisionBoundingBox(World worldIn, BlockPos pos, IBlockState state)
|
| 65 | {
|
| 66 | this.setBlockBoundsBasedOnState(worldIn, pos);
|
| 67 | return super.getCollisionBoundingBox(worldIn, pos, state);
|
| 68 | }
|
| 69 |
|
| 70 | public void setBlockBoundsBasedOnState(IBlockAccess worldIn, BlockPos pos)
|
| 71 | {
|
| 72 | this.setBounds(worldIn.getBlockState(pos));
|
| 73 | }
|
| 74 |
|
| 75 | /**
|
| 76 | * Sets the block's bounds for rendering it as an item
|
| 77 | */
|
| 78 | public void setBlockBoundsForItemRender()
|
| 79 | {
|
| 80 | float f = 0.1875F;
|
| 81 | this.setBlockBounds(0.0F, 0.40625F, 0.0F, 1.0F, 0.59375F, 1.0F);
|
| 82 | }
|
| 83 |
|
| 84 | public void setBounds(IBlockState state)
|
| 85 | {
|
| 86 | if (state.getBlock() == this)
|
| 87 | {
|
| 88 | boolean flag = state.getValue(HALF) == BlockTrapDoor.DoorHalf.TOP;
|
| 89 | Boolean obool = (Boolean)state.getValue(OPEN);
|
| 90 | EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
|
| 91 | float f = 0.1875F;
|
| 92 |
|
| 93 | if (flag)
|
| 94 | {
|
| 95 | this.setBlockBounds(0.0F, 0.8125F, 0.0F, 1.0F, 1.0F, 1.0F);
|
| 96 | }
|
| 97 | else
|
| 98 | {
|
| 99 | this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.1875F, 1.0F);
|
| 100 | }
|
| 101 |
|
| 102 | if (obool.booleanValue())
|
| 103 | {
|
| 104 | if (enumfacing == EnumFacing.NORTH)
|
| 105 | {
|
| 106 | this.setBlockBounds(0.0F, 0.0F, 0.8125F, 1.0F, 1.0F, 1.0F);
|
| 107 | }
|
| 108 |
|
| 109 | if (enumfacing == EnumFacing.SOUTH)
|
| 110 | {
|
| 111 | this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 0.1875F);
|
| 112 | }
|
| 113 |
|
| 114 | if (enumfacing == EnumFacing.WEST)
|
| 115 | {
|
| 116 | this.setBlockBounds(0.8125F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
|
| 117 | }
|
| 118 |
|
| 119 | if (enumfacing == EnumFacing.EAST)
|
| 120 | {
|
| 121 | this.setBlockBounds(0.0F, 0.0F, 0.0F, 0.1875F, 1.0F, 1.0F);
|
| 122 | }
|
| 123 | }
|
| 124 | }
|
| 125 | }
|
| 126 |
|
| 127 | public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
|
| 128 | {
|
| 129 | if (this.blockMaterial == Material.iron)
|
| 130 | {
|
| 131 | return true;
|
| 132 | }
|
| 133 | else
|
| 134 | {
|
| 135 | state = state.cycleProperty(OPEN);
|
| 136 | worldIn.setBlockState(pos, state, 2);
|
| 137 | worldIn.playAuxSFXAtEntity(playerIn, ((Boolean)state.getValue(OPEN)).booleanValue() ? 1003 : 1006, pos, 0);
|
| 138 | return true;
|
| 139 | }
|
| 140 | }
|
| 141 |
|
| 142 | /**
|
| 143 | * Called when a neighboring block changes.
|
| 144 | */
|
| 145 | public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock)
|
| 146 | {
|
| 147 | if (!worldIn.isRemote)
|
| 148 | {
|
| 149 | BlockPos blockpos = pos.offset(((EnumFacing)state.getValue(FACING)).getOpposite());
|
| 150 |
|
| 151 | if (!isValidSupportBlock(worldIn.getBlockState(blockpos).getBlock()))
|
| 152 | {
|
| 153 | worldIn.setBlockToAir(pos);
|
| 154 | this.dropBlockAsItem(worldIn, pos, state, 0);
|
| 155 | }
|
| 156 | else
|
| 157 | {
|
| 158 | boolean flag = worldIn.isBlockPowered(pos);
|
| 159 |
|
| 160 | if (flag || neighborBlock.canProvidePower())
|
| 161 | {
|
| 162 | boolean flag1 = ((Boolean)state.getValue(OPEN)).booleanValue();
|
| 163 |
|
| 164 | if (flag1 != flag)
|
| 165 | {
|
| 166 | worldIn.setBlockState(pos, state.withProperty(OPEN, Boolean.valueOf(flag)), 2);
|
| 167 | worldIn.playAuxSFXAtEntity((EntityPlayer)null, flag ? 1003 : 1006, pos, 0);
|
| 168 | }
|
| 169 | }
|
| 170 | }
|
| 171 | }
|
| 172 | }
|
| 173 |
|
| 174 | /**
|
| 175 | * Ray traces through the blocks collision from start vector to end vector returning a ray trace hit.
|
| 176 | */
|
| 177 | public MovingObjectPosition collisionRayTrace(World worldIn, BlockPos pos, Vec3 start, Vec3 end)
|
| 178 | {
|
| 179 | this.setBlockBoundsBasedOnState(worldIn, pos);
|
| 180 | return super.collisionRayTrace(worldIn, pos, start, end);
|
| 181 | }
|
| 182 |
|
| 183 | /**
|
| 184 | * Called by ItemBlocks just before a block is actually set in the world, to allow for adjustments to the
|
| 185 | * IBlockstate
|
| 186 | */
|
| 187 | public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
|
| 188 | {
|
| 189 | IBlockState iblockstate = this.getDefaultState();
|
| 190 |
|
| 191 | if (facing.getAxis().isHorizontal())
|
| 192 | {
|
| 193 | iblockstate = iblockstate.withProperty(FACING, facing).withProperty(OPEN, Boolean.valueOf(false));
|
| 194 | iblockstate = iblockstate.withProperty(HALF, hitY > 0.5F ? BlockTrapDoor.DoorHalf.TOP : BlockTrapDoor.DoorHalf.BOTTOM);
|
| 195 | }
|
| 196 |
|
| 197 | return iblockstate;
|
| 198 | }
|
| 199 |
|
| 200 | /**
|
| 201 | * Check whether this Block can be placed on the given side
|
| 202 | */
|
| 203 | public boolean canPlaceBlockOnSide(World worldIn, BlockPos pos, EnumFacing side)
|
| 204 | {
|
| 205 | return !side.getAxis().isVertical() && isValidSupportBlock(worldIn.getBlockState(pos.offset(side.getOpposite())).getBlock());
|
| 206 | }
|
| 207 |
|
| 208 | protected static EnumFacing getFacing(int meta)
|
| 209 | {
|
| 210 | switch (meta & 3)
|
| 211 | {
|
| 212 | case 0:
|
| 213 | return EnumFacing.NORTH;
|
| 214 |
|
| 215 | case 1:
|
| 216 | return EnumFacing.SOUTH;
|
| 217 |
|
| 218 | case 2:
|
| 219 | return EnumFacing.WEST;
|
| 220 |
|
| 221 | case 3:
|
| 222 | default:
|
| 223 | return EnumFacing.EAST;
|
| 224 | }
|
| 225 | }
|
| 226 |
|
| 227 | protected static int getMetaForFacing(EnumFacing facing)
|
| 228 | {
|
| 229 | switch (facing)
|
| 230 | {
|
| 231 | case NORTH:
|
| 232 | return 0;
|
| 233 |
|
| 234 | case SOUTH:
|
| 235 | return 1;
|
| 236 |
|
| 237 | case WEST:
|
| 238 | return 2;
|
| 239 |
|
| 240 | case EAST:
|
| 241 | default:
|
| 242 | return 3;
|
| 243 | }
|
| 244 | }
|
| 245 |
|
| 246 | private static boolean isValidSupportBlock(Block blockIn)
|
| 247 | {
|
| 248 | return blockIn.blockMaterial.isOpaque() && blockIn.isFullCube() || blockIn == Blocks.glowstone || blockIn instanceof BlockSlab || blockIn instanceof BlockStairs;
|
| 249 | }
|
| 250 |
|
| 251 | public EnumWorldBlockLayer getBlockLayer()
|
| 252 | {
|
| 253 | return EnumWorldBlockLayer.CUTOUT;
|
| 254 | }
|
| 255 |
|
| 256 | /**
|
| 257 | * Convert the given metadata into a BlockState for this Block
|
| 258 | */
|
| 259 | public IBlockState getStateFromMeta(int meta)
|
| 260 | {
|
| 261 | return this.getDefaultState().withProperty(FACING, getFacing(meta)).withProperty(OPEN, Boolean.valueOf((meta & 4) != 0)).withProperty(HALF, (meta & 8) == 0 ? BlockTrapDoor.DoorHalf.BOTTOM : BlockTrapDoor.DoorHalf.TOP);
|
| 262 | }
|
| 263 |
|
| 264 | /**
|
| 265 | * Convert the BlockState into the correct metadata value
|
| 266 | */
|
| 267 | public int getMetaFromState(IBlockState state)
|
| 268 | {
|
| 269 | int i = 0;
|
| 270 | i = i | getMetaForFacing((EnumFacing)state.getValue(FACING));
|
| 271 |
|
| 272 | if (((Boolean)state.getValue(OPEN)).booleanValue())
|
| 273 | {
|
| 274 | i |= 4;
|
| 275 | }
|
| 276 |
|
| 277 | if (state.getValue(HALF) == BlockTrapDoor.DoorHalf.TOP)
|
| 278 | {
|
| 279 | i |= 8;
|
| 280 | }
|
| 281 |
|
| 282 | return i;
|
| 283 | }
|
| 284 |
|
| 285 | protected BlockState createBlockState()
|
| 286 | {
|
| 287 | return new BlockState(this, new IProperty[] {FACING, OPEN, HALF});
|
| 288 | }
|
| 289 |
|
| 290 | public static enum DoorHalf implements IStringSerializable
|
| 291 | {
|
| 292 | TOP("top"),
|
| 293 | BOTTOM("bottom");
|
| 294 |
|
| 295 | private final String name;
|
| 296 |
|
| 297 | private DoorHalf(String name)
|
| 298 | {
|
| 299 | this.name = name;
|
| 300 | }
|
| 301 |
|
| 302 | public String toString()
|
| 303 | {
|
| 304 | return this.name;
|
| 305 | }
|
| 306 |
|
| 307 | public String getName()
|
| 308 | {
|
| 309 | return this.name;
|
| 310 | }
|
| 311 | }
|
| 312 | }
|
| 313 |
|