package net.minecraft.world.level.block; import com.mojang.serialization.MapCodec; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.server.level.ServerLevel; import net.minecraft.tags.BlockTags; import net.minecraft.util.RandomSource; import net.minecraft.world.item.context.BlockPlaceContext; import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.LevelAccessor; import net.minecraft.world.level.LevelReader; import net.minecraft.world.level.LightLayer; import net.minecraft.world.level.block.state.BlockBehaviour; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.StateDefinition; import net.minecraft.world.level.block.state.properties.BlockStateProperties; import net.minecraft.world.level.block.state.properties.IntegerProperty; import net.minecraft.world.level.pathfinder.PathComputationType; import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.Shapes; import net.minecraft.world.phys.shapes.VoxelShape; import org.jetbrains.annotations.Nullable; public class SnowLayerBlock extends Block { public static final MapCodec CODEC = simpleCodec(SnowLayerBlock::new); public static final int MAX_HEIGHT = 8; public static final IntegerProperty LAYERS = BlockStateProperties.LAYERS; protected static final VoxelShape[] SHAPE_BY_LAYER = new VoxelShape[]{ Shapes.empty(), Block.box(0.0, 0.0, 0.0, 16.0, 2.0, 16.0), Block.box(0.0, 0.0, 0.0, 16.0, 4.0, 16.0), Block.box(0.0, 0.0, 0.0, 16.0, 6.0, 16.0), Block.box(0.0, 0.0, 0.0, 16.0, 8.0, 16.0), Block.box(0.0, 0.0, 0.0, 16.0, 10.0, 16.0), Block.box(0.0, 0.0, 0.0, 16.0, 12.0, 16.0), Block.box(0.0, 0.0, 0.0, 16.0, 14.0, 16.0), Block.box(0.0, 0.0, 0.0, 16.0, 16.0, 16.0) }; public static final int HEIGHT_IMPASSABLE = 5; @Override public MapCodec codec() { return CODEC; } protected SnowLayerBlock(BlockBehaviour.Properties properties) { super(properties); this.registerDefaultState(this.stateDefinition.any().setValue(LAYERS, 1)); } @Override protected boolean isPathfindable(BlockState state, PathComputationType pathComputationType) { switch (pathComputationType) { case LAND: return (Integer)state.getValue(LAYERS) < 5; case WATER: return false; case AIR: return false; default: return false; } } @Override protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) { return SHAPE_BY_LAYER[state.getValue(LAYERS)]; } @Override protected VoxelShape getCollisionShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) { return SHAPE_BY_LAYER[state.getValue(LAYERS) - 1]; } @Override protected VoxelShape getBlockSupportShape(BlockState state, BlockGetter level, BlockPos pos) { return SHAPE_BY_LAYER[state.getValue(LAYERS)]; } @Override protected VoxelShape getVisualShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) { return SHAPE_BY_LAYER[state.getValue(LAYERS)]; } @Override protected boolean useShapeForLightOcclusion(BlockState state) { return true; } @Override protected float getShadeBrightness(BlockState state, BlockGetter level, BlockPos pos) { return state.getValue(LAYERS) == 8 ? 0.2F : 1.0F; } @Override protected boolean canSurvive(BlockState state, LevelReader level, BlockPos pos) { BlockState blockState = level.getBlockState(pos.below()); if (blockState.is(BlockTags.SNOW_LAYER_CANNOT_SURVIVE_ON)) { return false; } else { return blockState.is(BlockTags.SNOW_LAYER_CAN_SURVIVE_ON) ? true : Block.isFaceFull(blockState.getCollisionShape(level, pos.below()), Direction.UP) || blockState.is(this) && (Integer)blockState.getValue(LAYERS) == 8; } } @Override protected BlockState updateShape(BlockState state, Direction direction, BlockState neighborState, LevelAccessor level, BlockPos pos, BlockPos neighborPos) { return !state.canSurvive(level, pos) ? Blocks.AIR.defaultBlockState() : super.updateShape(state, direction, neighborState, level, pos, neighborPos); } @Override protected void randomTick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) { if (level.getBrightness(LightLayer.BLOCK, pos) > 11) { dropResources(state, level, pos); level.removeBlock(pos, false); } } @Override protected boolean canBeReplaced(BlockState state, BlockPlaceContext useContext) { int i = (Integer)state.getValue(LAYERS); if (!useContext.getItemInHand().is(this.asItem()) || i >= 8) { return i == 1; } else { return useContext.replacingClickedOnBlock() ? useContext.getClickedFace() == Direction.UP : true; } } @Nullable @Override public BlockState getStateForPlacement(BlockPlaceContext context) { BlockState blockState = context.getLevel().getBlockState(context.getClickedPos()); if (blockState.is(this)) { int i = (Integer)blockState.getValue(LAYERS); return blockState.setValue(LAYERS, Math.min(8, i + 1)); } else { return super.getStateForPlacement(context); } } @Override protected void createBlockStateDefinition(StateDefinition.Builder builder) { builder.add(LAYERS); } }