QuarterBukkit

SelectInventory

The select inventory is a simple class for creating fancy custom inventories, especially if you want to create selection GUIs.

Creation

There's two ways for creating a SelectInventory-object:

Anonymous Class

You can simply create an anonymous class which uses the abstract SelectInventory-class as superclass:

new SelectInventory(plugin, "Title") {

    @Override
    protected void onClick(Selection selection, ClickType clickType, Player player) {

        // Click action
    }
};

This actually creates an empty inventory with the title "Title". Of course you can define your own values, e.g. you can read the title from a language or message-config system. You can also colorize the title with ChatColors.

There are more constrcutors for creating the object:

public SelectInventory(Plugin plugin)
public SelectInventory(Plugin plugin, String title)
public SelectInventory(Plugin plugin, InventoryLayouter layouter)
public SelectInventory(Plugin plugin, String title, InventoryLayouter layouter)

Subclass

You can also use a subclass if you're code starts to get complicated:

public class TestSelectInventory extends SelectInventory {

    public Test(Plugin plugin) {

        super(plugin, "Title");
    }

    @Override
    protected void onClick(Selection selection, ClickType clickType, Player player) {

    }

}

Add/Remove Selections

You can add selection options with a bunch of useful methods:

public SelectInventory add(Object value, Material material)
public SelectInventory add(Object value, Material material, int amount)
public SelectInventory add(Object value, Material material, short data)
public SelectInventory add(Object value, Material material, int amount, short data)
public SelectInventory add(Object value, ItemStack itemStack)

public SelectInventory add(Object value, Material material, String name, String... descriptions)
public SelectInventory add(Object value, Material material, int amount, String name, String... descriptions)
public SelectInventory add(Object value, Material material, short data, String name, String... descriptions)
public SelectInventory add(Object value, Material material, int amount, short data, String name, String... descriptions)
public SelectInventory add(Object value, ItemStack itemStack, String name, String... descriptions)

public SelectInventory add(Object value, Material material, String name, List<String> descriptions)
public SelectInventory add(Object value, Material material, int amount, String name, List<String> descriptions)
public SelectInventory add(Object value, Material material, short data, String name, List<String> descriptions)
public SelectInventory add(Object value, Material material, int amount, short data, String name, List<String> descriptions)

There are all methods 3 times with different naming parameters.

The parameter value is important for checking the selected item. The data represents the damage value in the ItemStack constructor.

You can also remove existing selections or clear all ones:

public SelectInventory remove(Object value)
public SelectInventory clear()

You can request all added selections using a typical getter:

public List<Selection> getSelections()

Adding or removing selections will repaint the inventory, but don't resize it (it's currently not possible).

Open/Close the Inventory

You can check if the inventory is open or open and close the inventory view for a player or all players (only close) with those simple methods:

public boolean isOpen(Player player)
public void open(Player player)
public void close(Player player)
public void closeAll()

You can do this as many times you want. This will show up/hide the inventory.

Layout the Items

The items get layouted in the inventory by the default LineInventoryLayouter which sorts them horizontally in simple lines. Every layouter implements the interface InventoryLayouter and one method:

public InventoryLayout getLayout(SelectInventory selectInventory, List<Selection> selections)

The layouter gets the select inventory and all possible selections as a shortcut. Every Selection-object has two methods for getting the stored data:

public Object getValue()
public ItemStack getItemStack()

The layouter should returns an InventoryLayout which describes the arrangement of the items in a table format for easy usage.

You can create a new empty InventoryLayout with the default constructor and use it with some methods:

public int getMaxColumns()
public Selection get(int x, int y)
public boolean set(int x, int y, Selection selection)
public List<Selection> getColumn(int x)
public boolean setColumn(int x, List<Selection> column)
public List<Selection> getRow(int y)
public boolean setRow(int y, List<Selection> row)
public List<List<Selection>> getLayout()
public void setLayout(List<List<Selection>> layout)

Here's the example for the LineInventoryLayouter:

public InventoryLayout getLayout(SelectInventory selectInventory, List<Selection> selections) {

    InventoryLayout layout = new InventoryLayout();

    int x = 0;
    int y = 0;
    for (Selection selection : selections) {
        layout.set(x, y, selection);

        y++;
        if (y >= 9) {
            y = 0;
            x++;
        }
    }

    return layout;
}

You can set a custom layouter in the contructor and with setLayouter(Layouter layouter).

Check User Selections

If a player click on a registered option (see above), the abstract metod onClick() get called. The methods holds two parameters:

protected void onClick(Selection selection, ClickType clickType, Player player)

There's an enum for the click type in the class. Currently, there're four values:

Every type has three methods which can output the concrete boolean values:

public boolean isLeft()
public boolean isRight()
public boolean isShift()

Example Usage

Basic Demonstration

Here's a very basic example with no real function which basically adds 2 items with names to the inventory. If you click them, you will get a message with the name of the item (stored in the value) and the inventory will get closed.

new SelectInventory(plugin, ChatColor.DARK_RED + "Example GUI") {

    @Override
    protected void onClick(Selection selection, ClickType clickType, Player player) {

        close(player);
        player.sendMessage(ChatColor.GOLD + "Your value: " + ChatColor.AQUA + selection.getValue());
    }
}.add("Gold", Material.GOLD_INGOT, ChatColor.GOLD + "Click for Gold").add("Diamond", Material.DIAMOND, ChatColor.AQUA + "Click for Diamond").openInventoryView(player);

GUI Teleporter

Here's another example usage of the class which creates an advanced GUI-Teleporter with every player on the server. You can teleport yourself to a player (left click) or teleport a player to you (right click).

if (Bukkit.getOnlinePlayers().length == 0) {
    player.sendMessage(ChatColor.RED + "There is no other player online!");
} else {
    SelectInventory inventory = new SelectInventory(plugin, ChatColor.DARK_RED + "GUI Teleporter") {

        @Override
        protected void onClick(Selection selection, ClickType clickType, Player player) {

            close(player);

            Player teleportPlayer = Bukkit.getPlayerExact(String.valueOf(value));
            if (teleportPlayer.isOnline()) {
                if (clickType.isLeft()) {
                    player.teleport(teleportPlayer);
                    player.sendMessage(ChatColor.GRAY + "You got teleported to " + teleportPlayer.getDisplayName() + ChatColor.GRAY + "!");
                } else if (clickType.isRight()) {
                    teleportPlayer.teleport(player);
                    player.sendMessage(ChatColor.GRAY + "You teleported " + teleportPlayer.getDisplayName() + ChatColor.GRAY + " to you!");
                }
            } else {
                player.sendMessage(ChatColor.RED + "The player " + teleportPlayer.getDisplayName() + ChatColor.RED + " isn't online anymore!");
            }
        }
    };

    for (Player teleportPlayer : Bukkit.getOnlinePlayers()) {
        if (!player.equals(teleportPlayer)) {
            inventory.add(teleportPlayer.getName(), Material.SKULL_ITEM, ChatColor.GOLD + teleportPlayer.getDisplayName());
        }
    }
    inventory.open(player);
}