package me.otisdiver.otisprojectile.targeting;

import java.util.List;

import org.bukkit.entity.Entity;
import org.bukkit.entity.Projectile;

import me.otisdiver.otisprojectile.OtisProjectile;
import me.otisdiver.otisprojectile.Utils;

public class EggTargeter extends Targeter {
    
    private static int searchRange = 20;
    
    // Entity the egg has "locked on" to.
    private Entity target;
    private boolean lastUpdateFailed = false;
    
    public EggTargeter(OtisProjectile main, Projectile projectile) {
        super(main, projectile);
    }
    
    private boolean identifyTarget() {
        // Get a list of all entities within the search range.
        List<Entity> nearbyEntities = Utils.getNearbyEntitiesList(projectile, searchRange);
        // Remove from the list any entities that aren't hostile mobs.
        for(int i = 0; i < nearbyEntities.size(); i++) {
            Entity e = nearbyEntities.get(i);
            if (!Utils.isEntityHostile(e)) nearbyEntities.remove(i);
            if (projectile.getShooter().equals(e)) nearbyEntities.remove(i);
        }
        
        // Find the nearest entity. If it exists, update the target and report success.
        Entity entity = Utils.getNearestEntityInList(projectile.getLocation(), nearbyEntities);
        if (entity != null) {
            target = entity;
            return true;
        } else {
            return false;
        }
    }
    
    @Override
    public void updateTarget() {
        if (isProjectileGone()) {
            cancelTargetingTask();
            return;
        }
        
        // If there's no target, try to find one. If that fails, stop.
        if (target == null || target.isDead()) {
            if (!identifyTarget()) return;
        }
        
        // Continue chasing the target if it's still within the search range.
        if (Utils.calculateDistance(projectile.getLocation(), target.getLocation()) < searchRange) {
            projectile.setVelocity(target.getLocation().toVector());
            lastUpdateFailed = false;
        }
        // If the attempt fails, and the last attempt also failed, give up (will find new target on next tick).
        else if (lastUpdateFailed) {
            target = null;
            lastUpdateFailed = false;
        }
        // If the attempt fails, but the last one didn't, mark that this one did.
        else {
            lastUpdateFailed = true;
        }
    }
    
}

@@ -65,16 +65,44 @@
 .List;%0A%0A
+import org.bukkit.Location;%0A
 import o
@@ -1780,32 +1780,161 @@
      %7D%0A        %0A
+        Location projectileLocation = projectile.getLocation();%0A        Location targetLocation = target.getLocation();%0A        %0A
         // Conti
@@ -2037,36 +2037,32 @@
 e(projectile
-.get
 Location
 (), target.g
@@ -2053,18 +2053,16 @@
 tion
-()
 , target
 .get
@@ -2057,28 +2057,24 @@
 , target
-.get
 Location
 ()) %3C se
@@ -2065,18 +2065,16 @@
 Location
-()
 ) %3C sear
@@ -2119,22 +2119,21 @@
 elocity(
-target
+Utils
 .getLoca
@@ -2140,20 +2140,60 @@
 tion
-().to
+s
 Vector
-(
+Difference(projectileLocation, targetLocation
 ));%0A

Fix egg targeting