package com.gmail.nossr50.listeners;

import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Furnace;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.FurnaceBurnEvent;
import org.bukkit.event.inventory.FurnaceExtractEvent;
import org.bukkit.event.inventory.FurnaceSmeltEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.FurnaceInventory;
import org.bukkit.inventory.ItemStack;

import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.datatypes.McMMOPlayer;
import com.gmail.nossr50.skills.smelting.SmeltingManager;
import com.gmail.nossr50.util.ItemChecks;
import com.gmail.nossr50.util.Users;

public class InventoryListener implements Listener{
    private final mcMMO plugin;

    public InventoryListener(final mcMMO plugin) {
        this.plugin = plugin;
    }

    @EventHandler(priority = EventPriority.MONITOR)
    public void onInventoryOpen(InventoryOpenEvent event) {
        InventoryType inventoryType = event.getInventory().getType();

        if (inventoryType == InventoryType.FURNACE) {
            FurnaceInventory inventory = (FurnaceInventory) event.getInventory();
            Furnace furnace = inventory.getHolder();

            if (furnace == null) {
                return;
            }

            Block furnaceBlock = furnace.getBlock();

            if (furnace.getBurnTime() == 0 && !plugin.furnaceIsTracked(furnaceBlock)) {
                plugin.addToOpenFurnaceTracker(furnaceBlock, event.getPlayer().getName());
            }
        }
    }

    @EventHandler(priority = EventPriority.MONITOR)
    public void onInventoryClose(InventoryCloseEvent event) {
        InventoryType inventoryType = event.getInventory().getType();

        if (inventoryType == InventoryType.FURNACE) {
            FurnaceInventory inventory = (FurnaceInventory) event.getInventory();
            Furnace furnace = inventory.getHolder();

            if (furnace == null) {
                return;
            }

            Block furnaceBlock = furnace.getBlock();

            if (furnace.getBurnTime() == 0 && plugin.furnaceIsTracked(furnaceBlock)) {
                plugin.removeFromFurnaceTracker(furnaceBlock);
            }
        }
    }

    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    public void onFurnaceBurnEvent(FurnaceBurnEvent event) {
        Block furnaceBlock = event.getBlock();
        BlockState blockState = furnaceBlock.getState();

        if (blockState instanceof Furnace) {
            FurnaceInventory inventory = ((Furnace) blockState).getInventory();
            ItemStack smelting = inventory.getSmelting();
    
            if (plugin.furnaceIsTracked(furnaceBlock) && smelting != null && ItemChecks.isSmeltable(smelting)) {
                Player player = plugin.getFurnacePlayer(furnaceBlock);
    
                if (player != null) {
                    SmeltingManager smeltingManager = new SmeltingManager(Users.getPlayer(player));
                    smeltingManager.fuelEfficiency(event);
                }
            }
        }
    }

    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    public void onFurnaceSmeltEvent(FurnaceSmeltEvent event) {
        Block furnaceBlock = event.getBlock();
        BlockState blockState = furnaceBlock.getState();

        if (blockState instanceof Furnace) {
            FurnaceInventory inventory = ((Furnace) blockState).getInventory();
            ItemStack smelting = inventory.getSmelting();
    
            if (plugin.furnaceIsTracked(furnaceBlock) && smelting != null && ItemChecks.isSmeltable(smelting)) {
                Player player = plugin.getFurnacePlayer(furnaceBlock);
    
                if (player != null) {
                    SmeltingManager smeltingManager = new SmeltingManager(Users.getPlayer(player));
                    smeltingManager.smeltProcessing(event);
                }
            }
        }
    }

    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    public void onFurnaceExtractEvent(FurnaceExtractEvent event) {
        Block furnaceBlock = event.getBlock();
        BlockState blockState = furnaceBlock.getState();

        if (blockState instanceof Furnace) {
            FurnaceInventory inventory = ((Furnace) blockState).getInventory();
            ItemStack result = inventory.getResult();
    
            if (plugin.furnaceIsTracked(furnaceBlock) && result != null && ItemChecks.isSmelted(result)) {
                McMMOPlayer mcMMOPlayer = Users.getPlayer(event.getPlayer());

                if (mcMMOPlayer.getPlayer().equals(plugin.getFurnacePlayer(furnaceBlock))) {
                    SmeltingManager smeltingManager = new SmeltingManager(mcMMOPlayer);
                    smeltingManager.vanillaXPBoost(event);
                }
            }
        }
    }
}

@@ -160,19 +160,103 @@
 t.en
-tity.Player
+chantments.Enchantment;%0D%0Aimport org.bukkit.entity.Player;%0D%0Aimport org.bukkit.event.Event.Result
 ;%0D%0Ai
@@ -523,32 +523,88 @@
 aceSmeltEvent;%0D%0A
+import org.bukkit.event.inventory.InventoryClickEvent;%0D%0A
 import org.bukki
@@ -744,24 +744,69 @@
 ntoryType;%0D%0A
+import org.bukkit.inventory.AnvilInventory;%0D%0A
 import org.b
@@ -960,24 +960,24 @@
 MMOPlayer;%0D%0A
-
 import com.g
@@ -1019,24 +1019,79 @@
 ngManager;%0D%0A
+import com.gmail.nossr50.skills.utilities.SkillTools;%0D%0A
 import com.g
@@ -5473,12 +5473,307 @@
 %0D%0A    %7D%0D
+%0A%0D%0A    @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)%0D%0A    public void onInventoryClickEvent(InventoryClickEvent event) %7B%0D%0A        if (event.getInventory() instanceof AnvilInventory) %7B%0D%0A            SkillTools.removeAbilityBuff(event.getCurrentItem());%0D%0A        %7D%0D%0A    %7D%0D
 %0A%7D%0D%0A

Fix for exploiting new Super Breaker enchants using the anvil.