TrackedWaypoint .java

net.minecraft.world.waypoints.TrackedWaypoint (class, 337 lines): members: pitchDirectionToCamera, yawAngleToCamera, AzimuthWaypoint, PitchDirection, icon.

net.minecraft.world.waypointsclass337 linescommon11.5 KB

Implements

API Surface (27)

Either<UUID, String> id()
void write(final ByteBuf buf)
TrackedWaypoint setPosition(final UUID identifier, final Waypoint.Icon icon, final Vec3i position)
TrackedWaypoint setChunk(final UUID identifier, final Waypoint.Icon icon, final ChunkPos chunk)
TrackedWaypoint setAzimuth(final UUID identifier, final Waypoint.Icon icon, final float angle)
TrackedWaypoint empty(final UUID identifier)
Waypoint.Icon icon()
void update(final TrackedWaypoint other)
void writeContents(final ByteBuf buf)
double yawAngleToCamera(final Level level, final TrackedWaypoint.Camera camera, final PartialTickSupplier partialTickSupplier)
TrackedWaypoint.PitchDirection pitchDirectionToCamera(final Level level, final TrackedWaypoint.Projector projector, final PartialTickSupplier partialTickSupplier)
double distanceSquared(final Entity fromEntity)
void update(final TrackedWaypoint other)
void writeContents(final ByteBuf buf)
double yawAngleToCamera(final Level level, final TrackedWaypoint.Camera camera, final PartialTickSupplier partialTickSupplier)
TrackedWaypoint.PitchDirection pitchDirectionToCamera(final Level level, final TrackedWaypoint.Projector projector, final PartialTickSupplier partialTickSupplier)
double distanceSquared(final Entity fromEntity)
void update(final TrackedWaypoint other)
void writeContents(final ByteBuf buf)
double yawAngleToCamera(final Level level, final TrackedWaypoint.Camera camera, final PartialTickSupplier partialTickSupplier)
TrackedWaypoint.PitchDirection pitchDirectionToCamera(final Level level, final TrackedWaypoint.Projector projector, final PartialTickSupplier partialTickSupplier)
double distanceSquared(final Entity fromEntity)
void update(final TrackedWaypoint other)
void writeContents(final ByteBuf buf)
double yawAngleToCamera(final Level level, final TrackedWaypoint.Camera camera, final PartialTickSupplier partialTickSupplier)
TrackedWaypoint.PitchDirection pitchDirectionToCamera(final Level level, final TrackedWaypoint.Projector projector, final PartialTickSupplier partialTickSupplier)
double distanceSquared(final Entity fromEntity)

Fields & Constants (6)

LOGGER
STREAM_CODEC
identifier
icon
type
projector

Full Source

TrackedWaypoint.java337 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.waypoints;
2
3
import com.mojang.datafixers.util.Either;
4
import com.mojang.logging.LogUtils;
5
import io.netty.buffer.ByteBuf;
6
import java.util.UUID;
7
import net.minecraft.core.UUIDUtil;
8
import net.minecraft.core.Vec3i;
9
import net.minecraft.network.FriendlyByteBuf;
10
import net.minecraft.network.VarInt;
11
import net.minecraft.network.codec.StreamCodec;
12
import net.minecraft.util.Mth;
13
import net.minecraft.world.entity.Entity;
14
import net.minecraft.world.level.ChunkPos;
15
import net.minecraft.world.level.Level;
16
import net.minecraft.world.phys.Vec3;
17
import org.apache.commons.lang3.function.TriFunction;
18
import org.slf4j.Logger;
19
20
public abstract class TrackedWaypoint implements Waypoint {
21
	private static final Logger LOGGER = LogUtils.getLogger();
22
	public static final StreamCodec<ByteBuf, TrackedWaypoint> STREAM_CODEC = StreamCodec.ofMember(TrackedWaypoint::write, TrackedWaypoint::read);
23
	protected final Either<UUID, String> identifier;
24
	private final Waypoint.Icon icon;
25
	private final TrackedWaypoint.Type type;
26
27
	private TrackedWaypoint(final Either<UUID, String> identifier, final Waypoint.Icon icon, final TrackedWaypoint.Type type) {
28
		this.identifier = identifier;
29
		this.icon = icon;
30
		this.type = type;
31
	}
32
33
	public Either<UUID, String> id() {
34
		return this.identifier;
35
	}
36
37
	public abstract void update(final TrackedWaypoint other);
38
39
	public void write(final ByteBuf buf) {
40
		FriendlyByteBuf byteBuf = new FriendlyByteBuf(buf);
41
		byteBuf.writeEither(this.identifier, UUIDUtil.STREAM_CODEC, FriendlyByteBuf::writeUtf);
42
		Waypoint.Icon.STREAM_CODEC.encode(byteBuf, this.icon);
43
		byteBuf.writeEnum(this.type);
44
		this.writeContents(buf);
45
	}
46
47
	public abstract void writeContents(final ByteBuf buf);
48
49
	private static TrackedWaypoint read(final ByteBuf buf) {
50
		FriendlyByteBuf byteBuf = new FriendlyByteBuf(buf);
51
		Either<UUID, String> identifier = byteBuf.readEither(UUIDUtil.STREAM_CODEC, FriendlyByteBuf::readUtf);
52
		Waypoint.Icon icon = Waypoint.Icon.STREAM_CODEC.decode(byteBuf);
53
		TrackedWaypoint.Type type = byteBuf.readEnum(TrackedWaypoint.Type.class);
54
		return type.constructor.apply(identifier, icon, byteBuf);
55
	}
56
57
	public static TrackedWaypoint setPosition(final UUID identifier, final Waypoint.Icon icon, final Vec3i position) {
58
		return new TrackedWaypoint.Vec3iWaypoint(identifier, icon, position);
59
	}
60
61
	public static TrackedWaypoint setChunk(final UUID identifier, final Waypoint.Icon icon, final ChunkPos chunk) {
62
		return new TrackedWaypoint.ChunkWaypoint(identifier, icon, chunk);
63
	}
64
65
	public static TrackedWaypoint setAzimuth(final UUID identifier, final Waypoint.Icon icon, final float angle) {
66
		return new TrackedWaypoint.AzimuthWaypoint(identifier, icon, angle);
67
	}
68
69
	public static TrackedWaypoint empty(final UUID identifier) {
70
		return new TrackedWaypoint.EmptyWaypoint(identifier);
71
	}
72
73
	public abstract double yawAngleToCamera(final Level level, final TrackedWaypoint.Camera camera, final PartialTickSupplier partialTickSupplier);
74
75
	public abstract TrackedWaypoint.PitchDirection pitchDirectionToCamera(
76
		Level level, TrackedWaypoint.Projector projector, final PartialTickSupplier partialTickSupplier
77
	);
78
79
	public abstract double distanceSquared(final Entity fromEntity);
80
81
	public Waypoint.Icon icon() {
82
		return this.icon;
83
	}
84
85
	private static class AzimuthWaypoint extends TrackedWaypoint {
86
		private float angle;
87
88
		public AzimuthWaypoint(final UUID identifier, final Waypoint.Icon icon, final float angle) {
89
			super(Either.left(identifier), icon, TrackedWaypoint.Type.AZIMUTH);
90
			this.angle = angle;
91
		}
92
93
		public AzimuthWaypoint(final Either<UUID, String> identifier, final Waypoint.Icon icon, final FriendlyByteBuf byteBuf) {
94
			super(identifier, icon, TrackedWaypoint.Type.AZIMUTH);
95
			this.angle = byteBuf.readFloat();
96
		}
97
98
		@Override
99
		public void update(final TrackedWaypoint other) {
100
			if (other instanceof TrackedWaypoint.AzimuthWaypoint azimuthWaypoint) {
101
				this.angle = azimuthWaypoint.angle;
102
			} else {
103
				TrackedWaypoint.LOGGER.warn("Unsupported Waypoint update operation: {}", other.getClass());
104
			}
105
		}
106
107
		@Override
108
		public void writeContents(final ByteBuf buf) {
109
			buf.writeFloat(this.angle);
110
		}
111
112
		@Override
113
		public double yawAngleToCamera(final Level level, final TrackedWaypoint.Camera camera, final PartialTickSupplier partialTickSupplier) {
114
			return Mth.degreesDifference(camera.yaw(), this.angle * (180.0F / (float)Math.PI));
115
		}
116
117
		@Override
118
		public TrackedWaypoint.PitchDirection pitchDirectionToCamera(
119
			final Level level, final TrackedWaypoint.Projector projector, final PartialTickSupplier partialTickSupplier
120
		) {
121
			double horizon = projector.projectHorizonToScreen();
122
			if (horizon < -1.0) {
123
				return TrackedWaypoint.PitchDirection.DOWN;
124
			} else {
125
				return horizon > 1.0 ? TrackedWaypoint.PitchDirection.UP : TrackedWaypoint.PitchDirection.NONE;
126
			}
127
		}
128
129
		@Override
130
		public double distanceSquared(final Entity fromEntity) {
131
			return Double.POSITIVE_INFINITY;
132
		}
133
	}
134
135
	public interface Camera {
136
		float yaw();
137
138
		Vec3 position();
139
	}
140
141
	private static class ChunkWaypoint extends TrackedWaypoint {
142
		private ChunkPos chunkPos;
143
144
		public ChunkWaypoint(final UUID identifier, final Waypoint.Icon icon, final ChunkPos chunkPos) {
145
			super(Either.left(identifier), icon, TrackedWaypoint.Type.CHUNK);
146
			this.chunkPos = chunkPos;
147
		}
148
149
		public ChunkWaypoint(final Either<UUID, String> identifier, final Waypoint.Icon icon, final FriendlyByteBuf byteBuf) {
150
			super(identifier, icon, TrackedWaypoint.Type.CHUNK);
151
			this.chunkPos = new ChunkPos(byteBuf.readVarInt(), byteBuf.readVarInt());
152
		}
153
154
		@Override
155
		public void update(final TrackedWaypoint other) {
156
			if (other instanceof TrackedWaypoint.ChunkWaypoint chunkWaypoint) {
157
				this.chunkPos = chunkWaypoint.chunkPos;
158
			} else {
159
				TrackedWaypoint.LOGGER.warn("Unsupported Waypoint update operation: {}", other.getClass());
160
			}
161
		}
162
163
		@Override
164
		public void writeContents(final ByteBuf buf) {
165
			VarInt.write(buf, this.chunkPos.x());
166
			VarInt.write(buf, this.chunkPos.z());
167
		}
168
169
		private Vec3 position(final double positionY) {
170
			return Vec3.atCenterOf(this.chunkPos.getMiddleBlockPosition((int)positionY));
171
		}
172
173
		@Override
174
		public double yawAngleToCamera(final Level level, final TrackedWaypoint.Camera camera, final PartialTickSupplier partialTickSupplier) {
175
			Vec3 cameraPosition = camera.position();
176
			Vec3 direction = cameraPosition.subtract(this.position(cameraPosition.y())).rotateClockwise90();
177
			float waypointAngle = (float)Mth.atan2(direction.z(), direction.x()) * (180.0F / (float)Math.PI);
178
			return Mth.degreesDifference(camera.yaw(), waypointAngle);
179
		}
180
181
		@Override
182
		public TrackedWaypoint.PitchDirection pitchDirectionToCamera(
183
			final Level level, final TrackedWaypoint.Projector projector, final PartialTickSupplier partialTickSupplier
184
		) {
185
			double onScreenHorizon = projector.projectHorizonToScreen();
186
			if (onScreenHorizon < -1.0) {
187
				return TrackedWaypoint.PitchDirection.DOWN;
188
			} else {
189
				return onScreenHorizon > 1.0 ? TrackedWaypoint.PitchDirection.UP : TrackedWaypoint.PitchDirection.NONE;
190
			}
191
		}
192
193
		@Override
194
		public double distanceSquared(final Entity fromEntity) {
195
			return fromEntity.distanceToSqr(Vec3.atCenterOf(this.chunkPos.getMiddleBlockPosition(fromEntity.getBlockY())));
196
		}
197
	}
198
199
	private static class EmptyWaypoint extends TrackedWaypoint {
200
		private EmptyWaypoint(final Either<UUID, String> identifier, final Waypoint.Icon icon, final FriendlyByteBuf byteBuf) {
201
			super(identifier, icon, TrackedWaypoint.Type.EMPTY);
202
		}
203
204
		private EmptyWaypoint(final UUID identifier) {
205
			super(Either.left(identifier), Waypoint.Icon.NULL, TrackedWaypoint.Type.EMPTY);
206
		}
207
208
		@Override
209
		public void update(final TrackedWaypoint other) {
210
		}
211
212
		@Override
213
		public void writeContents(final ByteBuf buf) {
214
		}
215
216
		@Override
217
		public double yawAngleToCamera(final Level level, final TrackedWaypoint.Camera camera, final PartialTickSupplier partialTickSupplier) {
218
			return Double.NaN;
219
		}
220
221
		@Override
222
		public TrackedWaypoint.PitchDirection pitchDirectionToCamera(
223
			final Level level, final TrackedWaypoint.Projector projector, final PartialTickSupplier partialTickSupplier
224
		) {
225
			return TrackedWaypoint.PitchDirection.NONE;
226
		}
227
228
		@Override
229
		public double distanceSquared(final Entity fromEntity) {
230
			return Double.POSITIVE_INFINITY;
231
		}
232
	}
233
234
	public enum PitchDirection {
235
		NONE,
236
		UP,
237
		DOWN;
238
	}
239
240
	public interface Projector {
241
		Vec3 projectPointToScreen(final Vec3 point);
242
243
		double projectHorizonToScreen();
244
	}
245
246
	private enum Type {
247
		EMPTY(TrackedWaypoint.EmptyWaypoint::new),
248
		VEC3I(TrackedWaypoint.Vec3iWaypoint::new),
249
		CHUNK(TrackedWaypoint.ChunkWaypoint::new),
250
		AZIMUTH(TrackedWaypoint.AzimuthWaypoint::new);
251
252
		private final TriFunction<Either<UUID, String>, Waypoint.Icon, FriendlyByteBuf, TrackedWaypoint> constructor;
253
254
		Type(final TriFunction<Either<UUID, String>, Waypoint.Icon, FriendlyByteBuf, TrackedWaypoint> constructor) {
255
			this.constructor = constructor;
256
		}
257
	}
258
259
	private static class Vec3iWaypoint extends TrackedWaypoint {
260
		private Vec3i vector;
261
262
		public Vec3iWaypoint(final UUID identifier, final Waypoint.Icon icon, final Vec3i vector) {
263
			super(Either.left(identifier), icon, TrackedWaypoint.Type.VEC3I);
264
			this.vector = vector;
265
		}
266
267
		public Vec3iWaypoint(final Either<UUID, String> identifier, final Waypoint.Icon icon, final FriendlyByteBuf byteBuf) {
268
			super(identifier, icon, TrackedWaypoint.Type.VEC3I);
269
			this.vector = new Vec3i(byteBuf.readVarInt(), byteBuf.readVarInt(), byteBuf.readVarInt());
270
		}
271
272
		@Override
273
		public void update(final TrackedWaypoint other) {
274
			if (other instanceof TrackedWaypoint.Vec3iWaypoint vec3iWaypoint) {
275
				this.vector = vec3iWaypoint.vector;
276
			} else {
277
				TrackedWaypoint.LOGGER.warn("Unsupported Waypoint update operation: {}", other.getClass());
278
			}
279
		}
280
281
		@Override
282
		public void writeContents(final ByteBuf buf) {
283
			VarInt.write(buf, this.vector.getX());
284
			VarInt.write(buf, this.vector.getY());
285
			VarInt.write(buf, this.vector.getZ());
286
		}
287
288
		private Vec3 position(final Level level, final PartialTickSupplier partialTick) {
289
			return this.identifier
290
				.left()
291
				.map(level::getEntity)
292
				.map(e -> e.blockPosition().distManhattan(this.vector) > 3 ? null : e.getEyePosition(partialTick.apply(e)))
293
				.orElseGet(() -> Vec3.atCenterOf(this.vector));
294
		}
295
296
		@Override
297
		public double yawAngleToCamera(final Level level, final TrackedWaypoint.Camera camera, final PartialTickSupplier partialTickSupplier) {
298
			Vec3 direction = camera.position().subtract(this.position(level, partialTickSupplier)).rotateClockwise90();
299
			float waypointAngle = (float)Mth.atan2(direction.z(), direction.x()) * (180.0F / (float)Math.PI);
300
			return Mth.degreesDifference(camera.yaw(), waypointAngle);
301
		}
302
303
		@Override
304
		public TrackedWaypoint.PitchDirection pitchDirectionToCamera(
305
			final Level level, final TrackedWaypoint.Projector projector, final PartialTickSupplier partialTickSupplier
306
		) {
307
			Vec3 pointOnScreen = projector.projectPointToScreen(this.position(level, partialTickSupplier));
308
			boolean isBehindCamera = pointOnScreen.z > 1.0;
309
			double yInFrontOfCamera = isBehindCamera ? -pointOnScreen.y : pointOnScreen.y;
310
			if (yInFrontOfCamera < -1.0) {
311
				return TrackedWaypoint.PitchDirection.DOWN;
312
			}
313
314
			if (yInFrontOfCamera > 1.0) {
315
				return TrackedWaypoint.PitchDirection.UP;
316
			}
317
318
			if (isBehindCamera) {
319
				if (pointOnScreen.y > 0.0) {
320
					return TrackedWaypoint.PitchDirection.UP;
321
				}
322
323
				if (pointOnScreen.y < 0.0) {
324
					return TrackedWaypoint.PitchDirection.DOWN;
325
				}
326
			}
327
328
			return TrackedWaypoint.PitchDirection.NONE;
329
		}
330
331
		@Override
332
		public double distanceSquared(final Entity fromEntity) {
333
			return fromEntity.distanceToSqr(Vec3.atCenterOf(this.vector));
334
		}
335
	}
336
}
337

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