package kernitus.plugin.OldCombatMechanics.module;

import kernitus.plugin.OldCombatMechanics.OCMMain;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.entity.EnderPearl;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.ProjectileLaunchEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.projectiles.ProjectileSource;

import java.util.*;

/**
 * Allows you to throw enderpearls as often as you like, not only after a cooldown.
 */
public class ModuleDisableEnderpearlCooldown extends Module {

    /**
     * Contains players that threw an ender pearl. As the handler calls launchProjectile, which also calls the event,
     * we need to ignore that event call.
     */
    private final Set<UUID> ignoredPlayers = new HashSet<>();
    private final Map<UUID, Long> lastLaunched = new HashMap<>();
    private int cooldown;

    public ModuleDisableEnderpearlCooldown(OCMMain plugin){
        super(plugin, "disable-enderpearl-cooldown");
        reload();
    }

    public void reload(){
        cooldown = module().getInt("cooldown");
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    public void onPlayerShoot(ProjectileLaunchEvent e){
        if(!(e.getEntity() instanceof EnderPearl)) return;
        ProjectileSource shooter = e.getEntity().getShooter();

        if(!(shooter instanceof Player)) return;
        Player player = (Player) shooter;
        UUID uuid = player.getUniqueId();

        if(ignoredPlayers.contains(uuid)) return;

        e.setCancelled(true);

        // Check if the cooldown has expired yet
        final long currentTime = System.currentTimeMillis() / 1000;
        if(lastLaunched.containsKey(uuid) && currentTime - lastLaunched.get(uuid) < cooldown)
            return;

        lastLaunched.put(uuid, currentTime);

        // Make sure we ignore the event triggered by launchProjectile
        ignoredPlayers.add(uuid);
        EnderPearl pearl = player.launchProjectile(EnderPearl.class);
        ignoredPlayers.remove(uuid);

        pearl.setVelocity(player.getEyeLocation().getDirection().multiply(2));

        if(player.getGameMode() == GameMode.CREATIVE) return;

        ItemStack enderpearlItemStack;
        PlayerInventory playerInventory = player.getInventory();
        ItemStack mainHand = playerInventory.getItemInMainHand();
        ItemStack offHand = playerInventory.getItemInOffHand();

        if(isEnderPearl(mainHand)) enderpearlItemStack = mainHand;
        else if(isEnderPearl(offHand)) enderpearlItemStack = offHand;
        else return;

        enderpearlItemStack.setAmount(enderpearlItemStack.getAmount() - 1);
    }

    private boolean isEnderPearl(ItemStack itemStack){
        return itemStack != null && itemStack.getType() == Material.ENDER_PEARL;
    }

    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent e){
        lastLaunched.remove(e.getPlayer().getUniqueId());
    }
}

@@ -987,22 +987,16 @@
 private 
-final 
 Map%3CUUID
@@ -1019,26 +1019,8 @@
 ched
- = new HashMap%3C%3E()
 ;%0D%0A 
@@ -1263,24 +1263,160 @@
 ooldown%22);%0D%0A
+        if(cooldown %3E 0) %7B%0D%0A            if (lastLaunched == null) lastLaunched = new HashMap%3C%3E();%0D%0A        %7D else lastLaunched = null;%0D%0A
     %7D%0D%0A%0D%0A   
@@ -1922,16 +1922,56 @@
 ed yet%0D%0A
+        if(lastLaunched != null) %7B%0D%0A    
         
@@ -2031,34 +2031,39 @@
  1000;%0D%0A        
+    
 if
+ 
 (lastLaunched.co
@@ -2139,24 +2139,28 @@
             
+   
+ 
 return;%0D%0A%0D%0A 
@@ -2150,32 +2150,36 @@
      return;%0D%0A%0D%0A
+    
         lastLaun
@@ -2203,24 +2203,35 @@
 rrentTime);%0D
+%0A        %7D%0D
 %0A%0D%0A        /
@@ -3294,24 +3294,49 @@
 e)%7B%0D%0A       
+ if(lastLaunched != null)
  lastLaunche

Disable enderpearl launch time tracking if cooldown disabled