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

import in.twizmwaz.cardinal.GameHandler;
import in.twizmwaz.cardinal.module.Module;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.block.BlockBreakEvent;
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.logging.Level;

public class Tnt implements Module {

    private final boolean instantignite;
    private final boolean blockdamage;
    private final double yield;
    private final double power;
    private final int fuse;

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

    protected Tnt(final boolean instantignite, final boolean blockdamage, final double yield, final double power, final int fuse) {
        this.instantignite = instantignite;
        this.blockdamage = blockdamage;
        this.yield = yield;
        this.power = power;
        this.fuse = fuse;
    }

    @EventHandler
    public void onBlockPlace(BlockPlaceEvent event) {
        Block block = event.getBlock();
        if (block.getType().equals(Material.TNT) && instantignite) {
            event.setCancelled(true);
            Entity tntPrimed = GameHandler.getGameHandler().getMatchWorld().spawnEntity(block.getLocation(), EntityType.PRIMED_TNT);
            tntPrimed.setMetadata("instantignite", new FixedMetadataValue(GameHandler.getGameHandler().getPlugin(), true));
        }
    }

    @EventHandler
    public void onExplosionPrime(ExplosionPrimeEvent event) {
        if (event.getEntity() instanceof TNTPrimed && fuse != 4) {
            ((TNTPrimed) event.getEntity()).setFuseTicks(fuse * 20);
        }
    }

    @EventHandler
    public void onEntityExplode(EntityExplodeEvent event) {
        if (event.getEntity() instanceof TNTPrimed) {
            if (blockdamage) {
                if (instantignite && event.getEntity().getMetadata("instantignite").get(0).value().equals(true)) {
                    event.setCancelled(true);
                    GameHandler.getGameHandler().getMatchWorld().createExplosion(event.getLocation(), (float) power);
                }
                if (power != 4.0) {
                    event.setCancelled(true);
                    GameHandler.getGameHandler().getMatchWorld().createExplosion(event.getLocation(), (float) power);
                }
                if (yield != 0.3) {
                   // event.setYield((float) yield);
                }
            } else {
                event.setCancelled(true);
                GameHandler.getGameHandler().getMatchWorld().createExplosion(event.getLocation().getX(), event.getLocation().getY(), event.getLocation().getZ(), (float) power, false, false);
            }
        }
    }


}

@@ -1899,20 +1899,36 @@
 NTPrimed
- && 
+) %7B%0A            if (
 fuse != 
@@ -1928,24 +1928,28 @@
 use != 4) %7B%0A
+    
             
@@ -2005,16 +2005,129 @@
  * 20);%0A
+            %7D%0A            if (power != 4.0) %7B%0A                event.setRadius((float) power * 10);%0A            %7D%0A
         
@@ -2294,24 +2294,98 @@
 ckdamage) %7B%0A
+                if (event.getEntity().hasMetadata(%22instantignite%22)) %7B%0A    
             
@@ -2499,32 +2499,36 @@
                 
+    
 event.setCancell
@@ -2549,32 +2549,36 @@
                 
+  
+  
 GameHandler.getG
@@ -2651,32 +2651,54 @@
 (float) power);%0A
+                    %7D%0A
                 
@@ -2715,36 +2715,36 @@
         if (
-power
+yield
  != 
-4.0
+0.3
 ) %7B%0A        
@@ -2759,258 +2759,211 @@
     
-event.setCancelled(true);%0A                    GameHandler.getGameHandler().getMatchWorld().createExplosion(event.getLocation(), (float) power);%0A                %7D%0A                if (yield != 0.3) %7B%0A                   // event.setYield((float) yield);
+/** Currently Broken **/%0A                    /*%0A                    for (Block block : event.blockList()) %7B%0A                        block.getDrops().clear();%0A                    %7D%0A                    */%0A
 %0A   

Update TNT Module