MaterialRuleContext .java

net.minecraft.world.level.levelgen.material.MaterialRuleContext (class): members: HOW_FAR_BELOW_PRELIMINARY_SURFACE_LEVEL_TO_BUILD_SURFACE, waterHeight.

net.minecraft.world.level.levelgen.materialclass321 linescommon10.4 KB

API Surface (23)

double getSurfaceSecondary()
Holder<Biome> getBiome()
int getSeaLevel()
int getMinSurfaceLevel()
DoubleSupplier getNoiseSampler(final ResourceKey<NormalNoise> noiseId, final boolean is3d)
double getAsDouble()
double getAsDouble()
PositionalRandomFactory getOrCreateRandomFactory(final Identifier name)
MaterialRules.DensityGetter getDensitiesInChunk(final DensityFunction function, final boolean prefill)
int stoneDepthAbove()
int stoneDepthBelow()
int surfaceDepth()
int waterHeight()
int blockX()
int blockY()
int blockZ()
BlockPos blockPos()
int surfaceGradientX()
int surfaceGradientZ()
int resolveAnchorY(final VerticalAnchor anchor)
BlockState getBand(final int x, final int y, final int z)
boolean test()
boolean test()

Fields & Constants (29)

HOW_FAR_BELOW_PRELIMINARY_SURFACE_LEVEL_TO_BUILD_SURFACE
system
randomState
expectedVolume
densitySamplers
biomeGetter
context
possibleBiomes
preliminarySurfaceVolume
preliminarySurfaceBuffer
noiseSamplers2d
noiseSamplers3d
lastUpdateXZ
blockX
blockZ
surfaceGradientX
surfaceGradientZ
surfaceDepth
lastSurfaceDepth2Update
surfaceSecondary
lastMinSurfaceLevelUpdate
minSurfaceLevel
lastUpdateY
pos
biome

…and 4 more (see full source).

String literals (1)

"Update triggered but the result is null"

Full Source

MaterialRuleContext.java321 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.levelgen.material;
2
3
import java.util.IdentityHashMap;
4
import java.util.Map;
5
import java.util.Set;
6
import java.util.function.DoubleSupplier;
7
import java.util.function.Function;
8
import net.minecraft.core.BlockPos;
9
import net.minecraft.core.Holder;
10
import net.minecraft.resources.Identifier;
11
import net.minecraft.resources.ResourceKey;
12
import net.minecraft.util.Mth;
13
import net.minecraft.world.level.biome.Biome;
14
import net.minecraft.world.level.block.state.BlockState;
15
import net.minecraft.world.level.levelgen.PositionalRandomFactory;
16
import net.minecraft.world.level.levelgen.RandomState;
17
import net.minecraft.world.level.levelgen.VerticalAnchor;
18
import net.minecraft.world.level.levelgen.WorldGenerationContext;
19
import net.minecraft.world.level.levelgen.densityfunction.DensityBuffer;
20
import net.minecraft.world.level.levelgen.densityfunction.DensityFunction;
21
import net.minecraft.world.level.levelgen.densityfunction.DensitySampler;
22
import net.minecraft.world.level.levelgen.densityfunction.DensitySamplerSet;
23
import net.minecraft.world.level.levelgen.densityfunction.DensityVolume;
24
import net.minecraft.world.level.levelgen.material.condition.ConditionEvaluator;
25
import net.minecraft.world.level.levelgen.synth.Noise;
26
import net.minecraft.world.level.levelgen.synth.NormalNoise;
27
import org.jspecify.annotations.Nullable;
28
29
public final class MaterialRuleContext {
30
	private static final int HOW_FAR_BELOW_PRELIMINARY_SURFACE_LEVEL_TO_BUILD_SURFACE = 8;
31
	private final MaterialSystem system;
32
	private final RandomState randomState;
33
	private final DensityVolume expectedVolume;
34
	private final DensitySamplerSet densitySamplers;
35
	private final Function<BlockPos, Holder<Biome>> biomeGetter;
36
	private final WorldGenerationContext context;
37
	private final @Nullable Set<Holder<Biome>> possibleBiomes;
38
	private final DensityVolume preliminarySurfaceVolume;
39
	private @Nullable DensityBuffer preliminarySurfaceBuffer;
40
	private final Map<ResourceKey<NormalNoise>, DoubleSupplier> noiseSamplers2d = new IdentityHashMap<>();
41
	private final Map<ResourceKey<NormalNoise>, DoubleSupplier> noiseSamplers3d = new IdentityHashMap<>();
42
	private long lastUpdateXZ = -9223372036854775807L;
43
	private int blockX;
44
	private int blockZ;
45
	private int surfaceGradientX;
46
	private int surfaceGradientZ;
47
	private int surfaceDepth;
48
	private long lastSurfaceDepth2Update = this.lastUpdateXZ - 1L;
49
	private double surfaceSecondary;
50
	private long lastMinSurfaceLevelUpdate = this.lastUpdateXZ - 1L;
51
	private int minSurfaceLevel;
52
	private long lastUpdateY = -9223372036854775807L;
53
	private final BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
54
	private @Nullable Holder<Biome> biome;
55
	private int blockY;
56
	private int waterHeight;
57
	private int stoneDepthBelow;
58
	private int stoneDepthAbove;
59
60
	MaterialRuleContext(
61
		final MaterialSystem system,
62
		final RandomState randomState,
63
		final DensityVolume expectedVolume,
64
		final DensitySamplerSet densitySamplers,
65
		final Function<BlockPos, Holder<Biome>> biomeGetter,
66
		final WorldGenerationContext context,
67
		final @Nullable Set<Holder<Biome>> possibleBiomes
68
	) {
69
		this.system = system;
70
		this.randomState = randomState;
71
		this.expectedVolume = expectedVolume;
72
		this.densitySamplers = densitySamplers;
73
		this.biomeGetter = biomeGetter;
74
		this.context = context;
75
		this.possibleBiomes = possibleBiomes;
76
		this.preliminarySurfaceVolume = new DensityVolume(
77
			expectedVolume.sizeX(), 1, expectedVolume.sizeZ(), expectedVolume.minBlockX(), 0, expectedVolume.minBlockZ()
78
		);
79
	}
80
81
	void updateXZ(final int blockX, final int blockZ, final int surfaceGradientX, final int surfaceGradientZ) {
82
		this.lastUpdateXZ++;
83
		this.lastUpdateY++;
84
		this.blockX = blockX;
85
		this.blockZ = blockZ;
86
		this.surfaceGradientX = surfaceGradientX;
87
		this.surfaceGradientZ = surfaceGradientZ;
88
		this.surfaceDepth = this.system.getSurfaceDepth(blockX, blockZ);
89
	}
90
91
	void updateY(final int stoneDepthAbove, final int stoneDepthBelow, final int waterHeight, final int blockY) {
92
		this.lastUpdateY++;
93
		this.biome = null;
94
		this.blockY = blockY;
95
		this.waterHeight = waterHeight;
96
		this.stoneDepthBelow = stoneDepthBelow;
97
		this.stoneDepthAbove = stoneDepthAbove;
98
	}
99
100
	public double getSurfaceSecondary() {
101
		if (this.lastSurfaceDepth2Update != this.lastUpdateXZ) {
102
			this.lastSurfaceDepth2Update = this.lastUpdateXZ;
103
			this.surfaceSecondary = this.system.getSurfaceSecondary(this.blockX, this.blockZ);
104
		}
105
106
		return this.surfaceSecondary;
107
	}
108
109
	public Holder<Biome> getBiome() {
110
		if (this.biome == null) {
111
			this.biome = this.biomeGetter.apply(this.pos.set(this.blockX, this.blockY, this.blockZ));
112
		}
113
114
		return this.biome;
115
	}
116
117
	public int getSeaLevel() {
118
		return this.system.getSeaLevel();
119
	}
120
121
	public int getMinSurfaceLevel() {
122
		if (this.lastMinSurfaceLevelUpdate != this.lastUpdateXZ) {
123
			this.lastMinSurfaceLevelUpdate = this.lastUpdateXZ;
124
			int index = this.preliminarySurfaceVolume.indexOfBlock(this.blockX, 0, this.blockZ);
125
			float preliminarySurfaceLevel;
126
			if (index != -1) {
127
				if (this.preliminarySurfaceBuffer == null) {
128
					this.preliminarySurfaceBuffer = DensityBuffer.createUnpooled(this.preliminarySurfaceVolume.size());
129
					this.densitySamplers.get(this.system.preliminarySurfaceFunction()).sampleVolume(this.preliminarySurfaceBuffer, this.preliminarySurfaceVolume);
130
				}
131
132
				preliminarySurfaceLevel = this.preliminarySurfaceBuffer.get(index);
133
			} else {
134
				preliminarySurfaceLevel = this.densitySamplers.sampleValue(this.system.preliminarySurfaceFunction(), this.blockX, 0, this.blockZ);
135
			}
136
137
			this.minSurfaceLevel = Mth.floor(preliminarySurfaceLevel) + this.surfaceDepth - 8;
138
		}
139
140
		return this.minSurfaceLevel;
141
	}
142
143
	public DoubleSupplier getNoiseSampler(final ResourceKey<NormalNoise> noiseId, final boolean is3d) {
144
		return is3d
145
			? this.noiseSamplers3d.computeIfAbsent(noiseId, this::createNoiseSampler3d)
146
			: this.noiseSamplers2d.computeIfAbsent(noiseId, this::createNoiseSampler2d);
147
	}
148
149
	private DoubleSupplier createNoiseSampler2d(final ResourceKey<NormalNoise> noiseId) {
150
		final Noise noise = this.randomState.getOrCreateNoise(noiseId);
151
		return new DoubleSupplier() {
152
			private long lastUpdateXZ;
153
			private double lastNoise;
154
155
			{
156
				this.lastUpdateXZ = MaterialRuleContext.this.lastUpdateXZ - 1L;
157
			}
158
159
			@Override
160
			public double getAsDouble() {
161
				if (this.lastUpdateXZ != MaterialRuleContext.this.lastUpdateXZ) {
162
					this.lastNoise = noise.get(MaterialRuleContext.this.blockX, 0.0, MaterialRuleContext.this.blockZ);
163
					this.lastUpdateXZ = MaterialRuleContext.this.lastUpdateXZ;
164
				}
165
166
				return this.lastNoise;
167
			}
168
		};
169
	}
170
171
	private DoubleSupplier createNoiseSampler3d(final ResourceKey<NormalNoise> noiseId) {
172
		final Noise noise = this.randomState.getOrCreateNoise(noiseId);
173
		return new DoubleSupplier() {
174
			private long lastUpdateY;
175
			private double lastNoise;
176
177
			{
178
				this.lastUpdateY = MaterialRuleContext.this.lastUpdateY - 1L;
179
			}
180
181
			@Override
182
			public double getAsDouble() {
183
				if (this.lastUpdateY != MaterialRuleContext.this.lastUpdateY) {
184
					this.lastNoise = noise.get(MaterialRuleContext.this.blockX, MaterialRuleContext.this.blockY, MaterialRuleContext.this.blockZ);
185
					this.lastUpdateY = MaterialRuleContext.this.lastUpdateY;
186
				}
187
188
				return this.lastNoise;
189
			}
190
		};
191
	}
192
193
	public PositionalRandomFactory getOrCreateRandomFactory(final Identifier name) {
194
		return this.randomState.getOrCreateRandomFactory(name);
195
	}
196
197
	public MaterialRules.DensityGetter getDensitiesInChunk(final DensityFunction function, final boolean prefill) {
198
		DensitySampler.Bound sampler = this.densitySamplers.get(function);
199
		if (prefill) {
200
			DensityBuffer buffer = DensityBuffer.createUnpooled(this.expectedVolume.size());
201
			sampler.sampleVolume(buffer, this.expectedVolume);
202
			return () -> {
203
				int index = this.expectedVolume.indexOfBlock(this.blockX, this.blockY, this.blockZ);
204
				return index == -1 ? sampler.sampleValue(this.blockX, this.blockY, this.blockZ) : buffer.get(index);
205
			};
206
		} else {
207
			return () -> sampler.sampleValue(this.blockX, this.blockY, this.blockZ);
208
		}
209
	}
210
211
	public @Nullable Set<Holder<Biome>> possibleBiomes() {
212
		return this.possibleBiomes;
213
	}
214
215
	public int stoneDepthAbove() {
216
		return this.stoneDepthAbove;
217
	}
218
219
	public int stoneDepthBelow() {
220
		return this.stoneDepthBelow;
221
	}
222
223
	public int surfaceDepth() {
224
		return this.surfaceDepth;
225
	}
226
227
	public int waterHeight() {
228
		return this.waterHeight;
229
	}
230
231
	public int blockX() {
232
		return this.blockX;
233
	}
234
235
	public int blockY() {
236
		return this.blockY;
237
	}
238
239
	public int blockZ() {
240
		return this.blockZ;
241
	}
242
243
	public BlockPos blockPos() {
244
		return this.pos.set(this.blockX, this.blockY, this.blockZ);
245
	}
246
247
	public int surfaceGradientX() {
248
		return this.surfaceGradientX;
249
	}
250
251
	public int surfaceGradientZ() {
252
		return this.surfaceGradientZ;
253
	}
254
255
	public int resolveAnchorY(final VerticalAnchor anchor) {
256
		return anchor.resolveY(this.context);
257
	}
258
259
	public BlockState getBand(final int x, final int y, final int z) {
260
		return this.system.getBand(x, y, z);
261
	}
262
263
	public abstract static class LazyXZCondition implements ConditionEvaluator {
264
		protected final MaterialRuleContext context;
265
		private long lastUpdate;
266
		private @Nullable Boolean result;
267
268
		protected LazyXZCondition(final MaterialRuleContext context) {
269
			this.context = context;
270
			this.lastUpdate = context.lastUpdateXZ - 1L;
271
		}
272
273
		@Override
274
		public boolean test() {
275
			long lastContextUpdate = this.context.lastUpdateXZ;
276
			if (lastContextUpdate == this.lastUpdate) {
277
				if (this.result == null) {
278
					throw new IllegalStateException("Update triggered but the result is null");
279
				} else {
280
					return this.result;
281
				}
282
			} else {
283
				this.lastUpdate = lastContextUpdate;
284
				this.result = this.compute();
285
				return this.result;
286
			}
287
		}
288
289
		protected abstract boolean compute();
290
	}
291
292
	public abstract static class LazyYCondition implements ConditionEvaluator {
293
		protected final MaterialRuleContext context;
294
		private long lastUpdate;
295
		private @Nullable Boolean result;
296
297
		protected LazyYCondition(final MaterialRuleContext context) {
298
			this.context = context;
299
			this.lastUpdate = context.lastUpdateY - 1L;
300
		}
301
302
		@Override
303
		public boolean test() {
304
			long lastContextUpdate = this.context.lastUpdateY;
305
			if (lastContextUpdate == this.lastUpdate) {
306
				if (this.result == null) {
307
					throw new IllegalStateException("Update triggered but the result is null");
308
				} else {
309
					return this.result;
310
				}
311
			} else {
312
				this.lastUpdate = lastContextUpdate;
313
				this.result = this.compute();
314
				return this.result;
315
			}
316
		}
317
318
		protected abstract boolean compute();
319
	}
320
}
321

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