package me.deftware.client.framework.world.ray;

import me.deftware.client.framework.entity.Entity;
import me.deftware.client.framework.math.vector.Vector3d;
import me.deftware.client.framework.util.minecraft.EntitySwingResult;
import net.minecraft.entity.projectile.ProjectileUtil;
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.Box;

/**
 * @author Deftware
 */
public class EntityRayTrace extends RayTrace<EntitySwingResult> {

    private final double maxDistance;
    private final Vector3d rotation;

    public EntityRayTrace(Vector3d start, Vector3d end, Vector3d rotation, double distance, RayProfile profile) {
        super(start, end, profile);
        this.maxDistance = distance;
        this.rotation = rotation;
    }

    @Override
    public EntitySwingResult run(Entity in) {

        net.minecraft.entity.Entity entity = in.getMinecraftEntity();

        HitResult hitResult = entity.raycast(maxDistance, 1f, false);

        double distance = maxDistance * maxDistance;
        if (hitResult != null && hitResult.getType() == HitResult.Type.BLOCK)
            distance = hitResult.getPos().squaredDistanceTo(start.getMinecraftVector());

        Box box = entity.getBoundingBox().stretch(rotation.getMinecraftVector().multiply(maxDistance)).expand(1.0D, 1.0D, 1.0D);

        EntityHitResult result = ProjectileUtil.raycast(entity, start.getMinecraftVector(), end.getMinecraftVector(), box, e -> (!e.isSpectator() && e.collides()), distance);

        if (result != null) {
            double g = start.getMinecraftVector().squaredDistanceTo(result.getPos());
            if (g < distance) {
                return new EntitySwingResult(result);
            }
        }

        return null;
    }

}

@@ -213,32 +213,77 @@
 itySwingResult;%0A
+import net.minecraft.client.MinecraftClient;%0A
 import net.minec
@@ -321,16 +321,16 @@
 leUtil;%0A
-
 import n
@@ -444,16 +444,97 @@
 ath.Box;
+%0Aimport net.minecraft.util.math.Vec3d;%0Aimport net.minecraft.world.RaycastContext;
 %0A%0A/**%0A *
@@ -1092,23 +1092,16 @@
 t = 
-entity.
 raycast(
 maxD
@@ -1100,30 +1100,26 @@
 ast(
-maxDistance, 1f, false
+start, end, entity
 );%0A%0A
@@ -1868,24 +1868,24 @@
 %0A        %7D%0A%0A
-
         retu
@@ -1900,10 +1900,325 @@
 %0A    %7D%0A%0A
+    public HitResult raycast(Vector3d start, Vector3d end, net.minecraft.entity.Entity entity) %7B%0A        return MinecraftClient.getInstance().world.raycast(new RaycastContext(start.getMinecraftVector(), end.getMinecraftVector(), RaycastContext.ShapeType.OUTLINE, RaycastContext.FluidHandling.NONE, entity));%0A    %7D%0A%0A
 %7D%0A

Update EntityRayTrace