SteppedInterpolationHandler .java

net.minecraft.world.entity.SteppedInterpolationHandler (class): members: Step, InterpolationData, hasActiveInterpolation, startInterpolating, cancel.

net.minecraft.world.entityclass208 linescommonextends AbstractInterpolationHandler6.4 KB

API Surface (8)

InterpolationHandler create(final Entity entity)
PositionAndRotation.Mutable interpolationData()
void startInterpolating(final PositionPath position, final float yRot, final float xRot)
void doInterpolate()
boolean hasActiveInterpolation()
void cancel()
void addDelta(final Vec3 delta)
void addRotation(final float yRot, final float xRot)

Fields & Constants (1)

interpolationData

Full Source

SteppedInterpolationHandler.java208 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity;
2
3
import java.util.LinkedList;
4
import java.util.List;
5
import java.util.Objects;
6
import net.minecraft.core.PositionAndRotation;
7
import net.minecraft.util.Mth;
8
import net.minecraft.world.phys.Vec3;
9
10
public class SteppedInterpolationHandler extends AbstractInterpolationHandler {
11
	private final SteppedInterpolationHandler.InterpolationData interpolationData = new SteppedInterpolationHandler.InterpolationData();
12
13
	private SteppedInterpolationHandler(final Entity entity) {
14
		super(entity, entity.getType().updateInterval());
15
	}
16
17
	public static InterpolationHandler create(final Entity entity) {
18
		return entity.level().isClientSide() ? new SteppedInterpolationHandler(entity) : new SteppedInterpolationTracker(entity);
19
	}
20
21
	@Override
22
	protected PositionAndRotation.Mutable interpolationData() {
23
		return this.interpolationData;
24
	}
25
26
	@Override
27
	protected void startInterpolating(final PositionPath position, final float yRot, final float xRot) {
28
		if (!this.hasActiveInterpolation()) {
29
			this.interpolationData.setStartingPoint(this.entity.position(), this.entity.getYRot(), this.entity.getXRot());
30
		}
31
32
		Vec3 endPosition = position.endPosition();
33
		if (Objects.equals(endPosition, this.interpolationData.position())) {
34
			this.interpolationData.addStep(endPosition, yRot, xRot, this.interpolationSteps);
35
		} else {
36
			this.interpolationData.addSteps(position, yRot, xRot, this.interpolationSteps);
37
		}
38
39
		this.interpolationData.set(endPosition, yRot, xRot);
40
	}
41
42
	@Override
43
	protected void doInterpolate() {
44
		PositionAndRotation target = this.interpolationData.getNewPositionAndRotation();
45
		this.entity.setPos(target.position());
46
		this.entity.setRot(target.yRot(), target.xRot());
47
		float tick = this.entity.level().getRelativeTickSpeed();
48
		this.interpolationData.advance(tick, this.interpolationSteps);
49
	}
50
51
	@Override
52
	public boolean hasActiveInterpolation() {
53
		return !this.interpolationData.remainingSteps.isEmpty();
54
	}
55
56
	@Override
57
	public void cancel() {
58
		this.interpolationData.reset();
59
	}
60
61
	private static class InterpolationData extends PositionAndRotation.Mutable {
62
		private final LinkedList<SteppedInterpolationHandler.Step> remainingSteps = new LinkedList<>();
63
		private final PositionAndRotation.Mutable lastStepPosRot = new PositionAndRotation.Mutable();
64
		private float currentStepTicks;
65
		private float remainingTicks;
66
		private float interpolationSpeed = 1.0F;
67
68
		private void advance(float ticks, final int interpolationSteps) {
69
			float targetSpeed = Math.max(this.remainingTicks / interpolationSteps, 1.0F);
70
			this.interpolationSpeed = Mth.lerp(1.0F / interpolationSteps, this.interpolationSpeed, targetSpeed);
71
			if (ticks * this.interpolationSpeed < this.remainingTicks) {
72
				ticks *= this.interpolationSpeed;
73
			} else {
74
				ticks = this.remainingTicks;
75
				this.interpolationSpeed = 1.0F;
76
			}
77
78
			this.currentStepTicks += ticks;
79
			this.remainingTicks -= ticks;
80
		}
81
82
		private void reset() {
83
			this.remainingSteps.clear();
84
			this.remainingTicks = 0.0F;
85
			this.interpolationSpeed = 1.0F;
86
		}
87
88
		@Override
89
		public void addDelta(final Vec3 delta) {
90
			super.addDelta(delta);
91
92
			for (SteppedInterpolationHandler.Step step : this.remainingSteps) {
93
				step.addDelta(delta);
94
			}
95
96
			this.lastStepPosRot.addDelta(delta);
97
		}
98
99
		@Override
100
		public void addRotation(final float yRot, final float xRot) {
101
			super.addRotation(yRot, xRot);
102
103
			for (SteppedInterpolationHandler.Step step : this.remainingSteps) {
104
				step.addRotation(yRot, xRot);
105
			}
106
107
			this.lastStepPosRot.addRotation(yRot, xRot);
108
		}
109
110
		private void setStartingPoint(final Vec3 position, final float yRot, final float xRot) {
111
			this.lastStepPosRot.set(position, yRot, xRot);
112
			this.currentStepTicks = 1.0F;
113
		}
114
115
		private void addStep(final Vec3 position, final float yRot, final float xRot, final int interpolationSteps) {
116
			this.remainingSteps.add(new SteppedInterpolationHandler.Step(position, yRot, xRot, interpolationSteps));
117
			this.remainingTicks += interpolationSteps;
118
		}
119
120
		private void addSteps(final PositionPath position, final float yRot, final float xRot, final int interpolationSteps) {
121
			switch (position) {
122
				case PositionPath.Linear(Vec3 pos):
123
					this.addStep(pos, yRot, xRot, interpolationSteps);
124
					break;
125
				case PositionPath.Stepped var9: {
126
					PositionPath.Stepped var10000 = var9;
127
128
					try {
129
						var10000.endPosition();
130
					} catch (Throwable var17) {
131
						throw new MatchException(var17.toString(), var17);
132
					}
133
134
					var10000 = var9;
135
136
					try {
137
						var24 = var10000.steps();
138
					} catch (Throwable var16) {
139
						throw new MatchException(var16.toString(), var16);
140
					}
141
142
					List totalInterpolationTicks = var24;
143
					List<PositionStep> steps = totalInterpolationTicks;
144
					if (yRot == this.yRot() && xRot == this.xRot()) {
145
						for (PositionStep step : steps) {
146
							this.addStep(step.position(), yRot, xRot, step.tickOffset());
147
						}
148
149
						return;
150
					}
151
152
					int totalInterpolationTicks = getInterpolationTicks(steps);
153
					int offset = 0;
154
155
					for (PositionStep step : steps) {
156
						offset += step.tickOffset();
157
						float a = (float)offset / totalInterpolationTicks;
158
						this.addStep(step.position(), Mth.rotLerp(a, this.yRot(), yRot), Mth.lerp(a, this.xRot(), xRot), step.tickOffset());
159
					}
160
					break;
161
				}
162
				default:
163
					throw new MatchException(null, null);
164
			}
165
		}
166
167
		private static int getInterpolationTicks(final List<PositionStep> steps) {
168
			int ticks = 0;
169
170
			for (PositionStep step : steps) {
171
				ticks += step.tickOffset();
172
			}
173
174
			return ticks;
175
		}
176
177
		private PositionAndRotation getNewPositionAndRotation() {
178
			while (!this.remainingSteps.isEmpty()) {
179
				SteppedInterpolationHandler.Step step = this.remainingSteps.getFirst();
180
				int offset = step.tickOffset;
181
				if (this.currentStepTicks < offset) {
182
					float a = this.currentStepTicks / offset;
183
					return PositionAndRotation.of(
184
						this.lastStepPosRot.position().lerp(step.position(), a),
185
						Mth.rotLerp(a, this.lastStepPosRot.yRot(), step.yRot()),
186
						Mth.lerp(a, this.lastStepPosRot.xRot(), step.xRot())
187
					);
188
				}
189
190
				this.currentStepTicks -= offset;
191
				this.lastStepPosRot.set(step.position(), step.yRot(), step.xRot());
192
				this.remainingSteps.removeFirst();
193
			}
194
195
			return this;
196
		}
197
	}
198
199
	private static class Step extends PositionAndRotation.Mutable {
200
		private final int tickOffset;
201
202
		public Step(final Vec3 position, final float yRot, final float xRot, final int tickOffset) {
203
			this.tickOffset = tickOffset;
204
			this.set(position, yRot, xRot);
205
		}
206
	}
207
}
208

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