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

97 lines
4 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.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.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.BooleanProperty;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;
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 LanternBlock extends Block implements SimpleWaterloggedBlock {
public static final MapCodec<LanternBlock> CODEC = simpleCodec(LanternBlock::new);
public static final BooleanProperty HANGING = BlockStateProperties.HANGING;
public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
protected static final VoxelShape AABB = Shapes.or(Block.box(5.0, 0.0, 5.0, 11.0, 7.0, 11.0), Block.box(6.0, 7.0, 6.0, 10.0, 9.0, 10.0));
protected static final VoxelShape HANGING_AABB = Shapes.or(Block.box(5.0, 1.0, 5.0, 11.0, 8.0, 11.0), Block.box(6.0, 8.0, 6.0, 10.0, 10.0, 10.0));
@Override
public MapCodec<LanternBlock> codec() {
return CODEC;
}
public LanternBlock(BlockBehaviour.Properties properties) {
super(properties);
this.registerDefaultState(this.stateDefinition.any().setValue(HANGING, false).setValue(WATERLOGGED, false));
}
@Nullable
@Override
public BlockState getStateForPlacement(BlockPlaceContext context) {
FluidState fluidState = context.getLevel().getFluidState(context.getClickedPos());
for (Direction direction : context.getNearestLookingDirections()) {
if (direction.getAxis() == Direction.Axis.Y) {
BlockState blockState = this.defaultBlockState().setValue(HANGING, direction == Direction.UP);
if (blockState.canSurvive(context.getLevel(), context.getClickedPos())) {
return blockState.setValue(WATERLOGGED, fluidState.getType() == Fluids.WATER);
}
}
}
return null;
}
@Override
protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
return state.getValue(HANGING) ? HANGING_AABB : AABB;
}
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(HANGING, WATERLOGGED);
}
@Override
protected boolean canSurvive(BlockState state, LevelReader level, BlockPos pos) {
Direction direction = getConnectedDirection(state).getOpposite();
return Block.canSupportCenter(level, pos.relative(direction), direction.getOpposite());
}
protected static Direction getConnectedDirection(BlockState state) {
return state.getValue(HANGING) ? Direction.DOWN : Direction.UP;
}
@Override
protected BlockState updateShape(BlockState state, Direction direction, BlockState neighborState, LevelAccessor level, BlockPos pos, BlockPos neighborPos) {
if ((Boolean)state.getValue(WATERLOGGED)) {
level.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
}
return getConnectedDirection(state).getOpposite() == direction && !state.canSurvive(level, pos)
? Blocks.AIR.defaultBlockState()
: super.updateShape(state, direction, neighborState, level, pos, neighborPos);
}
@Override
protected FluidState getFluidState(BlockState state) {
return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
}
@Override
protected boolean isPathfindable(BlockState state, PathComputationType pathComputationType) {
return false;
}
}