package com.cjm721.overloaded.util;

import net.minecraft.entity.Entity;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;

import javax.annotation.Nonnull;
import java.util.List;

public class WorldUtil {

    public static RayTraceResult rayTraceWithEntities(@Nonnull World world, @Nonnull Vec3d startingLocation, @Nonnull Vec3d direction, @Nonnull Entity excludedEntity, double maxDistance) {
        Vec3d endingLocation = startingLocation.add(direction.scale(maxDistance));
        RayTraceResult raytraceresult = world.rayTraceBlocks(startingLocation, endingLocation, false, true, false);

        if (raytraceresult != null) {
            endingLocation = new Vec3d(raytraceresult.hitVec.x, raytraceresult.hitVec.y, raytraceresult.hitVec.z);
        }

        Entity entity = null;

        double[] vertexes = new double[6];

        vertexes[0] = startingLocation.x - 0.5D;
        vertexes[1] = startingLocation.y - 0.5D;
        vertexes[2] = startingLocation.z - 0.5D;
        vertexes[3] = startingLocation.x + 0.5D;
        vertexes[4] = startingLocation.y + 0.5D;
        vertexes[5] = startingLocation.z + 0.5D;

        int length = (int) Math.ceil(startingLocation.subtract(endingLocation).lengthVector());

        for(double i = 0; i < length; i+= 0.1) {
            vertexes[0] += direction.x * i;
            vertexes[1] += direction.y * i;
            vertexes[2] += direction.z * i;
            vertexes[3] += direction.x * i;
            vertexes[4] += direction.y * i;
            vertexes[5] += direction.z * i;

            AxisAlignedBB boundingBox = new AxisAlignedBB(vertexes[0], vertexes[1], vertexes[2], vertexes[3], vertexes[4], vertexes[5]);

            List<Entity> list = world.getEntitiesWithinAABBExcludingEntity(excludedEntity, boundingBox);
            double smallestEntityDistance = 0.0D;
            for (int k = 0; k < list.size(); ++k) {
                Entity entity1 = list.get(k);

                if (entity1.canBeCollidedWith() && (!entity1.isEntityEqual(excludedEntity)) && !entity1.noClip) {
                    AxisAlignedBB axisalignedbb = entity1.getEntityBoundingBox().grow(0.30000001192092896D);
                    RayTraceResult raytraceresult1 = axisalignedbb.calculateIntercept(startingLocation, endingLocation);

                    if (raytraceresult1 != null) {
                        double currentEntityDistance = startingLocation.squareDistanceTo(raytraceresult1.hitVec);

                        if (currentEntityDistance < smallestEntityDistance || smallestEntityDistance == 0.0D) {
                            entity = entity1;
                            smallestEntityDistance = currentEntityDistance;
                        }
                    }
                }
            }

            if (entity != null) {
                Vec3d hitVec = boundingBox.calculateIntercept(startingLocation,endingLocation).hitVec;
                return new RayTraceResult(entity,hitVec == null ? new Vec3d(0.5,0.5,0.5) : hitVec);
            }
        }
        return raytraceresult;
    }
}

@@ -733,16 +733,49 @@
 esult !=
+ null && raytraceresult.hitVec !=
  null) %7B
@@ -1996,59 +1996,8 @@
 or (
-int k = 0; k %3C list.size(); ++k) %7B%0A                
 Enti
@@ -2011,23 +2011,17 @@
 ty1 
-=
+:
  list
-.get(k);%0A
+) %7B
 %0A   
@@ -2275,31 +2275,25 @@
 eResult 
-raytraceresult1
+intercept
  = axisa
@@ -2380,31 +2380,53 @@
     if (
-raytraceresult1
+intercept != null && intercept.hitVec
  != null
@@ -2522,23 +2522,17 @@
 eTo(
-raytraceresult1
+intercept
 .hit
@@ -2907,20 +2907,32 @@
     
-Vec3d hitVec
+RayTraceResult intercept
  = b
@@ -2978,16 +2978,17 @@
 ocation,
+ 
 endingLo
@@ -2994,16 +2994,103 @@
 ocation)
+;%0A%0A                if(intercept != null) %7B%0A                    Vec3d hitVec = intercept
 .hitVec;
@@ -3102,24 +3102,28 @@
             
+    
 return new R
@@ -3143,16 +3143,17 @@
 (entity,
+ 
 hitVec =
@@ -3175,20 +3175,22 @@
 c3d(0.5,
+ 
 0.5,
+ 
 0.5) : h
@@ -3197,16 +3197,34 @@
 itVec);%0A
+                %7D%0A
         

Fix NPE in WorldUtil