| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.core.particles.BlockParticleOption;
|
| 6 | import net.minecraft.core.particles.ParticleTypes;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.sounds.SoundEvent;
|
| 9 | import net.minecraft.util.RandomSource;
|
| 10 | import net.minecraft.world.entity.item.FallingBlockEntity;
|
| 11 | import net.minecraft.world.level.Level;
|
| 12 | import net.minecraft.world.level.LevelReader;
|
| 13 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 14 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 15 | import net.minecraft.world.level.block.entity.BrushableBlockEntity;
|
| 16 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 17 | import net.minecraft.world.level.block.state.BlockState;
|
| 18 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 19 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 20 | import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
| 21 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 22 | import net.minecraft.world.phys.Vec3;
|
| 23 | import org.jspecify.annotations.Nullable;
|
| 24 |
|
| 25 | public class BrushableBlock extends BaseEntityBlock implements Fallable {
|
| 26 | private static final IntegerProperty DUSTED = BlockStateProperties.DUSTED;
|
| 27 | public static final int TICK_DELAY = 2;
|
| 28 | private final Block turnsInto;
|
| 29 | private final SoundEvent brushSound;
|
| 30 | private final SoundEvent brushCompletedSound;
|
| 31 |
|
| 32 | public BrushableBlock(final Block turnsInto, final SoundEvent brushSound, final SoundEvent brushCompletedSound, final BlockBehaviour.Properties properties) {
|
| 33 | super(properties);
|
| 34 | this.turnsInto = turnsInto;
|
| 35 | this.brushSound = brushSound;
|
| 36 | this.brushCompletedSound = brushCompletedSound;
|
| 37 | this.registerDefaultState(this.stateDefinition.any().setValue(DUSTED, 0));
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 42 | builder.add(DUSTED);
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | public void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston) {
|
| 47 | level.scheduleTick(pos, this, 2);
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | public BlockState updateShape(
|
| 52 | final BlockState state,
|
| 53 | final LevelReader level,
|
| 54 | final ScheduledTickAccess ticks,
|
| 55 | final BlockPos pos,
|
| 56 | final Direction directionToNeighbour,
|
| 57 | final BlockPos neighbourPos,
|
| 58 | final BlockState neighbourState,
|
| 59 | final RandomSource random
|
| 60 | ) {
|
| 61 | ticks.scheduleTick(pos, this, 2);
|
| 62 | return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | public void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 67 | if (level.getBlockEntity(pos) instanceof BrushableBlockEntity brushableBlockEntity) {
|
| 68 | brushableBlockEntity.checkReset(level);
|
| 69 | }
|
| 70 |
|
| 71 | if (FallingBlock.isFree(level.getBlockState(pos.below())) && pos.getY() >= level.getMinY()) {
|
| 72 | FallingBlockEntity entity = FallingBlockEntity.fall(level, pos, state);
|
| 73 | entity.disableDrop();
|
| 74 | }
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | public void onBrokenAfterFall(final Level level, final BlockPos pos, final FallingBlockEntity entity) {
|
| 79 | Vec3 centerOfEntity = entity.getBoundingBox().getCenter();
|
| 80 | level.levelEvent(2001, BlockPos.containing(centerOfEntity), Block.getId(entity.getBlockState()));
|
| 81 | level.gameEvent(entity, GameEvent.BLOCK_DESTROY, centerOfEntity);
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public void animateTick(final BlockState state, final Level level, final BlockPos pos, final RandomSource random) {
|
| 86 | if (random.nextInt(16) == 0) {
|
| 87 | BlockPos below = pos.below();
|
| 88 | if (FallingBlock.isFree(level.getBlockState(below))) {
|
| 89 | double xx = pos.getX() + random.nextDouble();
|
| 90 | double yy = pos.getY() - 0.05;
|
| 91 | double zz = pos.getZ() + random.nextDouble();
|
| 92 | level.addParticle(new BlockParticleOption(ParticleTypes.FALLING_DUST, state), xx, yy, zz, 0.0, 0.0, 0.0);
|
| 93 | }
|
| 94 | }
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | public @Nullable BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 99 | return new BrushableBlockEntity(worldPosition, blockState);
|
| 100 | }
|
| 101 |
|
| 102 | public Block getTurnsInto() {
|
| 103 | return this.turnsInto;
|
| 104 | }
|
| 105 |
|
| 106 | public SoundEvent getBrushSound() {
|
| 107 | return this.brushSound;
|
| 108 | }
|
| 109 |
|
| 110 | public SoundEvent getBrushCompletedSound() {
|
| 111 | return this.brushCompletedSound;
|
| 112 | }
|
| 113 | }
|
| 114 |
|