| 1 | package net.minecraft.commands.arguments.coordinates;
|
| 2 |
|
| 3 | import com.mojang.brigadier.StringReader;
|
| 4 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 5 | import net.minecraft.commands.CommandSourceStack;
|
| 6 | import net.minecraft.util.Mth;
|
| 7 | import net.minecraft.world.phys.Vec2;
|
| 8 | import net.minecraft.world.phys.Vec3;
|
| 9 |
|
| 10 | public record LocalCoordinates(double left, double up, double forwards) implements Coordinates {
|
| 11 | public static final char PREFIX_LOCAL_COORDINATE = '^';
|
| 12 |
|
| 13 | @Override
|
| 14 | public Vec3 getPosition(final CommandSourceStack sender) {
|
| 15 | Vec3 source = sender.getAnchor().apply(sender);
|
| 16 | return this.apply(source, sender.getRotation());
|
| 17 | }
|
| 18 |
|
| 19 | public Vec3 apply(final Vec3 source, final Vec2 rotation) {
|
| 20 | float yCos = Mth.cos((rotation.y + 90.0F) * (float) (Math.PI / 180.0));
|
| 21 | float ySin = Mth.sin((rotation.y + 90.0F) * (float) (Math.PI / 180.0));
|
| 22 | float xCos = Mth.cos(-rotation.x * (float) (Math.PI / 180.0));
|
| 23 | float xSin = Mth.sin(-rotation.x * (float) (Math.PI / 180.0));
|
| 24 | float xCosUp = Mth.cos((-rotation.x + 90.0F) * (float) (Math.PI / 180.0));
|
| 25 | float xSinUp = Mth.sin((-rotation.x + 90.0F) * (float) (Math.PI / 180.0));
|
| 26 | Vec3 forwards = new Vec3(yCos * xCos, xSin, ySin * xCos);
|
| 27 | Vec3 up = new Vec3(yCos * xCosUp, xSinUp, ySin * xCosUp);
|
| 28 | Vec3 left = forwards.cross(up).scale(-1.0);
|
| 29 | return source.add(
|
| 30 | forwards.x * this.forwards + up.x * this.up + left.x * this.left,
|
| 31 | forwards.y * this.forwards + up.y * this.up + left.y * this.left,
|
| 32 | forwards.z * this.forwards + up.z * this.up + left.z * this.left
|
| 33 | );
|
| 34 | }
|
| 35 |
|
| 36 | @Override
|
| 37 | public Vec2 getRotation(final CommandSourceStack sender) {
|
| 38 | return Vec2.ZERO;
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | public boolean isXRelative() {
|
| 43 | return true;
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | public boolean isYRelative() {
|
| 48 | return true;
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | public boolean isZRelative() {
|
| 53 | return true;
|
| 54 | }
|
| 55 |
|
| 56 | public static LocalCoordinates parse(final StringReader reader) throws CommandSyntaxException {
|
| 57 | int start = reader.getCursor();
|
| 58 | double left = readDouble(reader, start);
|
| 59 | if (reader.canRead() && reader.peek() == ' ') {
|
| 60 | reader.skip();
|
| 61 | double up = readDouble(reader, start);
|
| 62 | if (reader.canRead() && reader.peek() == ' ') {
|
| 63 | reader.skip();
|
| 64 | double forwards = readDouble(reader, start);
|
| 65 | return new LocalCoordinates(left, up, forwards);
|
| 66 | } else {
|
| 67 | reader.setCursor(start);
|
| 68 | throw Vec3Argument.ERROR_NOT_COMPLETE.createWithContext(reader);
|
| 69 | }
|
| 70 | } else {
|
| 71 | reader.setCursor(start);
|
| 72 | throw Vec3Argument.ERROR_NOT_COMPLETE.createWithContext(reader);
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | private static double readDouble(final StringReader reader, final int start) throws CommandSyntaxException {
|
| 77 | if (!reader.canRead()) {
|
| 78 | throw WorldCoordinate.ERROR_EXPECTED_DOUBLE.createWithContext(reader);
|
| 79 | } else if (reader.peek() != '^') {
|
| 80 | reader.setCursor(start);
|
| 81 | throw Vec3Argument.ERROR_MIXED_TYPE.createWithContext(reader);
|
| 82 | } else {
|
| 83 | reader.skip();
|
| 84 | return reader.canRead() && reader.peek() != ' ' ? reader.readDouble() : 0.0;
|
| 85 | }
|
| 86 | }
|
| 87 | }
|
| 88 |
|