package com.gmail.nossr50.listeners;

import org.bukkit.block.Block;
import org.bukkit.block.Furnace;
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.skills.smelting.SmeltingManager;
import com.gmail.nossr50.util.ItemChecks;

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();
        FurnaceInventory inventory = ((Furnace)furnaceBlock.getState()).getInventory();
        ItemStack smelting = inventory.getSmelting();

        if (plugin.furnaceIsTracked(furnaceBlock) && smelting != null && ItemChecks.isSmeltable(smelting)) {
            SmeltingManager smeltingManager = new SmeltingManager(plugin.getFurnacePlayer(furnaceBlock));
            smeltingManager.fuelEfficiency(event);
        }
    }

    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    public void onFurnaceSmeltEvent(FurnaceSmeltEvent event) {
        Block furnaceBlock = event.getBlock();
        FurnaceInventory inventory = ((Furnace)furnaceBlock.getState()).getInventory();
        ItemStack smelting = inventory.getSmelting();

        if (plugin.furnaceIsTracked(furnaceBlock) && smelting != null && ItemChecks.isSmeltable(smelting)) {
            SmeltingManager smeltingManager = new SmeltingManager(plugin.getFurnacePlayer(furnaceBlock));
            smeltingManager.smeltProcessing(event);
        }
    }

    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    public void onFurnaceExtractEvent(FurnaceExtractEvent event) {
        Block furnaceBlock = event.getBlock();
        FurnaceInventory inventory = ((Furnace)furnaceBlock.getState()).getInventory();
        ItemStack result = inventory.getResult();

        if (plugin.furnaceIsTracked(furnaceBlock) && result != null && ItemChecks.isSmelted(result)) {
            SmeltingManager smeltingManager = new SmeltingManager(plugin.getFurnacePlayer(furnaceBlock));
            smeltingManager.vanillaXPBoost(event);
        }
    }
}

@@ -95,24 +95,58 @@
 k.Furnace;%0D%0A
+import org.bukkit.entity.Player;%0D%0A
 import org.b
@@ -2847,32 +2847,141 @@
 e(smelting)) %7B%0D%0A
+            Player player = plugin.getFurnacePlayer(furnaceBlock);%0D%0A%0D%0A            if (player != null) %7B%0D%0A    
             Smel
@@ -3036,47 +3036,20 @@
 r(pl
-ugin.getFurnacePlayer(furnaceBlock));%0D%0A
+ayer);%0D%0A    
     
@@ -3088,32 +3088,47 @@
 ciency(event);%0D%0A
+            %7D%0D%0A
         %7D%0D%0A    %7D
@@ -3568,32 +3568,141 @@
 e(smelting)) %7B%0D%0A
+            Player player = plugin.getFurnacePlayer(furnaceBlock);%0D%0A%0D%0A            if (player != null) %7B%0D%0A    
             Smel
@@ -3757,47 +3757,20 @@
 r(pl
-ugin.getFurnacePlayer(furnaceBlock));%0D%0A
+ayer);%0D%0A    
     
@@ -3810,32 +3810,47 @@
 essing(event);%0D%0A
+            %7D%0D%0A
         %7D%0D%0A    %7D

Fix NPE when a furnace belonging to a player that has logged out keeps burning.