Files
src
build.gradle
gradle.properties
manifest.kod
README.md
settings.gradle
Code
10 476
src/client/java/dev/kodari/maceaimassist/client/MaceAimAssistClient.java
package dev.kodari.maceaimassist.client;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.rendering.v1.hud.HudElementRegistry;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.network.AbstractClientPlayerEntity;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Items;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Vec3d;
public final class MaceAimAssistClient implements ClientModInitializer {
public static final String MOD_ID = "maceaimassist";
private static final double MAX_ANGLE_PER_TICK = 4.0;
private static AbstractClientPlayerEntity lockedTarget;
private static int switchStabilityTicks;
private static int targetGraceTicks;
private static boolean targetSelected;
@Override
public void onInitializeClient() {
AimAssistConfig.load();
ClientTickEvents.END_CLIENT_TICK.register(MaceAimAssistClient::onClientTick);
HudElementRegistry.addLast(Identifier.of(MOD_ID, "aim_indicator"),
(context, tickCounter) -> renderIndicator(context));
}
private static void onClientTick(MinecraftClient client) {
ClientPlayerEntity player = client.player;
ClientWorld world = client.world;
targetSelected = false;
if (player == null || world == null || !AimAssistConfig.enabled
|| !player.getMainHandStack().isOf(Items.MACE)) {
resetTracking();
return;
}
Vec3d lookDirection = player.getRotationVec(1.0f).normalize();
Candidate bestCandidate = null;
Candidate lockedCandidate = null;
boolean lockedTargetStillValid = false;
for (AbstractClientPlayerEntity target : world.getPlayers()) {
if (!isValidTarget(player, target)) {
continue;
}
if (target == lockedTarget) {
lockedTargetStillValid = true;
}
Vec3d targetPoint = target.getEntityPos().add(0.0, target.getHeight() * 0.5, 0.0);
Vec3d direction = targetPoint.subtract(player.getEyePos()).normalize();
double dot = Math.clamp(lookDirection.dotProduct(direction), -1.0, 1.0);
float angle = (float) Math.toDegrees(Math.acos(dot));
if (angle > AimAssistConfig.activationAngle) {
continue;
}
Candidate candidate = new Candidate(target, direction, angle);
if (target == lockedTarget) {
lockedCandidate = candidate;
}
if (bestCandidate == null || candidate.angle() < bestCandidate.angle()) {
bestCandidate = candidate;
}
}
if (lockedTarget != null && !lockedTargetStillValid) {
lockedTarget = null;
switchStabilityTicks = 0;
targetGraceTicks = 0;
}
if (bestCandidate == null) {
if (lockedTarget != null && ++targetGraceTicks > AimAssistConfig.targetLockStability) {
lockedTarget = null;
switchStabilityTicks = 0;
}
return;
}
targetGraceTicks = 0;
Candidate selectedCandidate;
if (lockedCandidate != null && bestCandidate.player() != lockedCandidate.player()
&& switchStabilityTicks > 0) {
selectedCandidate = lockedCandidate;
switchStabilityTicks--;
} else {
selectedCandidate = bestCandidate;
if (selectedCandidate.player() != lockedTarget) {
lockedTarget = selectedCandidate.player();
switchStabilityTicks = AimAssistConfig.targetLockStability;
} else if (bestCandidate.player() == lockedTarget) {
switchStabilityTicks = AimAssistConfig.targetLockStability;
}
}
targetSelected = true;
adjustAim(player, selectedCandidate.direction());
}
private static boolean isValidTarget(PlayerEntity player, PlayerEntity target) {
return target != player && target.isAlive() && !target.isSpectator() && !player.isTeammate(target);
}
private static void adjustAim(ClientPlayerEntity player, Vec3d direction) {
double horizontalLength = Math.sqrt(direction.x * direction.x + direction.z * direction.z);
float desiredYaw = (float) (Math.toDegrees(Math.atan2(direction.z, direction.x)) - 90.0);
float desiredPitch = (float) -Math.toDegrees(Math.atan2(direction.y, horizontalLength));
float yawDifference = wrapDegrees(desiredYaw - player.getYaw());
float pitchDifference = desiredPitch - player.getPitch();
double baseStrength = AimAssistConfig.assistanceStrength / 100.0;
double adjustmentFraction = 1.0 - Math.pow(1.0 - baseStrength, AimAssistConfig.speedMultiplier);
double yawAdjustment = yawDifference * adjustmentFraction;
double pitchAdjustment = pitchDifference * adjustmentFraction;
double stepLength = Math.sqrt(yawAdjustment * yawAdjustment + pitchAdjustment * pitchAdjustment);
if (stepLength > MAX_ANGLE_PER_TICK) {
double scale = MAX_ANGLE_PER_TICK / stepLength;
yawAdjustment *= scale;
pitchAdjustment *= scale;
}
player.setYaw(player.getYaw() + (float) yawAdjustment);
player.setPitch((float) Math.clamp(player.getPitch() + pitchAdjustment, -90.0, 90.0));
}
private static float wrapDegrees(float angle) {
angle %= 360.0f;
if (angle >= 180.0f) {
angle -= 360.0f;
} else if (angle < -180.0f) {
angle += 360.0f;
}
return angle;
}
private static void renderIndicator(DrawContext context) {
MinecraftClient client = MinecraftClient.getInstance();
if (!targetSelected || !AimAssistConfig.targetIndicator || client.player == null
|| client.world == null || !AimAssistConfig.enabled
|| !client.player.getMainHandStack().isOf(Items.MACE)) {
return;
}
int x = client.getWindow().getScaledWidth() / 2 + 8;
int y = client.getWindow().getScaledHeight() / 2 + 8;
context.drawTextWithShadow(client.textRenderer, "•", x, y, 0xFF73DFA0);
}
public static void resetTracking() {
lockedTarget = null;
switchStabilityTicks = 0;
targetGraceTicks = 0;
targetSelected = false;
}
private record Candidate(AbstractClientPlayerEntity player, Vec3d direction, float angle) {
}
}