OreFeature .java

net.minecraft.world.level.levelgen.feature.OreFeature (class): members: sizeXZ, doPlace, xStart, zStart, z1, z0, yStart, x1, y1, x0, y0, place, random.

net.minecraft.world.level.levelgen.featureclass178 linescommonextends AbstractOreFeature6.2 KB

API Surface (3)

MapCodec<OreFeature> codec()
boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin)
boolean doPlace(final WorldGenLevel level, final RandomSource random, final double x0, final double x1, final double z0, final double z1, final double y0, final double y1, final int xStart, final int yStart, final int zStart, final int sizeXZ, final int sizeY)

Fields & Constants (13)

CODEC
level
random
x0
x1
z0
z1
y0
y1
xStart
yStart
zStart
sizeXZ

Full Source

OreFeature.java178 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.levelgen.feature;
2
3
import com.mojang.serialization.MapCodec;
4
import java.util.BitSet;
5
import java.util.List;
6
import net.minecraft.core.BlockPos;
7
import net.minecraft.core.SectionPos;
8
import net.minecraft.util.Mth;
9
import net.minecraft.util.RandomSource;
10
import net.minecraft.world.level.WorldGenLevel;
11
import net.minecraft.world.level.block.state.BlockState;
12
import net.minecraft.world.level.chunk.BulkSectionAccess;
13
import net.minecraft.world.level.chunk.ChunkGenerator;
14
import net.minecraft.world.level.chunk.LevelChunkSection;
15
import net.minecraft.world.level.levelgen.Heightmap;
16
import net.minecraft.world.level.levelgen.structure.templatesystem.RuleTest;
17
18
public class OreFeature extends AbstractOreFeature {
19
	public static final MapCodec<OreFeature> CODEC = makeCodec(OreFeature::new);
20
21
	public OreFeature(final List<BlockReplacement> targetStates, final int size, final float discardChanceOnAirExposure) {
22
		super(targetStates, size, discardChanceOnAirExposure);
23
	}
24
25
	public OreFeature(final List<BlockReplacement> targetStates, final int size) {
26
		this(targetStates, size, 0.0F);
27
	}
28
29
	public OreFeature(final RuleTest target, final BlockState state, final int size) {
30
		this(List.of(new BlockReplacement(target, state)), size, 0.0F);
31
	}
32
33
	@Override
34
	public MapCodec<OreFeature> codec() {
35
		return CODEC;
36
	}
37
38
	@Override
39
	public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin) {
40
		float dir = random.nextFloat() * (float) Math.PI;
41
		float spreadXY = this.size / 8.0F;
42
		int maxRadius = Mth.ceil((this.size / 16.0F * 2.0F + 1.0F) / 2.0F);
43
		double x0 = origin.getX() + Math.sin(dir) * spreadXY;
44
		double x1 = origin.getX() - Math.sin(dir) * spreadXY;
45
		double z0 = origin.getZ() + Math.cos(dir) * spreadXY;
46
		double z1 = origin.getZ() - Math.cos(dir) * spreadXY;
47
		int spreadY = 2;
48
		double y0 = origin.getY() + random.nextInt(3) - 2;
49
		double y1 = origin.getY() + random.nextInt(3) - 2;
50
		int xStart = origin.getX() - Mth.ceil(spreadXY) - maxRadius;
51
		int yStart = origin.getY() - 2 - maxRadius;
52
		int zStart = origin.getZ() - Mth.ceil(spreadXY) - maxRadius;
53
		int sizeXZ = 2 * (Mth.ceil(spreadXY) + maxRadius);
54
		int sizeY = 2 * (2 + maxRadius);
55
56
		for (int xprobe = xStart; xprobe <= xStart + sizeXZ; xprobe++) {
57
			for (int zprobe = zStart; zprobe <= zStart + sizeXZ; zprobe++) {
58
				if (yStart <= level.getHeight(Heightmap.Types.OCEAN_FLOOR_WG, xprobe, zprobe)) {
59
					return this.doPlace(level, random, x0, x1, z0, z1, y0, y1, xStart, yStart, zStart, sizeXZ, sizeY);
60
				}
61
			}
62
		}
63
64
		return false;
65
	}
66
67
	protected boolean doPlace(
68
		final WorldGenLevel level,
69
		final RandomSource random,
70
		final double x0,
71
		final double x1,
72
		final double z0,
73
		final double z1,
74
		final double y0,
75
		final double y1,
76
		final int xStart,
77
		final int yStart,
78
		final int zStart,
79
		final int sizeXZ,
80
		final int sizeY
81
	) {
82
		int placed = 0;
83
		BitSet tested = new BitSet(sizeXZ * sizeY * sizeXZ);
84
		BlockPos.MutableBlockPos orePos = new BlockPos.MutableBlockPos();
85
		double[] data = new double[this.size * 4];
86
87
		for (int i = 0; i < this.size; i++) {
88
			float step = (float)i / this.size;
89
			double xx = Mth.lerp(step, x0, x1);
90
			double yy = Mth.lerp(step, y0, y1);
91
			double zz = Mth.lerp(step, z0, z1);
92
			double ss = random.nextDouble() * this.size / 16.0;
93
			double r = ((Mth.sin((float) Math.PI * step) + 1.0F) * ss + 1.0) / 2.0;
94
			data[i * 4 + 0] = xx;
95
			data[i * 4 + 1] = yy;
96
			data[i * 4 + 2] = zz;
97
			data[i * 4 + 3] = r;
98
		}
99
100
		for (int i1 = 0; i1 < this.size - 1; i1++) {
101
			if (!(data[i1 * 4 + 3] <= 0.0)) {
102
				for (int i2 = i1 + 1; i2 < this.size; i2++) {
103
					if (!(data[i2 * 4 + 3] <= 0.0)) {
104
						double dx = data[i1 * 4 + 0] - data[i2 * 4 + 0];
105
						double dy = data[i1 * 4 + 1] - data[i2 * 4 + 1];
106
						double dz = data[i1 * 4 + 2] - data[i2 * 4 + 2];
107
						double dr = data[i1 * 4 + 3] - data[i2 * 4 + 3];
108
						if (dr * dr > dx * dx + dy * dy + dz * dz) {
109
							if (dr > 0.0) {
110
								data[i2 * 4 + 3] = -1.0;
111
							} else {
112
								data[i1 * 4 + 3] = -1.0;
113
							}
114
						}
115
					}
116
				}
117
			}
118
		}
119
120
		try (BulkSectionAccess sectionGetter = new BulkSectionAccess(level)) {
121
			for (int i = 0; i < this.size; i++) {
122
				double r = data[i * 4 + 3];
123
				if (!(r < 0.0)) {
124
					double xx = data[i * 4 + 0];
125
					double yy = data[i * 4 + 1];
126
					double zz = data[i * 4 + 2];
127
					int xMin = Math.max(Mth.floor(xx - r), xStart);
128
					int yMin = Math.max(Mth.floor(yy - r), yStart);
129
					int zMin = Math.max(Mth.floor(zz - r), zStart);
130
					int xMax = Math.max(Mth.floor(xx + r), xMin);
131
					int yMax = Math.max(Mth.floor(yy + r), yMin);
132
					int zMax = Math.max(Mth.floor(zz + r), zMin);
133
134
					for (int x = xMin; x <= xMax; x++) {
135
						double xd = (x + 0.5 - xx) / r;
136
						if (xd * xd < 1.0) {
137
							for (int y = yMin; y <= yMax; y++) {
138
								double yd = (y + 0.5 - yy) / r;
139
								if (xd * xd + yd * yd < 1.0) {
140
									for (int z = zMin; z <= zMax; z++) {
141
										double zd = (z + 0.5 - zz) / r;
142
										if (xd * xd + yd * yd + zd * zd < 1.0 && !level.isOutsideBuildHeight(y)) {
143
											int bitSetIndex = x - xStart + (y - yStart) * sizeXZ + (z - zStart) * sizeXZ * sizeY;
144
											if (!tested.get(bitSetIndex)) {
145
												tested.set(bitSetIndex);
146
												orePos.set(x, y, z);
147
												if (level.ensureCanWrite(orePos)) {
148
													LevelChunkSection section = sectionGetter.getSection(orePos);
149
													if (section != null) {
150
														int sectionRelativeX = SectionPos.sectionRelative(x);
151
														int sectionRelativeY = SectionPos.sectionRelative(y);
152
														int sectionRelativeZ = SectionPos.sectionRelative(z);
153
														BlockState blockState = section.getBlockState(sectionRelativeX, sectionRelativeY, sectionRelativeZ);
154
155
														for (BlockReplacement targetState : this.targetStates) {
156
															if (this.canPlaceOre(blockState, sectionGetter::getBlockState, random, targetState, orePos)) {
157
																section.setBlockState(sectionRelativeX, sectionRelativeY, sectionRelativeZ, targetState.state(), false);
158
																placed++;
159
																break;
160
															}
161
														}
162
													}
163
												}
164
											}
165
										}
166
									}
167
								}
168
							}
169
						}
170
					}
171
				}
172
			}
173
		}
174
175
		return placed > 0;
176
	}
177
}
178

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