/*
 * 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 org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;

import java.util.function.Function;

public class GuiContents {

    private final Plugin plugin;
    private final InventoryType type;
    private final String name;
    private final int size;
    private GuiContents parent;
    private final GuiItem[] itemStackFunctions;
    private final GuiClickHandler[] clickHandlers;

    public GuiContents(Plugin plugin, InventoryType type, String name, int size) {
        this.plugin = plugin;
        this.type = type;
        this.name = name;
        this.size = size;
        this.parent = null;
        this.itemStackFunctions = new GuiItem[this.size];
        this.clickHandlers = new GuiClickHandler[this.size];
    }

    public Plugin getPlugin() {
        return this.plugin;
    }

    public InventoryType getType() {
        return this.type;
    }

    public String getName() {
        return this.name;
    }

    public int getSize() {
        return this.size;
    }

    public GuiContents getParent() {
        return this.parent;
    }

    public boolean hasItem(int slot) {
        return this.itemStackFunctions[slot] != null;
    }

    public boolean hasAction(int slot) {
        return this.clickHandlers[slot] != null;
    }

    public ItemStack[] getItems(Player player) {
        ItemStack[] contents = new ItemStack[this.size];
        for (int i = 0; i < this.size; i++) {
            contents[i] = this.itemStackFunctions[i].apply(player);
        }
        return contents;
    }

    public void setParent(GuiContents parent) {
        this.parent = parent;
    }

    public void set(int slot, GuiItem item) {
        this.itemStackFunctions[slot] = item;
    }

    public void set(int slot, GuiClickHandler clickHandler) {
        this.clickHandlers[slot] = clickHandler;
    }

    public void set(int slot, GuiItem item, GuiClickHandler clickHandler) {
        this.set(slot, item);
        this.set(slot, clickHandler);
    }

    public void fillEmpty(GuiItem item) {
        for (int i = 0; i < this.size; i++) {
            if (this.itemStackFunctions[i] == null) {
                this.itemStackFunctions[i] = item;
            }
        }
    }

    public void onClick(InventoryClickEvent event) {
        GuiClickHandler clickHandler = this.clickHandlers[event.getSlot()];
        if (clickHandler != null) {
            clickHandler.accept(event);
        }
    }
    public void open(Player player) {
        InventoryHolder inventoryHolder = new GuiInventoryHolder(this, player);
        Inventory inventory = inventoryHolder.getInventory();
        player.openInventory(inventory);
    }

    public void update(Inventory inventory, Player player) {
        inventory.setContents(this.getItems(player));
    }

    public void back(Player player) {
        if (this.parent != null) {
            this.parent.open(player);
        }
    }

    public void close(Player player) {
        player.closeInventory();
    }
}

@@ -1443,16 +1443,52 @@
 lugin;%0A%0A
+import java.util.function.Consumer;%0A
 import j
@@ -1515,16 +1515,16 @@
 nction;%0A
-
 %0Apublic 
@@ -1670,24 +1670,77 @@
 l int size;%0A
+    private final Consumer%3C? super Player%3E consumer;%0A
     private 
@@ -1938,16 +1938,51 @@
 int size
+, Consumer%3C? super Player%3E consumer
 ) %7B%0A    
@@ -2081,24 +2081,58 @@
 ize = size;%0A
+        this.consumer = consumer;%0A
         this
@@ -2265,32 +2265,168 @@
 s.size%5D;%0A    %7D%0A%0A
+    public GuiContents(Plugin plugin, InventoryType type, String name, int size) %7B%0A        this(plugin, type, name, size, null);%0A    %7D%0A%0A
     public Plugi
@@ -2661,32 +2661,121 @@
 is.size;%0A    %7D%0A%0A
+    public Consumer%3C? super Player%3E getConsumer() %7B%0A        return this.consumer;%0A    %7D%0A%0A
     public GuiCo
@@ -4183,24 +4183,25 @@
     %7D%0A    %7D%0A
+%0A
     public v
@@ -4408,32 +4408,32 @@
 entory);%0A    %7D%0A%0A
-
     public void 
@@ -4469,32 +4469,70 @@
 layer player) %7B%0A
+        this.consumer.accept(player);%0A
         inventor

Add Consumer to GuiContents for pre-processing of actions before updating contents.