package moonlight.solutions.functions.impl.combat;
import com.google.common.eventbus.Subscribe;
import lombok.Getter;
import moonlight.solutions.configurations.FriendStorage;
import moonlight.solutions.events.EventDisplay;
import moonlight.solutions.events.EventLivingTick;
import moonlight.solutions.events.TickEvent;
import moonlight.solutions.functions.api.Mod;
import moonlight.solutions.functions.api.ModGroup;
import moonlight.solutions.functions.api.ModReg;
import moonlight.solutions.functions.settings.impl.BooleanSetting;
import moonlight.solutions.functions.settings.impl.SliderSetting;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.vector.Vector3d;
import static net.minecraft.util.math.MathHelper.wrapDegrees;
@Getter
@ModReg(name = "Aim Assist", category = ModGroup.Combat)
public class AimAssist extends Mod {
private final SliderSetting fov = new SliderSetting("Угол поиска цели", 25, 1, 90, 1);
private final SliderSetting pitchSpeed = new SliderSetting("Скорость по pitch", 5, 1, 20, 1);
private final SliderSetting yawSpeed = new SliderSetting("Скорость по yaw", 25, 1, 40, 1);
private final SliderSetting smoothFactor = new SliderSetting("Плавность", 0.5F, 0.1F, 1, 0.1F);
private final BooleanSetting ignoreNaked = new BooleanSetting("Не работать на голых", true);
private final BooleanSetting ignoreFriends = new BooleanSetting("Не работать на друзей", true);
private final BooleanSetting ignoreInvisible = new BooleanSetting("Не работать на инвиз", true);
private PlayerEntity target;
public AimAssist() {
addSettings(fov, yawSpeed, pitchSpeed, smoothFactor, ignoreNaked, ignoreFriends, ignoreInvisible);
}
@Subscribe
public void onTick(EventLivingTick event) {
target = getEntity(mc.player, 4, fov.get());
}
@Subscribe
public void onGui(EventDisplay event) {
if (target != null) {
setRotation(target, yawSpeed.get(), pitchSpeed.get(), smoothFactor.get());
}
}
private void setRotation(PlayerEntity target, float rotationYawSpeed, float rotationPitchSpeed, double smoothFactor) {
Vector3d targetPos = target.getPositionVec().add(0, target.getEyeHeight() / 2.0, 0);
Vector3d playerEyesPos = mc.player.getEyePosition(1.0F);
double deltaX = targetPos.x - playerEyesPos.x;
double deltaY = targetPos.y - playerEyesPos.y;
double deltaZ = targetPos.z - playerEyesPos.z;
float rawYaw = (float) Math.toDegrees(Math.atan2(deltaZ, deltaX)) - 90;
float distanceXZ = MathHelper.sqrt(deltaX * deltaX + deltaZ * deltaZ);
float rawPitch = (float) -Math.toDegrees(Math.atan2(deltaY, distanceXZ));
float yawDelta = wrapDegrees(rawYaw - mc.player.rotationYaw);
float pitchDelta = rawPitch - mc.player.rotationPitch;
float clampedYaw = MathHelper.clamp(yawDelta, -rotationYawSpeed, rotationYawSpeed);
float clampedPitch = MathHelper.clamp(pitchDelta, -rotationPitchSpeed, rotationPitchSpeed);
float smoothYaw = (float) (mc.player.rotationYaw + clampedYaw * smoothFactor);
float smoothPitch = (float) (mc.player.rotationPitch + clampedPitch * smoothFactor);
mc.player.rotationYaw = smoothYaw;
mc.player.rotationPitch = smoothPitch;
}
private PlayerEntity getEntity(PlayerEntity player, double distance, double fov) {
for (PlayerEntity entity : player.world.getEntitiesWithinAABB(PlayerEntity.class, player.getBoundingBox().expand(player.getLook(1.0F).scale(distance)).grow(1.0D, 1.0D, 1.0D), e -> e != mc.player && !e.isSpectator() && e.isAlive())) {
if (ignoreNaked.get() && isNaked(entity)) {
continue;
}
if (ignoreFriends.get() && FriendStorage.isFriend(entity.getScoreboardName())) {
continue;
}
if (ignoreInvisible.get() && entity.isInvisible() && !entity.isSpectator() && entity.getTotalArmorValue() == 0) {
continue;
}
double angle = Math.acos(player.getLook(1.0F).dotProduct(entity.getPositionVec().subtract(player.getEyePosition(1.0F)).normalize())) * (180D / Math.PI);
double distanceToEntity = player.getEyePosition(1.0F).distanceTo(entity.getPositionVec());
if (angle <= fov && distanceToEntity < distance) {
return entity;
}
}
return null;
}
private boolean isNaked(PlayerEntity entity) {
return entity.getTotalArmorValue() == 0;
}
}