superb-warfare/src/main/java/net/mcreator/superbwarfare/tools/TraceTool.java
2024-07-27 02:20:09 +08:00

40 lines
2 KiB
Java

package net.mcreator.superbwarfare.tools;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.projectile.ProjectileUtil;
import net.minecraft.world.phys.*;
public class TraceTool {
public static Entity findLookingEntity(Entity player, double entityReach) {
double distance = entityReach * entityReach;
Vec3 eyePos = player.getEyePosition(1.0f);
HitResult hitResult = player.pick(entityReach, 1.0f, false);
if (hitResult.getType() != HitResult.Type.MISS) {
distance = hitResult.getLocation().distanceToSqr(eyePos);
double blockReach = 5;
if (distance > blockReach * blockReach) {
Vec3 pos = hitResult.getLocation();
hitResult = BlockHitResult.miss(pos, Direction.getNearest(eyePos.x, eyePos.y, eyePos.z), BlockPos.containing(pos));
}
}
Vec3 viewVec = player.getViewVector(1.0F);
Vec3 toVec = eyePos.add(viewVec.x * entityReach, viewVec.y * entityReach, viewVec.z * entityReach);
AABB aabb = player.getBoundingBox().expandTowards(viewVec.scale(entityReach)).inflate(1.0D, 1.0D, 1.0D);
EntityHitResult entityhitresult = ProjectileUtil.getEntityHitResult(player, eyePos, toVec, aabb, p -> !p.isSpectator(), distance);
if (entityhitresult != null) {
Vec3 targetPos = entityhitresult.getLocation();
double distanceToTarget = eyePos.distanceToSqr(targetPos);
if (distanceToTarget > distance || distanceToTarget > entityReach * entityReach) {
hitResult = BlockHitResult.miss(targetPos, Direction.getNearest(viewVec.x, viewVec.y, viewVec.z), BlockPos.containing(targetPos));
} else if (distanceToTarget < distance) {
hitResult = entityhitresult;
}
}
if (hitResult.getType() == HitResult.Type.ENTITY) {
return ((EntityHitResult) hitResult).getEntity();
}
return null;
}
}