| 1 | /*
|
| 2 | * This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client).
|
| 3 | * Copyright (c) Meteor Development.
|
| 4 | */
|
| 5 |
|
| 6 | package meteordevelopment.meteorclient.utils.player;
|
| 7 |
|
| 8 | import meteordevelopment.meteorclient.MeteorClient;
|
| 9 | import meteordevelopment.meteorclient.events.entity.player.SendMovementPacketsEvent;
|
| 10 | import meteordevelopment.meteorclient.events.world.TickEvent;
|
| 11 | import meteordevelopment.meteorclient.systems.config.Config;
|
| 12 | import meteordevelopment.meteorclient.utils.PreInit;
|
| 13 | import meteordevelopment.meteorclient.utils.entity.Target;
|
| 14 | import meteordevelopment.meteorclient.utils.misc.Pool;
|
| 15 | import meteordevelopment.orbit.EventHandler;
|
| 16 | import net.minecraft.core.BlockPos;
|
| 17 | import net.minecraft.network.protocol.game.ServerboundMovePlayerPacket;
|
| 18 | import net.minecraft.util.Mth;
|
| 19 | import net.minecraft.world.entity.Entity;
|
| 20 | import net.minecraft.world.phys.Vec3;
|
| 21 |
|
| 22 | import java.util.ArrayList;
|
| 23 | import java.util.List;
|
| 24 |
|
| 25 | import static meteordevelopment.meteorclient.MeteorClient.mc;
|
| 26 |
|
| 27 | public class Rotations {
|
| 28 | private static final Pool<Rotation> rotationPool = new Pool<>(Rotation::new);
|
| 29 | private static final List<Rotation> rotations = new ArrayList<>();
|
| 30 | public static float serverYaw;
|
| 31 | public static float serverPitch;
|
| 32 | public static int rotationTimer;
|
| 33 | private static float preYaw, prePitch;
|
| 34 | private static int i = 0;
|
| 35 |
|
| 36 | private static Rotation lastRotation;
|
| 37 | private static int lastRotationTimer;
|
| 38 | private static boolean sentLastRotation;
|
| 39 | public static boolean rotating = false;
|
| 40 |
|
| 41 | private Rotations() {
|
| 42 | }
|
| 43 |
|
| 44 | @PreInit
|
| 45 | public static void init() {
|
| 46 | MeteorClient.EVENT_BUS.subscribe(Rotations.class);
|
| 47 | }
|
| 48 |
|
| 49 | public static void rotate(double yaw, double pitch, int priority, boolean clientSide, Runnable callback) {
|
| 50 | Rotation rotation = rotationPool.get();
|
| 51 | rotation.set(yaw, pitch, priority, clientSide, callback);
|
| 52 |
|
| 53 | int i = 0;
|
| 54 | for (; i < rotations.size(); i++) {
|
| 55 | if (priority > rotations.get(i).priority) break;
|
| 56 | }
|
| 57 |
|
| 58 | rotations.add(i, rotation);
|
| 59 | }
|
| 60 |
|
| 61 | public static void rotate(double yaw, double pitch, int priority, Runnable callback) {
|
| 62 | rotate(yaw, pitch, priority, false, callback);
|
| 63 | }
|
| 64 |
|
| 65 | public static void rotate(double yaw, double pitch, Runnable callback) {
|
| 66 | rotate(yaw, pitch, 0, callback);
|
| 67 | }
|
| 68 |
|
| 69 | public static void rotate(double yaw, double pitch, int priority) {
|
| 70 | rotate(yaw, pitch, priority, null);
|
| 71 | }
|
| 72 |
|
| 73 | public static void rotate(double yaw, double pitch) {
|
| 74 | rotate(yaw, pitch, 0, null);
|
| 75 | }
|
| 76 |
|
| 77 | private static void resetLastRotation() {
|
| 78 | if (lastRotation != null) {
|
| 79 | rotationPool.free(lastRotation);
|
| 80 |
|
| 81 | lastRotation = null;
|
| 82 | lastRotationTimer = 0;
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | @EventHandler
|
| 87 | private static void onSendMovementPacketsPre(SendMovementPacketsEvent.Pre event) {
|
| 88 | if (mc.getCameraEntity() != mc.player) return;
|
| 89 | sentLastRotation = false;
|
| 90 |
|
| 91 | if (!rotations.isEmpty()) {
|
| 92 | rotating = true;
|
| 93 | resetLastRotation();
|
| 94 |
|
| 95 | Rotation rotation = rotations.get(i);
|
| 96 | setupMovementPacketRotation(rotation);
|
| 97 |
|
| 98 | if (rotations.size() > 1) rotationPool.free(rotation);
|
| 99 |
|
| 100 | i++;
|
| 101 | } else if (lastRotation != null) {
|
| 102 | if (lastRotationTimer >= Config.get().rotationHoldTicks.get()) {
|
| 103 | resetLastRotation();
|
| 104 | rotating = false;
|
| 105 | } else {
|
| 106 | setupMovementPacketRotation(lastRotation);
|
| 107 | sentLastRotation = true;
|
| 108 |
|
| 109 | lastRotationTimer++;
|
| 110 | }
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | private static void setupMovementPacketRotation(Rotation rotation) {
|
| 115 | setClientRotation(rotation);
|
| 116 | setCamRotation(rotation.yaw, rotation.pitch);
|
| 117 | }
|
| 118 |
|
| 119 | private static void setClientRotation(Rotation rotation) {
|
| 120 | preYaw = mc.player.getYRot();
|
| 121 | prePitch = mc.player.getXRot();
|
| 122 |
|
| 123 | mc.player.setYRot((float) rotation.yaw);
|
| 124 | mc.player.setXRot((float) rotation.pitch);
|
| 125 | }
|
| 126 |
|
| 127 | @EventHandler
|
| 128 | private static void onSendMovementPacketsPost(SendMovementPacketsEvent.Post event) {
|
| 129 | if (!rotations.isEmpty()) {
|
| 130 | if (mc.getCameraEntity() == mc.player) {
|
| 131 | rotations.get(i - 1).runCallback();
|
| 132 |
|
| 133 | if (rotations.size() == 1) lastRotation = rotations.get(i - 1);
|
| 134 |
|
| 135 | resetPreRotation();
|
| 136 | }
|
| 137 |
|
| 138 | for (; i < rotations.size(); i++) {
|
| 139 | Rotation rotation = rotations.get(i);
|
| 140 |
|
| 141 | setCamRotation(rotation.yaw, rotation.pitch);
|
| 142 | if (rotation.clientSide) setClientRotation(rotation);
|
| 143 | rotation.sendPacket();
|
| 144 | if (rotation.clientSide) resetPreRotation();
|
| 145 |
|
| 146 | if (i == rotations.size() - 1) lastRotation = rotation;
|
| 147 | else rotationPool.free(rotation);
|
| 148 | }
|
| 149 |
|
| 150 | rotations.clear();
|
| 151 | i = 0;
|
| 152 | } else if (sentLastRotation) {
|
| 153 | resetPreRotation();
|
| 154 | }
|
| 155 | }
|
| 156 |
|
| 157 | private static void resetPreRotation() {
|
| 158 | mc.player.setYRot(preYaw);
|
| 159 | mc.player.setXRot(prePitch);
|
| 160 | }
|
| 161 |
|
| 162 | @EventHandler
|
| 163 | private static void onTick(TickEvent.Pre event) {
|
| 164 | rotationTimer++;
|
| 165 | }
|
| 166 |
|
| 167 | public static double getYaw(Entity entity) {
|
| 168 | return mc.player.getYRot() + Mth.wrapDegrees((float) Math.toDegrees(Math.atan2(entity.getZ() - mc.player.getZ(), entity.getX() - mc.player.getX())) - 90f - mc.player.getYRot());
|
| 169 | }
|
| 170 |
|
| 171 | public static double getYaw(Vec3 pos) {
|
| 172 | return mc.player.getYRot() + Mth.wrapDegrees((float) Math.toDegrees(Math.atan2(pos.z() - mc.player.getZ(), pos.x() - mc.player.getX())) - 90f - mc.player.getYRot());
|
| 173 | }
|
| 174 |
|
| 175 | public static double getPitch(Vec3 pos) {
|
| 176 | double diffX = pos.x() - mc.player.getX();
|
| 177 | double diffY = pos.y() - (mc.player.getY() + mc.player.getEyeHeight(mc.player.getPose()));
|
| 178 | double diffZ = pos.z() - mc.player.getZ();
|
| 179 |
|
| 180 | double diffXZ = Math.sqrt(diffX * diffX + diffZ * diffZ);
|
| 181 |
|
| 182 | return mc.player.getXRot() + Mth.wrapDegrees((float) -Math.toDegrees(Math.atan2(diffY, diffXZ)) - mc.player.getXRot());
|
| 183 | }
|
| 184 |
|
| 185 | public static double getPitch(Entity entity, Target target) {
|
| 186 | double y = switch (target) {
|
| 187 | case Head -> entity.getEyeY();
|
| 188 | case Body -> entity.getY() + entity.getBbHeight() / 2;
|
| 189 | case Feet -> entity.getY();
|
| 190 | };
|
| 191 |
|
| 192 | double diffX = entity.getX() - mc.player.getX();
|
| 193 | double diffY = y - (mc.player.getY() + mc.player.getEyeHeight(mc.player.getPose()));
|
| 194 | double diffZ = entity.getZ() - mc.player.getZ();
|
| 195 |
|
| 196 | double diffXZ = Math.sqrt(diffX * diffX + diffZ * diffZ);
|
| 197 |
|
| 198 | return mc.player.getXRot() + Mth.wrapDegrees((float) -Math.toDegrees(Math.atan2(diffY, diffXZ)) - mc.player.getXRot());
|
| 199 | }
|
| 200 |
|
| 201 | public static double getPitch(Entity entity) {
|
| 202 | return getPitch(entity, Target.Body);
|
| 203 | }
|
| 204 |
|
| 205 | public static double getYaw(BlockPos pos) {
|
| 206 | return mc.player.getYRot() + Mth.wrapDegrees((float) Math.toDegrees(Math.atan2(pos.getZ() + 0.5 - mc.player.getZ(), pos.getX() + 0.5 - mc.player.getX())) - 90f - mc.player.getYRot());
|
| 207 | }
|
| 208 |
|
| 209 | public static double getPitch(BlockPos pos) {
|
| 210 | double diffX = pos.getX() + 0.5 - mc.player.getX();
|
| 211 | double diffY = pos.getY() + 0.5 - (mc.player.getY() + mc.player.getEyeHeight(mc.player.getPose()));
|
| 212 | double diffZ = pos.getZ() + 0.5 - mc.player.getZ();
|
| 213 |
|
| 214 | double diffXZ = Math.sqrt(diffX * diffX + diffZ * diffZ);
|
| 215 |
|
| 216 | return mc.player.getXRot() + Mth.wrapDegrees((float) -Math.toDegrees(Math.atan2(diffY, diffXZ)) - mc.player.getXRot());
|
| 217 | }
|
| 218 |
|
| 219 | public static void setCamRotation(double yaw, double pitch) {
|
| 220 | serverYaw = (float) yaw;
|
| 221 | serverPitch = (float) pitch;
|
| 222 | rotationTimer = 0;
|
| 223 | }
|
| 224 |
|
| 225 | private static class Rotation {
|
| 226 | public double yaw, pitch;
|
| 227 | public int priority;
|
| 228 | public boolean clientSide;
|
| 229 | public Runnable callback;
|
| 230 |
|
| 231 | public void set(double yaw, double pitch, int priority, boolean clientSide, Runnable callback) {
|
| 232 | this.yaw = yaw;
|
| 233 | this.pitch = pitch;
|
| 234 | this.priority = priority;
|
| 235 | this.clientSide = clientSide;
|
| 236 | this.callback = callback;
|
| 237 | }
|
| 238 |
|
| 239 | public void sendPacket() {
|
| 240 | mc.getConnection().send(new ServerboundMovePlayerPacket.Rot((float) yaw, (float) pitch, mc.player.onGround(), mc.player.horizontalCollision));
|
| 241 | runCallback();
|
| 242 | }
|
| 243 |
|
| 244 | public void runCallback() {
|
| 245 | if (callback != null) callback.run();
|
| 246 | }
|
| 247 | }
|
| 248 | }
|
| 249 |
|