VegetationPatchFeature .java

net.minecraft.world.level.levelgen.feature.VegetationPatchFeature (class): members: extraEdgeColumnChance, belowPos, extraBottomBlockChance, verticalRange.

net.minecraft.world.level.levelgen.featureclass175 linescommon7.4 KB

Implements

API Surface (5)

<T extends VegetationPatchFeature> MapCodec<T> makeCodec(final Function10<HolderSet<Block>, Holder<BlockStateProvider>, Holder<PlacedFeature>, CaveSurface, IntProvider, Float, Integer, Float, IntProvider, Float, T> constructor)
MapCodec<? extends VegetationPatchFeature> codec()
boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin)
Set<BlockPos> placeGroundPatch(final WorldGenLevel level, final RandomSource random, final BlockPos origin, final Predicate<BlockState> replaceable, final int xRadius, final int zRadius)
boolean placeVegetation(final WorldGenLevel level, final ChunkGenerator generator, final RandomSource random, final BlockPos vegetationPos)

Fields & Constants (14)

CODEC
replaceable
groundState
vegetationFeature
surface
depth
extraBottomBlockChance
verticalRange
vegetationChance
xzRadius
extraEdgeColumnChance
Float
xRadius
belowPos

String literals (10)

"replaceable"
"ground_state"
"vegetation_feature"
"surface"
"depth"
"extra_bottom_block_chance"
"vertical_range"
"vegetation_chance"
"xz_radius"
"extra_edge_column_chance"

Full Source

VegetationPatchFeature.java175 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.levelgen.feature;
2
3
import com.mojang.datafixers.util.Function10;
4
import com.mojang.serialization.Codec;
5
import com.mojang.serialization.MapCodec;
6
import com.mojang.serialization.codecs.RecordCodecBuilder;
7
import java.util.HashSet;
8
import java.util.Set;
9
import java.util.function.Predicate;
10
import net.minecraft.core.BlockPos;
11
import net.minecraft.core.Direction;
12
import net.minecraft.core.Holder;
13
import net.minecraft.core.HolderSet;
14
import net.minecraft.core.registries.Registries;
15
import net.minecraft.core.registries.codec.RegistryCodecs;
16
import net.minecraft.util.RandomSource;
17
import net.minecraft.util.valueproviders.IntProvider;
18
import net.minecraft.util.valueproviders.IntProviders;
19
import net.minecraft.world.level.WorldGenLevel;
20
import net.minecraft.world.level.block.Block;
21
import net.minecraft.world.level.block.state.BlockBehaviour;
22
import net.minecraft.world.level.block.state.BlockState;
23
import net.minecraft.world.level.chunk.ChunkGenerator;
24
import net.minecraft.world.level.levelgen.feature.stateproviders.BlockStateProvider;
25
import net.minecraft.world.level.levelgen.placement.CaveSurface;
26
import net.minecraft.world.level.levelgen.placement.PlacedFeature;
27
28
public class VegetationPatchFeature implements Feature {
29
	public static final MapCodec<VegetationPatchFeature> CODEC = makeCodec(VegetationPatchFeature::new);
30
	protected final HolderSet<Block> replaceable;
31
	protected final Holder<BlockStateProvider> groundState;
32
	protected final Holder<PlacedFeature> vegetationFeature;
33
	protected final CaveSurface surface;
34
	protected final IntProvider depth;
35
	protected final float extraBottomBlockChance;
36
	protected final int verticalRange;
37
	protected final float vegetationChance;
38
	protected final IntProvider xzRadius;
39
	protected final float extraEdgeColumnChance;
40
41
	protected static <T extends VegetationPatchFeature> MapCodec<T> makeCodec(
42
		final Function10<HolderSet<Block>, Holder<BlockStateProvider>, Holder<PlacedFeature>, CaveSurface, IntProvider, Float, Integer, Float, IntProvider, Float, T> constructor
43
	) {
44
		return RecordCodecBuilder.mapCodec(
45
			i -> i.group(
46
					RegistryCodecs.holderSet(Registries.BLOCK).fieldOf("replaceable").forGetter(f -> f.replaceable),
47
					BlockStateProvider.CODEC.fieldOf("ground_state").forGetter(f -> f.groundState),
48
					PlacedFeature.CODEC.fieldOf("vegetation_feature").forGetter(f -> f.vegetationFeature),
49
					CaveSurface.CODEC.fieldOf("surface").forGetter(f -> f.surface),
50
					IntProviders.codec(1, 128).fieldOf("depth").forGetter(f -> f.depth),
51
					Codec.floatRange(0.0F, 1.0F).fieldOf("extra_bottom_block_chance").forGetter(f -> f.extraBottomBlockChance),
52
					Codec.intRange(1, 256).fieldOf("vertical_range").forGetter(f -> f.verticalRange),
53
					Codec.floatRange(0.0F, 1.0F).fieldOf("vegetation_chance").forGetter(f -> f.vegetationChance),
54
					IntProviders.CODEC.fieldOf("xz_radius").forGetter(f -> f.xzRadius),
55
					Codec.floatRange(0.0F, 1.0F).fieldOf("extra_edge_column_chance").forGetter(f -> f.extraEdgeColumnChance)
56
				)
57
				.apply(i, constructor)
58
		);
59
	}
60
61
	public VegetationPatchFeature(
62
		final HolderSet<Block> replaceable,
63
		final Holder<BlockStateProvider> groundState,
64
		final Holder<PlacedFeature> vegetationFeature,
65
		final CaveSurface surface,
66
		final IntProvider depth,
67
		final float extraBottomBlockChance,
68
		final int verticalRange,
69
		final float vegetationChance,
70
		final IntProvider xzRadius,
71
		final float extraEdgeColumnChance
72
	) {
73
		this.replaceable = replaceable;
74
		this.groundState = groundState;
75
		this.vegetationFeature = vegetationFeature;
76
		this.surface = surface;
77
		this.depth = depth;
78
		this.extraBottomBlockChance = extraBottomBlockChance;
79
		this.verticalRange = verticalRange;
80
		this.vegetationChance = vegetationChance;
81
		this.xzRadius = xzRadius;
82
		this.extraEdgeColumnChance = extraEdgeColumnChance;
83
	}
84
85
	@Override
86
	public MapCodec<? extends VegetationPatchFeature> codec() {
87
		return CODEC;
88
	}
89
90
	@Override
91
	public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin) {
92
		int xRadius = this.xzRadius.sample(random) + 1;
93
		int zRadius = this.xzRadius.sample(random) + 1;
94
		Set<BlockPos> surface = this.placeGroundPatch(level, random, origin, s -> s.is(this.replaceable), xRadius, zRadius);
95
		this.distributeVegetation(level, chunkGenerator, random, surface);
96
		return !surface.isEmpty();
97
	}
98
99
	protected Set<BlockPos> placeGroundPatch(
100
		final WorldGenLevel level, final RandomSource random, final BlockPos origin, final Predicate<BlockState> replaceable, final int xRadius, final int zRadius
101
	) {
102
		BlockPos.MutableBlockPos pos = origin.mutable();
103
		BlockPos.MutableBlockPos belowPos = pos.mutable();
104
		Direction inwards = this.surface.getDirection();
105
		Direction outwards = inwards.getOpposite();
106
		Set<BlockPos> surface = new HashSet<>();
107
108
		for (int dx = -xRadius; dx <= xRadius; dx++) {
109
			boolean isXEdge = dx == -xRadius || dx == xRadius;
110
111
			for (int dz = -zRadius; dz <= zRadius; dz++) {
112
				boolean isZEdge = dz == -zRadius || dz == zRadius;
113
				boolean isEdge = isXEdge || isZEdge;
114
				boolean isCorner = isXEdge && isZEdge;
115
				boolean isEdgeButNotCorner = isEdge && !isCorner;
116
				if (!isCorner && (!isEdgeButNotCorner || this.extraEdgeColumnChance != 0.0F && !(random.nextFloat() > this.extraEdgeColumnChance))) {
117
					pos.setWithOffset(origin, dx, 0, dz);
118
119
					for (int offset = 0; level.isStateAtPosition(pos, BlockBehaviour.BlockStateBase::isAir) && offset < this.verticalRange; offset++) {
120
						pos.move(inwards);
121
					}
122
123
					for (int var24 = 0; level.isStateAtPosition(pos, s -> !s.isAir()) && var24 < this.verticalRange; var24++) {
124
						pos.move(outwards);
125
					}
126
127
					belowPos.setWithOffset(pos, this.surface.getDirection());
128
					BlockState belowState = level.getBlockState(belowPos);
129
					if (level.isEmptyBlock(pos) && belowState.isFaceSturdy(level, belowPos, this.surface.getDirection().getOpposite())) {
130
						int depth = this.depth.sample(random) + (this.extraBottomBlockChance > 0.0F && random.nextFloat() < this.extraBottomBlockChance ? 1 : 0);
131
						BlockPos groundPos = belowPos.immutable();
132
						boolean groundPlaced = this.placeGround(level, replaceable, random, belowPos, depth);
133
						if (groundPlaced) {
134
							surface.add(groundPos);
135
						}
136
					}
137
				}
138
			}
139
		}
140
141
		return surface;
142
	}
143
144
	private void distributeVegetation(final WorldGenLevel level, final ChunkGenerator generator, final RandomSource random, final Set<BlockPos> surface) {
145
		for (BlockPos surfacePos : surface) {
146
			if (this.vegetationChance > 0.0F && random.nextFloat() < this.vegetationChance) {
147
				this.placeVegetation(level, generator, random, surfacePos);
148
			}
149
		}
150
	}
151
152
	protected boolean placeVegetation(final WorldGenLevel level, final ChunkGenerator generator, final RandomSource random, final BlockPos vegetationPos) {
153
		return this.vegetationFeature.value().place(level, generator, random, vegetationPos.relative(this.surface.getDirection().getOpposite()));
154
	}
155
156
	private boolean placeGround(
157
		final WorldGenLevel level, final Predicate<BlockState> replaceable, final RandomSource random, final BlockPos.MutableBlockPos belowPos, final int depth
158
	) {
159
		for (int i = 0; i < depth; i++) {
160
			BlockState stateToPlace = this.groundState.value().getState(level, random, belowPos);
161
			BlockState belowState = level.getBlockState(belowPos);
162
			if (!stateToPlace.is(belowState.getBlock())) {
163
				if (!replaceable.test(belowState)) {
164
					return i != 0;
165
				}
166
167
				level.setBlock(belowPos, stateToPlace, 2);
168
				belowPos.move(this.surface.getDirection());
169
			}
170
		}
171
172
		return true;
173
	}
174
}
175

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