package com.lb_stuff.kataparty.gui;

import com.lb_stuff.kataparty.KataPartyPlugin;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.*;
import org.bukkit.event.inventory.*;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.inventory.InventoryType.SlotType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.List;

public abstract class PartyGui implements Listener
{
	protected final KataPartyPlugin inst;
	protected final Player player;
	private final Inventory inv;
	public PartyGui(KataPartyPlugin plugin, Player p, int guirows, String guiname)
	{
		inst = plugin;
		player = p;
		inv = Bukkit.createInventory(null, 9*guirows, guiname);
	}
	public PartyGui(KataPartyPlugin plugin, Player p, Inventory inventory)
	{
		inst = plugin;
		player = p;
		inv = inventory;
	}

	protected final void addButton(int slot, String name, Material icon, List<String> info)
	{
		ItemStack i = new ItemStack(icon);
		ItemMeta m = i.getItemMeta();
		m.setDisplayName(name);
		m.setLore(info);
		i.setItemMeta(m);
		inv.setItem(slot, i);
	}
	protected final void addButton(int slot, ItemStack button)
	{
		inv.setItem(slot, button);
	}
	protected final int getButton(int slot)
	{
		return inv.getItem(slot).getAmount();
	}
	protected final String getButtonName(int slot)
	{
		return inv.getItem(slot ) != null? inv.getItem(slot).getItemMeta().getDisplayName() : null;
	}
	protected final void setButton(int slot, int value)
	{
		ItemStack i = inv.getItem(slot);
		i.setAmount(value);
		inv.setItem(slot, i);
	}
	protected final void setButton(int slot, Material icon)
	{
		ItemStack i = inv.getItem(slot);
		i.setType(icon);
		inv.setItem(slot, i);
	}
	protected final void setButton(int slot, int value, Material icon)
	{
		ItemStack i = inv.getItem(slot);
		i.setType(icon);
		i.setAmount(value);
		inv.setItem(slot, i);
	}
	protected final void setButton(int slot, String name)
	{
		ItemStack i = inv.getItem(slot);
		ItemMeta m = i.getItemMeta();
		m.setDisplayName(name);
		i.setItemMeta(m);
		inv.setItem(slot, i);
	}
	protected final void setButton(int slot, List<String> info)
	{
		ItemStack i = inv.getItem(slot);
		ItemMeta m = i.getItemMeta();
		m.setLore(info);
		i.setItemMeta(m);
		inv.setItem(slot, i);
	}
	protected final void setButton(int slot, String name, List<String> info)
	{
		ItemStack i = inv.getItem(slot);
		ItemMeta m = i.getItemMeta();
		m.setDisplayName(name);
		m.setLore(info);
		i.setItemMeta(m);
		inv.setItem(slot, i);
	}
	protected abstract void onButton(int slot, ClickType click);

	public final void show()
	{
		player.closeInventory();
		inst.getServer().getPluginManager().registerEvents(this, inst);
		player.openInventory(inv);
	}
	public final void hide()
	{
		player.closeInventory();
	}

	@EventHandler
	public final void onInvClick(InventoryClickEvent e)
	{
		if(e.getWhoClicked() == player && e.getRawSlot() < e.getView().getTopInventory().getSize())
		{
			e.setCancelled(true);
			if(!e.getSlotType().equals(SlotType.OUTSIDE))
			{
				onButton(e.getSlot(), e.getClick());
			}
		}
	}
	@EventHandler
	public final void OnInvDrag(InventoryDragEvent e)
	{
		if(e.getWhoClicked() == player)
		{
			for(Integer rawslot : e.getRawSlots())
			{
				if(rawslot < e.getView().getTopInventory().getSize())
				{
					e.setCancelled(true);
					return;
				}
			}
		}
	}
	@EventHandler
	public final void onInvClose(InventoryCloseEvent e)
	{
		if(e.getPlayer() == player)
		{
			HandlerList.unregisterAll(this);
		}
	}
}

@@ -196,45 +196,8 @@
 .*;%0A
-import org.bukkit.event.inventory.*;%0A
 impo
@@ -2820,24 +2820,150 @@
 d show()%0A%09%7B%0A
+%09%09final PartyGui This = this;%0A%09%09inst.getServer().getScheduler().runTask(inst, new Runnable()%7B@Override public void run()%0A%09%09%7B%0A%09
 %09%09player.clo
@@ -2977,16 +2977,17 @@
 tory();%0A
+%09
 %09%09inst.g
@@ -3031,17 +3031,17 @@
 rEvents(
-t
+T
 his, ins
@@ -3044,16 +3044,17 @@
  inst);%0A
+%09
 %09%09player
@@ -3074,16 +3074,23 @@
 y(inv);%0A
+%09%09%7D%7D);%0A
 %09%7D%0A%09publ
@@ -3113,16 +3113,112 @@
 de()%0A%09%7B%0A
+%09%09inst.getServer().getScheduler().runTask(inst, new Runnable()%7B@Override public void run()%0A%09%09%7B%0A%09
 %09%09player
@@ -3236,16 +3236,23 @@
 tory();%0A
+%09%09%7D%7D);%0A
 %09%7D%0A%0A%09@Ev

Fix potential bugs with opening/closing inventories in inventory events