AttributeTrackSampler .java

net.minecraft.world.timeline.AttributeTrackSampler (class): members: argumentSampler, applyTimeBased, cachedArgument, argumentLerp, cachedTickId, clock.

net.minecraft.world.timelineclass46 linescommon1.7 KB

Implements

API Surface (1)

Value applyTimeBased(final Value baseValue, final int cacheTickId)

Fields & Constants (9)

clock
modifier
argumentSampler
clockManager
cachedTickId
cachedArgument
periodTicks
argumentTrack
argumentLerp

Full Source

AttributeTrackSampler.java46 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.timeline;
2
3
import java.util.Optional;
4
import net.minecraft.core.Holder;
5
import net.minecraft.util.KeyframeTrack;
6
import net.minecraft.util.KeyframeTrackSampler;
7
import net.minecraft.world.attribute.EnvironmentAttributeLayer;
8
import net.minecraft.world.attribute.LerpFunction;
9
import net.minecraft.world.attribute.modifier.AttributeModifier;
10
import net.minecraft.world.clock.ClockManager;
11
import net.minecraft.world.clock.WorldClock;
12
import org.jspecify.annotations.Nullable;
13
14
public class AttributeTrackSampler<Value, Argument> implements EnvironmentAttributeLayer.TimeBased<Value> {
15
	private final Holder<WorldClock> clock;
16
	private final AttributeModifier<Value, Argument> modifier;
17
	private final KeyframeTrackSampler<Argument> argumentSampler;
18
	private final ClockManager clockManager;
19
	private int cachedTickId;
20
	private @Nullable Argument cachedArgument;
21
22
	public AttributeTrackSampler(
23
		final Holder<WorldClock> clock,
24
		final Optional<Integer> periodTicks,
25
		final AttributeModifier<Value, Argument> modifier,
26
		final KeyframeTrack<Argument> argumentTrack,
27
		final LerpFunction<Argument> argumentLerp,
28
		final ClockManager clockManager
29
	) {
30
		this.clock = clock;
31
		this.modifier = modifier;
32
		this.clockManager = clockManager;
33
		this.argumentSampler = argumentTrack.bakeSampler(periodTicks, argumentLerp);
34
	}
35
36
	@Override
37
	public Value applyTimeBased(final Value baseValue, final int cacheTickId) {
38
		if (this.cachedArgument == null || cacheTickId != this.cachedTickId) {
39
			this.cachedTickId = cacheTickId;
40
			this.cachedArgument = this.argumentSampler.sample(this.clockManager.getInstance(this.clock).totalTicks());
41
		}
42
43
		return this.modifier.apply(baseValue, this.cachedArgument);
44
	}
45
}
46

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