85 lines
2.7 KiB
Java
85 lines
2.7 KiB
Java
package de.nicolasklier.custom_structures;
|
|
|
|
import net.minecraft.client.MinecraftClient;
|
|
import net.minecraft.entity.Entity;
|
|
import net.minecraft.entity.LivingEntity;
|
|
import net.minecraft.entity.decoration.ItemFrameEntity;
|
|
import net.minecraft.entity.projectile.ProjectileUtil;
|
|
import net.minecraft.util.hit.EntityHitResult;
|
|
import net.minecraft.util.hit.HitResult;
|
|
import net.minecraft.util.math.Box;
|
|
import net.minecraft.util.math.Vec3d;
|
|
import net.minecraft.world.RaycastContext;
|
|
|
|
public class Helper {
|
|
public static HitResult raycastInDirection(MinecraftClient client, float tickDelta, Vec3d direction) {
|
|
Entity entity = client.getCameraEntity();
|
|
if (entity == null || client.world == null) {
|
|
return null;
|
|
}
|
|
|
|
double reachDistance = 100f; // Custom reach size of 100 blocks.
|
|
HitResult target = raycast(entity, reachDistance, tickDelta, false, direction);
|
|
double extendedReach = reachDistance;
|
|
|
|
Vec3d cameraPos = entity.getCameraPosVec(tickDelta);
|
|
|
|
extendedReach = extendedReach * extendedReach;
|
|
if (target != null) {
|
|
extendedReach = target.getPos().squaredDistanceTo(cameraPos);
|
|
}
|
|
|
|
Vec3d vec3d3 = cameraPos.add(direction.multiply(reachDistance));
|
|
Box box = entity
|
|
.getBoundingBox()
|
|
.stretch(entity.getRotationVec(1.0F).multiply(reachDistance))
|
|
.expand(1.0D, 1.0D, 1.0D);
|
|
|
|
EntityHitResult entityHitResult = ProjectileUtil.raycast(
|
|
entity,
|
|
cameraPos,
|
|
vec3d3,
|
|
box,
|
|
(entityx) -> !entityx.isSpectator(),
|
|
extendedReach
|
|
);
|
|
|
|
if (entityHitResult == null) {
|
|
return target;
|
|
}
|
|
|
|
Entity entity2 = entityHitResult.getEntity();
|
|
Vec3d vec3d4 = entityHitResult.getPos();
|
|
double g = cameraPos.squaredDistanceTo(vec3d4);
|
|
|
|
if (g > reachDistance) {
|
|
return null;
|
|
} else if (g < extendedReach || target == null) {
|
|
target = entityHitResult;
|
|
if (entity2 instanceof LivingEntity || entity2 instanceof ItemFrameEntity) {
|
|
client.targetedEntity = entity2;
|
|
}
|
|
}
|
|
|
|
return target;
|
|
}
|
|
|
|
private static HitResult raycast(
|
|
Entity entity,
|
|
double maxDistance,
|
|
float tickDelta,
|
|
boolean includeFluids,
|
|
Vec3d direction
|
|
) {
|
|
Vec3d end = entity.getCameraPosVec(tickDelta).add(direction.multiply(maxDistance));
|
|
return entity.world.raycast(new RaycastContext(
|
|
entity.getCameraPosVec(tickDelta),
|
|
end,
|
|
RaycastContext.ShapeType.OUTLINE,
|
|
includeFluids ? RaycastContext.FluidHandling.ANY : RaycastContext.FluidHandling.NONE,
|
|
entity
|
|
));
|
|
}
|
|
|
|
}
|