CoralClawFeature .java

net.minecraft.world.level.levelgen.feature.CoralClawFeature (record): members: feature, place, codec, CODEC. strings: "feature". imports: PlacedFeature.

net.minecraft.world.level.levelgen.featurerecord74 linescommon2.6 KB

Implements

API Surface (2)

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

Fields & Constants (1)

CODEC

String literals (1)

"feature"

Full Source

CoralClawFeature.java74 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.levelgen.feature;
2
3
import com.mojang.serialization.MapCodec;
4
import com.mojang.serialization.codecs.RecordCodecBuilder;
5
import java.util.List;
6
import java.util.stream.Stream;
7
import net.minecraft.core.BlockPos;
8
import net.minecraft.core.Direction;
9
import net.minecraft.core.Holder;
10
import net.minecraft.util.RandomSource;
11
import net.minecraft.util.Util;
12
import net.minecraft.world.level.WorldGenLevel;
13
import net.minecraft.world.level.chunk.ChunkGenerator;
14
import net.minecraft.world.level.levelgen.placement.PlacedFeature;
15
16
public record CoralClawFeature(Holder<PlacedFeature> feature) implements Feature {
17
	public static final MapCodec<CoralClawFeature> CODEC = RecordCodecBuilder.mapCodec(
18
		i -> i.group(PlacedFeature.CODEC.fieldOf("feature").forGetter(CoralClawFeature::feature)).apply(i, CoralClawFeature::new)
19
	);
20
21
	@Override
22
	public MapCodec<CoralClawFeature> codec() {
23
		return CODEC;
24
	}
25
26
	@Override
27
	public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin) {
28
		if (!this.feature.value().place(level, chunkGenerator, random, origin)) {
29
			return false;
30
		}
31
32
		Direction clawDirection = Direction.Plane.HORIZONTAL.getRandomDirection(random);
33
		int nBranches = random.nextInt(2) + 2;
34
		List<Direction> possibleDirections = Util.toShuffledList(Stream.of(clawDirection, clawDirection.getClockWise(), clawDirection.getCounterClockWise()), random);
35
36
		for (Direction branchDirection : possibleDirections.subList(0, nBranches)) {
37
			BlockPos.MutableBlockPos mutPos = origin.mutable();
38
			int sidewayLength = random.nextInt(2) + 1;
39
			mutPos.move(branchDirection);
40
			int inwayLenth;
41
			Direction segmentDirection;
42
			if (branchDirection == clawDirection) {
43
				segmentDirection = clawDirection;
44
				inwayLenth = random.nextInt(3) + 2;
45
			} else {
46
				mutPos.move(Direction.UP);
47
				Direction[] segmentPossibleDirections = new Direction[]{branchDirection, Direction.UP};
48
				segmentDirection = Util.getRandom(segmentPossibleDirections, random);
49
				inwayLenth = random.nextInt(3) + 3;
50
			}
51
52
			for (int i = 0; i < sidewayLength && this.feature.value().place(level, chunkGenerator, random, mutPos); i++) {
53
				mutPos.move(segmentDirection);
54
			}
55
56
			mutPos.move(segmentDirection.getOpposite());
57
			mutPos.move(Direction.UP);
58
59
			for (int i = 0; i < inwayLenth; i++) {
60
				mutPos.move(clawDirection);
61
				if (!this.feature.value().place(level, chunkGenerator, random, mutPos)) {
62
					break;
63
				}
64
65
				if (random.nextFloat() < 0.25F) {
66
					mutPos.move(Direction.UP);
67
				}
68
			}
69
		}
70
71
		return true;
72
	}
73
}
74

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