SpeleothemClusterFeature .java

net.minecraft.world.level.levelgen.feature.SpeleothemClusterFeature (record): members: baseBlock, pointedBlock, replaceableBlocks, height, radius, density.

net.minecraft.world.level.levelgen.featurerecord274 linescommon11.3 KB

Implements

API Surface (2)

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

Fields & Constants (9)

CODEC
level
random
pos
dx
dz
chanceOfWater
chanceOfStalagmiteOrStalactite
clusterHeight

String literals (13)

"base_block"
"pointed_block"
"replaceable_blocks"
"floor_to_ceiling_search_range"
"height"
"radius"
"max_stalagmite_stalactite_height_diff"
"height_deviation"
"speleothem_block_layer_thickness"
"density"
"wetness"
"chance_of_speleothem_at_max_distance_from_center"
"max_distance_from_center_affecting_height_bias"

Full Source

SpeleothemClusterFeature.java274 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 java.util.Optional;
7
import java.util.OptionalInt;
8
import net.minecraft.core.BlockPos;
9
import net.minecraft.core.Direction;
10
import net.minecraft.core.HolderSet;
11
import net.minecraft.core.registries.Registries;
12
import net.minecraft.core.registries.codec.RegistryCodecs;
13
import net.minecraft.tags.BlockTags;
14
import net.minecraft.tags.FluidTags;
15
import net.minecraft.util.Mth;
16
import net.minecraft.util.RandomSource;
17
import net.minecraft.util.valueproviders.ClampedNormalFloat;
18
import net.minecraft.util.valueproviders.FloatProvider;
19
import net.minecraft.util.valueproviders.FloatProviders;
20
import net.minecraft.util.valueproviders.IntProvider;
21
import net.minecraft.util.valueproviders.IntProviders;
22
import net.minecraft.world.level.LevelAccessor;
23
import net.minecraft.world.level.LevelReader;
24
import net.minecraft.world.level.WorldGenLevel;
25
import net.minecraft.world.level.block.Block;
26
import net.minecraft.world.level.block.Blocks;
27
import net.minecraft.world.level.block.state.BlockState;
28
import net.minecraft.world.level.chunk.ChunkGenerator;
29
import net.minecraft.world.level.levelgen.Column;
30
31
public record SpeleothemClusterFeature(
32
	BlockState baseBlock,
33
	BlockState pointedBlock,
34
	HolderSet<Block> replaceableBlocks,
35
	int floorToCeilingSearchRange,
36
	IntProvider height,
37
	IntProvider radius,
38
	int maxStalagmiteStalactiteHeightDiff,
39
	int heightDeviation,
40
	IntProvider speleothemBlockLayerThickness,
41
	FloatProvider density,
42
	FloatProvider wetness,
43
	float chanceOfSpeleothemAtMaxDistanceFromCenter,
44
	int maxDistanceFromEdgeAffectingChanceOfSpeleothem,
45
	int maxDistanceFromCenterAffectingHeightBias
46
) implements Feature {
47
	public static final MapCodec<SpeleothemClusterFeature> CODEC = RecordCodecBuilder.mapCodec(
48
		i -> i.group(
49
				BlockState.CODEC.fieldOf("base_block").forGetter(SpeleothemClusterFeature::baseBlock),
50
				BlockState.CODEC.fieldOf("pointed_block").forGetter(SpeleothemClusterFeature::pointedBlock),
51
				RegistryCodecs.holderSet(Registries.BLOCK).fieldOf("replaceable_blocks").forGetter(SpeleothemClusterFeature::replaceableBlocks),
52
				Codec.intRange(1, 512).fieldOf("floor_to_ceiling_search_range").forGetter(SpeleothemClusterFeature::floorToCeilingSearchRange),
53
				IntProviders.codec(1, 128).fieldOf("height").forGetter(SpeleothemClusterFeature::height),
54
				IntProviders.codec(1, 128).fieldOf("radius").forGetter(SpeleothemClusterFeature::radius),
55
				Codec.intRange(0, 64).fieldOf("max_stalagmite_stalactite_height_diff").forGetter(SpeleothemClusterFeature::maxStalagmiteStalactiteHeightDiff),
56
				Codec.intRange(1, 64).fieldOf("height_deviation").forGetter(SpeleothemClusterFeature::heightDeviation),
57
				IntProviders.codec(0, 128).fieldOf("speleothem_block_layer_thickness").forGetter(SpeleothemClusterFeature::speleothemBlockLayerThickness),
58
				FloatProviders.codec(0.0F, 2.0F).fieldOf("density").forGetter(SpeleothemClusterFeature::density),
59
				FloatProviders.codec(0.0F, 2.0F).fieldOf("wetness").forGetter(SpeleothemClusterFeature::wetness),
60
				Codec.floatRange(0.0F, 1.0F)
61
					.fieldOf("chance_of_speleothem_at_max_distance_from_center")
62
					.forGetter(SpeleothemClusterFeature::chanceOfSpeleothemAtMaxDistanceFromCenter),
63
				Codec.intRange(1, 64)
64
					.fieldOf("max_distance_from_edge_affecting_chance_of_speleothem")
65
					.forGetter(SpeleothemClusterFeature::maxDistanceFromEdgeAffectingChanceOfSpeleothem),
66
				Codec.intRange(1, 64)
67
					.fieldOf("max_distance_from_center_affecting_height_bias")
68
					.forGetter(SpeleothemClusterFeature::maxDistanceFromCenterAffectingHeightBias)
69
			)
70
			.apply(i, SpeleothemClusterFeature::new)
71
	);
72
73
	@Override
74
	public MapCodec<SpeleothemClusterFeature> codec() {
75
		return CODEC;
76
	}
77
78
	@Override
79
	public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin) {
80
		if (!SpeleothemUtils.isEmptyOrWater(level, origin)) {
81
			return false;
82
		}
83
84
		int height = this.height.sample(random);
85
		float wetness = this.wetness.sample(random);
86
		float density = this.density.sample(random);
87
		int xRadius = this.radius.sample(random);
88
		int zRadius = this.radius.sample(random);
89
90
		for (int dx = -xRadius; dx <= xRadius; dx++) {
91
			for (int dz = -zRadius; dz <= zRadius; dz++) {
92
				double chanceOfStalagmiteOrStalactite = this.getChanceOfStalagmiteOrStalactite(xRadius, zRadius, dx, dz);
93
				BlockPos pos = origin.offset(dx, 0, dz);
94
				this.placeColumn(level, random, pos, dx, dz, wetness, chanceOfStalagmiteOrStalactite, height, density);
95
			}
96
		}
97
98
		return true;
99
	}
100
101
	private void placeColumn(
102
		final WorldGenLevel level,
103
		final RandomSource random,
104
		final BlockPos pos,
105
		final int dx,
106
		final int dz,
107
		final float chanceOfWater,
108
		final double chanceOfStalagmiteOrStalactite,
109
		final int clusterHeight,
110
		final float density
111
	) {
112
		Optional<Column> baseColumn = Column.scan(
113
			level, pos, this.floorToCeilingSearchRange, SpeleothemUtils::isEmptyOrWater, SpeleothemUtils::isNeitherEmptyNorWater
114
		);
115
		if (!baseColumn.isEmpty()) {
116
			OptionalInt ceiling = baseColumn.get().getCeiling();
117
			OptionalInt baseFloor = baseColumn.get().getFloor();
118
			if (!ceiling.isEmpty() || !baseFloor.isEmpty()) {
119
				boolean wantPool = random.nextFloat() < chanceOfWater;
120
				Column column;
121
				if (wantPool && baseFloor.isPresent() && this.canPlacePool(level, pos.atY(baseFloor.getAsInt()))) {
122
					int baseFloorY = baseFloor.getAsInt();
123
					column = baseColumn.get().withFloor(OptionalInt.of(baseFloorY - 1));
124
					level.setBlock(pos.atY(baseFloorY), Blocks.WATER.defaultBlockState(), 2);
125
				} else {
126
					column = baseColumn.get();
127
				}
128
129
				OptionalInt floor = column.getFloor();
130
				boolean wantStalactite = random.nextDouble() < chanceOfStalagmiteOrStalactite;
131
				int stalactiteHeight;
132
				if (ceiling.isPresent() && wantStalactite && !this.isLava(level, pos.atY(ceiling.getAsInt()))) {
133
					int ceilingThickness = this.speleothemBlockLayerThickness.sample(random);
134
					this.replaceBlocksWithBaseBlocks(level, pos.atY(ceiling.getAsInt()), ceilingThickness, Direction.UP);
135
					int maxHeightForThisColumn;
136
					if (floor.isPresent()) {
137
						maxHeightForThisColumn = Math.min(clusterHeight, ceiling.getAsInt() - floor.getAsInt());
138
					} else {
139
						maxHeightForThisColumn = clusterHeight;
140
					}
141
142
					stalactiteHeight = this.getSpeleothemHeight(random, dx, dz, density, maxHeightForThisColumn);
143
				} else {
144
					stalactiteHeight = 0;
145
				}
146
147
				boolean wantStalagmite = random.nextDouble() < chanceOfStalagmiteOrStalactite;
148
				int stalagmiteHeight;
149
				if (floor.isPresent() && wantStalagmite && !this.isLava(level, pos.atY(floor.getAsInt()))) {
150
					int floorThickness = this.speleothemBlockLayerThickness.sample(random);
151
					this.replaceBlocksWithBaseBlocks(level, pos.atY(floor.getAsInt()), floorThickness, Direction.DOWN);
152
					if (ceiling.isPresent()) {
153
						stalagmiteHeight = Math.max(
154
							0, stalactiteHeight + Mth.randomBetweenInclusive(random, -this.maxStalagmiteStalactiteHeightDiff, this.maxStalagmiteStalactiteHeightDiff)
155
						);
156
					} else {
157
						stalagmiteHeight = this.getSpeleothemHeight(random, dx, dz, density, clusterHeight);
158
					}
159
				} else {
160
					stalagmiteHeight = 0;
161
				}
162
163
				int actualStalagmiteHeight;
164
				int actualStalactiteHeight;
165
				if (ceiling.isPresent() && floor.isPresent() && ceiling.getAsInt() - stalactiteHeight <= floor.getAsInt() + stalagmiteHeight) {
166
					int floorY = floor.getAsInt();
167
					int ceilingY = ceiling.getAsInt();
168
					int lowestStalactiteBottom = Math.max(ceilingY - stalactiteHeight, floorY + 1);
169
					int highestStalagmiteTop = Math.min(floorY + stalagmiteHeight, ceilingY - 1);
170
					int actualStalactiteBottom = Mth.randomBetweenInclusive(random, lowestStalactiteBottom, highestStalagmiteTop + 1);
171
					int actualStalagmiteTop = actualStalactiteBottom - 1;
172
					actualStalactiteHeight = ceilingY - actualStalactiteBottom;
173
					actualStalagmiteHeight = actualStalagmiteTop - floorY;
174
				} else {
175
					actualStalactiteHeight = stalactiteHeight;
176
					actualStalagmiteHeight = stalagmiteHeight;
177
				}
178
179
				boolean mergeTips = random.nextBoolean()
180
					&& actualStalactiteHeight > 0
181
					&& actualStalagmiteHeight > 0
182
					&& column.getHeight().isPresent()
183
					&& actualStalactiteHeight + actualStalagmiteHeight == column.getHeight().getAsInt();
184
				if (ceiling.isPresent()) {
185
					SpeleothemUtils.growSpeleothem(
186
						level,
187
						pos.atY(ceiling.getAsInt() - 1),
188
						Direction.DOWN,
189
						actualStalactiteHeight,
190
						mergeTips,
191
						this.baseBlock.getBlock(),
192
						this.pointedBlock.getBlock(),
193
						this.replaceableBlocks
194
					);
195
				}
196
197
				if (floor.isPresent()) {
198
					SpeleothemUtils.growSpeleothem(
199
						level,
200
						pos.atY(floor.getAsInt() + 1),
201
						Direction.UP,
202
						actualStalagmiteHeight,
203
						mergeTips,
204
						this.baseBlock.getBlock(),
205
						this.pointedBlock.getBlock(),
206
						this.replaceableBlocks
207
					);
208
				}
209
			}
210
		}
211
	}
212
213
	private boolean isLava(final LevelReader level, final BlockPos pos) {
214
		return level.getBlockState(pos).is(Blocks.LAVA);
215
	}
216
217
	private int getSpeleothemHeight(final RandomSource random, final int dx, final int dz, final float density, final int maxHeight) {
218
		if (random.nextFloat() > density) {
219
			return 0;
220
		}
221
222
		int distanceFromCenter = Math.abs(dx) + Math.abs(dz);
223
		float heightMean = (float)Mth.clampedMap(distanceFromCenter, 0.0, this.maxDistanceFromCenterAffectingHeightBias, maxHeight / 2.0, 0.0);
224
		return (int)randomBetweenBiased(random, 0.0F, maxHeight, heightMean, this.heightDeviation);
225
	}
226
227
	private boolean canPlacePool(final WorldGenLevel level, final BlockPos pos) {
228
		BlockState state = level.getBlockState(pos);
229
		if (!state.is(Blocks.WATER) && !state.is(this.baseBlock.getBlock()) && !state.is(this.pointedBlock.getBlock())) {
230
			if (level.getBlockState(pos.above()).getFluidState().is(FluidTags.WATER)) {
231
				return false;
232
			}
233
234
			for (Direction direction : Direction.Plane.HORIZONTAL) {
235
				if (!this.canBeAdjacentToWater(level, pos.relative(direction))) {
236
					return false;
237
				}
238
			}
239
240
			return this.canBeAdjacentToWater(level, pos.below());
241
		} else {
242
			return false;
243
		}
244
	}
245
246
	private boolean canBeAdjacentToWater(final LevelAccessor level, final BlockPos pos) {
247
		BlockState state = level.getBlockState(pos);
248
		return state.is(BlockTags.BASE_STONE_OVERWORLD) || state.getFluidState().is(FluidTags.WATER);
249
	}
250
251
	private void replaceBlocksWithBaseBlocks(final WorldGenLevel level, final BlockPos firstPos, final int maxCount, final Direction direction) {
252
		BlockPos.MutableBlockPos pos = firstPos.mutable();
253
254
		for (int i = 0; i < maxCount; i++) {
255
			if (!SpeleothemUtils.placeBaseBlockIfPossible(level, pos, this.baseBlock.getBlock(), this.replaceableBlocks)) {
256
				return;
257
			}
258
259
			pos.move(direction);
260
		}
261
	}
262
263
	private double getChanceOfStalagmiteOrStalactite(final int xRadius, final int zRadius, final int dx, final int dz) {
264
		int xDistanceFromEdge = xRadius - Math.abs(dx);
265
		int zDistanceFromEdge = zRadius - Math.abs(dz);
266
		int distanceFromEdge = Math.min(xDistanceFromEdge, zDistanceFromEdge);
267
		return Mth.clampedMap(distanceFromEdge, 0.0F, this.maxDistanceFromEdgeAffectingChanceOfSpeleothem, this.chanceOfSpeleothemAtMaxDistanceFromCenter, 1.0F);
268
	}
269
270
	private static float randomBetweenBiased(final RandomSource random, final float min, final float maxExclusive, final float mean, final float deviation) {
271
		return ClampedNormalFloat.sample(random, mean, deviation, min, maxExclusive);
272
	}
273
}
274

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