BlockGravelSlab.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. if(state == getDefaultState().with(TYPE, SlabType.TOP))
  50. {
  51. fallState = w.getBlockState(pos).with(TYPE, SlabType.BOTTOM);
  52. }
  53. else
  54. {
  55. fallState = w.getBlockState(pos);
  56. }
  57. FallingBlockEntity fallingblockentity = new FallingBlockEntity(w, pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5, fallState);
  58. w.addEntity(fallingblockentity);
  59. }
  60. }
  61. @Override
  62. public int tickRate(IWorldReader w)
  63. {
  64. return 2;
  65. }
  66. public static boolean canFallThrough(BlockState state)
  67. {
  68. Block block = state.getBlock();
  69. Material material = state.getMaterial();
  70. return state.isAir() || block == Blocks.FIRE || material.isLiquid() || material.isReplaceable();
  71. }
  72. @OnlyIn(Dist.CLIENT)
  73. @Override
  74. public void animateTick(BlockState state, World w, BlockPos pos, Random rand)
  75. {
  76. if(rand.nextInt(16) == 0)
  77. {
  78. BlockPos blockpos = pos.down();
  79. if(w.isAirBlock(blockpos) || canFallThrough(w.getBlockState(blockpos)))
  80. {
  81. double x = pos.getX() + rand.nextFloat();
  82. double y = pos.getY() - 0.05;
  83. double z = pos.getZ() + rand.nextFloat();
  84. w.addParticle(new BlockParticleData(ParticleTypes.FALLING_DUST, state), x, y, z, 0.0, 0.0, 0.0);
  85. }
  86. }
  87. }
  88. @OnlyIn(Dist.CLIENT)
  89. public int getDustColor(BlockState state)
  90. {
  91. return -8356741;
  92. }
  93. }