RootSystemFeature .java

net.minecraft.world.level.levelgen.feature.RootSystemFeature (record): members: treeFeature, requiredVerticalSpaceForTree, levelTestDistance, rootRadius.

net.minecraft.world.level.levelgen.featurerecord186 linescommon7.7 KB

Implements

API Surface (2)

MapCodec<RootSystemFeature> codec()
boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin)

Fields & Constants (3)

CODEC
workingPos
originZ

String literals (15)

"feature"
"required_vertical_space_for_tree"
"level_test_distance"
"max_level_deviation"
"root_radius"
"root_replaceable"
"root_state_provider"
"root_placement_attempts"
"root_column_max_height"
"hanging_root_radius"
"hanging_roots_vertical_span"
"hanging_root_state_provider"
"hanging_root_placement_attempts"
"allowed_vertical_water_for_tree"
"allowed_tree_position"

Full Source

RootSystemFeature.java186 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.levelgen.feature;
2
3
import com.mojang.serialization.Codec;
4
import com.mojang.serialization.MapCodec;
5
import com.mojang.serialization.codecs.RecordCodecBuilder;
6
import net.minecraft.core.BlockPos;
7
import net.minecraft.core.Direction;
8
import net.minecraft.core.Holder;
9
import net.minecraft.core.HolderSet;
10
import net.minecraft.core.registries.Registries;
11
import net.minecraft.core.registries.codec.RegistryCodecs;
12
import net.minecraft.tags.FluidTags;
13
import net.minecraft.util.RandomSource;
14
import net.minecraft.world.level.WorldGenLevel;
15
import net.minecraft.world.level.block.Block;
16
import net.minecraft.world.level.block.state.BlockState;
17
import net.minecraft.world.level.chunk.ChunkGenerator;
18
import net.minecraft.world.level.levelgen.Heightmap;
19
import net.minecraft.world.level.levelgen.blockpredicates.BlockPredicate;
20
import net.minecraft.world.level.levelgen.feature.stateproviders.BlockStateProvider;
21
import net.minecraft.world.level.levelgen.placement.PlacedFeature;
22
23
public record RootSystemFeature(
24
	Holder<PlacedFeature> treeFeature,
25
	int requiredVerticalSpaceForTree,
26
	int levelTestDistance,
27
	int maxLevelDeviation,
28
	int rootRadius,
29
	HolderSet<Block> rootReplaceable,
30
	Holder<BlockStateProvider> rootStateProvider,
31
	int rootPlacementAttempts,
32
	int rootColumnMaxHeight,
33
	int hangingRootRadius,
34
	int hangingRootsVerticalSpan,
35
	Holder<BlockStateProvider> hangingRootStateProvider,
36
	int hangingRootPlacementAttempts,
37
	int allowedVerticalWaterForTree,
38
	BlockPredicate allowedTreePosition
39
) implements Feature {
40
	public static final MapCodec<RootSystemFeature> CODEC = RecordCodecBuilder.mapCodec(
41
		i -> i.group(
42
				PlacedFeature.CODEC.fieldOf("feature").forGetter(RootSystemFeature::treeFeature),
43
				Codec.intRange(1, 64).fieldOf("required_vertical_space_for_tree").forGetter(RootSystemFeature::requiredVerticalSpaceForTree),
44
				Codec.intRange(0, 16).fieldOf("level_test_distance").forGetter(RootSystemFeature::levelTestDistance),
45
				Codec.intRange(0, 64).fieldOf("max_level_deviation").forGetter(RootSystemFeature::maxLevelDeviation),
46
				Codec.intRange(1, 64).fieldOf("root_radius").forGetter(RootSystemFeature::rootRadius),
47
				RegistryCodecs.holderSet(Registries.BLOCK).fieldOf("root_replaceable").forGetter(RootSystemFeature::rootReplaceable),
48
				BlockStateProvider.CODEC.fieldOf("root_state_provider").forGetter(RootSystemFeature::rootStateProvider),
49
				Codec.intRange(1, 256).fieldOf("root_placement_attempts").forGetter(RootSystemFeature::rootPlacementAttempts),
50
				Codec.intRange(1, 4096).fieldOf("root_column_max_height").forGetter(RootSystemFeature::rootColumnMaxHeight),
51
				Codec.intRange(1, 64).fieldOf("hanging_root_radius").forGetter(RootSystemFeature::hangingRootRadius),
52
				Codec.intRange(1, 16).fieldOf("hanging_roots_vertical_span").forGetter(RootSystemFeature::hangingRootsVerticalSpan),
53
				BlockStateProvider.CODEC.fieldOf("hanging_root_state_provider").forGetter(RootSystemFeature::hangingRootStateProvider),
54
				Codec.intRange(1, 256).fieldOf("hanging_root_placement_attempts").forGetter(RootSystemFeature::hangingRootPlacementAttempts),
55
				Codec.intRange(1, 64).fieldOf("allowed_vertical_water_for_tree").forGetter(RootSystemFeature::allowedVerticalWaterForTree),
56
				BlockPredicate.CODEC.fieldOf("allowed_tree_position").forGetter(RootSystemFeature::allowedTreePosition)
57
			)
58
			.apply(i, RootSystemFeature::new)
59
	);
60
61
	@Override
62
	public MapCodec<RootSystemFeature> codec() {
63
		return CODEC;
64
	}
65
66
	@Override
67
	public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin) {
68
		if (!level.getBlockState(origin).isAir()) {
69
			return false;
70
		}
71
72
		BlockPos.MutableBlockPos workingPos = origin.mutable();
73
		if (this.placeDirtAndTree(level, chunkGenerator, random, workingPos, origin)) {
74
			this.placeRoots(level, random, origin, workingPos);
75
		}
76
77
		return true;
78
	}
79
80
	private boolean spaceForTree(final WorldGenLevel level, final BlockPos pos) {
81
		BlockPos.MutableBlockPos columnUpPos = pos.mutable();
82
83
		for (int i = 1; i <= this.requiredVerticalSpaceForTree; i++) {
84
			columnUpPos.move(Direction.UP);
85
			BlockState state = level.getBlockState(columnUpPos);
86
			if (!isAllowedTreeSpace(state, i, this.allowedVerticalWaterForTree)) {
87
				return false;
88
			}
89
		}
90
91
		if (this.levelTestDistance > 0) {
92
			BlockPos.MutableBlockPos cornerPos = pos.mutable();
93
94
			for (int i = 0; i < 4; i++) {
95
				cornerPos.move(Direction.from2DDataValue(i), this.levelTestDistance);
96
				BlockState below = level.getBlockState(cornerPos.below(this.maxLevelDeviation));
97
				BlockState above = level.getBlockState(cornerPos.above(this.maxLevelDeviation));
98
				if (below.isAir() || !above.isAir()) {
99
					return false;
100
				}
101
102
				cornerPos.set(pos);
103
			}
104
		}
105
106
		return true;
107
	}
108
109
	private static boolean isAllowedTreeSpace(final BlockState state, final int blocksAboveOrigin, final int allowedVerticalWaterHeight) {
110
		if (state.isAir()) {
111
			return true;
112
		}
113
114
		int blocksAboveGround = blocksAboveOrigin + 1;
115
		return blocksAboveGround <= allowedVerticalWaterHeight && state.getFluidState().is(FluidTags.WATER);
116
	}
117
118
	private boolean placeDirtAndTree(
119
		final WorldGenLevel level, final ChunkGenerator generator, final RandomSource random, final BlockPos.MutableBlockPos workingPos, final BlockPos pos
120
	) {
121
		for (int y = 0; y < this.rootColumnMaxHeight; y++) {
122
			workingPos.move(Direction.UP);
123
			if (level.getHeight(Heightmap.Types.WORLD_SURFACE, workingPos) < workingPos.getY()) {
124
				return false;
125
			}
126
127
			if (this.allowedTreePosition.test(level, workingPos) && this.spaceForTree(level, workingPos)) {
128
				BlockPos belowPos = workingPos.below();
129
				if (level.getFluidState(belowPos).is(FluidTags.LAVA) || !level.getBlockState(belowPos).isSolid()) {
130
					return false;
131
				}
132
133
				if (this.treeFeature.value().place(level, generator, random, workingPos)) {
134
					this.placeDirt(pos, pos.getY() + y, level, random);
135
					return true;
136
				}
137
			}
138
		}
139
140
		return false;
141
	}
142
143
	private void placeDirt(final BlockPos origin, final int targetHeight, final WorldGenLevel level, final RandomSource random) {
144
		int originX = origin.getX();
145
		int originZ = origin.getZ();
146
		BlockPos.MutableBlockPos workingPos = origin.mutable();
147
148
		for (int y = origin.getY(); y < targetHeight; y++) {
149
			this.placeRootedDirt(level, random, originX, originZ, workingPos.set(originX, y, originZ));
150
		}
151
	}
152
153
	private void placeRootedDirt(
154
		final WorldGenLevel level, final RandomSource random, final int originX, final int originZ, final BlockPos.MutableBlockPos workingPos
155
	) {
156
		for (int i = 0; i < this.rootPlacementAttempts; i++) {
157
			workingPos.setWithOffset(
158
				workingPos, random.nextInt(this.rootRadius) - random.nextInt(this.rootRadius), 0, random.nextInt(this.rootRadius) - random.nextInt(this.rootRadius)
159
			);
160
			if (level.getBlockState(workingPos).is(this.rootReplaceable)) {
161
				level.setBlock(workingPos, this.rootStateProvider.value().getState(level, random, workingPos), 2);
162
			}
163
164
			workingPos.setX(originX);
165
			workingPos.setZ(originZ);
166
		}
167
	}
168
169
	private void placeRoots(final WorldGenLevel level, final RandomSource random, final BlockPos pos, final BlockPos.MutableBlockPos workingPos) {
170
		for (int i = 0; i < this.hangingRootPlacementAttempts; i++) {
171
			workingPos.setWithOffset(
172
				pos,
173
				random.nextInt(this.hangingRootRadius) - random.nextInt(this.hangingRootRadius),
174
				random.nextInt(this.hangingRootsVerticalSpan) - random.nextInt(this.hangingRootsVerticalSpan),
175
				random.nextInt(this.hangingRootRadius) - random.nextInt(this.hangingRootRadius)
176
			);
177
			if (level.isEmptyBlock(workingPos)) {
178
				BlockState targetState = this.hangingRootStateProvider.value().getState(level, random, workingPos);
179
				if (targetState.canSurvive(level, workingPos) && level.getBlockState(workingPos.above()).isFaceSturdy(level, workingPos, Direction.DOWN)) {
180
					level.setBlock(workingPos, targetState, 2);
181
				}
182
			}
183
		}
184
	}
185
}
186

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