BlockBed.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package me.km.blocks;
  2. import java.util.Random;
  3. import javax.annotation.Nullable;
  4. import net.minecraft.block.SoundType;
  5. import net.minecraft.block.state.IBlockState;
  6. import net.minecraft.entity.Entity;
  7. import net.minecraft.entity.player.EntityPlayer;
  8. import net.minecraft.entity.player.EntityPlayerMP;
  9. import net.minecraft.init.Biomes;
  10. import net.minecraft.init.Items;
  11. import net.minecraft.item.Item;
  12. import net.minecraft.item.ItemStack;
  13. import net.minecraft.util.EnumFacing;
  14. import net.minecraft.util.EnumHand;
  15. import net.minecraft.util.math.BlockPos;
  16. import net.minecraft.util.text.TextComponentTranslation;
  17. import net.minecraft.world.IBlockAccess;
  18. import net.minecraft.world.World;
  19. public class BlockBed extends net.minecraft.block.BlockBed
  20. {
  21. protected String name;
  22. private SoundType type;
  23. private final Item drop;
  24. public BlockBed(String name, String local, Item drop)
  25. {
  26. this.name = name;
  27. this.setRegistryName(name);
  28. super.setUnlocalizedName(local);
  29. this.type = SoundType.CLOTH;
  30. this.drop = drop;
  31. super.setHardness(0.2F);
  32. super.disableStats();
  33. }
  34. @Override
  35. public final Item getItemDropped(IBlockState state, Random rand, int fortune)
  36. {
  37. return state.getValue(PART) == BlockBed.EnumPartType.HEAD ? Items.AIR : drop;
  38. }
  39. @Override
  40. public final ItemStack getItem(World worldIn, BlockPos pos, IBlockState state)
  41. {
  42. return new ItemStack(drop);
  43. }
  44. @Override
  45. public final BlockBed setSoundType(SoundType sound)
  46. {
  47. this.type = sound;
  48. return this;
  49. }
  50. @Override
  51. public final SoundType getSoundType(IBlockState state, World w, BlockPos pos, Entity entity)
  52. {
  53. return type;
  54. }
  55. @Override
  56. public final boolean isBed(IBlockState state, IBlockAccess world, BlockPos pos, Entity player)
  57. {
  58. return true;
  59. }
  60. // copied from net.minecraft.block.BlockBed
  61. @Override
  62. public final boolean onBlockActivated(World w, BlockPos pos, IBlockState state, EntityPlayer p, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
  63. {
  64. if(w.isRemote)
  65. {
  66. return true;
  67. }
  68. else
  69. {
  70. if(state.getValue(PART) != EnumPartType.HEAD)
  71. {
  72. pos = pos.offset((EnumFacing)state.getValue(FACING));
  73. state = w.getBlockState(pos);
  74. if (state.getBlock() != this)
  75. {
  76. return true;
  77. }
  78. }
  79. if(w.provider.canRespawnHere() && w.getBiome(pos) != Biomes.HELL)
  80. {
  81. if(state.getValue(OCCUPIED))
  82. {
  83. EntityPlayer anotherPlayer = this.getPlayerInBed(w, pos);
  84. if (anotherPlayer != null)
  85. {
  86. p.sendStatusMessage(new TextComponentTranslation("tile.bed.occupied", new Object[0]), true);
  87. return true;
  88. }
  89. state = state.withProperty(OCCUPIED, false);
  90. w.setBlockState(pos, state, 4);
  91. }
  92. EntityPlayer.SleepResult result = p.trySleep(pos);
  93. if(result == EntityPlayer.SleepResult.OK)
  94. {
  95. state = state.withProperty(OCCUPIED, true);
  96. // set players real position
  97. // subtracting normal bedsize, adding custom
  98. p.setPosition(p.posX, p.posY - 0.4375f + getLayingHigh(), p.posZ);
  99. // this is for evil servers which render the position wrong
  100. if(p instanceof EntityPlayerMP)
  101. {
  102. ((EntityPlayerMP) p).connection.setPlayerLocation(p.posX, p.posY, p.posZ, p.rotationYaw, p.rotationPitch);
  103. }
  104. // end
  105. w.setBlockState(pos, state, 4);
  106. return true;
  107. }
  108. else
  109. {
  110. switch (result)
  111. {
  112. case NOT_POSSIBLE_NOW:
  113. p.sendStatusMessage(new TextComponentTranslation("tile.bed.noSleep", new Object[0]), true);
  114. break;
  115. case NOT_SAFE:
  116. p.sendStatusMessage(new TextComponentTranslation("tile.bed.notSafe", new Object[0]), true);
  117. break;
  118. case TOO_FAR_AWAY:
  119. p.sendStatusMessage(new TextComponentTranslation("tile.bed.tooFarAway", new Object[0]), true);
  120. break;
  121. }
  122. return true;
  123. }
  124. }
  125. else
  126. {
  127. w.setBlockToAir(pos);
  128. BlockPos blockpos = pos.offset(state.getValue(FACING).getOpposite());
  129. if(w.getBlockState(blockpos).getBlock() == this)
  130. {
  131. w.setBlockToAir(blockpos);
  132. }
  133. w.newExplosion(null, pos.getX() + 0.5d, pos.getY() + 0.5d, pos.getZ() + 0.5d, 5, true, true);
  134. return true;
  135. }
  136. }
  137. }
  138. public float getLayingHigh()
  139. {
  140. return 0.6875f;
  141. }
  142. // copied from net.minecraft.block.BlockBed
  143. @Nullable
  144. private EntityPlayer getPlayerInBed(World w, BlockPos pos)
  145. {
  146. for(EntityPlayer entityplayer : w.playerEntities)
  147. {
  148. if(entityplayer.isPlayerSleeping() && entityplayer.bedLocation.equals(pos))
  149. {
  150. return entityplayer;
  151. }
  152. }
  153. return null;
  154. }
  155. }