package net.knarcraft.knargui; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.InventoryType; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import java.util.HashMap; import java.util.Map; import java.util.UUID; public abstract class AbstractGUI { private final UUID uuid; private final Inventory inventory; private final Map> actions; /** * Instantiates a new abstract GUI * * @param inventorySize

The size of the inventory (ignored if inventory type is set)

* @param inventoryName

The name of the inventory

* @param inventoryType

The type of inventory to use for the GUI, or null for a variable chest GUI

*/ public AbstractGUI(int inventorySize, String inventoryName, InventoryType inventoryType) { this.uuid = UUID.randomUUID(); if (inventoryType == null) { this.inventory = Bukkit.createInventory(null, inventorySize, inventoryName); } else { this.inventory = Bukkit.createInventory(null, inventoryType, inventoryName); } this.actions = new HashMap<>(); GUIRegistry.registerGUI(this); } /** * Gets this inventory's unique id * * @return

This inventory's unique id

*/ public UUID getUuid() { return this.uuid; } /** * Gets the inventory used for this GUI * * @return

The inventory used for this GUI

*/ public Inventory getInventory() { return this.inventory; } /** * Sets the item in the given inventory slot * * @param inventorySlot

The inventory slot to set the item for

* @param itemStack

The item to display in the slot

* @return

This GUI. Used for chaining commands

*/ public AbstractGUI setItem(int inventorySlot, ItemStack itemStack) { this.inventory.setItem(inventorySlot, itemStack); return this; } /** * Sets the action for clicking the given item slot * * @param inventorySlot

The inventory slot to set the action for

* @param clickType

The type of click to set an action for

* @param action

The action triggered when a click occurs

* @return

This GUI. Used for chaining commands

*/ public AbstractGUI setClickAction(int inventorySlot, ClickType clickType, GUIAction action) { if (!this.actions.containsKey(inventorySlot)) { this.actions.put(inventorySlot, new HashMap<>()); } this.actions.get(inventorySlot).put(clickType, action); return this; } /** * Opens this inventory for the given player * * @param player

The player to open this inventory for

*/ public void openFor(Player player) { player.openInventory(this.inventory); GUIRegistry.registerOpenGUI(player, getUuid()); } /** * Gets the action to trigger for the given inventory slot, and the given click type * * @param inventorySlot

The inventory slot to get the action for

* @param clickType

The click type to get the action for

* @return

The action to run, or null if not set

*/ public GUIAction getAction(int inventorySlot, ClickType clickType) { Map storedActions = this.actions.get(inventorySlot); if (storedActions == null) { return null; } else { return storedActions.get(clickType); } } }