| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.CommandDispatcher;
|
| 4 | import net.minecraft.commands.CommandSourceStack;
|
| 5 | import net.minecraft.commands.Commands;
|
| 6 | import net.minecraft.commands.arguments.EntityAnchorArgument;
|
| 7 | import net.minecraft.commands.arguments.EntityArgument;
|
| 8 | import net.minecraft.commands.arguments.coordinates.Coordinates;
|
| 9 | import net.minecraft.commands.arguments.coordinates.RotationArgument;
|
| 10 | import net.minecraft.commands.arguments.coordinates.Vec3Argument;
|
| 11 | import net.minecraft.network.chat.Component;
|
| 12 | import net.minecraft.world.entity.Entity;
|
| 13 | import net.minecraft.world.phys.Vec2;
|
| 14 |
|
| 15 | public class RotateCommand {
|
| 16 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
|
| 17 | dispatcher.register(
|
| 18 | Commands.literal("rotate")
|
| 19 | .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
|
| 20 | .then(
|
| 21 | Commands.argument("target", EntityArgument.entity())
|
| 22 | .then(
|
| 23 | Commands.argument("rotation", RotationArgument.rotation())
|
| 24 | .executes(c -> rotate(c.getSource(), EntityArgument.getEntity(c, "target"), RotationArgument.getRotation(c, "rotation")))
|
| 25 | )
|
| 26 | .then(
|
| 27 | Commands.literal("facing")
|
| 28 | .then(
|
| 29 | Commands.literal("entity")
|
| 30 | .then(
|
| 31 | Commands.argument("facingEntity", EntityArgument.entity())
|
| 32 | .executes(
|
| 33 | c -> rotate(
|
| 34 | c.getSource(),
|
| 35 | EntityArgument.getEntity(c, "target"),
|
| 36 | new LookAt.LookAtEntity(EntityArgument.getEntity(c, "facingEntity"), EntityAnchorArgument.Anchor.FEET)
|
| 37 | )
|
| 38 | )
|
| 39 | .then(
|
| 40 | Commands.argument("facingAnchor", EntityAnchorArgument.anchor())
|
| 41 | .executes(
|
| 42 | c -> rotate(
|
| 43 | c.getSource(),
|
| 44 | EntityArgument.getEntity(c, "target"),
|
| 45 | new LookAt.LookAtEntity(EntityArgument.getEntity(c, "facingEntity"), EntityAnchorArgument.getAnchor(c, "facingAnchor"))
|
| 46 | )
|
| 47 | )
|
| 48 | )
|
| 49 | )
|
| 50 | )
|
| 51 | .then(
|
| 52 | Commands.argument("facingLocation", Vec3Argument.vec3())
|
| 53 | .executes(c -> rotate(c.getSource(), EntityArgument.getEntity(c, "target"), new LookAt.LookAtPosition(Vec3Argument.getVec3(c, "facingLocation"))))
|
| 54 | )
|
| 55 | )
|
| 56 | )
|
| 57 | );
|
| 58 | }
|
| 59 |
|
| 60 | private static int rotate(final CommandSourceStack source, final Entity entity, final Coordinates rotation) {
|
| 61 | Vec2 rot = rotation.getRotation(source);
|
| 62 | float relativeOrAbsoluteYRot = rotation.isYRelative() ? rot.y - entity.getYRot() : rot.y;
|
| 63 | float relativeOrAbsoluteXRot = rotation.isXRelative() ? rot.x - entity.getXRot() : rot.x;
|
| 64 | entity.forceSetRotation(relativeOrAbsoluteYRot, rotation.isYRelative(), relativeOrAbsoluteXRot, rotation.isXRelative());
|
| 65 | source.sendSuccess(() -> Component.translatable("commands.rotate.success", entity.getDisplayName()), true);
|
| 66 | return 1;
|
| 67 | }
|
| 68 |
|
| 69 | private static int rotate(final CommandSourceStack source, final Entity entity, final LookAt facing) {
|
| 70 | facing.perform(source, entity);
|
| 71 | source.sendSuccess(() -> Component.translatable("commands.rotate.success", entity.getDisplayName()), true);
|
| 72 | return 1;
|
| 73 | }
|
| 74 | }
|
| 75 |
|