minecraft-src/net/minecraft/world/level/block/FallingBlock.java
2025-07-04 01:41:11 +03:00

70 lines
2.5 KiB
Java

package net.minecraft.world.level.block;
import com.mojang.serialization.MapCodec;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.particles.BlockParticleOption;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.tags.BlockTags;
import net.minecraft.util.ParticleUtils;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.item.FallingBlockEntity;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
public abstract class FallingBlock extends Block implements Fallable {
public FallingBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
protected abstract MapCodec<? extends FallingBlock> codec();
@Override
protected void onPlace(BlockState state, Level level, BlockPos pos, BlockState oldState, boolean movedByPiston) {
level.scheduleTick(pos, this, this.getDelayAfterPlace());
}
@Override
protected BlockState updateShape(BlockState state, Direction direction, BlockState neighborState, LevelAccessor level, BlockPos pos, BlockPos neighborPos) {
level.scheduleTick(pos, this, this.getDelayAfterPlace());
return super.updateShape(state, direction, neighborState, level, pos, neighborPos);
}
@Override
protected void tick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
if (isFree(level.getBlockState(pos.below())) && pos.getY() >= level.getMinBuildHeight()) {
FallingBlockEntity fallingBlockEntity = FallingBlockEntity.fall(level, pos, state);
this.falling(fallingBlockEntity);
}
}
protected void falling(FallingBlockEntity entity) {
}
protected int getDelayAfterPlace() {
return 2;
}
public static boolean isFree(BlockState state) {
return state.isAir() || state.is(BlockTags.FIRE) || state.liquid() || state.canBeReplaced();
}
@Override
public void animateTick(BlockState state, Level level, BlockPos pos, RandomSource random) {
if (random.nextInt(16) == 0) {
BlockPos blockPos = pos.below();
if (isFree(level.getBlockState(blockPos))) {
ParticleUtils.spawnParticleBelow(level, pos, random, new BlockParticleOption(ParticleTypes.FALLING_DUST, state));
}
}
}
public int getDustColor(BlockState state, BlockGetter level, BlockPos pos) {
return -16777216;
}
}