/*
 * Copyright (c) 2015 Legend Zero LLC
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.legendzero.lzlib.gui;

import com.legendzero.lzlib.service.Service;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.plugin.Plugin;

public class GuiService implements Listener, Service {

    private final Plugin plugin;

    public GuiService(Plugin plugin) {
        this.plugin = plugin;
    }

    @Override
    public void initialize() {
        this.plugin.getServer().getPluginManager().registerEvents(this, this.plugin);
    }

    @Override
    public void uninitialize() {
        HandlerList.unregisterAll(this);
    }

    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
        if(event.getWhoClicked() instanceof Player) {
            if(event.getInventory().getHolder() instanceof GuiInventoryHolder) {
                event.setCancelled(true);
                if(event.getRawSlot() < event.getInventory().getSize()) {
                    if(event.getCurrentItem() != null) {
                        GuiInventoryHolder inventory = (GuiInventoryHolder) event.getInventory().getHolder();
                        GuiItem item = inventory.getItem(event.getSlot());
                        if(item != null){
                            GuiHandler handler = item.getGuiHandler();
                            handler.onClick((Player) event.getWhoClicked(), event.getClick());
                        }
                    }
                }
            }
        }
    }
}

@@ -1908,66 +1908,8 @@
 ) %7B%0A
-        if(event.getWhoClicked() instanceof Player) %7B%0A    
     
@@ -1993,20 +1993,16 @@
         
-    
 event.se
@@ -2023,36 +2023,32 @@
 e);%0A            
-    
 if(event.getRawS
@@ -2047,49 +2047,116 @@
 .get
-RawSlot() %3C event.getInventory().getSize(
+WhoClicked() instanceof Player) %7B%0A                if(event.getInventory().equals(event.getClickedInventory()
 )) %7B

Reorder InventoryClickEvent handling to improve performance.