minecraft-src/net/minecraft/world/level/block/SnowLayerBlock.java
2025-07-04 03:45:38 +03:00

138 lines
4.7 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.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.LevelReader;
import net.minecraft.world.level.LightLayer;
import net.minecraft.world.level.ScheduledTickAccess;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition.Builder;
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.VoxelShape;
import org.jetbrains.annotations.Nullable;
public class SnowLayerBlock extends Block {
public static final MapCodec<SnowLayerBlock> CODEC = simpleCodec(SnowLayerBlock::new);
public static final int MAX_HEIGHT = 8;
public static final IntegerProperty LAYERS = BlockStateProperties.LAYERS;
private static final VoxelShape[] SHAPES = Block.boxes(8, i -> Block.column(16.0, 0.0, i * 2));
public static final int HEIGHT_IMPASSABLE = 5;
@Override
public MapCodec<SnowLayerBlock> 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) {
return pathComputationType == PathComputationType.LAND ? (Integer)state.getValue(LAYERS) < 5 : false;
}
@Override
protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
return SHAPES[state.getValue(LAYERS)];
}
@Override
protected VoxelShape getCollisionShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
return SHAPES[state.getValue(LAYERS) - 1];
}
@Override
protected VoxelShape getBlockSupportShape(BlockState state, BlockGetter level, BlockPos pos) {
return SHAPES[state.getValue(LAYERS)];
}
@Override
protected VoxelShape getVisualShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
return SHAPES[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,
LevelReader level,
ScheduledTickAccess scheduledTickAccess,
BlockPos pos,
Direction direction,
BlockPos neighborPos,
BlockState neighborState,
RandomSource random
) {
return !state.canSurvive(level, pos)
? Blocks.AIR.defaultBlockState()
: super.updateShape(state, level, scheduledTickAccess, pos, direction, neighborPos, neighborState, random);
}
@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(Builder<Block, BlockState> builder) {
builder.add(LAYERS);
}
}