Files
src
build.gradle
gradle.properties
settings.gradle
Code
6 168
src/client/java/com/example/pvpaimer/PvpAimerClient.java
package com.example.pvpaimer;
import com.mojang.blaze3d.platform.InputConstants;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keymapping.v1.KeyMappingHelper;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.player.AbstractClientPlayer;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.world.phys.Vec3;
import org.lwjgl.glfw.GLFW;
public class PvpAimerClient implements ClientModInitializer {
private static final double MAX_RANGE_SQUARED = 12.0 * 12.0;
private static final double MAX_AIM_ANGLE_SQUARED = 55.0 * 55.0;
private static final float SMOOTHING = 0.35F;
private static KeyMapping toggleAimerKey;
private static boolean enabled;
@Override
public void onInitializeClient() {
toggleAimerKey = KeyMappingHelper.registerKeyMapping(new KeyMapping(
"key.pvpaimer.toggle",
InputConstants.Type.KEYSYM,
GLFW.GLFW_KEY_R,
KeyMapping.Category.MISC
));
ClientTickEvents.END_CLIENT_TICK.register(client -> {
while (toggleAimerKey.consumeClick()) {
enabled = !enabled;
}
if (enabled) {
aimAtPlayer(client);
}
});
}
private static void aimAtPlayer(Minecraft client) {
LocalPlayer localPlayer = client.player;
ClientLevel level = client.level;
if (localPlayer == null || level == null) {
return;
}
float bestYaw = 0.0F;
float bestPitch = 0.0F;
double bestAngleSquared = Double.MAX_VALUE;
boolean foundTarget = false;
Vec3 eyePosition = localPlayer.getEyePosition();
for (AbstractClientPlayer target : level.players()) {
if (target == localPlayer || !target.isAlive() || target.isSpectator()) {
continue;
}
Vec3 direction = eyePosition.vectorTo(target.getEyePosition());
if (direction.lengthSqr() > MAX_RANGE_SQUARED) {
continue;
}
double horizontalDistance = Math.sqrt(direction.x() * direction.x() + direction.z() * direction.z());
float targetYaw = (float) (Math.atan2(direction.z(), direction.x()) * 180.0 / Math.PI - 90.0);
float targetPitch = (float) (-Math.atan2(direction.y(), horizontalDistance) * 180.0 / Math.PI);
float yawDifference = wrapDegrees(targetYaw - localPlayer.getYRot());
float pitchDifference = targetPitch - localPlayer.getXRot();
double angleSquared = yawDifference * yawDifference + pitchDifference * pitchDifference;
if (angleSquared > MAX_AIM_ANGLE_SQUARED || angleSquared >= bestAngleSquared) {
continue;
}
bestAngleSquared = angleSquared;
bestYaw = targetYaw;
bestPitch = targetPitch;
foundTarget = true;
}
if (foundTarget) {
float yaw = localPlayer.getYRot() + wrapDegrees(bestYaw - localPlayer.getYRot()) * SMOOTHING;
float pitch = localPlayer.getXRot() + (bestPitch - localPlayer.getXRot()) * SMOOTHING;
localPlayer.setYRot(yaw);
localPlayer.setXRot(Math.max(-90.0F, Math.min(90.0F, pitch)));
}
}
private static float wrapDegrees(float degrees) {
degrees %= 360.0F;
if (degrees >= 180.0F) {
degrees -= 360.0F;
}
if (degrees < -180.0F) {
degrees += 360.0F;
}
return degrees;
}
}