|
| 1 | +package com.plusls.ommc.api.command; |
| 2 | + |
| 3 | +import com.mojang.brigadier.StringReader; |
| 4 | +import com.mojang.brigadier.exceptions.CommandSyntaxException; |
| 5 | +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; |
| 6 | +import net.minecraft.commands.arguments.coordinates.Vec3Argument; |
| 7 | +import net.minecraft.commands.arguments.coordinates.WorldCoordinate; |
| 8 | +import net.minecraft.util.Mth; |
| 9 | +import net.minecraft.world.phys.Vec2; |
| 10 | +import net.minecraft.world.phys.Vec3; |
| 11 | +import org.jetbrains.annotations.Contract; |
| 12 | +import org.jetbrains.annotations.NotNull; |
| 13 | + |
| 14 | +import java.util.Objects; |
| 15 | + |
| 16 | +// Modified from brigadier |
| 17 | +public class ClientLocalCoordinates implements ClientCoordinates { |
| 18 | + private final double left; |
| 19 | + private final double up; |
| 20 | + private final double forwards; |
| 21 | + |
| 22 | + public ClientLocalCoordinates(double left, double up, double forwards) { |
| 23 | + this.left = left; |
| 24 | + this.up = up; |
| 25 | + this.forwards = forwards; |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public Vec3 getPosition(@NotNull FabricClientCommandSource commandSourceStack) { |
| 30 | + Vec2 rotation = commandSourceStack.getRotation(); |
| 31 | + Vec3 pos = ClientEntityAnchorArgument.Anchor.FEET.apply(commandSourceStack); |
| 32 | + float f = Mth.cos((rotation.y + 90.0F) * (float) (Math.PI / 180.0)); |
| 33 | + float g = Mth.sin((rotation.y + 90.0F) * (float) (Math.PI / 180.0)); |
| 34 | + float h = Mth.cos(-rotation.x * (float) (Math.PI / 180.0)); |
| 35 | + float i = Mth.sin(-rotation.x * (float) (Math.PI / 180.0)); |
| 36 | + float j = Mth.cos((-rotation.x + 90.0F) * (float) (Math.PI / 180.0)); |
| 37 | + float k = Mth.sin((-rotation.x + 90.0F) * (float) (Math.PI / 180.0)); |
| 38 | + Vec3 vec32 = new Vec3(f * h, i, g * h); |
| 39 | + Vec3 vec33 = new Vec3(f * j, k, g * j); |
| 40 | + Vec3 vec34 = vec32.cross(vec33).scale(-1.0); |
| 41 | + double d = vec32.x * this.forwards + vec33.x * this.up + vec34.x * this.left; |
| 42 | + double e = vec32.y * this.forwards + vec33.y * this.up + vec34.y * this.left; |
| 43 | + double l = vec32.z * this.forwards + vec33.z * this.up + vec34.z * this.left; |
| 44 | + return new Vec3(pos.x + d, pos.y + e, pos.z + l); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public Vec2 getRotation(FabricClientCommandSource commandSourceStack) { |
| 49 | + return Vec2.ZERO; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public boolean isXRelative() { |
| 54 | + return true; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public boolean isYRelative() { |
| 59 | + return true; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public boolean isZRelative() { |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + @Contract("_ -> new") |
| 68 | + public static @NotNull ClientLocalCoordinates parse(@NotNull StringReader stringReader) throws CommandSyntaxException { |
| 69 | + int i = stringReader.getCursor(); |
| 70 | + double d = readDouble(stringReader, i); |
| 71 | + if (stringReader.canRead() && stringReader.peek() == ' ') { |
| 72 | + stringReader.skip(); |
| 73 | + double e = readDouble(stringReader, i); |
| 74 | + if (stringReader.canRead() && stringReader.peek() == ' ') { |
| 75 | + stringReader.skip(); |
| 76 | + double f = readDouble(stringReader, i); |
| 77 | + return new ClientLocalCoordinates(d, e, f); |
| 78 | + } else { |
| 79 | + stringReader.setCursor(i); |
| 80 | + throw Vec3Argument.ERROR_NOT_COMPLETE.createWithContext(stringReader); |
| 81 | + } |
| 82 | + } else { |
| 83 | + stringReader.setCursor(i); |
| 84 | + throw Vec3Argument.ERROR_NOT_COMPLETE.createWithContext(stringReader); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private static double readDouble(@NotNull StringReader stringReader, int i) throws CommandSyntaxException { |
| 89 | + if (!stringReader.canRead()) { |
| 90 | + throw WorldCoordinate.ERROR_EXPECTED_DOUBLE.createWithContext(stringReader); |
| 91 | + } else if (stringReader.peek() != '^') { |
| 92 | + stringReader.setCursor(i); |
| 93 | + throw Vec3Argument.ERROR_MIXED_TYPE.createWithContext(stringReader); |
| 94 | + } else { |
| 95 | + stringReader.skip(); |
| 96 | + return stringReader.canRead() && stringReader.peek() != ' ' ? stringReader.readDouble() : 0.0; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public boolean equals(Object object) { |
| 102 | + if (this == object) { |
| 103 | + return true; |
| 104 | + } else if (!(object instanceof ClientLocalCoordinates)) { |
| 105 | + return false; |
| 106 | + } else { |
| 107 | + ClientLocalCoordinates clientLocalCoordinates = (ClientLocalCoordinates) object; |
| 108 | + return this.left == clientLocalCoordinates.left && this.up == clientLocalCoordinates.up && this.forwards == clientLocalCoordinates.forwards; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public int hashCode() { |
| 114 | + return Objects.hash(this.left, this.up, this.forwards); |
| 115 | + } |
| 116 | +} |
0 commit comments