SpikeFeature .java

net.minecraft.world.level.levelgen.feature.SpikeFeature (record, 108 lines): members: canPlaceOn, canReplace, place, state, codec, CODEC. strings: "state".

net.minecraft.world.level.levelgen.featurerecord108 linescommon3.5 KB

Implements

API Surface (2)

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

Fields & Constants (1)

CODEC

String literals (3)

"state"
"can_place_on"
"can_replace"

Full Source

SpikeFeature.java108 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 net.minecraft.core.BlockPos;
6
import net.minecraft.util.Mth;
7
import net.minecraft.util.RandomSource;
8
import net.minecraft.world.level.WorldGenLevel;
9
import net.minecraft.world.level.block.state.BlockState;
10
import net.minecraft.world.level.chunk.ChunkGenerator;
11
import net.minecraft.world.level.levelgen.blockpredicates.BlockPredicate;
12
13
public record SpikeFeature(BlockState state, BlockPredicate canPlaceOn, BlockPredicate canReplace) implements Feature {
14
	public static final MapCodec<SpikeFeature> CODEC = RecordCodecBuilder.mapCodec(
15
		i -> i.group(
16
				BlockState.CODEC.fieldOf("state").forGetter(SpikeFeature::state),
17
				BlockPredicate.CODEC.fieldOf("can_place_on").forGetter(SpikeFeature::canPlaceOn),
18
				BlockPredicate.CODEC.fieldOf("can_replace").forGetter(SpikeFeature::canReplace)
19
			)
20
			.apply(i, SpikeFeature::new)
21
	);
22
23
	@Override
24
	public MapCodec<SpikeFeature> codec() {
25
		return CODEC;
26
	}
27
28
	@Override
29
	public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, BlockPos origin) {
30
		while (level.isEmptyBlock(origin) && origin.getY() > level.getMinY() + 2) {
31
			origin = origin.below();
32
		}
33
34
		if (!this.canPlaceOn.test(level, origin)) {
35
			return false;
36
		}
37
38
		origin = origin.above(random.nextInt(4));
39
		int height = random.nextInt(4) + 7;
40
		int width = height / 4 + random.nextInt(2);
41
		if (width > 1 && random.nextInt(60) == 0) {
42
			origin = origin.above(10 + random.nextInt(30));
43
		}
44
45
		for (int yOff = 0; yOff < height; yOff++) {
46
			float scale = (1.0F - (float)yOff / height) * width;
47
			int newWidth = Mth.ceil(scale);
48
49
			for (int xo = -newWidth; xo <= newWidth; xo++) {
50
				float dx = Mth.abs(xo) - 0.25F;
51
52
				for (int zo = -newWidth; zo <= newWidth; zo++) {
53
					float dz = Mth.abs(zo) - 0.25F;
54
					if ((xo == 0 && zo == 0 || !(dx * dx + dz * dz > scale * scale))
55
						&& (xo != -newWidth && xo != newWidth && zo != -newWidth && zo != newWidth || !(random.nextFloat() > 0.75F))) {
56
						BlockPos positiveOffset = origin.offset(xo, yOff, zo);
57
						BlockState state = level.getBlockState(positiveOffset);
58
						if (state.isAir() || this.canReplace.test(level, positiveOffset)) {
59
							this.setBlock(level, positiveOffset, this.state);
60
						}
61
62
						if (yOff != 0 && newWidth > 1) {
63
							BlockPos negativeOffset = origin.offset(xo, -yOff, zo);
64
							state = level.getBlockState(negativeOffset);
65
							if (state.isAir() || this.canReplace.test(level, negativeOffset)) {
66
								this.setBlock(level, negativeOffset, this.state);
67
							}
68
						}
69
					}
70
				}
71
			}
72
		}
73
74
		int pillarWidth = width - 1;
75
		if (pillarWidth < 0) {
76
			pillarWidth = 0;
77
		} else if (pillarWidth > 1) {
78
			pillarWidth = 1;
79
		}
80
81
		for (int xo = -pillarWidth; xo <= pillarWidth; xo++) {
82
			for (int zo = -pillarWidth; zo <= pillarWidth; zo++) {
83
				BlockPos cursor = origin.offset(xo, -1, zo);
84
				int runLength = 50;
85
				if (Math.abs(xo) == 1 && Math.abs(zo) == 1) {
86
					runLength = random.nextInt(5);
87
				}
88
89
				while (cursor.getY() > 50) {
90
					BlockState state = level.getBlockState(cursor);
91
					if (!state.isAir() && !this.canReplace.test(level, cursor) && state != this.state) {
92
						break;
93
					}
94
95
					this.setBlock(level, cursor, this.state);
96
					cursor = cursor.below();
97
					if (--runLength <= 0) {
98
						cursor = cursor.below(random.nextInt(5) + 1);
99
						runLength = random.nextInt(5);
100
					}
101
				}
102
			}
103
		}
104
105
		return true;
106
	}
107
}
108

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