package com.cjburkey.claimchunk.event;

import com.cjburkey.claimchunk.ChunkHelper;
import com.cjburkey.claimchunk.ClaimChunk;
import com.cjburkey.claimchunk.Config;
import java.util.Objects;
import org.bukkit.entity.Animals;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.hanging.HangingBreakByEntityEvent;
import org.bukkit.event.hanging.HangingPlaceEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;

@SuppressWarnings("unused")
public class CancellableChunkEvents implements Listener {

    // Block Break
    @EventHandler
    public void onBlockBroken(BlockBreakEvent e) {
        if (e != null) {
            ChunkHelper.cancelEventIfNotOwned(e.getPlayer(), e.getBlock().getChunk(), e);
        }
    }

    // Clicking on Blocks
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
        if (e != null && e.getClickedBlock() != null) {
            if (e.getAction() == Action.LEFT_CLICK_BLOCK) {
                return;
            }
            if (e.getAction() == Action.LEFT_CLICK_AIR) {
                return;
            }
            if (e.getAction() == Action.RIGHT_CLICK_AIR) {
                return;
            }
            ChunkHelper.cancelEventIfNotOwned(e.getPlayer(), e.getClickedBlock().getChunk(), e);
        }
    }

    // Placing Blocks
    @EventHandler
    public void onBlockPlaced(BlockPlaceEvent e) {
        if (e != null) {
            ChunkHelper.cancelEventIfNotOwned(e.getPlayer(), e.getBlock().getChunk(), e);
        }
    }

    // Item Frame Rotation
    @EventHandler
    public void onPlayerInteract(PlayerInteractEntityEvent e) {
        if (e != null && e.getRightClicked().getType().equals(EntityType.ITEM_FRAME)) {
            ChunkHelper.cancelEventIfNotOwned(e.getPlayer(), e.getRightClicked().getLocation().getChunk(), e);
        }
    }

    // Item Frame Break
    @EventHandler
    public void onItemFrameBroken(HangingBreakByEntityEvent e) {
        if (e != null && e.getEntity().getType().equals(EntityType.ITEM_FRAME)
                && Objects.requireNonNull(e.getRemover()).getType().equals(EntityType.PLAYER)) {
            ChunkHelper.cancelEventIfNotOwned((Player) e.getRemover(), e.getEntity().getLocation().getChunk(), e);
        }
    }

    // Item Frame Place
    @EventHandler
    public void onItemFramePlaced(HangingPlaceEvent e) {
        if (e != null && e.getEntity().getType().equals(EntityType.ITEM_FRAME) && e.getPlayer() != null) {
            ChunkHelper.cancelEventIfNotOwned(e.getPlayer(), e.getEntity().getLocation().getChunk(), e);
        }
    }

    // Item Frame Remove Item
    @EventHandler
    public void onItemFramePlaced(EntityDamageByEntityEvent e) {
        if (e != null && e.getEntity().getType().equals(EntityType.ITEM_FRAME)
                && e.getDamager().getType().equals(EntityType.PLAYER)) {
            ChunkHelper.cancelEventIfNotOwned((Player) e.getDamager(), e.getEntity().getLocation().getChunk(), e);
        }
    }

    // TnT and Creeper explosions
    @EventHandler
    public void onEntityExplode(EntityExplodeEvent e) {
        if (!e.isCancelled()) {
            if (ClaimChunk.getInstance().getChunkHandler().isUnclaimed(e.getLocation().getChunk())
                    && Config.getBool("protection", "blockUnclaimedChunks")) {
                return;
            }
            ChunkHelper.cancelExplosionIfConfig(e);
        }
    }

    // Animal damage
    @EventHandler()
    public void onEntityDamage(EntityDamageByEntityEvent e) {
        if (ClaimChunk.getInstance().getChunkHandler().isUnclaimed(e.getEntity().getLocation().getChunk())) {
            return;
        }
        if (e.getDamager() instanceof Player && e.getEntity() instanceof Animals)
            ChunkHelper.cancelAnimalDamage((Player) e.getDamager(), e.getDamager().getLocation().getChunk(), e);
    }

}

@@ -1344,19 +1344,16 @@
 CK_BLOCK
-) %7B
 %0A       
@@ -1365,46 +1365,15 @@
     
-return;%0A            %7D%0A            if (
+    %7C%7C 
 e.ge
@@ -1398,35 +1398,32 @@
 n.LEFT_CLICK_AIR
-) %7B
 %0A               
@@ -1427,46 +1427,15 @@
     
-return;%0A            %7D%0A            if (
+    %7C%7C 
 e.ge
@@ -1977,25 +1977,42 @@
 e != null &&
- 
+%0A                (
 e.getRightCl
@@ -2058,16 +2058,78 @@
 M_FRAME)
+ %7C%7C e.getRightClicked().getType().equals(EntityType.PAINTING))
 ) %7B%0A    
@@ -2387,81 +2387,8 @@
 l &&
- e.getEntity().getType().equals(EntityType.ITEM_FRAME)%0A                &&
  Obj
@@ -2721,65 +2721,8 @@
 l &&
- e.getEntity().getType().equals(EntityType.ITEM_FRAME) &&
  e.g
@@ -2999,24 +2999,25 @@
  != null && 
+(
 e.getEntity(
@@ -3057,16 +3057,72 @@
 M_FRAME)
+ %7C%7C e.getEntity().getType().equals(EntityType.PAINTING))
 %0A       

Add painting protection