Skip to content
Amaury Carrade edited this page Apr 18, 2017 · 8 revisions

This component displays complexe Minecraft GUIs with ease.

Documentation JavaDoc
Loader class
(for loadComponents)
fr.zcraft.zlib.components.gui.Gui

zLib comes with a full-featured component to create GUIs (Graphical User Interfaces), either read-only or read-and-write. There is also a specific class to ask for any text through a sign.

Basic chest GUIs (« action GUI »)

All chest GUIs are created using a class extending ActionGui. The only method you have to override is onUpdate, called when the GUI is created or updated.

In this method, you'll first have to define the GUI's properties, using the setSize and setTitle methods. Then, to populate the GUI with items, use the action methods. They all take an action name (keep that in mind, we will talk about them later), then a slot ID (from 0 to the size minus one), and finally some kind of ItemStack (an ItemStack, an ItemStackBuilder, or some variants with a Material, title, lore... directly).

Simple enough. As example, you may have this now:

public class MyGUI extends ActionGui
{
    @Override
    public void onUpdate()
    {
        setTitle(ChatColor.BLACK + "My toasting GUI!");
        setSize(9); // One line

        action("my_action", 0, new ItemStack(Material.CARROT));
        action("close", 8, Material.BARRIER, "Close GUI");
    }
}

You can open this GUI using this method—it's the same for all zLib GUIs, where player is a Player.

Gui.open(player, new MyGUI());

Ok so, now, if you execute this, you'll have a nice GUI. How to do something when the player clicks somewhere? It's really easy.

Remember the names we gave to the actions? These will be used to reference methods executed when the item representing the action is clicked. You'll just have to annotate the method with @GuiAction, like so:

@GuiAction("my_action")
private void the_method_executing_my_action()
{
    // do something (anything!)
}

Yes, that's all! To give another example, here is a working implementation of the second action defined in the example:

@GuiAction("close")
private void close_gui()
{
    close();
}

(see the javadoc to get a list of usable methods in GUIs; don't forget to check parent classes too!).

The action methods can either take no argument, or the underlying InventoryClickEvent.

GUIs hierarchy

If you use Gui.open(player, new MyGUI()); and close the GUI, the GUI will be... well, closed. But if it was opened from another GUI, you may want to reopen this GUI (think of it like a sub-GUI). If you want this behaviour, open the GUI this way:

Gui.open(player, new MyGUI(), parentGUI);

where parentGUI is a reference to another GUI, re-opened when the GUI is closed using it's method close(). As you will typically open sub-GUIs from the parent GUI, the code will look, most of the time, like this:

Gui.open(player, new MyGUI(), this);
Clone this wiki locally