ItemBed.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package me.km.items;
  2. import me.km.blocks.ModBlocks;
  3. import net.minecraft.block.Block;
  4. import net.minecraft.block.BlockBed;
  5. import net.minecraft.block.SoundType;
  6. import net.minecraft.block.state.IBlockState;
  7. import net.minecraft.creativetab.CreativeTabs;
  8. import net.minecraft.entity.player.EntityPlayer;
  9. import net.minecraft.item.ItemStack;
  10. import net.minecraft.util.EnumActionResult;
  11. import net.minecraft.util.EnumFacing;
  12. import net.minecraft.util.EnumHand;
  13. import net.minecraft.util.SoundCategory;
  14. import net.minecraft.util.math.BlockPos;
  15. import net.minecraft.util.math.MathHelper;
  16. import net.minecraft.world.World;
  17. public class ItemBed extends ItemBase
  18. {
  19. public ItemBed(String name, String local)
  20. {
  21. super(name, local);
  22. super.setCreativeTab(CreativeTabs.DECORATIONS);
  23. }
  24. @Override
  25. public EnumActionResult onItemUse(EntityPlayer p, World w, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
  26. {
  27. if(w.isRemote)
  28. {
  29. return EnumActionResult.SUCCESS;
  30. }
  31. else if(facing != EnumFacing.UP)
  32. {
  33. return EnumActionResult.FAIL;
  34. }
  35. else
  36. {
  37. IBlockState iblockstate = w.getBlockState(pos);
  38. Block block = iblockstate.getBlock();
  39. boolean flag = block.isReplaceable(w, pos);
  40. if (!flag)
  41. {
  42. pos = pos.up();
  43. }
  44. int i = MathHelper.floor((double)(p.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
  45. EnumFacing enumfacing = EnumFacing.getHorizontal(i);
  46. BlockPos blockpos = pos.offset(enumfacing);
  47. ItemStack itemstack = p.getHeldItem(hand);
  48. if (p.canPlayerEdit(pos, facing, itemstack) && p.canPlayerEdit(blockpos, facing, itemstack))
  49. {
  50. IBlockState iblockstate1 = w.getBlockState(blockpos);
  51. boolean flag1 = iblockstate1.getBlock().isReplaceable(w, blockpos);
  52. boolean flag2 = flag || w.isAirBlock(pos);
  53. boolean flag3 = flag1 || w.isAirBlock(blockpos);
  54. if (flag2 && flag3 && w.getBlockState(pos.down()).isTopSolid() && w.getBlockState(blockpos.down()).isTopSolid())
  55. {
  56. IBlockState iblockstate2 = ModBlocks.realHayBed.getDefaultState().withProperty(BlockBed.OCCUPIED, false).withProperty(BlockBed.FACING, enumfacing).withProperty(BlockBed.PART, BlockBed.EnumPartType.FOOT);
  57. w.setBlockState(pos, iblockstate2, 10);
  58. w.setBlockState(blockpos, iblockstate2.withProperty(BlockBed.PART, BlockBed.EnumPartType.HEAD), 10);
  59. w.notifyNeighborsRespectDebug(pos, block, false);
  60. w.notifyNeighborsRespectDebug(blockpos, iblockstate1.getBlock(), false);
  61. SoundType soundtype = iblockstate2.getBlock().getSoundType(iblockstate2, w, pos, p);
  62. w.playSound((EntityPlayer)null, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);
  63. itemstack.shrink(1);
  64. return EnumActionResult.SUCCESS;
  65. }
  66. else
  67. {
  68. return EnumActionResult.FAIL;
  69. }
  70. }
  71. else
  72. {
  73. return EnumActionResult.FAIL;
  74. }
  75. }
  76. }
  77. }