package in.twizmwaz.cardinal.module.modules.tntTracker;

import in.twizmwaz.cardinal.GameHandler;
import in.twizmwaz.cardinal.module.Module;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.ExplosionPrimeEvent;
import org.bukkit.metadata.FixedMetadataValue;

import java.util.HashMap;
import java.util.UUID;

public class TntTracker implements Module {
    private HashMap<String, UUID> tntPlaced = new HashMap<>();

    protected TntTracker() {
    }

    public static UUID getWhoPlaced(Entity tnt) {
        if (tnt.getType().equals(EntityType.PRIMED_TNT)) {
            if (tnt.hasMetadata("source")) {
                return (UUID) tnt.getMetadata("source").get(0).value();
            }
        }
        return null;
    }

    @Override
    public void unload() {
        HandlerList.unregisterAll(this);
    }

    @EventHandler
    public void onBlockPlace(BlockPlaceEvent event) {
        if (event.getBlock().getType() == Material.TNT) {
            Location location = event.getBlock().getLocation();
            tntPlaced.put(location.getBlockX() + "," + location.getBlockY() + "," + location.getBlockZ(), event.getPlayer().getUniqueId());
        }
    }

    @EventHandler
    public void onExplosionPrime(ExplosionPrimeEvent event) {
        if (event.getEntity().getType() == EntityType.PRIMED_TNT) {
            Location location = event.getEntity().getLocation();
            if (tntPlaced.containsKey(location.getBlockX() + "," + location.getBlockY() + "," + location.getBlockZ())) {
                UUID player = tntPlaced.get(location.getBlockX() + "," + location.getBlockY() + "," + location.getBlockZ());
                event.getEntity().setMetadata("source", new FixedMetadataValue(GameHandler.getGameHandler().getPlugin(), player));
                tntPlaced.remove(location.getBlockX() + "," + location.getBlockY() + "," + location.getBlockZ());
            }
        }
    }

    @EventHandler
    public void onEntityExplode(EntityExplodeEvent event) {
        if (event.getEntity() != null) {
            if (event.getEntity().getType() == EntityType.PRIMED_TNT) {
                for (Block block : event.blockList()) {
                    if (block.getType() == Material.TNT && getWhoPlaced(event.getEntity()) != null) {
                        Location location = block.getLocation();
                        tntPlaced.put(location.getBlockX() + "," + location.getBlockY() + "," + location.getBlockZ(), getWhoPlaced(event.getEntity()));
                    }
                }
            }
        }
    }
}

@@ -366,16 +366,72 @@
 erList;%0A
+import org.bukkit.event.block.BlockDispenseEntityEvent;%0A
 import o
@@ -461,32 +461,32 @@
 lockPlaceEvent;%0A
-
 import org.bukki
@@ -776,16 +776,85 @@
 Map%3C%3E();
+%0A    private HashMap%3CString, UUID%3E dispenserPlaced = new HashMap%3C%3E();
 %0A%0A    pr
@@ -1580,24 +1580,305 @@
 niqueId());%0A
+        %7D else if (event.getBlock().getType() == Material.DISPENSER) %7B%0A            Location location = event.getBlock().getLocation();%0A            dispenserPlaced.put(location.getBlockX() + %22,%22 + location.getBlockY() + %22,%22 + location.getBlockZ(), event.getPlayer().getUniqueId());%0A
         %7D%0A  
@@ -3179,24 +3179,24 @@
 Entity()));%0A
-
             
@@ -3252,11 +3252,748 @@
  %7D%0A    %7D
+%0A%0A    @EventHandler%0A    public void onEntityDispense(BlockDispenseEntityEvent event)%7B%0A        if (event.getBlock().getType() == Material.DISPENSER)%7B%0A            if (event.getEntity().getType() == EntityType.PRIMED_TNT) %7B%0A                Location location = event.getBlock().getLocation();%0A                if (dispenserPlaced.containsKey(location.getBlockX() + %22,%22 + location.getBlockY() + %22,%22 + location.getBlockZ())) %7B%0A                    UUID player = dispenserPlaced.get(location.getBlockX() + %22,%22 + location.getBlockY() + %22,%22 + location.getBlockZ());%0A                    event.getEntity().setMetadata(%22source%22, new FixedMetadataValue(GameHandler.getGameHandler().getPlugin(), player));%0A                %7D%0A            %7D%0A        %7D%0A    %7D
 %0A%7D%0A

Add Dispenser TNT tracking.