SpeleothemFeature .java

net.minecraft.world.level.levelgen.feature.SpeleothemFeature (record, 95 lines): record with 7 components. members: baseBlock, pointedBlock, place, codec.

net.minecraft.world.level.levelgen.featurerecord95 linescommon4.4 KB

Implements

API Surface (2)

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

Fields & Constants (1)

CODEC

String literals (7)

"base_block"
"pointed_block"
"replaceable_blocks"
"chance_of_taller_generation"
"chance_of_directional_spread"
"chance_of_spread_radius2"
"chance_of_spread_radius3"

Full Source

SpeleothemFeature.java95 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 net.minecraft.core.BlockPos;
8
import net.minecraft.core.Direction;
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.util.RandomSource;
13
import net.minecraft.world.level.LevelAccessor;
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
19
public record SpeleothemFeature(
20
	BlockState baseBlock,
21
	BlockState pointedBlock,
22
	HolderSet<Block> replaceableBlocks,
23
	float chanceOfTallerGeneration,
24
	float chanceOfDirectionalSpread,
25
	float chanceOfSpreadRadius2,
26
	float chanceOfSpreadRadius3
27
) implements Feature {
28
	public static final MapCodec<SpeleothemFeature> CODEC = RecordCodecBuilder.mapCodec(
29
		i -> i.group(
30
				BlockState.CODEC.fieldOf("base_block").forGetter(SpeleothemFeature::baseBlock),
31
				BlockState.CODEC.fieldOf("pointed_block").forGetter(SpeleothemFeature::pointedBlock),
32
				RegistryCodecs.holderSet(Registries.BLOCK).fieldOf("replaceable_blocks").forGetter(SpeleothemFeature::replaceableBlocks),
33
				Codec.floatRange(0.0F, 1.0F).optionalFieldOf("chance_of_taller_generation", 0.2F).forGetter(SpeleothemFeature::chanceOfTallerGeneration),
34
				Codec.floatRange(0.0F, 1.0F).optionalFieldOf("chance_of_directional_spread", 0.7F).forGetter(SpeleothemFeature::chanceOfDirectionalSpread),
35
				Codec.floatRange(0.0F, 1.0F).optionalFieldOf("chance_of_spread_radius2", 0.5F).forGetter(SpeleothemFeature::chanceOfSpreadRadius2),
36
				Codec.floatRange(0.0F, 1.0F).optionalFieldOf("chance_of_spread_radius3", 0.5F).forGetter(SpeleothemFeature::chanceOfSpreadRadius3)
37
			)
38
			.apply(i, SpeleothemFeature::new)
39
	);
40
41
	@Override
42
	public MapCodec<SpeleothemFeature> codec() {
43
		return CODEC;
44
	}
45
46
	@Override
47
	public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin) {
48
		Optional<Direction> tipDirection = this.getTipDirection(level, origin, random);
49
		if (tipDirection.isEmpty()) {
50
			return false;
51
		}
52
53
		BlockPos rootPos = origin.relative(tipDirection.get().getOpposite());
54
		this.createPatchOfBaseBlocks(level, random, rootPos);
55
		int height = random.nextFloat() < this.chanceOfTallerGeneration && SpeleothemUtils.isEmptyOrWater(level.getBlockState(origin.relative(tipDirection.get())))
56
			? 2
57
			: 1;
58
		SpeleothemUtils.growSpeleothem(
59
			level, origin, tipDirection.get(), height, false, this.baseBlock.getBlock(), this.pointedBlock.getBlock(), this.replaceableBlocks
60
		);
61
		return true;
62
	}
63
64
	private Optional<Direction> getTipDirection(final LevelAccessor level, final BlockPos pos, final RandomSource random) {
65
		boolean canPlaceAbove = SpeleothemUtils.isBase(level.getBlockState(pos.above()), this.baseBlock.getBlock(), this.replaceableBlocks);
66
		boolean canPlaceBelow = SpeleothemUtils.isBase(level.getBlockState(pos.below()), this.baseBlock.getBlock(), this.replaceableBlocks);
67
		if (canPlaceAbove && canPlaceBelow) {
68
			return Optional.of(random.nextBoolean() ? Direction.DOWN : Direction.UP);
69
		} else if (canPlaceAbove) {
70
			return Optional.of(Direction.DOWN);
71
		} else {
72
			return canPlaceBelow ? Optional.of(Direction.UP) : Optional.empty();
73
		}
74
	}
75
76
	private void createPatchOfBaseBlocks(final LevelAccessor level, final RandomSource random, final BlockPos pos) {
77
		SpeleothemUtils.placeBaseBlockIfPossible(level, pos, this.baseBlock.getBlock(), this.replaceableBlocks);
78
79
		for (Direction direction : Direction.Plane.HORIZONTAL) {
80
			if (!(random.nextFloat() > this.chanceOfDirectionalSpread)) {
81
				BlockPos pos1 = pos.relative(direction);
82
				SpeleothemUtils.placeBaseBlockIfPossible(level, pos1, this.baseBlock.getBlock(), this.replaceableBlocks);
83
				if (!(random.nextFloat() > this.chanceOfSpreadRadius2)) {
84
					BlockPos pos2 = pos1.relative(Direction.getRandom(random));
85
					SpeleothemUtils.placeBaseBlockIfPossible(level, pos2, this.baseBlock.getBlock(), this.replaceableBlocks);
86
					if (!(random.nextFloat() > this.chanceOfSpreadRadius3)) {
87
						BlockPos pos3 = pos2.relative(Direction.getRandom(random));
88
						SpeleothemUtils.placeBaseBlockIfPossible(level, pos3, this.baseBlock.getBlock(), this.replaceableBlocks);
89
					}
90
				}
91
			}
92
		}
93
	}
94
}
95

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