BlockGravelSlab.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package me.km.blocks;
  2. import java.util.Random;
  3. import net.minecraft.block.Block;
  4. import net.minecraft.block.BlockState;
  5. import net.minecraft.block.Blocks;
  6. import net.minecraft.block.SlabBlock;
  7. import net.minecraft.block.material.Material;
  8. import net.minecraft.entity.item.FallingBlockEntity;
  9. import net.minecraft.particles.BlockParticleData;
  10. import net.minecraft.particles.ParticleTypes;
  11. import net.minecraft.state.properties.SlabType;
  12. import net.minecraft.util.Direction;
  13. import net.minecraft.util.math.BlockPos;
  14. import net.minecraft.world.IWorld;
  15. import net.minecraft.world.IWorldReader;
  16. import net.minecraft.world.World;
  17. import net.minecraft.world.server.ServerWorld;
  18. import net.minecraftforge.api.distmarker.Dist;
  19. import net.minecraftforge.api.distmarker.OnlyIn;
  20. import net.minecraftforge.common.ToolType;
  21. public class BlockGravelSlab extends SlabBlock
  22. {
  23. public BlockGravelSlab(Properties properties)
  24. {
  25. super(properties);
  26. }
  27. @Override
  28. public ToolType getHarvestTool(BlockState state)
  29. {
  30. return ToolType.SHOVEL;
  31. }
  32. @Override
  33. public void onBlockAdded(BlockState state, World w, BlockPos pos, BlockState oldState, boolean isMoving)
  34. {
  35. w.getPendingBlockTicks().scheduleTick(pos, this, this.tickRate(w));
  36. }
  37. @Override
  38. public BlockState updatePostPlacement(BlockState state, Direction facing, BlockState facingState, IWorld w, BlockPos currentPos, BlockPos facingPos)
  39. {
  40. w.getPendingBlockTicks().scheduleTick(currentPos, this, this.tickRate(w));
  41. return super.updatePostPlacement(state, facing, facingState, w, currentPos, facingPos);
  42. }
  43. @Override
  44. public void tick(BlockState state, ServerWorld w, BlockPos pos, Random r)
  45. {
  46. if(w.isAirBlock(pos.down()) || canFallThrough(w.getBlockState(pos.down())) && pos.getY() >= 0)
  47. {
  48. BlockState fallState;
  49. double y = pos.getY();
  50. if(state == getDefaultState().with(TYPE, SlabType.TOP))
  51. {
  52. fallState = w.getBlockState(pos).with(TYPE, SlabType.BOTTOM);
  53. y += 0.5;
  54. }
  55. else
  56. {
  57. fallState = w.getBlockState(pos);
  58. }
  59. FallingBlockEntity fallingblockentity = new FallingBlockEntity(w, pos.getX() + 0.5, y, pos.getZ() + 0.5, fallState);
  60. w.addEntity(fallingblockentity);
  61. }
  62. }
  63. @Override
  64. public int tickRate(IWorldReader w)
  65. {
  66. return 2;
  67. }
  68. public static boolean canFallThrough(BlockState state)
  69. {
  70. Block block = state.getBlock();
  71. Material material = state.getMaterial();
  72. return state.isAir() || block == Blocks.FIRE || material.isLiquid() || material.isReplaceable();
  73. }
  74. @OnlyIn(Dist.CLIENT)
  75. @Override
  76. public void animateTick(BlockState state, World w, BlockPos pos, Random rand)
  77. {
  78. if(rand.nextInt(16) == 0)
  79. {
  80. BlockPos blockpos = pos.down();
  81. if(w.isAirBlock(blockpos) || canFallThrough(w.getBlockState(blockpos)))
  82. {
  83. double x = pos.getX() + rand.nextFloat();
  84. double y = pos.getY() - 0.05;
  85. double z = pos.getZ() + rand.nextFloat();
  86. w.addParticle(new BlockParticleData(ParticleTypes.FALLING_DUST, state), x, y, z, 0.0, 0.0, 0.0);
  87. }
  88. }
  89. }
  90. @OnlyIn(Dist.CLIENT)
  91. public int getDustColor(BlockState state)
  92. {
  93. return -8356741;
  94. }
  95. }