FarmlandBlock .java

net.minecraft.world.level.block.FarmlandBlock (class, 147 lines): members: MAX_MOISTURE, turnToBaseBlock, MOISTURE, baseBlock, fallOn, randomTick, SHAPE.

net.minecraft.world.level.blockclass147 linescommonextends Block5.7 KB

API Surface (11)

BlockState updateShape(final BlockState state, final LevelReader level, final ScheduledTickAccess ticks, final BlockPos pos, final Direction directionToNeighbour, final BlockPos neighbourPos, final BlockState neighbourState, final RandomSource random)
boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos)
BlockState getStateForPlacement(final BlockPlaceContext context)
boolean useShapeForLightOcclusion(final BlockState state)
VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context)
void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random)
void randomTick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random)
void fallOn(final Level level, final BlockState state, final BlockPos pos, final Entity entity, final double fallDistance)
void turnToBaseBlock(final @Nullable Entity sourceEntity, final BlockState state, final Level level, final BlockPos pos)
void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder)
boolean isPathfindable(final BlockState state, final PathComputationType type)

Fields & Constants (11)

MOISTURE
SHAPE
MAX_MOISTURE
baseBlock
state
level
ticks
pos
directionToNeighbour
neighbourPos
neighbourState

Full Source

FarmlandBlock.java147 lines · Minecraft 26.3 · verbatim
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.tags.FluidTags;
8
import net.minecraft.util.RandomSource;
9
import net.minecraft.world.entity.Entity;
10
import net.minecraft.world.entity.LivingEntity;
11
import net.minecraft.world.entity.player.Player;
12
import net.minecraft.world.item.context.BlockPlaceContext;
13
import net.minecraft.world.level.BlockGetter;
14
import net.minecraft.world.level.Level;
15
import net.minecraft.world.level.LevelReader;
16
import net.minecraft.world.level.ScheduledTickAccess;
17
import net.minecraft.world.level.block.state.BlockBehaviour;
18
import net.minecraft.world.level.block.state.BlockState;
19
import net.minecraft.world.level.block.state.StateDefinition;
20
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
21
import net.minecraft.world.level.block.state.properties.IntegerProperty;
22
import net.minecraft.world.level.gameevent.GameEvent;
23
import net.minecraft.world.level.gamerules.GameRules;
24
import net.minecraft.world.level.pathfinder.PathComputationType;
25
import net.minecraft.world.phys.shapes.CollisionContext;
26
import net.minecraft.world.phys.shapes.VoxelShape;
27
import org.jspecify.annotations.Nullable;
28
29
/**
30
 * Access widened by fabric-transitive-access-wideners-v1 to accessible
31
 */
32
public class FarmlandBlock extends Block {
33
	private final Block baseBlock;
34
	public static final IntegerProperty MOISTURE = BlockStateProperties.MOISTURE;
35
	private static final VoxelShape SHAPE = Block.column(16.0, 0.0, 15.0);
36
	public static final int MAX_MOISTURE = 7;
37
38
	/**
39
	 * Access widened by fabric-transitive-access-wideners-v1 to accessible
40
	 */
41
	public FarmlandBlock(final Block baseBlock, final BlockBehaviour.Properties properties) {
42
		super(properties);
43
		this.registerDefaultState(this.stateDefinition.any().setValue(MOISTURE, 0));
44
		this.baseBlock = baseBlock;
45
	}
46
47
	@Override
48
	protected BlockState updateShape(
49
		final BlockState state,
50
		final LevelReader level,
51
		final ScheduledTickAccess ticks,
52
		final BlockPos pos,
53
		final Direction directionToNeighbour,
54
		final BlockPos neighbourPos,
55
		final BlockState neighbourState,
56
		final RandomSource random
57
	) {
58
		if (directionToNeighbour == Direction.UP && !state.canSurvive(level, pos)) {
59
			ticks.scheduleTick(pos, this, 1);
60
		}
61
62
		return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
63
	}
64
65
	@Override
66
	protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
67
		BlockState aboveState = level.getBlockState(pos.above());
68
		return !aboveState.isSolid() || shouldMaintainFarmland(level, pos);
69
	}
70
71
	@Override
72
	public BlockState getStateForPlacement(final BlockPlaceContext context) {
73
		return !this.defaultBlockState().canSurvive(context.getLevel(), context.getClickedPos())
74
			? this.baseBlock.defaultBlockState()
75
			: super.getStateForPlacement(context);
76
	}
77
78
	@Override
79
	protected boolean useShapeForLightOcclusion(final BlockState state) {
80
		return true;
81
	}
82
83
	@Override
84
	protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
85
		return SHAPE;
86
	}
87
88
	@Override
89
	protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
90
		if (!state.canSurvive(level, pos)) {
91
			this.turnToBaseBlock(null, state, level, pos);
92
		}
93
	}
94
95
	@Override
96
	protected void randomTick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
97
		int moisture = state.getValue(MOISTURE);
98
		if (!isNearWater(level, pos) && !level.isRainingAt(pos.above())) {
99
			if (moisture > 0) {
100
				level.setBlock(pos, state.setValue(MOISTURE, moisture - 1), 2);
101
			} else if (!shouldMaintainFarmland(level, pos)) {
102
				this.turnToBaseBlock(null, state, level, pos);
103
			}
104
		} else if (moisture < 7) {
105
			level.setBlock(pos, state.setValue(MOISTURE, 7), 2);
106
		}
107
	}
108
109
	@Override
110
	public void fallOn(final Level level, final BlockState state, final BlockPos pos, final Entity entity, final double fallDistance) {
111
		if (level instanceof ServerLevel serverLevel && level.getRandom().nextFloat() < fallDistance - 0.5 && entity instanceof LivingEntity) {
112
			boolean canTrample = entity instanceof Player player
113
				? !level.getServer().isUnderSpawnProtection(serverLevel, pos, player)
114
				: serverLevel.getGameRules().get(GameRules.MOB_GRIEFING);
115
			if (canTrample && entity.getBbWidth() * entity.getBbWidth() * entity.getBbHeight() > 0.512F) {
116
				this.turnToBaseBlock(entity, state, level, pos);
117
			}
118
		}
119
120
		super.fallOn(level, state, pos, entity, fallDistance);
121
	}
122
123
	public void turnToBaseBlock(final @Nullable Entity sourceEntity, final BlockState state, final Level level, final BlockPos pos) {
124
		BlockState newState = pushEntitiesUp(state, this.baseBlock.defaultBlockState(), level, pos);
125
		level.setBlockAndUpdate(pos, newState);
126
		level.gameEvent(GameEvent.BLOCK_CHANGE, pos, GameEvent.Context.of(sourceEntity, newState));
127
	}
128
129
	private static boolean shouldMaintainFarmland(final BlockGetter level, final BlockPos pos) {
130
		return level.getBlockState(pos.above()).is(BlockTags.MAINTAINS_FARMLAND);
131
	}
132
133
	private static boolean isNearWater(final LevelReader level, final BlockPos pos) {
134
		return level.findBlocksIn(pos.offset(-4, 0, -4), pos.offset(4, 1, 4)).filterState(state -> state.getFluidState().is(FluidTags.WATER)).anyMatched();
135
	}
136
137
	@Override
138
	protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
139
		builder.add(MOISTURE);
140
	}
141
142
	@Override
143
	protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
144
		return false;
145
	}
146
}
147

Decompiled Minecraft 26.3 source of FarmlandBlock (net/minecraft/world/level/block/FarmlandBlock.java) with Mojang official mappings, stored verbatim on the site.