package in.twizmwaz.cardinal.module.modules.appliedRegion.type;

import in.twizmwaz.cardinal.module.modules.appliedRegion.AppliedRegion;
import in.twizmwaz.cardinal.module.modules.filter.FilterModule;
import in.twizmwaz.cardinal.module.modules.filter.FilterState;
import in.twizmwaz.cardinal.module.modules.regions.RegionModule;
import in.twizmwaz.cardinal.module.modules.regions.type.BlockRegion;
import in.twizmwaz.cardinal.module.modules.tntTracker.TntTracker;
import in.twizmwaz.cardinal.util.ChatUtils;
import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.player.PlayerBucketFillEvent;
import org.bukkit.util.Vector;

import java.util.HashSet;
import java.util.Set;

public class BlockBreakRegion extends AppliedRegion {
    
    public BlockBreakRegion(RegionModule region, FilterModule filter, String message) {
        super(region, filter, message);
    }
    
    @EventHandler
    public void onBlockBreak(BlockBreakEvent event) {
        if (!event.isCancelled() && region.contains(new BlockRegion(null, event.getBlock().getLocation().toVector().add(new Vector(0.5, 0.5, 0.5)))) && (filter.evaluate(event.getPlayer()).equals(FilterState.DENY) || filter.evaluate(event.getBlock()).equals(FilterState.DENY))) {
            event.setCancelled(true);
            ChatUtils.sendWarningMessage(event.getPlayer(), message);
        }
    }

    @EventHandler
    public void onBucketFill(PlayerBucketFillEvent event) {
        if (!event.isCancelled() && region.contains(new BlockRegion(null, event.getBlockClicked().getRelative(event.getBlockFace()).getLocation().toVector().add(new Vector(0.5, 0.5, 0.5))))
                && (filter.evaluate(event.getPlayer()).equals(FilterState.DENY)
                || filter.evaluate(event.getBlockClicked().getRelative(event.getBlockFace())).equals(FilterState.DENY))) {
            event.setCancelled(true);
            ChatUtils.sendWarningMessage(event.getPlayer(), message);
        }
    }

    @EventHandler
    public void onEntityExplode(EntityExplodeEvent event) {
        Set<Block> blocksToRemove = new HashSet<>();
        for (Block block : event.blockList()) {
            if (region.contains(new BlockRegion(null, block.getLocation().toVector().add(new Vector(0.5, 0.5, 0.5))))) {
                if (TntTracker.getWhoPlaced(event.getEntity()) != null) {
                    if (Bukkit.getOfflinePlayer(TntTracker.getWhoPlaced(event.getEntity())).isOnline()) {
                        if (filter.evaluate(Bukkit.getPlayer(TntTracker.getWhoPlaced(event.getEntity()))).equals(FilterState.DENY) || filter.evaluate(block).equals(FilterState.DENY)) blocksToRemove.add(block);;
                    } else blocksToRemove.add(block);
                } else blocksToRemove.add(block);
            }
        }
        for (Block block : blocksToRemove) {
            event.blockList().remove(block);
        }
    }
}

@@ -2101,933 +2101,7 @@
    %7D
-%0A%0A    @EventHandler%0A    public void onEntityExplode(EntityExplodeEvent event) %7B%0A        Set%3CBlock%3E blocksToRemove = new HashSet%3C%3E();%0A        for (Block block : event.blockList()) %7B%0A            if (region.contains(new BlockRegion(null, block.getLocation().toVector().add(new Vector(0.5, 0.5, 0.5))))) %7B%0A                if (TntTracker.getWhoPlaced(event.getEntity()) != null) %7B%0A                    if (Bukkit.getOfflinePlayer(TntTracker.getWhoPlaced(event.getEntity())).isOnline()) %7B%0A                        if (filter.evaluate(Bukkit.getPlayer(TntTracker.getWhoPlaced(event.getEntity()))).equals(FilterState.DENY) %7C%7C filter.evaluate(block).equals(FilterState.DENY)) blocksToRemove.add(block);;%0A                    %7D else blocksToRemove.add(block);%0A                %7D else blocksToRemove.add(block);%0A            %7D%0A        %7D%0A        for (Block block : blocksToRemove) %7B%0A            event.blockList().remove(block);%0A        %7D%0A    %7D
 %0A%7D%0A

Fix block break applied regions being affected by entity explosions, fixes #120