BlockGravelSlab.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. public BlockGravelSlab(Properties properties) {
  23. super(properties);
  24. }
  25. @Override
  26. public ToolType getHarvestTool(BlockState state) {
  27. return ToolType.SHOVEL;
  28. }
  29. @Override
  30. public void onBlockAdded(BlockState state, World w, BlockPos pos, BlockState oldState, boolean isMoving) {
  31. w.getPendingBlockTicks().scheduleTick(pos, this, this.tickRate(w));
  32. }
  33. @Override
  34. public BlockState updatePostPlacement(BlockState state, Direction facing, BlockState facingState, IWorld w, BlockPos currentPos, BlockPos facingPos) {
  35. w.getPendingBlockTicks().scheduleTick(currentPos, this, this.tickRate(w));
  36. return super.updatePostPlacement(state, facing, facingState, w, currentPos, facingPos);
  37. }
  38. @Override
  39. public void tick(BlockState state, ServerWorld w, BlockPos pos, Random r) {
  40. if(w.isAirBlock(pos.down()) || canFallThrough(w.getBlockState(pos.down())) && pos.getY() >= 0) {
  41. BlockState fallState;
  42. double y = pos.getY();
  43. if(state == getDefaultState().with(TYPE, SlabType.TOP)) {
  44. fallState = w.getBlockState(pos).with(TYPE, SlabType.BOTTOM);
  45. y += 0.5;
  46. } else {
  47. fallState = w.getBlockState(pos);
  48. }
  49. FallingBlockEntity fallingblockentity = new FallingBlockEntity(w, pos.getX() + 0.5, y, pos.getZ() + 0.5, fallState);
  50. w.addEntity(fallingblockentity);
  51. }
  52. }
  53. @Override
  54. public int tickRate(IWorldReader w) {
  55. return 2;
  56. }
  57. public static boolean canFallThrough(BlockState state) {
  58. Block block = state.getBlock();
  59. Material material = state.getMaterial();
  60. return state.isAir() || block == Blocks.FIRE || material.isLiquid() || material.isReplaceable();
  61. }
  62. @OnlyIn(Dist.CLIENT)
  63. @Override
  64. public void animateTick(BlockState state, World w, BlockPos pos, Random rand) {
  65. if(rand.nextInt(16) == 0) {
  66. BlockPos blockpos = pos.down();
  67. if(w.isAirBlock(blockpos) || canFallThrough(w.getBlockState(blockpos))) {
  68. double x = pos.getX() + rand.nextFloat();
  69. double y = pos.getY() - 0.05;
  70. double z = pos.getZ() + rand.nextFloat();
  71. w.addParticle(new BlockParticleData(ParticleTypes.FALLING_DUST, state), x, y, z, 0.0, 0.0, 0.0);
  72. }
  73. }
  74. }
  75. @OnlyIn(Dist.CLIENT)
  76. public int getDustColor(BlockState state) {
  77. return -8356741;
  78. }
  79. }