TeleportCommand .java

net.minecraft.server.commands.TeleportCommand (class): members: RESPONSE_TELEPORT_TO_ENTITY, RESPONSE_TELEPORT_TO_POS, INVALID_POSITION, victim, relatives.

net.minecraft.server.commandsclass244 linesserver9.8 KB

API Surface (1)

void register(final CommandDispatcher<CommandSourceStack> dispatcher)

Fields & Constants (15)

INVALID_POSITION
RESPONSE_TELEPORT_TO_ENTITY
RESPONSE_TELEPORT_TO_POS
source
entities
level
destination
rotation
victim
x
y
z
relatives
yRot
xRot

String literals (15)

"commands.teleport.invalidPosition"
"commands.teleport.success.entity.single"
"commands.teleport.success.entity.multiple"
"commands.teleport.success.location.single"
"commands.teleport.success.location.multiple"
"teleport"
"location"
"destination"
"targets"
"rotation"
"facing"
"entity"
"facingEntity"
"facingAnchor"
"facingLocation"

Full Source

TeleportCommand.java244 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.commands;
2
3
import com.mojang.brigadier.CommandDispatcher;
4
import com.mojang.brigadier.exceptions.CommandSyntaxException;
5
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
6
import com.mojang.brigadier.tree.LiteralCommandNode;
7
import java.util.Collection;
8
import java.util.Collections;
9
import java.util.EnumSet;
10
import java.util.Locale;
11
import java.util.Set;
12
import net.minecraft.commands.CommandSourceStack;
13
import net.minecraft.commands.Commands;
14
import net.minecraft.commands.arguments.EntityAnchorArgument;
15
import net.minecraft.commands.arguments.EntityArgument;
16
import net.minecraft.commands.arguments.coordinates.Coordinates;
17
import net.minecraft.commands.arguments.coordinates.RotationArgument;
18
import net.minecraft.commands.arguments.coordinates.Vec3Argument;
19
import net.minecraft.core.BlockPos;
20
import net.minecraft.network.chat.Component;
21
import net.minecraft.server.level.ServerLevel;
22
import net.minecraft.util.Mth;
23
import net.minecraft.world.entity.Entity;
24
import net.minecraft.world.entity.LivingEntity;
25
import net.minecraft.world.entity.PathfinderMob;
26
import net.minecraft.world.entity.Relative;
27
import net.minecraft.world.level.Level;
28
import net.minecraft.world.phys.Vec2;
29
import net.minecraft.world.phys.Vec3;
30
import org.jspecify.annotations.Nullable;
31
32
public class TeleportCommand {
33
	private static final SimpleCommandExceptionType INVALID_POSITION = new SimpleCommandExceptionType(Component.translatable("commands.teleport.invalidPosition"));
34
	private static final CommandResponseTracker.MessagesWithArg<Entity, Entity> RESPONSE_TELEPORT_TO_ENTITY = CommandResponseTracker.messages(
35
		(entity, var1, destination) -> Component.translatable("commands.teleport.success.entity.single", entity.getDisplayName(), destination.getDisplayName()),
36
		(elementCount, var1, destination) -> Component.translatable("commands.teleport.success.entity.multiple", elementCount, destination.getDisplayName())
37
	);
38
	private static final CommandResponseTracker.MessagesWithArg<Entity, Vec3> RESPONSE_TELEPORT_TO_POS = CommandResponseTracker.messages(
39
		(entity, var1, pos) -> Component.translatable(
40
			"commands.teleport.success.location.single", entity.getDisplayName(), formatDouble(pos.x), formatDouble(pos.y), formatDouble(pos.z)
41
		),
42
		(elementCount, var1, pos) -> Component.translatable(
43
			"commands.teleport.success.location.multiple", elementCount, formatDouble(pos.x), formatDouble(pos.y), formatDouble(pos.z)
44
		)
45
	);
46
47
	public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
48
		LiteralCommandNode<CommandSourceStack> teleport = dispatcher.register(
49
			Commands.literal("teleport")
50
				.requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
51
				.then(
52
					Commands.argument("location", Vec3Argument.vec3())
53
						.executes(
54
							c -> teleportToPos(
55
								c.getSource(),
56
								Collections.singleton(c.getSource().getEntityOrException()),
57
								c.getSource().getLevel(),
58
								Vec3Argument.getCoordinates(c, "location"),
59
								null,
60
								null
61
							)
62
						)
63
				)
64
				.then(
65
					Commands.argument("destination", EntityArgument.entity())
66
						.executes(c -> teleportToEntity(c.getSource(), Collections.singleton(c.getSource().getEntityOrException()), EntityArgument.getEntity(c, "destination")))
67
				)
68
				.then(
69
					Commands.argument("targets", EntityArgument.entities())
70
						.then(
71
							Commands.argument("location", Vec3Argument.vec3())
72
								.executes(
73
									c -> teleportToPos(
74
										c.getSource(), EntityArgument.getEntities(c, "targets"), c.getSource().getLevel(), Vec3Argument.getCoordinates(c, "location"), null, null
75
									)
76
								)
77
								.then(
78
									Commands.argument("rotation", RotationArgument.rotation())
79
										.executes(
80
											c -> teleportToPos(
81
												c.getSource(),
82
												EntityArgument.getEntities(c, "targets"),
83
												c.getSource().getLevel(),
84
												Vec3Argument.getCoordinates(c, "location"),
85
												RotationArgument.getRotation(c, "rotation"),
86
												null
87
											)
88
										)
89
								)
90
								.then(
91
									Commands.literal("facing")
92
										.then(
93
											Commands.literal("entity")
94
												.then(
95
													Commands.argument("facingEntity", EntityArgument.entity())
96
														.executes(
97
															c -> teleportToPos(
98
																c.getSource(),
99
																EntityArgument.getEntities(c, "targets"),
100
																c.getSource().getLevel(),
101
																Vec3Argument.getCoordinates(c, "location"),
102
																null,
103
																new LookAt.LookAtEntity(EntityArgument.getEntity(c, "facingEntity"), EntityAnchorArgument.Anchor.FEET)
104
															)
105
														)
106
														.then(
107
															Commands.argument("facingAnchor", EntityAnchorArgument.anchor())
108
																.executes(
109
																	c -> teleportToPos(
110
																		c.getSource(),
111
																		EntityArgument.getEntities(c, "targets"),
112
																		c.getSource().getLevel(),
113
																		Vec3Argument.getCoordinates(c, "location"),
114
																		null,
115
																		new LookAt.LookAtEntity(EntityArgument.getEntity(c, "facingEntity"), EntityAnchorArgument.getAnchor(c, "facingAnchor"))
116
																	)
117
																)
118
														)
119
												)
120
										)
121
										.then(
122
											Commands.argument("facingLocation", Vec3Argument.vec3())
123
												.executes(
124
													c -> teleportToPos(
125
														c.getSource(),
126
														EntityArgument.getEntities(c, "targets"),
127
														c.getSource().getLevel(),
128
														Vec3Argument.getCoordinates(c, "location"),
129
														null,
130
														new LookAt.LookAtPosition(Vec3Argument.getVec3(c, "facingLocation"))
131
													)
132
												)
133
										)
134
								)
135
						)
136
						.then(
137
							Commands.argument("destination", EntityArgument.entity())
138
								.executes(c -> teleportToEntity(c.getSource(), EntityArgument.getEntities(c, "targets"), EntityArgument.getEntity(c, "destination")))
139
						)
140
				)
141
		);
142
		dispatcher.register(Commands.literal("tp").requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS)).redirect(teleport));
143
	}
144
145
	private static int teleportToEntity(final CommandSourceStack source, final Collection<? extends Entity> entities, final Entity destination) throws CommandSyntaxException {
146
		CommandResponseTracker<Entity> tracker = CommandResponseTracker.create();
147
148
		for (Entity entity : entities) {
149
			performTeleport(
150
				source,
151
				entity,
152
				(ServerLevel)destination.level(),
153
				destination.getX(),
154
				destination.getY(),
155
				destination.getZ(),
156
				EnumSet.noneOf(Relative.class),
157
				destination.getYRot(),
158
				destination.getXRot(),
159
				null
160
			);
161
			tracker.track(entity);
162
		}
163
164
		return tracker.sendFeedback(source, true, RESPONSE_TELEPORT_TO_ENTITY, destination);
165
	}
166
167
	private static int teleportToPos(
168
		final CommandSourceStack source,
169
		final Collection<? extends Entity> entities,
170
		final ServerLevel level,
171
		final Coordinates destination,
172
		final @Nullable Coordinates rotation,
173
		final @Nullable LookAt lookAt
174
	) throws CommandSyntaxException {
175
		CommandResponseTracker<Entity> tracker = CommandResponseTracker.create();
176
		Vec3 pos = destination.getPosition(source);
177
		Vec2 rot = rotation == null ? null : rotation.getRotation(source);
178
179
		for (Entity entity : entities) {
180
			Set<Relative> relatives = getRelatives(destination, rotation, entity.level().dimension() == level.dimension());
181
			if (rot == null) {
182
				performTeleport(source, entity, level, pos.x, pos.y, pos.z, relatives, entity.getYRot(), entity.getXRot(), lookAt);
183
			} else {
184
				performTeleport(source, entity, level, pos.x, pos.y, pos.z, relatives, rot.y, rot.x, lookAt);
185
			}
186
187
			tracker.track(entity);
188
		}
189
190
		return tracker.sendFeedback(source, true, RESPONSE_TELEPORT_TO_POS, pos);
191
	}
192
193
	private static Set<Relative> getRelatives(final Coordinates destination, final @Nullable Coordinates rotation, final boolean sameDimension) {
194
		Set<Relative> dir = Relative.direction(destination.isXRelative(), destination.isYRelative(), destination.isZRelative());
195
		Set<Relative> pos = sameDimension ? Relative.position(destination.isXRelative(), destination.isYRelative(), destination.isZRelative()) : Set.of();
196
		Set<Relative> rot = rotation == null ? Relative.ROTATION : Relative.rotation(rotation.isYRelative(), rotation.isXRelative());
197
		return Relative.union(dir, pos, rot);
198
	}
199
200
	private static String formatDouble(final double value) {
201
		return String.format(Locale.ROOT, "%f", value);
202
	}
203
204
	private static void performTeleport(
205
		final CommandSourceStack source,
206
		final Entity victim,
207
		final ServerLevel level,
208
		final double x,
209
		final double y,
210
		final double z,
211
		final Set<Relative> relatives,
212
		final float yRot,
213
		final float xRot,
214
		final @Nullable LookAt lookAt
215
	) throws CommandSyntaxException {
216
		BlockPos blockPos = BlockPos.containing(x, y, z);
217
		if (!Level.isInSpawnableBounds(blockPos)) {
218
			throw INVALID_POSITION.create();
219
		}
220
221
		double relativeOrAbsoluteX = relatives.contains(Relative.X) ? x - victim.getX() : x;
222
		double relativeOrAbsoluteY = relatives.contains(Relative.Y) ? y - victim.getY() : y;
223
		double relativeOrAbsoluteZ = relatives.contains(Relative.Z) ? z - victim.getZ() : z;
224
		float relativeOrAbsoluteYRot = relatives.contains(Relative.Y_ROT) ? yRot - victim.getYRot() : yRot;
225
		float relativeOrAbsoluteXRot = relatives.contains(Relative.X_ROT) ? xRot - victim.getXRot() : xRot;
226
		float newYRot = Mth.wrapDegrees(relativeOrAbsoluteYRot);
227
		float newXRot = Mth.wrapDegrees(relativeOrAbsoluteXRot);
228
		if (victim.teleportTo(level, relativeOrAbsoluteX, relativeOrAbsoluteY, relativeOrAbsoluteZ, relatives, newYRot, newXRot, true)) {
229
			if (lookAt != null) {
230
				lookAt.perform(source, victim);
231
			}
232
233
			if (!(victim instanceof LivingEntity living && living.isFallFlying())) {
234
				victim.setDeltaMovement(victim.getDeltaMovement().multiply(1.0, 0.0, 1.0));
235
				victim.setOnGround(true);
236
			}
237
238
			if (victim instanceof PathfinderMob mob) {
239
				mob.getNavigation().stop();
240
			}
241
		}
242
	}
243
}
244

Decompiled Minecraft 26.3 source of TeleportCommand (net/minecraft/server/commands/TeleportCommand.java) with Mojang official mappings, stored verbatim on the site.