SamplerContext .java

net.minecraft.world.level.levelgen.densityfunction.SamplerContext (class, 136 lines): members: getFieldOrDefault, CACHE_SIZE_STEP, EMPTY_UNCACHED, builder.

net.minecraft.world.level.levelgen.densityfunctionclass136 linescommon4.0 KB

API Surface (7)

SamplerContext.Builder builder()
<T> T getFieldOrDefault(final ContextKey<? extends T> key, final T defaultValue)
ScopedDensityBuffer acquireBuffer(final DensityVolume volume)
SamplerContext.Builder setUserFields(final ContextMap userFields)
SamplerContext.Builder useBufferArena(final DensityBufferArena bufferArena)
SamplerContext.Builder enableCaches()
SamplerContext build()

Fields & Constants (5)

EMPTY_UNCACHED
CACHE_SIZE_STEP
userFields
bufferArena
cacheCells

Full Source

SamplerContext.java136 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.levelgen.densityfunction;
2
3
import java.util.Arrays;
4
import java.util.Objects;
5
import net.minecraft.core.BlockPos;
6
import net.minecraft.util.Mth;
7
import net.minecraft.util.context.ContextKey;
8
import net.minecraft.util.context.ContextMap;
9
import org.jspecify.annotations.Nullable;
10
11
public class SamplerContext {
12
	public static final SamplerContext EMPTY_UNCACHED = new SamplerContext(ContextMap.EMPTY, DensityBufferArena.GLOBAL, false);
13
	private static final int CACHE_SIZE_STEP = 16;
14
	private final ContextMap userFields;
15
	private final DensityBufferArena bufferArena;
16
	private SamplerContext.CacheCell @Nullable [] cacheCells;
17
18
	private SamplerContext(final ContextMap userFields, final DensityBufferArena bufferArena, final boolean enableCaches) {
19
		this.userFields = userFields;
20
		this.bufferArena = bufferArena;
21
		this.cacheCells = enableCaches ? new SamplerContext.CacheCell[0] : null;
22
	}
23
24
	public static SamplerContext.Builder builder() {
25
		return new SamplerContext.Builder();
26
	}
27
28
	public <T> @Nullable T getField(final ContextKey<T> key) {
29
		return this.userFields.get(key);
30
	}
31
32
	public <T> T getFieldOrDefault(final ContextKey<? extends T> key, final T defaultValue) {
33
		return Objects.requireNonNullElse(this.getField((ContextKey<T>)key), defaultValue);
34
	}
35
36
	public ScopedDensityBuffer acquireBuffer(final DensityVolume volume) {
37
		return this.bufferArena.acquire(volume.size());
38
	}
39
40
	private SamplerContext.@Nullable CacheCell getCacheCell(final int cacheId) {
41
		if (this.cacheCells == null) {
42
			return null;
43
		}
44
45
		int oldSize = this.cacheCells.length;
46
		if (cacheId >= oldSize) {
47
			int newSize = Mth.roundToward(cacheId + 1, 16);
48
			this.cacheCells = Arrays.copyOf(this.cacheCells, newSize);
49
50
			for (int i = oldSize; i < newSize; i++) {
51
				this.cacheCells[i] = new SamplerContext.CacheCell();
52
			}
53
		}
54
55
		return this.cacheCells[cacheId];
56
	}
57
58
	void sampleVolumeCached(final int cacheId, final DensitySampler input, final DensityBuffer outputBuffer, final DensityVolume volume) {
59
		SamplerContext.CacheCell cell = this.getCacheCell(cacheId);
60
		if (cell == null) {
61
			input.sampleVolume(this, outputBuffer, volume);
62
		} else {
63
			if (cell.buffer == null || !volume.equals(cell.volume)) {
64
				if (cell.buffer != null) {
65
					cell.buffer.close();
66
				}
67
68
				cell.volume = volume;
69
				cell.buffer = this.acquireBuffer(volume);
70
				input.sampleVolume(this, cell.buffer, volume);
71
			}
72
73
			outputBuffer.copyFrom(cell.buffer);
74
		}
75
	}
76
77
	float sampleValueCached(final int cacheId, final DensitySampler input, final int blockX, final int blockY, final int blockZ) {
78
		SamplerContext.CacheCell cell = this.getCacheCell(cacheId);
79
		if (cell == null) {
80
			return input.sampleValue(this, blockX, blockY, blockZ);
81
		}
82
83
		long cacheKey = BlockPos.asLong(blockX, blockY, blockZ);
84
		if (cell.valueKey == cacheKey && !Float.isNaN(cell.value)) {
85
			return cell.value;
86
		}
87
88
		if (cell.buffer != null && cell.volume != null) {
89
			int index = cell.volume.indexOfBlock(blockX, blockY, blockZ);
90
			if (index != -1) {
91
				return cell.buffer.get(index);
92
			}
93
		}
94
95
		float value = input.sampleValue(this, blockX, blockY, blockZ);
96
		cell.valueKey = cacheKey;
97
		cell.value = value;
98
		return value;
99
	}
100
101
	public static class Builder {
102
		private ContextMap userFields = ContextMap.EMPTY;
103
		private DensityBufferArena bufferArena = DensityBufferArena.GLOBAL;
104
		private boolean enableCaches;
105
106
		private Builder() {
107
		}
108
109
		public SamplerContext.Builder setUserFields(final ContextMap userFields) {
110
			this.userFields = userFields;
111
			return this;
112
		}
113
114
		public SamplerContext.Builder useBufferArena(final DensityBufferArena bufferArena) {
115
			this.bufferArena = bufferArena;
116
			return this;
117
		}
118
119
		public SamplerContext.Builder enableCaches() {
120
			this.enableCaches = true;
121
			return this;
122
		}
123
124
		public SamplerContext build() {
125
			return new SamplerContext(this.userFields, this.bufferArena, this.enableCaches);
126
		}
127
	}
128
129
	private static class CacheCell {
130
		private @Nullable DensityVolume volume;
131
		private @Nullable ScopedDensityBuffer buffer;
132
		private long valueKey;
133
		private float value = Float.NaN;
134
	}
135
}
136

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