| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.tags.BlockTags;
|
| 7 | import net.minecraft.util.RandomSource;
|
| 8 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 9 | import net.minecraft.world.level.BlockGetter;
|
| 10 | import net.minecraft.world.level.LevelReader;
|
| 11 | import net.minecraft.world.level.LightLayer;
|
| 12 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 13 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 14 | import net.minecraft.world.level.block.state.BlockState;
|
| 15 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 16 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 17 | import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
| 18 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 19 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 20 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 21 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 22 | import org.jspecify.annotations.Nullable;
|
| 23 |
|
| 24 | /**
|
| 25 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 26 | */
|
| 27 | public class SnowLayerBlock extends Block {
|
| 28 | public static final int MAX_HEIGHT = 8;
|
| 29 | public static final IntegerProperty LAYERS = BlockStateProperties.LAYERS;
|
| 30 | private static final VoxelShape[] SHAPES = Block.boxes(8, height -> Block.column(16.0, 0.0, height * 2));
|
| 31 | public static final int HEIGHT_IMPASSABLE = 5;
|
| 32 |
|
| 33 | /**
|
| 34 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 35 | */
|
| 36 | public SnowLayerBlock(final BlockBehaviour.Properties properties) {
|
| 37 | super(properties);
|
| 38 | this.registerDefaultState(this.stateDefinition.any().setValue(LAYERS, 1));
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 43 | return type == PathComputationType.LAND ? state.getValue(LAYERS) < 5 : false;
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 48 | return SHAPES[state.getValue(LAYERS)];
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | protected VoxelShape getCollisionShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 53 | return SHAPES[state.getValue(LAYERS) - 1];
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | protected VoxelShape getBlockSupportShape(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 58 | return SHAPES[state.getValue(LAYERS)];
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | protected VoxelShape getVisualShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 63 | return SHAPES[state.getValue(LAYERS)];
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | protected boolean useShapeForLightOcclusion(final BlockState state) {
|
| 68 | return true;
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | protected float getShadeBrightness(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 73 | return state.getValue(LAYERS) == 8 ? 0.2F : 1.0F;
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
|
| 78 | BlockState belowState = level.getBlockState(pos.below());
|
| 79 | if (belowState.is(BlockTags.CANNOT_SUPPORT_SNOW_LAYER)) {
|
| 80 | return false;
|
| 81 | } else {
|
| 82 | return belowState.is(BlockTags.SUPPORT_OVERRIDE_SNOW_LAYER)
|
| 83 | ? true
|
| 84 | : Block.isFaceFull(belowState.getCollisionShape(level, pos.below()), Direction.UP) || belowState.is(this) && belowState.getValue(LAYERS) == 8;
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | protected BlockState updateShape(
|
| 90 | final BlockState state,
|
| 91 | final LevelReader level,
|
| 92 | final ScheduledTickAccess ticks,
|
| 93 | final BlockPos pos,
|
| 94 | final Direction directionToNeighbour,
|
| 95 | final BlockPos neighbourPos,
|
| 96 | final BlockState neighbourState,
|
| 97 | final RandomSource random
|
| 98 | ) {
|
| 99 | return !state.canSurvive(level, pos)
|
| 100 | ? Blocks.AIR.defaultBlockState()
|
| 101 | : super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | protected void randomTick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 106 | if (level.getBrightness(LightLayer.BLOCK, pos) > 11) {
|
| 107 | dropResources(state, level, pos);
|
| 108 | level.removeBlock(pos, false);
|
| 109 | level.gameEvent(GameEvent.BLOCK_DESTROY, pos, GameEvent.Context.of(state));
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | protected boolean canBeReplaced(final BlockState state, final BlockPlaceContext context) {
|
| 115 | int layers = state.getValue(LAYERS);
|
| 116 | if (!context.getItemInHand().is(this.asItem()) || layers >= 8) {
|
| 117 | return layers == 1;
|
| 118 | } else {
|
| 119 | return context.replacingClickedOnBlock() ? context.getClickedFace() == Direction.UP : true;
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | public @Nullable BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 125 | BlockState state = context.getLevel().getBlockState(context.getClickedPos());
|
| 126 | if (state.is(this)) {
|
| 127 | int layers = state.getValue(LAYERS);
|
| 128 | return state.setValue(LAYERS, Math.min(8, layers + 1));
|
| 129 | } else {
|
| 130 | return super.getStateForPlacement(context);
|
| 131 | }
|
| 132 | }
|
| 133 |
|
| 134 | @Override
|
| 135 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 136 | builder.add(LAYERS);
|
| 137 | }
|
| 138 | }
|
| 139 |
|