137 lines
		
	
	
	
		
			5.6 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			137 lines
		
	
	
	
		
			5.6 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.util.Mth;
 | |
| import net.minecraft.util.RandomSource;
 | |
| import net.minecraft.world.entity.LivingEntity;
 | |
| import net.minecraft.world.entity.player.Player;
 | |
| import net.minecraft.world.item.ItemStack;
 | |
| import net.minecraft.world.item.context.BlockPlaceContext;
 | |
| import net.minecraft.world.level.Level;
 | |
| import net.minecraft.world.level.LevelAccessor;
 | |
| import net.minecraft.world.level.LevelReader;
 | |
| import net.minecraft.world.level.ScheduledTickAccess;
 | |
| import net.minecraft.world.level.block.entity.BlockEntity;
 | |
| 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.DoubleBlockHalf;
 | |
| import net.minecraft.world.level.block.state.properties.EnumProperty;
 | |
| import net.minecraft.world.level.material.Fluids;
 | |
| import org.jetbrains.annotations.Nullable;
 | |
| 
 | |
| public class DoublePlantBlock extends VegetationBlock {
 | |
| 	public static final MapCodec<DoublePlantBlock> CODEC = simpleCodec(DoublePlantBlock::new);
 | |
| 	public static final EnumProperty<DoubleBlockHalf> HALF = BlockStateProperties.DOUBLE_BLOCK_HALF;
 | |
| 
 | |
| 	@Override
 | |
| 	public MapCodec<? extends DoublePlantBlock> codec() {
 | |
| 		return CODEC;
 | |
| 	}
 | |
| 
 | |
| 	public DoublePlantBlock(BlockBehaviour.Properties properties) {
 | |
| 		super(properties);
 | |
| 		this.registerDefaultState(this.stateDefinition.any().setValue(HALF, DoubleBlockHalf.LOWER));
 | |
| 	}
 | |
| 
 | |
| 	@Override
 | |
| 	protected BlockState updateShape(
 | |
| 		BlockState state,
 | |
| 		LevelReader level,
 | |
| 		ScheduledTickAccess scheduledTickAccess,
 | |
| 		BlockPos pos,
 | |
| 		Direction direction,
 | |
| 		BlockPos neighborPos,
 | |
| 		BlockState neighborState,
 | |
| 		RandomSource random
 | |
| 	) {
 | |
| 		DoubleBlockHalf doubleBlockHalf = state.getValue(HALF);
 | |
| 		if (direction.getAxis() != Direction.Axis.Y
 | |
| 			|| doubleBlockHalf == DoubleBlockHalf.LOWER != (direction == Direction.UP)
 | |
| 			|| neighborState.is(this) && neighborState.getValue(HALF) != doubleBlockHalf) {
 | |
| 			return doubleBlockHalf == DoubleBlockHalf.LOWER && direction == Direction.DOWN && !state.canSurvive(level, pos)
 | |
| 				? Blocks.AIR.defaultBlockState()
 | |
| 				: super.updateShape(state, level, scheduledTickAccess, pos, direction, neighborPos, neighborState, random);
 | |
| 		} else {
 | |
| 			return Blocks.AIR.defaultBlockState();
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	@Nullable
 | |
| 	@Override
 | |
| 	public BlockState getStateForPlacement(BlockPlaceContext context) {
 | |
| 		BlockPos blockPos = context.getClickedPos();
 | |
| 		Level level = context.getLevel();
 | |
| 		return blockPos.getY() < level.getMaxY() && level.getBlockState(blockPos.above()).canBeReplaced(context) ? super.getStateForPlacement(context) : null;
 | |
| 	}
 | |
| 
 | |
| 	@Override
 | |
| 	public void setPlacedBy(Level level, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) {
 | |
| 		BlockPos blockPos = pos.above();
 | |
| 		level.setBlock(blockPos, copyWaterloggedFrom(level, blockPos, this.defaultBlockState().setValue(HALF, DoubleBlockHalf.UPPER)), 3);
 | |
| 	}
 | |
| 
 | |
| 	@Override
 | |
| 	protected boolean canSurvive(BlockState state, LevelReader level, BlockPos pos) {
 | |
| 		if (state.getValue(HALF) != DoubleBlockHalf.UPPER) {
 | |
| 			return super.canSurvive(state, level, pos);
 | |
| 		} else {
 | |
| 			BlockState blockState = level.getBlockState(pos.below());
 | |
| 			return blockState.is(this) && blockState.getValue(HALF) == DoubleBlockHalf.LOWER;
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	public static void placeAt(LevelAccessor level, BlockState state, BlockPos pos, int flags) {
 | |
| 		BlockPos blockPos = pos.above();
 | |
| 		level.setBlock(pos, copyWaterloggedFrom(level, pos, state.setValue(HALF, DoubleBlockHalf.LOWER)), flags);
 | |
| 		level.setBlock(blockPos, copyWaterloggedFrom(level, blockPos, state.setValue(HALF, DoubleBlockHalf.UPPER)), flags);
 | |
| 	}
 | |
| 
 | |
| 	public static BlockState copyWaterloggedFrom(LevelReader level, BlockPos pos, BlockState state) {
 | |
| 		return state.hasProperty(BlockStateProperties.WATERLOGGED) ? state.setValue(BlockStateProperties.WATERLOGGED, level.isWaterAt(pos)) : state;
 | |
| 	}
 | |
| 
 | |
| 	@Override
 | |
| 	public BlockState playerWillDestroy(Level level, BlockPos pos, BlockState state, Player player) {
 | |
| 		if (!level.isClientSide) {
 | |
| 			if (player.preventsBlockDrops()) {
 | |
| 				preventDropFromBottomPart(level, pos, state, player);
 | |
| 			} else {
 | |
| 				dropResources(state, level, pos, null, player, player.getMainHandItem());
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		return super.playerWillDestroy(level, pos, state, player);
 | |
| 	}
 | |
| 
 | |
| 	@Override
 | |
| 	public void playerDestroy(Level level, Player player, BlockPos pos, BlockState state, @Nullable BlockEntity blockEntity, ItemStack tool) {
 | |
| 		super.playerDestroy(level, player, pos, Blocks.AIR.defaultBlockState(), blockEntity, tool);
 | |
| 	}
 | |
| 
 | |
| 	protected static void preventDropFromBottomPart(Level level, BlockPos pos, BlockState state, Player player) {
 | |
| 		DoubleBlockHalf doubleBlockHalf = state.getValue(HALF);
 | |
| 		if (doubleBlockHalf == DoubleBlockHalf.UPPER) {
 | |
| 			BlockPos blockPos = pos.below();
 | |
| 			BlockState blockState = level.getBlockState(blockPos);
 | |
| 			if (blockState.is(state.getBlock()) && blockState.getValue(HALF) == DoubleBlockHalf.LOWER) {
 | |
| 				BlockState blockState2 = blockState.getFluidState().is(Fluids.WATER) ? Blocks.WATER.defaultBlockState() : Blocks.AIR.defaultBlockState();
 | |
| 				level.setBlock(blockPos, blockState2, 35);
 | |
| 				level.levelEvent(player, 2001, blockPos, Block.getId(blockState));
 | |
| 			}
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	@Override
 | |
| 	protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
 | |
| 		builder.add(HALF);
 | |
| 	}
 | |
| 
 | |
| 	@Override
 | |
| 	protected long getSeed(BlockState state, BlockPos pos) {
 | |
| 		return Mth.getSeed(pos.getX(), pos.below(state.getValue(HALF) == DoubleBlockHalf.LOWER ? 0 : 1).getY(), pos.getZ());
 | |
| 	}
 | |
| }
 |