BoneMealItem .java

net.minecraft.world.item.BoneMealItem (class): members: GRASS_COUNT_MULTIPLIER, GRASS_SPREAD_HEIGHT, GRASS_SPREAD_WIDTH, addGrowthParticles, growCrop.

net.minecraft.world.itemclass161 linescommonextends Item6.3 KB

API Surface (4)

InteractionResult useOn(final UseOnContext context)
boolean growCrop(final ItemStack itemStack, final Level level, final BlockPos pos)
boolean growWaterPlant(final ItemStack itemStack, final Level level, final BlockPos pos, final @Nullable Direction clickedFace)
void addGrowthParticles(final LevelAccessor level, final BlockPos pos, final int count)

Fields & Constants (3)

GRASS_SPREAD_WIDTH
GRASS_SPREAD_HEIGHT
GRASS_COUNT_MULTIPLIER

Full Source

BoneMealItem.java161 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.item;
2
3
import net.minecraft.core.BlockPos;
4
import net.minecraft.core.Direction;
5
import net.minecraft.core.Holder;
6
import net.minecraft.core.particles.ParticleTypes;
7
import net.minecraft.core.registries.BuiltInRegistries;
8
import net.minecraft.server.level.ServerLevel;
9
import net.minecraft.tags.BiomeTags;
10
import net.minecraft.tags.BlockTags;
11
import net.minecraft.util.ParticleUtils;
12
import net.minecraft.util.RandomSource;
13
import net.minecraft.world.InteractionResult;
14
import net.minecraft.world.item.context.UseOnContext;
15
import net.minecraft.world.level.Level;
16
import net.minecraft.world.level.LevelAccessor;
17
import net.minecraft.world.level.biome.Biome;
18
import net.minecraft.world.level.block.BaseCoralWallFanBlock;
19
import net.minecraft.world.level.block.Blocks;
20
import net.minecraft.world.level.block.BonemealSource;
21
import net.minecraft.world.level.block.BonemealableBlock;
22
import net.minecraft.world.level.block.state.BlockState;
23
import net.minecraft.world.level.gameevent.GameEvent;
24
import org.jspecify.annotations.Nullable;
25
26
public class BoneMealItem extends Item {
27
	public static final int GRASS_SPREAD_WIDTH = 3;
28
	public static final int GRASS_SPREAD_HEIGHT = 1;
29
	public static final int GRASS_COUNT_MULTIPLIER = 3;
30
31
	public BoneMealItem(final Item.Properties properties) {
32
		super(properties);
33
	}
34
35
	@Override
36
	public InteractionResult useOn(final UseOnContext context) {
37
		Level level = context.getLevel();
38
		BlockPos pos = context.getClickedPos();
39
		BlockPos relative = pos.relative(context.getClickedFace());
40
		ItemStack boneMealStack = context.getItemInHand();
41
		if (growCrop(boneMealStack, level, pos)) {
42
			if (!level.isClientSide()) {
43
				boneMealStack.causeUseVibration(context.getPlayer(), GameEvent.ITEM_INTERACT_FINISH);
44
				level.levelEvent(1505, pos, 15);
45
				return InteractionResult.SUCCESS_SERVER;
46
			} else {
47
				return InteractionResult.SUCCESS;
48
			}
49
		} else {
50
			BlockState clickedState = level.getBlockState(pos);
51
			boolean solidBlockFace = clickedState.isFaceSturdy(level, pos, context.getClickedFace());
52
			if (solidBlockFace && growWaterPlant(boneMealStack, level, relative, context.getClickedFace())) {
53
				if (!level.isClientSide()) {
54
					boneMealStack.causeUseVibration(context.getPlayer(), GameEvent.ITEM_INTERACT_FINISH);
55
					level.levelEvent(1505, relative, 15);
56
				}
57
58
				return InteractionResult.SUCCESS;
59
			} else {
60
				return InteractionResult.PASS;
61
			}
62
		}
63
	}
64
65
	public static boolean growCrop(final ItemStack itemStack, final Level level, final BlockPos pos) {
66
		BlockState state = level.getBlockState(pos);
67
		if (state.getBlock() instanceof BonemealableBlock block && block.isValidBonemealTarget(level, pos, state, BonemealSource.INTERACTION)) {
68
			if (level instanceof ServerLevel serverLevel) {
69
				if (block.isBonemealSuccess(level, level.getRandom(), pos, state, BonemealSource.INTERACTION)) {
70
					block.performBonemeal(serverLevel, level.getRandom(), pos, state, BonemealSource.INTERACTION);
71
				}
72
73
				itemStack.shrink(1);
74
			}
75
76
			return true;
77
		} else {
78
			return false;
79
		}
80
	}
81
82
	public static boolean growWaterPlant(final ItemStack itemStack, final Level level, final BlockPos pos, final @Nullable Direction clickedFace) {
83
		if (level.getBlockState(pos).is(Blocks.WATER) && level.getFluidState(pos).isFull()) {
84
			if (!(level instanceof ServerLevel serverLevel)) {
85
				return true;
86
			} else {
87
				RandomSource random = level.getRandom();
88
89
				label81:
90
				for (int j = 0; j < 128; j++) {
91
					BlockPos testPos = pos;
92
					BlockState stateToGrow = Blocks.SEAGRASS.defaultBlockState();
93
94
					for (int i = 0; i < j / 16; i++) {
95
						testPos = testPos.offset(random.nextInt(3) - 1, (random.nextInt(3) - 1) * random.nextInt(3) / 2, random.nextInt(3) - 1);
96
						if (level.getBlockState(testPos).isCollisionShapeFullBlock(level, testPos)) {
97
							continue label81;
98
						}
99
					}
100
101
					Holder<Biome> testBiome = level.getBiome(testPos);
102
					if (testBiome.is(BiomeTags.PRODUCES_CORALS_FROM_BONEMEAL)) {
103
						if (j == 0 && clickedFace != null && clickedFace.getAxis().isHorizontal()) {
104
							stateToGrow = BuiltInRegistries.BLOCK
105
								.getRandomElementOf(BlockTags.WALL_CORALS, level.getRandom())
106
								.map(h -> h.value().defaultBlockState())
107
								.orElse(stateToGrow);
108
							if (stateToGrow.hasProperty(BaseCoralWallFanBlock.FACING)) {
109
								stateToGrow = stateToGrow.setValue(BaseCoralWallFanBlock.FACING, clickedFace);
110
							}
111
						} else if (random.nextInt(4) == 0) {
112
							stateToGrow = BuiltInRegistries.BLOCK
113
								.getRandomElementOf(BlockTags.UNDERWATER_BONEMEALS, level.getRandom())
114
								.map(h -> h.value().defaultBlockState())
115
								.orElse(stateToGrow);
116
						}
117
					}
118
119
					if (stateToGrow.is(BlockTags.WALL_CORALS, s -> s.hasProperty(BaseCoralWallFanBlock.FACING))) {
120
						for (int d = 0; !stateToGrow.canSurvive(level, testPos) && d < 4; d++) {
121
							stateToGrow = stateToGrow.setValue(BaseCoralWallFanBlock.FACING, Direction.Plane.HORIZONTAL.getRandomDirection(random));
122
						}
123
					}
124
125
					if (stateToGrow.canSurvive(level, testPos)) {
126
						BlockState testState = level.getBlockState(testPos);
127
						if (testState.is(Blocks.WATER) && level.getFluidState(testPos).isFull()) {
128
							level.setBlockAndUpdate(testPos, stateToGrow);
129
						} else if (testState.is(Blocks.SEAGRASS)
130
							&& ((BonemealableBlock)Blocks.SEAGRASS).isValidBonemealTarget(level, testPos, testState, BonemealSource.INTERACTION)
131
							&& random.nextInt(10) == 0) {
132
							((BonemealableBlock)Blocks.SEAGRASS).performBonemeal(serverLevel, random, testPos, testState, BonemealSource.INTERACTION);
133
						}
134
					}
135
				}
136
137
				itemStack.shrink(1);
138
				return true;
139
			}
140
		} else {
141
			return false;
142
		}
143
	}
144
145
	public static void addGrowthParticles(final LevelAccessor level, final BlockPos pos, final int count) {
146
		BlockState blockState = level.getBlockState(pos);
147
		if (blockState.getBlock() instanceof BonemealableBlock bonemealableBlock) {
148
			BlockPos particlePos = bonemealableBlock.getParticlePos(pos);
149
			switch (bonemealableBlock.getType()) {
150
				case NEIGHBOR_SPREADER:
151
					ParticleUtils.spawnParticles(level, particlePos, count * 3, 3.0, 1.0, false, ParticleTypes.HAPPY_VILLAGER);
152
					break;
153
				case GROWER:
154
					ParticleUtils.spawnParticleInBlock(level, particlePos, count, ParticleTypes.HAPPY_VILLAGER);
155
			}
156
		} else if (blockState.is(Blocks.WATER)) {
157
			ParticleUtils.spawnParticles(level, pos, count * 3, 3.0, 1.0, false, ParticleTypes.HAPPY_VILLAGER);
158
		}
159
	}
160
}
161

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