package net.simpvp.Misc;

import org.bukkit.entity.*;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.ExplosionPrimeEvent;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;

/**
 * Modifies damage source data in various ways
 * to prevent it from being used to track players positions
 *
 * @author cats
 */
public class DamageSource implements Listener {

    private final int maxDistance;

    public DamageSource(int maxDistance) {
        this.maxDistance = maxDistance;
    }

    /**
     * This handler removes the TNT explosion source
     * By doing this, we prevent the tnt knockback from an improper damage source
     */
    @EventHandler
    public void onExplosionPrimed(ExplosionPrimeEvent event) {
        if (event.getEntityType() == EntityType.PRIMED_TNT) {
            final TNTPrimed tnt = (TNTPrimed) event.getEntity();

            tnt.setSource(null);
        }
    }

    /**
     * Reset the velocity 1 tick after the player takes damage
     *
     * Possible todo: maybe make this run async to avoid tps lag playing a factor
     * I don't think it does because bukkit scheduler, but who knows
     */
    @EventHandler
    public void onDamage(EntityDamageByEntityEvent event) {
        if (event.getCause() == EntityDamageEvent.DamageCause.PROJECTILE) {
            // This SHOULD always be a successful cast
            final Projectile projectile = (Projectile) event.getDamager();

            if (projectile.getShooter() instanceof Player) {
                final Player shooter = (Player) projectile.getShooter();


                // I'm wincing but it shouldn't be too much math
                if (projectile.getLocation().distance(shooter.getLocation()) > this.maxDistance) {
                    new BukkitRunnable() {
                        @Override
                        public void run() {
                            event.getEntity().setVelocity(new Vector(0, event.getEntity().getVelocity().getY(), 0));

                        }
                        // The velocity should be sent all at once, so we don't send 2 conflicting ones
                        // Just 1 all at once
                    }.runTaskLater(Misc.instance, 1L);
                }
            }
        }
    }


}

@@ -571,16 +571,18 @@
 Distance
+Sq
 ;%0A%0A    p
@@ -643,18 +643,34 @@
 Distance
- =
+Sq = maxDistance *
  maxDist
@@ -1868,16 +1868,23 @@
 distance
+Squared
 (shooter
@@ -1909,32 +1909,34 @@
 this.maxDistance
+Sq
 ) %7B%0A            
@@ -2441,12 +2441,10 @@
 %7D%0A    %7D%0A
-%0A%0A
 %7D%0A

Switch to using squared distance