CropBlock .java

net.minecraft.world.level.block.CropBlock (class): members: hasSufficientLight, getGrowthSpeed, growCrops, getStateForAge, isMaxAge, getAgeProperty, AGE.

net.minecraft.world.level.blockclass192 linescommonextends VegetationBlock6.4 KB

Implements

API Surface (21)

VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context)
boolean mayPlaceOn(final BlockState state, final BlockGetter level, final BlockPos pos)
IntegerProperty getAgeProperty()
int getMaxAge()
int getAge(final BlockState state)
BlockState getStateForAge(final int age)
boolean isMaxAge(final BlockState state)
boolean isRandomlyTicking(final BlockState state)
void randomTick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random)
void growCrops(final Level level, final BlockPos pos, final BlockState state)
int getBonemealAgeIncrease(final Level level)
float getGrowthSpeed(final Block type, final BlockGetter level, final BlockPos pos)
boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos)
boolean hasSufficientLight(final LevelReader level, final BlockPos pos)
void entityInside(final BlockState state, final Level level, final BlockPos pos, final Entity entity, final InsideBlockEffectApplier effectApplier, final boolean isPrecise)
ItemLike getBaseSeedId()
ItemStack getCloneItemStack(final LevelReader level, final BlockPos pos, final BlockState state, final boolean includeData)
boolean isValidBonemealTarget(final LevelReader level, final BlockPos pos, final BlockState state, final BonemealSource source)
boolean isBonemealSuccess(final Level level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source)
void performBonemeal(final ServerLevel level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source)
void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder)

Fields & Constants (4)

MAX_AGE
AGE
SHAPES
effectApplier

Full Source

CropBlock.java192 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block;
2
3
import net.minecraft.core.BlockPos;
4
import net.minecraft.server.level.ServerLevel;
5
import net.minecraft.tags.BlockTags;
6
import net.minecraft.util.Mth;
7
import net.minecraft.util.RandomSource;
8
import net.minecraft.world.entity.Entity;
9
import net.minecraft.world.entity.InsideBlockEffectApplier;
10
import net.minecraft.world.entity.monster.Ravager;
11
import net.minecraft.world.item.ItemStack;
12
import net.minecraft.world.item.Items;
13
import net.minecraft.world.level.BlockGetter;
14
import net.minecraft.world.level.ItemLike;
15
import net.minecraft.world.level.Level;
16
import net.minecraft.world.level.LevelReader;
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.gamerules.GameRules;
23
import net.minecraft.world.phys.shapes.CollisionContext;
24
import net.minecraft.world.phys.shapes.VoxelShape;
25
26
/**
27
 * Access widened by fabric-transitive-access-wideners-v1 to accessible
28
 */
29
public class CropBlock extends VegetationBlock implements BonemealableBlock {
30
	public static final int MAX_AGE = 7;
31
	public static final IntegerProperty AGE = BlockStateProperties.AGE_7;
32
	private static final VoxelShape[] SHAPES = Block.boxes(7, age -> Block.column(16.0, 0.0, 2 + age * 2));
33
34
	/**
35
	 * Access widened by fabric-transitive-access-wideners-v1 to accessible
36
	 */
37
	public CropBlock(final BlockBehaviour.Properties properties) {
38
		super(properties);
39
		this.registerDefaultState(this.stateDefinition.any().setValue(this.getAgeProperty(), 0));
40
	}
41
42
	@Override
43
	protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
44
		return SHAPES[this.getAge(state)];
45
	}
46
47
	@Override
48
	protected boolean mayPlaceOn(final BlockState state, final BlockGetter level, final BlockPos pos) {
49
		return state.is(BlockTags.SUPPORTS_CROPS);
50
	}
51
52
	protected IntegerProperty getAgeProperty() {
53
		return AGE;
54
	}
55
56
	public int getMaxAge() {
57
		return 7;
58
	}
59
60
	public int getAge(final BlockState state) {
61
		return state.getValue(this.getAgeProperty());
62
	}
63
64
	public BlockState getStateForAge(final int age) {
65
		return this.defaultBlockState().setValue(this.getAgeProperty(), age);
66
	}
67
68
	public final boolean isMaxAge(final BlockState state) {
69
		return this.getAge(state) >= this.getMaxAge();
70
	}
71
72
	@Override
73
	protected boolean isRandomlyTicking(final BlockState state) {
74
		return !this.isMaxAge(state);
75
	}
76
77
	@Override
78
	protected void randomTick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
79
		if (level.getRawBrightness(pos, 0) >= 9) {
80
			int age = this.getAge(state);
81
			if (age < this.getMaxAge()) {
82
				float growthSpeed = getGrowthSpeed(this, level, pos);
83
				if (random.nextInt((int)(25.0F / growthSpeed) + 1) == 0) {
84
					level.setBlock(pos, this.getStateForAge(age + 1), 2);
85
				}
86
			}
87
		}
88
	}
89
90
	public void growCrops(final Level level, final BlockPos pos, final BlockState state) {
91
		int age = Math.min(this.getMaxAge(), this.getAge(state) + this.getBonemealAgeIncrease(level));
92
		level.setBlock(pos, this.getStateForAge(age), 2);
93
	}
94
95
	protected int getBonemealAgeIncrease(final Level level) {
96
		return Mth.nextInt(level.getRandom(), 2, 5);
97
	}
98
99
	protected static float getGrowthSpeed(final Block type, final BlockGetter level, final BlockPos pos) {
100
		float speed = 1.0F;
101
		BlockPos below = pos.below();
102
103
		for (int xx = -1; xx <= 1; xx++) {
104
			for (int zz = -1; zz <= 1; zz++) {
105
				float blockSpeed = 0.0F;
106
				BlockState blockState = level.getBlockState(below.offset(xx, 0, zz));
107
				if (blockState.is(BlockTags.GROWS_CROPS)) {
108
					blockSpeed = 1.0F;
109
					if (blockState.getValueOrElse(FarmlandBlock.MOISTURE, 0) > 0) {
110
						blockSpeed = 3.0F;
111
					}
112
				}
113
114
				if (xx != 0 || zz != 0) {
115
					blockSpeed /= 4.0F;
116
				}
117
118
				speed += blockSpeed;
119
			}
120
		}
121
122
		BlockPos north = pos.north();
123
		BlockPos south = pos.south();
124
		BlockPos west = pos.west();
125
		BlockPos east = pos.east();
126
		boolean horizontal = level.getBlockState(west).is(type) || level.getBlockState(east).is(type);
127
		boolean vertical = level.getBlockState(north).is(type) || level.getBlockState(south).is(type);
128
		if (horizontal && vertical) {
129
			speed /= 2.0F;
130
		} else {
131
			boolean diagonal = level.getBlockState(west.north()).is(type)
132
				|| level.getBlockState(east.north()).is(type)
133
				|| level.getBlockState(east.south()).is(type)
134
				|| level.getBlockState(west.south()).is(type);
135
			if (diagonal) {
136
				speed /= 2.0F;
137
			}
138
		}
139
140
		return speed;
141
	}
142
143
	@Override
144
	protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
145
		return hasSufficientLight(level, pos) && super.canSurvive(state, level, pos);
146
	}
147
148
	protected static boolean hasSufficientLight(final LevelReader level, final BlockPos pos) {
149
		return level.getRawBrightness(pos, 0) >= 8;
150
	}
151
152
	@Override
153
	protected void entityInside(
154
		final BlockState state, final Level level, final BlockPos pos, final Entity entity, final InsideBlockEffectApplier effectApplier, final boolean isPrecise
155
	) {
156
		if (level instanceof ServerLevel serverLevel && entity instanceof Ravager && serverLevel.getGameRules().get(GameRules.MOB_GRIEFING)) {
157
			serverLevel.destroyBlock(pos, true, entity);
158
		}
159
160
		super.entityInside(state, level, pos, entity, effectApplier, isPrecise);
161
	}
162
163
	protected ItemLike getBaseSeedId() {
164
		return Items.WHEAT_SEEDS;
165
	}
166
167
	@Override
168
	protected ItemStack getCloneItemStack(final LevelReader level, final BlockPos pos, final BlockState state, final boolean includeData) {
169
		return new ItemStack(this.getBaseSeedId());
170
	}
171
172
	@Override
173
	public boolean isValidBonemealTarget(final LevelReader level, final BlockPos pos, final BlockState state, final BonemealSource source) {
174
		return !this.isMaxAge(state);
175
	}
176
177
	@Override
178
	public boolean isBonemealSuccess(final Level level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
179
		return true;
180
	}
181
182
	@Override
183
	public void performBonemeal(final ServerLevel level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
184
		this.growCrops(level, pos, state);
185
	}
186
187
	@Override
188
	protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
189
		builder.add(AGE);
190
	}
191
}
192

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