Skip to content

GUIUtils

MrLetsplay edited this page Oct 13, 2018 · 22 revisions

This page is currently outdated and needs updating

GUIUtils

GUIUtils is a class for the easy creation of complex GUIs. All the GUI actions are handled by MrCore and do not have to be fetched manually. It still provides full access to the events fired by Spigot

Creating a basic GUI

GUIBuilder

To create a GUI you need a GUIBuilder.
You can either use the constructor which takes the number of rows (1 - 6)

GUIBuilder builder = new GUIBuilder(title, rows);

or the one which takes an InventoryType (beware, not all InventoryTypes may work!)

GUIBuilder builder = new GUIBuilder(title, type);

title is just the title of the inventory. You can set this to whatever you want.
All the steps after this are exactly the same for both one of these methods.

GUIElement

GUIElements are basically the core to make interactive GUIs.
You can add an element as simple as saying

builder.addElement(slot, new StaticGUIElement(itemStack));

though that just adds a non-clickable element with the fixed ItemStack itemStack

To add a dynamic, interactive element to the builder, you can use

builder.addElement(slot, new GUIElement() {

    public ItemStack getItem(Player p) {
      // Here you need to return an ItemStack for that specific player
    }

  }.setAction(new GUIElementAction() {

    public void onAction(GUIElementActionEvent event) {
      // Here you can handle the onClicked event
      // You can also access the underlying InventoryClickEvent if you need to
    }

  });

You can also add an action to a StaticGUIElement

Building the GUI

To actually use the GUI, you need to build the GUI using

GUI gui = builder.build();

You can save this GUI instance to a variable as it doesn't need to be rebuilt every single time (in normal cases at least).

Opening the GUI to a player

You can open a built GUI to a player (convert it into a bukkit Inventory) by calling

gui.getForPlayer(player);

Every subfunction to make the GUIElements into ItemStacks will be passed this player. This also means that you don't have to pass one if you don't rely on it being given to you

And that's pretty much it (for the single page GUI at least). Now you can create a really complex GUI with just a few simple function calls

Multi page GUIs

All the basic functions of the GUIBuilder are available as described above.
This section just describes the features specific to the GUIBuilderMultiPage

Setting the supplier

Any multi page GUI needs to have a supplier to provide the dynamically changing page items.
You can set it using

builderMulti.setSupplier(new ItemSupplier<T> {

    public List<T> getItems() {
      // Here you need to return a list of all items for the GUIMultiPage
    }

    public ItemStack toItemStack(T item, Player p) {
      // This method is used to convert an element of type T to a bukkit ItemStack
    }

  });

Setting the page item slots

To tell MrCore where to place the items you supply like described above, you need to call either of these methods.

builderMulti.addPageSlots(someSlot, someOtherSlot, etc);

or

builderMulti.addPageSlotsInRange(from, to);

This will be used to calculate the number of items on each page.
MrCore will place the items according to the order they're added in

Adding "switch page" elements

MrCore allows you to add basic "switch page" elements really easily.
The only thing you need to do is call

builderMulti.addNextPageElement(slot, nextPageItemStack);

and/or

builderMulti.addNextPageElement(slot, previousPageItemStack);

(Note: You can also make next/previous page elements yourself using the GUI properties described below)

Other useful features

GUI Properties

Default properties

Currently, only the GUIMultiPage contains a default property, "page".
You can access it using

int page = (int) ((GUIHolder)inv.getHolder()).getProperties().get("page");

where inv is the inventory (not GUI!)
You can also achieve the same by calling

int page = GUIMultiPage.getPage(inv);

Custom Properties

The GUIBuilder defines a method to set default properties for all sub-instances of this GUI. All GUIHolders (which you can get like described above) will contain these

builder.setProperties(propertyHashMap);

You can access the HashMap as described above to also make instance-specific properties

Another, more convenient way of setting custom properties at build time is by using the #getForPlayer(..., Map<String, Object) methods
To make property management easier, the GUIUtils class also provides a class called "GUIHolderPropertyMap".
You can create an instance using ´´´java GUIHolderPropertyMap map = new GUIHolderPropertyMap(); ´´´ and then call the methods like you would on a GUIHolder

map.put("abc", "cde"); // Not recommended
map.put(plugin, "abc", "def"); // 'plugin' is your plugin instance. This method exists to prevent field name collision between plugins

map.get("abc"); // Returns "cde"
map.get(plugin, "abc"); // Returns "def"