| 1 | package net.minecraft.item;
|
| 2 |
|
| 3 | import net.minecraft.block.Block;
|
| 4 | import net.minecraft.block.BlockDoor;
|
| 5 | import net.minecraft.block.state.IBlockState;
|
| 6 | import net.minecraft.creativetab.CreativeTabs;
|
| 7 | import net.minecraft.entity.player.EntityPlayer;
|
| 8 | import net.minecraft.util.BlockPos;
|
| 9 | import net.minecraft.util.EnumFacing;
|
| 10 | import net.minecraft.world.World;
|
| 11 |
|
| 12 | public class ItemDoor extends Item
|
| 13 | {
|
| 14 | private Block block;
|
| 15 |
|
| 16 | public ItemDoor(Block block)
|
| 17 | {
|
| 18 | this.block = block;
|
| 19 | this.setCreativeTab(CreativeTabs.tabRedstone);
|
| 20 | }
|
| 21 |
|
| 22 | /**
|
| 23 | * Called when a Block is right-clicked with this Item
|
| 24 | */
|
| 25 | public boolean onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
|
| 26 | {
|
| 27 | if (side != EnumFacing.UP)
|
| 28 | {
|
| 29 | return false;
|
| 30 | }
|
| 31 | else
|
| 32 | {
|
| 33 | IBlockState iblockstate = worldIn.getBlockState(pos);
|
| 34 | Block block = iblockstate.getBlock();
|
| 35 |
|
| 36 | if (!block.isReplaceable(worldIn, pos))
|
| 37 | {
|
| 38 | pos = pos.offset(side);
|
| 39 | }
|
| 40 |
|
| 41 | if (!playerIn.canPlayerEdit(pos, side, stack))
|
| 42 | {
|
| 43 | return false;
|
| 44 | }
|
| 45 | else if (!this.block.canPlaceBlockAt(worldIn, pos))
|
| 46 | {
|
| 47 | return false;
|
| 48 | }
|
| 49 | else
|
| 50 | {
|
| 51 | placeDoor(worldIn, pos, EnumFacing.fromAngle((double)playerIn.rotationYaw), this.block);
|
| 52 | --stack.stackSize;
|
| 53 | return true;
|
| 54 | }
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | public static void placeDoor(World worldIn, BlockPos pos, EnumFacing facing, Block door)
|
| 59 | {
|
| 60 | BlockPos blockpos = pos.offset(facing.rotateY());
|
| 61 | BlockPos blockpos1 = pos.offset(facing.rotateYCCW());
|
| 62 | int i = (worldIn.getBlockState(blockpos1).getBlock().isNormalCube() ? 1 : 0) + (worldIn.getBlockState(blockpos1.up()).getBlock().isNormalCube() ? 1 : 0);
|
| 63 | int j = (worldIn.getBlockState(blockpos).getBlock().isNormalCube() ? 1 : 0) + (worldIn.getBlockState(blockpos.up()).getBlock().isNormalCube() ? 1 : 0);
|
| 64 | boolean flag = worldIn.getBlockState(blockpos1).getBlock() == door || worldIn.getBlockState(blockpos1.up()).getBlock() == door;
|
| 65 | boolean flag1 = worldIn.getBlockState(blockpos).getBlock() == door || worldIn.getBlockState(blockpos.up()).getBlock() == door;
|
| 66 | boolean flag2 = false;
|
| 67 |
|
| 68 | if (flag && !flag1 || j > i)
|
| 69 | {
|
| 70 | flag2 = true;
|
| 71 | }
|
| 72 |
|
| 73 | BlockPos blockpos2 = pos.up();
|
| 74 | IBlockState iblockstate = door.getDefaultState().withProperty(BlockDoor.FACING, facing).withProperty(BlockDoor.HINGE, flag2 ? BlockDoor.EnumHingePosition.RIGHT : BlockDoor.EnumHingePosition.LEFT);
|
| 75 | worldIn.setBlockState(pos, iblockstate.withProperty(BlockDoor.HALF, BlockDoor.EnumDoorHalf.LOWER), 2);
|
| 76 | worldIn.setBlockState(blockpos2, iblockstate.withProperty(BlockDoor.HALF, BlockDoor.EnumDoorHalf.UPPER), 2);
|
| 77 | worldIn.notifyNeighborsOfStateChange(pos, door);
|
| 78 | worldIn.notifyNeighborsOfStateChange(blockpos2, door);
|
| 79 | }
|
| 80 | }
|
| 81 |
|