-
Notifications
You must be signed in to change notification settings - Fork 85
GUIs are the main part of the framework and the entry point for every pane and inventory. The GUI is always the top-level part of the inventory. A single GUI has the same characteristics for every player viewing it. E.g. if two players are seeing the same GUI and you change the title of the GUI, both players will get to see the new title. If you want the GUI's behavior to be independent for every player, you'll have to create a new GUI for every player.
Alright, let's see how to create GUIs.
To create a GUI simply create a new Gui
object. The Gui
takes in three parameters. The first one is the main class of your plugin. The second one is the amount of rows the GUI has and the third one is the title of the GUI.
See how the GUI takes in the amount of rows the GUI has, not the amount of slots. So where as you would previously specify 36 slots, you'd now specify 4 rows.
Gui gui = new Gui(plugin, 5, "My GUI");
This will create a GUI with 5 rows and a title saying "My GUI".
If you want to change the amount of rows or the title later, you can call setRows
and setTitle
respectively.
Every method called on a GUI or pane requires you to update the GUI manually. These two methods are an exception however. Due to technical reasons, these two methods will cause an update by themselves. If possible, call these methods last in a chain as you don't have to update the GUI again.
There are also getters for the amount of rows and the title, getRows
and getTitle
(resp.).
If you make any change to any part of the GUI, you'll have to update it. You can do so by calling update
on the GUI object to update the GUI for every player that is currently viewing it.
gui.update();
You can specify what will happen when a player clicks on the GUI and what happens when you close it. You can do so by calling setOnLocalClick
, setOnGlobalClick
and setOnClose
. Both take in a Consumer with an InventoryClickEvent and an InventoryCloseEvent respectively.
gui.setOnLocalClick(event -> {});
gui.setOnGlobalClick(event -> {});
gui.setOnClose(event -> {});
Don't use these methods for click events on single items; there are much easier ways of doing this that'll be explained later.
The difference between the local click and the global click is that the local click will only be called when the top inventory part is being clicked in. The global click will be called if a click was detected in the bottom part of the inventory.
If you want to for any particular reason, you can also get all the panes, all the items and the inventory itself from the GUI.
Collection<Pane> panes = gui.getPanes();
Collection<GuiItem> items = gui.getItems();
Inventory inventory = gui.getInventory();
If you have an XML file defined for your GUI, you can load it by calling load
on the Gui
class. This takes in three parameters; the main plugin class, the class you want to load the GUI into and the input stream pointing to the XML file.
Gui gui = Gui.load(plugin, this, myXMLFile);
Alright, so the plugin is self-explanatory, just the main class of your plugin.
The second parameter is a little different though. If you have specified any kind of methods or fields that will be accessed you need to specify the class in which these methods and fields are located. That's what the second parameter is for. This will almost always be the class you're in, so if you aren't really sure what to do, just specify this
.
The third parameter is the InputStream to your XML file. There is a built-in method in Bukkit for getting this, namely
InputStream inputStream = plugin.getResource("gui.xml");
Where plugin, is your main plugin object and the "gui.xml" the name of your XML file.
To create a base GUI in an XML file add the following element to the file.
<gui/>
As attributes you are required to have a rows
and title
attribute. The rows
attribute specifies the amount of rows the GUI will have and the title
attribute specifies the title of the GUI.
<gui rows="5" title="My GUI"/>
You can specify several optional attributes to the GUI element. The first one is field
. With field
you can specify a field name in your code which should be initialized with the GUI once it is loading.
In the XML file:
<gui rows="5" title="My GUI" field="gui"/>
In the code:
Gui gui; //this field will get initialized with the gui
You can also specify an onClick attribute. Here you can specify the method name of the method that will be called once someone has clicked on the GUI.
In the XML file:
<gui rows="5" title="My GUI" onClick="guiClick"/>
In the code:
public void guiClick(InventoryClickEvent event) {}
The InventoryClickEvent
parameter is optional.
You may set any return type you want, but the result will be ignored in all cases.
You can specify an onClose attribute. Here you specify the method name of the method that will be called once someone has closed the GUI.
In the XML file:
<gui rows="5" title="My GUI" onClose="guiClose"/>
In the code:
public void guiClose(InventoryCloseEvent event) {}
The InventoryCloseEvent
parameter is optional.
You may set any return type you want, but the result will be ignored in all cases.
You can specify a populate attribute. Here you specify the method name of the method that will take over the population of the GUI when loading. Any child elements appended to the GUI element will be ignored when this attribute is set.
In the XML file:
<gui rows="5" title="My GUI" populate="populateGui"/>
In the code:
public void populateGui(Gui gui) {}
You may set any return type you want, but the result will be ignored in all cases.