Rotation arguments
The RotationArgument
allows users to specify a pair of pitch and yaw coordinates. By default (using the ~
symbol), this refers to the player's current pitch and yaw of where they are looking at.
The RotationArgument
class returns a Rotation
object, which consists of the following methods:
Method name | What it does |
---|---|
float getPitch() | Returns a player's pitch (up and down rotation) |
float getYaw() | Returns a player's yaw (left and right rotation) |
float getNormalizedPitch() | Returns a player's pitch between -90 and 90 degrees |
float getNormalizedYaw() | Returns a player's yaw between -180 and 180 degrees |
Example: Rotate an armor stand head
Say we want to make an armor stand look in a certain direction. To do this, we'll use the following command:
/rotate <rotation> <target>
To do this, we'll use the rotation from the RotationArgument
and select an entity using the EntitySelectorArgument.OneEntity
class. We then check if our entity is an armor stand and if so, we set its head pose to the given rotation.
new CommandAPICommand("rotate")
.withArguments(new RotationArgument("rotation"))
.withArguments(new EntitySelectorArgument.OneEntity("target"))
.executes((sender, args) -> {
Rotation rotation = (Rotation) args.get("rotation");
Entity target = (Entity) args.get("target");
if (target instanceof ArmorStand armorStand) {
armorStand.setHeadPose(new EulerAngle(Math.toRadians(rotation.getPitch()), Math.toRadians(rotation.getYaw() - 90), 0));
}
})
.register();
CommandAPICommand("rotate")
.withArguments(RotationArgument("rotation"))
.withArguments(EntitySelectorArgument.OneEntity("target"))
.executes(CommandExecutor { _, args ->
val rotation = args["rotation"] as Rotation
val target = args["target"] as Entity
if (target is ArmorStand) {
target.headPose = EulerAngle(Math.toRadians(rotation.pitch.toDouble()), Math.toRadians(rotation.yaw.toDouble() - 90), 0.0)
}
})
.register()
commandAPICommand("rotate") {
rotationArgument("rotation")
entitySelectorArgumentOneEntity("target")
anyExecutor { _, args ->
val rotation = args["rotation"] as Rotation
val target = args["target"] as Entity
if (target is ArmorStand) {
target.headPose = EulerAngle(Math.toRadians(rotation.pitch.toDouble()), Math.toRadians(rotation.yaw.toDouble() - 90), 0.0)
}
}
}
Note how the head pose requires an EulerAngle
as opposed to a pitch and yaw. To account for this, we convert our rotation (which is in degrees) into an EulerAngle
in radians.