Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 175
src/client/java/com/kodari/facedistance/FaceDistanceClient.java
package com.kodari.facedistance;
import java.util.UUID;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.message.v1.ClientSendMessageEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.AbstractClientPlayerEntity;
import net.minecraft.util.math.Vec3d;
public final class FaceDistanceClient implements ClientModInitializer {
private static final double CLOSE_DISTANCE = 0.45;
private static final double FAR_DISTANCE = 3.0;
private UUID targetUuid;
private boolean movingAway;
@Override
public void onInitializeClient() {
ClientSendMessageEvents.ALLOW_CHAT.register(this::handleChatMessage);
ClientTickEvents.END_CLIENT_TICK.register(this::tick);
}
private boolean handleChatMessage(String message) {
String command = message.trim();
if (command.equalsIgnoreCase("#stop")) {
targetUuid = null;
return false;
}
if (command.equalsIgnoreCase("#target") || command.regionMatches(true, 0, "#target ", 0, 8)) {
String targetName = command.length() > 7 ? command.substring(8).trim() : "";
MinecraftClient client = MinecraftClient.getInstance();
targetUuid = null;
movingAway = false;
if (!targetName.isEmpty() && client.world != null && client.player != null) {
for (AbstractClientPlayerEntity candidate : client.world.getPlayers()) {
if (!candidate.getUuid().equals(client.player.getUuid())
&& candidate.getName().getString().equalsIgnoreCase(targetName)) {
targetUuid = candidate.getUuid();
break;
}
}
}
return false;
}
return true;
}
private void tick(MinecraftClient client) {
if (targetUuid == null || client.player == null || client.world == null) {
return;
}
AbstractClientPlayerEntity target = null;
for (AbstractClientPlayerEntity candidate : client.world.getPlayers()) {
if (candidate.getUuid().equals(targetUuid)) {
target = candidate;
break;
}
}
if (target == null) {
targetUuid = null;
return;
}
Vec3d offset = target.getEyePos().subtract(client.player.getEyePos());
double distance = offset.length();
if (distance == 0.0) {
return;
}
double horizontalDistance = Math.sqrt(offset.x * offset.x + offset.z * offset.z);
client.player.setYaw((float) Math.toDegrees(Math.atan2(offset.z, offset.x)) - 90.0F);
client.player.setPitch((float) -Math.toDegrees(Math.atan2(offset.y, horizontalDistance)));
if (!client.player.getAbilities().flying) {
return;
}
if (movingAway && distance >= FAR_DISTANCE) {
movingAway = false;
} else if (!movingAway && distance <= CLOSE_DISTANCE) {
movingAway = true;
}
double remainingDistance = movingAway ? FAR_DISTANCE - distance : distance - CLOSE_DISTANCE;
double speed = Math.min(client.player.getAbilities().getFlySpeed(), remainingDistance);
Vec3d velocity = offset.normalize().multiply(movingAway ? -speed : speed);
client.player.setVelocity(velocity);
}
}