/*
 * This file is part of AmpMenus.
 *
 * Copyright (c) 2014 <http://github.com/ampayne2/AmpMenus/>
 *
 * AmpMenus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * AmpMenus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with AmpMenus.  If not, see <http://www.gnu.org/licenses/>.
 */
package ninja.amp.ampmenus;

import ninja.amp.ampmenus.menus.MenuHolder;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.server.PluginDisableEvent;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredListener;
import org.bukkit.plugin.java.JavaPlugin;

/**
 * Passes inventory click events to their menus for handling.
 */
public class MenuListener implements Listener {
    private Plugin plugin = null;
    private static final MenuListener INSTANCE = new MenuListener();

    private MenuListener() {
    }

    /**
     * Gets the {@link ninja.amp.ampmenus.MenuListener} instance.
     *
     * @return The {@link ninja.amp.ampmenus.MenuListener} instance.
     */
    public static MenuListener getInstance() {
        return INSTANCE;
    }

    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    public void onInventoryClick(InventoryClickEvent event) {
        if (event.getWhoClicked() instanceof Player && event.getInventory().getHolder() instanceof MenuHolder) {
            event.setCancelled(true);
            ((MenuHolder) event.getInventory().getHolder()).getMenu().onInventoryClick(event);
        }
    }

    /**
     * Registers the events of the {@link ninja.amp.ampmenus.MenuListener} to a plugin.
     *
     * @param plugin The plugin used to register the events.
     */
    public void register(JavaPlugin plugin) {
        if (!isRegistered(plugin)) {
            plugin.getServer().getPluginManager().registerEvents(INSTANCE, plugin);
            this.plugin = plugin;
        }
    }

    /**
     * Checks if the {@link ninja.amp.ampmenus.MenuListener} is registered to a plugin.
     *
     * @param plugin The plugin.
     * @return True if the {@link ninja.amp.ampmenus.MenuListener} is registered to the plugin, else false.
     */
    public boolean isRegistered(JavaPlugin plugin) {
        if (plugin.equals(this.plugin)) {
            for (RegisteredListener listener : HandlerList.getRegisteredListeners(plugin)) {
                if (listener.getListener().equals(INSTANCE)) {
                    return true;
                }
            }
        }
        return false;
    }

    @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
    public void onPluginDisable(PluginDisableEvent event) {
        if (event.getPlugin().equals(plugin)) {
            plugin = null;
        }
    }
}

@@ -825,24 +825,50 @@
 MenuHolder;%0A
+import org.bukkit.Bukkit;%0A
 import org.b
@@ -1138,24 +1138,63 @@
 sableEvent;%0A
+import org.bukkit.inventory.Inventory;%0A
 import org.b
@@ -3391,30 +3391,550 @@
         
-plugin = null;
+closeOpenMenus();%0A            plugin = null;%0A        %7D%0A    %7D%0A%0A    /**%0A     * Closes all %7B@link ninja.amp.ampmenus.menus.ItemMenu%7Ds currently open.%0A     */%0A    public static void closeOpenMenus() %7B%0A        for (Player player : Bukkit.getOnlinePlayers()) %7B%0A            if (player.getOpenInventory() != null) %7B%0A                Inventory inventory = player.getOpenInventory().getTopInventory();%0A                if (inventory.getHolder() instanceof MenuHolder) %7B%0A                    player.closeInventory();%0A                %7D%0A            %7D
 %0A       

Add a method to close all open menus