SpatialAttributeInterpolator .java

net.minecraft.world.attribute.SpatialAttributeInterpolator (class): members: applyAttributeLayer, weightsBySource, accumulate, clear. imports: Entry.

net.minecraft.world.attributeclass50 linescommon1.8 KB

API Surface (3)

void clear()
SpatialAttributeInterpolator accumulate(final double weight, final EnvironmentAttributeMap attributes)
<Value> Value applyAttributeLayer(final EnvironmentAttribute<Value> attribute, final Value baseValue)

Fields & Constants (1)

weightsBySource

Code Graph: Dependencies (2)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

SpatialAttributeInterpolator.java50 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.attribute;
2
3
import it.unimi.dsi.fastutil.objects.Reference2DoubleArrayMap;
4
import it.unimi.dsi.fastutil.objects.Reference2DoubleMaps;
5
import it.unimi.dsi.fastutil.objects.Reference2DoubleMap.Entry;
6
import java.util.Objects;
7
8
public class SpatialAttributeInterpolator {
9
	private final Reference2DoubleArrayMap<EnvironmentAttributeMap> weightsBySource = new Reference2DoubleArrayMap<>();
10
11
	public void clear() {
12
		this.weightsBySource.clear();
13
	}
14
15
	public SpatialAttributeInterpolator accumulate(final double weight, final EnvironmentAttributeMap attributes) {
16
		this.weightsBySource.mergeDouble(attributes, weight, Double::sum);
17
		return this;
18
	}
19
20
	public <Value> Value applyAttributeLayer(final EnvironmentAttribute<Value> attribute, final Value baseValue) {
21
		if (this.weightsBySource.isEmpty()) {
22
			return baseValue;
23
		}
24
25
		if (this.weightsBySource.size() == 1) {
26
			EnvironmentAttributeMap sourceAttributes = this.weightsBySource.keySet().iterator().next();
27
			return sourceAttributes.applyModifier(attribute, baseValue);
28
		}
29
30
		LerpFunction<Value> lerp = attribute.type().spatialLerp();
31
		Value resultValue = null;
32
		double accumulatedWeight = 0.0;
33
34
		for (Entry<EnvironmentAttributeMap> entry : Reference2DoubleMaps.fastIterable(this.weightsBySource)) {
35
			EnvironmentAttributeMap sourceAttributes = entry.getKey();
36
			double sourceWeight = entry.getDoubleValue();
37
			Value sourceValue = sourceAttributes.applyModifier(attribute, baseValue);
38
			accumulatedWeight += sourceWeight;
39
			if (resultValue == null) {
40
				resultValue = sourceValue;
41
			} else {
42
				float relativeFraction = (float)(sourceWeight / accumulatedWeight);
43
				resultValue = lerp.apply(relativeFraction, resultValue, sourceValue);
44
			}
45
		}
46
47
		return Objects.requireNonNull(resultValue);
48
	}
49
}
50

Decompiled Minecraft 26.3 source of SpatialAttributeInterpolator (net/minecraft/world/attribute/SpatialAttributeInterpolator.java) with Mojang official mappings, stored verbatim on the site.