151 lines
		
	
	
	
		
			5.4 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			151 lines
		
	
	
	
		
			5.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.server.level.ServerLevel;
 | |
| import net.minecraft.tags.BlockTags;
 | |
| import net.minecraft.tags.FluidTags;
 | |
| import net.minecraft.util.RandomSource;
 | |
| import net.minecraft.world.entity.Entity;
 | |
| import net.minecraft.world.entity.LivingEntity;
 | |
| import net.minecraft.world.entity.player.Player;
 | |
| import net.minecraft.world.item.context.BlockPlaceContext;
 | |
| import net.minecraft.world.level.BlockGetter;
 | |
| import net.minecraft.world.level.GameRules;
 | |
| import net.minecraft.world.level.Level;
 | |
| import net.minecraft.world.level.LevelReader;
 | |
| import net.minecraft.world.level.ScheduledTickAccess;
 | |
| import net.minecraft.world.level.block.piston.MovingPistonBlock;
 | |
| 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.gameevent.GameEvent;
 | |
| 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 FarmBlock extends Block {
 | |
| 	public static final MapCodec<FarmBlock> CODEC = simpleCodec(FarmBlock::new);
 | |
| 	public static final IntegerProperty MOISTURE = BlockStateProperties.MOISTURE;
 | |
| 	private static final VoxelShape SHAPE = Block.column(16.0, 0.0, 15.0);
 | |
| 	public static final int MAX_MOISTURE = 7;
 | |
| 
 | |
| 	@Override
 | |
| 	public MapCodec<FarmBlock> codec() {
 | |
| 		return CODEC;
 | |
| 	}
 | |
| 
 | |
| 	protected FarmBlock(BlockBehaviour.Properties properties) {
 | |
| 		super(properties);
 | |
| 		this.registerDefaultState(this.stateDefinition.any().setValue(MOISTURE, 0));
 | |
| 	}
 | |
| 
 | |
| 	@Override
 | |
| 	protected BlockState updateShape(
 | |
| 		BlockState state,
 | |
| 		LevelReader level,
 | |
| 		ScheduledTickAccess scheduledTickAccess,
 | |
| 		BlockPos pos,
 | |
| 		Direction direction,
 | |
| 		BlockPos neighborPos,
 | |
| 		BlockState neighborState,
 | |
| 		RandomSource random
 | |
| 	) {
 | |
| 		if (direction == Direction.UP && !state.canSurvive(level, pos)) {
 | |
| 			scheduledTickAccess.scheduleTick(pos, this, 1);
 | |
| 		}
 | |
| 
 | |
| 		return super.updateShape(state, level, scheduledTickAccess, pos, direction, neighborPos, neighborState, random);
 | |
| 	}
 | |
| 
 | |
| 	@Override
 | |
| 	protected boolean canSurvive(BlockState state, LevelReader level, BlockPos pos) {
 | |
| 		BlockState blockState = level.getBlockState(pos.above());
 | |
| 		return !blockState.isSolid() || blockState.getBlock() instanceof FenceGateBlock || blockState.getBlock() instanceof MovingPistonBlock;
 | |
| 	}
 | |
| 
 | |
| 	@Override
 | |
| 	public BlockState getStateForPlacement(BlockPlaceContext context) {
 | |
| 		return !this.defaultBlockState().canSurvive(context.getLevel(), context.getClickedPos())
 | |
| 			? Blocks.DIRT.defaultBlockState()
 | |
| 			: super.getStateForPlacement(context);
 | |
| 	}
 | |
| 
 | |
| 	@Override
 | |
| 	protected boolean useShapeForLightOcclusion(BlockState state) {
 | |
| 		return true;
 | |
| 	}
 | |
| 
 | |
| 	@Override
 | |
| 	protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
 | |
| 		return SHAPE;
 | |
| 	}
 | |
| 
 | |
| 	@Override
 | |
| 	protected void tick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
 | |
| 		if (!state.canSurvive(level, pos)) {
 | |
| 			turnToDirt(null, state, level, pos);
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	@Override
 | |
| 	protected void randomTick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
 | |
| 		int i = (Integer)state.getValue(MOISTURE);
 | |
| 		if (!isNearWater(level, pos) && !level.isRainingAt(pos.above())) {
 | |
| 			if (i > 0) {
 | |
| 				level.setBlock(pos, state.setValue(MOISTURE, i - 1), 2);
 | |
| 			} else if (!shouldMaintainFarmland(level, pos)) {
 | |
| 				turnToDirt(null, state, level, pos);
 | |
| 			}
 | |
| 		} else if (i < 7) {
 | |
| 			level.setBlock(pos, state.setValue(MOISTURE, 7), 2);
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	@Override
 | |
| 	public void fallOn(Level level, BlockState state, BlockPos pos, Entity entity, double fallDistance) {
 | |
| 		if (level instanceof ServerLevel serverLevel
 | |
| 			&& level.random.nextFloat() < fallDistance - 0.5
 | |
| 			&& entity instanceof LivingEntity
 | |
| 			&& (entity instanceof Player || serverLevel.getGameRules().getBoolean(GameRules.RULE_MOBGRIEFING))
 | |
| 			&& entity.getBbWidth() * entity.getBbWidth() * entity.getBbHeight() > 0.512F) {
 | |
| 			turnToDirt(entity, state, level, pos);
 | |
| 		}
 | |
| 
 | |
| 		super.fallOn(level, state, pos, entity, fallDistance);
 | |
| 	}
 | |
| 
 | |
| 	public static void turnToDirt(@Nullable Entity entity, BlockState state, Level level, BlockPos pos) {
 | |
| 		BlockState blockState = pushEntitiesUp(state, Blocks.DIRT.defaultBlockState(), level, pos);
 | |
| 		level.setBlockAndUpdate(pos, blockState);
 | |
| 		level.gameEvent(GameEvent.BLOCK_CHANGE, pos, GameEvent.Context.of(entity, blockState));
 | |
| 	}
 | |
| 
 | |
| 	private static boolean shouldMaintainFarmland(BlockGetter level, BlockPos pos) {
 | |
| 		return level.getBlockState(pos.above()).is(BlockTags.MAINTAINS_FARMLAND);
 | |
| 	}
 | |
| 
 | |
| 	private static boolean isNearWater(LevelReader level, BlockPos pos) {
 | |
| 		for (BlockPos blockPos : BlockPos.betweenClosed(pos.offset(-4, 0, -4), pos.offset(4, 1, 4))) {
 | |
| 			if (level.getFluidState(blockPos).is(FluidTags.WATER)) {
 | |
| 				return true;
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		return false;
 | |
| 	}
 | |
| 
 | |
| 	@Override
 | |
| 	protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
 | |
| 		builder.add(MOISTURE);
 | |
| 	}
 | |
| 
 | |
| 	@Override
 | |
| 	protected boolean isPathfindable(BlockState state, PathComputationType pathComputationType) {
 | |
| 		return false;
 | |
| 	}
 | |
| }
 |