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.EntityType;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
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.inventory.ItemStack;
import org.bukkit.util.Vector;

import java.util.ArrayList;
import java.util.List;

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;

    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;
    }

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

    @EventHandler
    public void onBlockPlace(BlockPlaceEvent event) {
        Block block = event.getBlock();
        if (block.getType().equals(Material.TNT) && instantIgnite) {
            event.setCancelled(true);
            ItemStack tntStack = event.getPlayer().getItemInHand();
            tntStack.setAmount(tntStack.getAmount() - 1);
            event.getPlayer().setItemInHand(tntStack);
            TNTPrimed tnt = (TNTPrimed) GameHandler.getGameHandler().getMatchWorld().spawnEntity(block.getLocation().add(new Vector(0.5, 0.5, 0.5)), EntityType.PRIMED_TNT);
            tnt.setYield((float) power);
            Bukkit.getServer().getPluginManager().callEvent(new ExplosionPrimeEvent(tnt, (float) power * 10, false));
        }
    }

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

    @EventHandler(priority = EventPriority.HIGHEST)
    public void onEntityExplode(EntityExplodeEvent event) {
        if (event.getEntity() instanceof TNTPrimed) {
            if (!blockDamage) {
                List<Block> toRemove = new ArrayList<>();
                for (Block block : event.blockList()) {
                    toRemove.add(block);
                }
                for (Block block : toRemove) {
                    event.blockList().remove(block);
                }
            }
        }
    }


}

@@ -553,47 +553,8 @@
 nt;%0A
-import org.bukkit.inventory.ItemStack;%0A
 impo
@@ -580,16 +580,16 @@
 Vector;%0A
+
 %0Aimport 
@@ -1247,32 +1247,66 @@
    @EventHandler
+(priority = EventPriority.HIGHEST)
 %0A    public void
@@ -1453,33 +1453,21 @@
 nite
-) %7B%0A            
+ && !
 event.
+i
 s
-et
 Canc
@@ -1476,14 +1476,12 @@
 led(
-true);
+)) %7B
 %0A   
@@ -1493,174 +1493,45 @@
     
-ItemStack tntStack = event.getPlayer().getItemInHand();%0A            tntStack.setAmount(tntStack.getAmount() - 1);%0A            event.getPlayer().setItemInHand(tntStack
+event.getBlock().setType(Material.AIR
 );%0A 
@@ -1706,49 +1706,8 @@
 T);%0A
-            tnt.setYield((float) power);%0A
     
@@ -2421,282 +2421,118 @@
     
-List%3CBlock%3E toRemove = new ArrayList%3C%3E();%0A                for (Block block : event.blockList()) %7B%0A                    toRemove.add(block);%0A                %7D%0A                for (Block block : toRemove) %7B%0A                    event.blockList().remove(block);%0A                %7D
+event.blockList().clear();%0A            %7D else if (yield != 0.3)%7B%0A                event.setYield((float)yield);
 %0A   

Fix tnt module