BlockGravelSlab.java 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.World;
  16. import net.minecraft.world.server.ServerWorld;
  17. import net.minecraftforge.api.distmarker.Dist;
  18. import net.minecraftforge.api.distmarker.OnlyIn;
  19. import net.minecraftforge.common.ToolType;
  20. public class BlockGravelSlab extends SlabBlock {
  21. public BlockGravelSlab(Properties properties) {
  22. super(properties);
  23. }
  24. @Override
  25. public ToolType getHarvestTool(BlockState state) {
  26. return ToolType.SHOVEL;
  27. }
  28. @Override
  29. public void onBlockAdded(BlockState state, World w, BlockPos pos, BlockState oldState, boolean isMoving) {
  30. w.getPendingBlockTicks().scheduleTick(pos, this, getFallDelay());
  31. }
  32. @Override
  33. public BlockState updatePostPlacement(BlockState state, Direction facing, BlockState facingState, IWorld w,
  34. BlockPos currentPos, BlockPos facingPos) {
  35. w.getPendingBlockTicks().scheduleTick(currentPos, this, getFallDelay());
  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. protected int getFallDelay() {
  54. return 2;
  55. }
  56. public static boolean canFallThrough(BlockState state) {
  57. Block block = state.getBlock();
  58. Material material = state.getMaterial();
  59. return state.isAir() || block == Blocks.FIRE || material.isLiquid() || material.isReplaceable();
  60. }
  61. @OnlyIn(Dist.CLIENT)
  62. @Override
  63. public void animateTick(BlockState state, World w, BlockPos pos, Random rand) {
  64. if(rand.nextInt(16) == 0) {
  65. BlockPos blockpos = pos.down();
  66. if(w.isAirBlock(blockpos) || canFallThrough(w.getBlockState(blockpos))) {
  67. double x = pos.getX() + rand.nextFloat();
  68. double y = pos.getY() - 0.05;
  69. double z = pos.getZ() + rand.nextFloat();
  70. w.addParticle(new BlockParticleData(ParticleTypes.FALLING_DUST, state), x, y, z, 0.0, 0.0, 0.0);
  71. }
  72. }
  73. }
  74. @OnlyIn(Dist.CLIENT)
  75. public int getDustColor(BlockState state) {
  76. return -8356741;
  77. }
  78. }