| 1 | package net.minecraft.item;
|
| 2 |
|
| 3 | import net.minecraft.block.Block;
|
| 4 | import net.minecraft.block.BlockSlab;
|
| 5 | import net.minecraft.block.properties.IProperty;
|
| 6 | import net.minecraft.block.state.IBlockState;
|
| 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 ItemSlab extends ItemBlock
|
| 13 | {
|
| 14 | private final BlockSlab singleSlab;
|
| 15 | private final BlockSlab doubleSlab;
|
| 16 |
|
| 17 | public ItemSlab(Block block, BlockSlab singleSlab, BlockSlab doubleSlab)
|
| 18 | {
|
| 19 | super(block);
|
| 20 | this.singleSlab = singleSlab;
|
| 21 | this.doubleSlab = doubleSlab;
|
| 22 | this.setMaxDamage(0);
|
| 23 | this.setHasSubtypes(true);
|
| 24 | }
|
| 25 |
|
| 26 | /**
|
| 27 | * Converts the given ItemStack damage value into a metadata value to be placed in the world when this Item is
|
| 28 | * placed as a Block (mostly used with ItemBlocks).
|
| 29 | */
|
| 30 | public int getMetadata(int damage)
|
| 31 | {
|
| 32 | return damage;
|
| 33 | }
|
| 34 |
|
| 35 | /**
|
| 36 | * Returns the unlocalized name of this item. This version accepts an ItemStack so different stacks can have
|
| 37 | * different names based on their damage or NBT.
|
| 38 | */
|
| 39 | public String getUnlocalizedName(ItemStack stack)
|
| 40 | {
|
| 41 | return this.singleSlab.getUnlocalizedName(stack.getMetadata());
|
| 42 | }
|
| 43 |
|
| 44 | /**
|
| 45 | * Called when a Block is right-clicked with this Item
|
| 46 | */
|
| 47 | public boolean onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
|
| 48 | {
|
| 49 | if (stack.stackSize == 0)
|
| 50 | {
|
| 51 | return false;
|
| 52 | }
|
| 53 | else if (!playerIn.canPlayerEdit(pos.offset(side), side, stack))
|
| 54 | {
|
| 55 | return false;
|
| 56 | }
|
| 57 | else
|
| 58 | {
|
| 59 | Object object = this.singleSlab.getVariant(stack);
|
| 60 | IBlockState iblockstate = worldIn.getBlockState(pos);
|
| 61 |
|
| 62 | if (iblockstate.getBlock() == this.singleSlab)
|
| 63 | {
|
| 64 | IProperty iproperty = this.singleSlab.getVariantProperty();
|
| 65 | Comparable comparable = iblockstate.getValue(iproperty);
|
| 66 | BlockSlab.EnumBlockHalf blockslab$enumblockhalf = (BlockSlab.EnumBlockHalf)iblockstate.getValue(BlockSlab.HALF);
|
| 67 |
|
| 68 | if ((side == EnumFacing.UP && blockslab$enumblockhalf == BlockSlab.EnumBlockHalf.BOTTOM || side == EnumFacing.DOWN && blockslab$enumblockhalf == BlockSlab.EnumBlockHalf.TOP) && comparable == object)
|
| 69 | {
|
| 70 | IBlockState iblockstate1 = this.doubleSlab.getDefaultState().withProperty(iproperty, comparable);
|
| 71 |
|
| 72 | if (worldIn.checkNoEntityCollision(this.doubleSlab.getCollisionBoundingBox(worldIn, pos, iblockstate1)) && worldIn.setBlockState(pos, iblockstate1, 3))
|
| 73 | {
|
| 74 | worldIn.playSoundEffect((double)((float)pos.getX() + 0.5F), (double)((float)pos.getY() + 0.5F), (double)((float)pos.getZ() + 0.5F), this.doubleSlab.stepSound.getPlaceSound(), (this.doubleSlab.stepSound.getVolume() + 1.0F) / 2.0F, this.doubleSlab.stepSound.getFrequency() * 0.8F);
|
| 75 | --stack.stackSize;
|
| 76 | }
|
| 77 |
|
| 78 | return true;
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | return this.tryPlace(stack, worldIn, pos.offset(side), object) ? true : super.onItemUse(stack, playerIn, worldIn, pos, side, hitX, hitY, hitZ);
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | public boolean canPlaceBlockOnSide(World worldIn, BlockPos pos, EnumFacing side, EntityPlayer player, ItemStack stack)
|
| 87 | {
|
| 88 | BlockPos blockpos = pos;
|
| 89 | IProperty iproperty = this.singleSlab.getVariantProperty();
|
| 90 | Object object = this.singleSlab.getVariant(stack);
|
| 91 | IBlockState iblockstate = worldIn.getBlockState(pos);
|
| 92 |
|
| 93 | if (iblockstate.getBlock() == this.singleSlab)
|
| 94 | {
|
| 95 | boolean flag = iblockstate.getValue(BlockSlab.HALF) == BlockSlab.EnumBlockHalf.TOP;
|
| 96 |
|
| 97 | if ((side == EnumFacing.UP && !flag || side == EnumFacing.DOWN && flag) && object == iblockstate.getValue(iproperty))
|
| 98 | {
|
| 99 | return true;
|
| 100 | }
|
| 101 | }
|
| 102 |
|
| 103 | pos = pos.offset(side);
|
| 104 | IBlockState iblockstate1 = worldIn.getBlockState(pos);
|
| 105 | return iblockstate1.getBlock() == this.singleSlab && object == iblockstate1.getValue(iproperty) ? true : super.canPlaceBlockOnSide(worldIn, blockpos, side, player, stack);
|
| 106 | }
|
| 107 |
|
| 108 | private boolean tryPlace(ItemStack stack, World worldIn, BlockPos pos, Object variantInStack)
|
| 109 | {
|
| 110 | IBlockState iblockstate = worldIn.getBlockState(pos);
|
| 111 |
|
| 112 | if (iblockstate.getBlock() == this.singleSlab)
|
| 113 | {
|
| 114 | Comparable comparable = iblockstate.getValue(this.singleSlab.getVariantProperty());
|
| 115 |
|
| 116 | if (comparable == variantInStack)
|
| 117 | {
|
| 118 | IBlockState iblockstate1 = this.doubleSlab.getDefaultState().withProperty((IProperty)this.singleSlab.getVariantProperty(), comparable);
|
| 119 |
|
| 120 | if (worldIn.checkNoEntityCollision(this.doubleSlab.getCollisionBoundingBox(worldIn, pos, iblockstate1)) && worldIn.setBlockState(pos, iblockstate1, 3))
|
| 121 | {
|
| 122 | worldIn.playSoundEffect((double)((float)pos.getX() + 0.5F), (double)((float)pos.getY() + 0.5F), (double)((float)pos.getZ() + 0.5F), this.doubleSlab.stepSound.getPlaceSound(), (this.doubleSlab.stepSound.getVolume() + 1.0F) / 2.0F, this.doubleSlab.stepSound.getFrequency() * 0.8F);
|
| 123 | --stack.stackSize;
|
| 124 | }
|
| 125 |
|
| 126 | return true;
|
| 127 | }
|
| 128 | }
|
| 129 |
|
| 130 | return false;
|
| 131 | }
|
| 132 | }
|
| 133 |
|