Skip to content
stefvanschie edited this page Jun 13, 2022 · 9 revisions

Languages: Dutch (Nederlands)

Gui Items differ slightly from a normal ItemStack in the way that they have an action assigned to them. That action will be called once someone clicks on that item, so you don't have to register any InventoryClickEvents anymore.

Creating a Gui Item is really simple, just take your normal ItemStack and put it in the GuiItem constructor.

GuiItem item = new GuiItem(new ItemStack(Material.STONE));

If you want this Gui Item to have an action, you can specify one as the second parameter.

GuiItem item = new GuiItem(new ItemStack(Material.STONE), event -> event.getWhoClicked().sendMessage("You clicked!"));

And that's basically it for the non-XML section.

XML

But the XML section on the other hand, is huge.

Alright let's go through this step by step. So to add an item to a pane, just add an item element as a child to this pane.

<outlinepane x="0" y="0" length="9" height="6">
  <item/>
</outlinepane>

Every item at least needs an id, which is the same as the normal Material value. The value is not case sensitive.

<item id="stone"/>

This is the bare minimum for an item, so we'll now be going to the optional attributes.

Optional attributes

So we can also add how many of the item there should be with the amount attribute. By default the amount is one.

<item id="stone" amount="5"/>

And we can also specify a damage value for the item. By default this is zero.

<item id="stone" damage="3"/>

We can, just like the GUIs also specify a field, populate and onClick attribute.

For the field, you do:

<item id="stone" field="item"/>

And in the code:

GuiItem item;

For the populate, you do:

<item id="stone" populate="populateItem"/>

And in the code:

public void populateItem(GuiItem item) {}

The return type can be anything you want, but the result will be discarded.

And for the onClick, you do:

<item id="stone" onClick="itemClick"/>

And in the code:

public void itemClick(InventoryClickEvent event) {}

The method specified needs to be public. The return type can be anything you want, but the result will be discarded. The InventoryClickEvent parameter is optional.

Optional children

So we can specify a display name to set as the display name for the item with the displayname element.

<item id="stone">
  <displayname>A custom Name!</displayname>
</item>

We can also specify a custom skull texture to use. This works only when the item is a skull item with damage value three. You can either specify the owner which takes the name of the owner and sets the texture to that player's skull or you specify the id which is the id from a minecraft texture that will be loaded from the internet. As an example when you have a texture like this one: http://textures.minecraft.net/texture/7c57f9192e81eb6897c24ecd4935cfb5a731a6f9a57abb51f2b35e8b4be7ebc, you specify the id, which is the last part with all the numbers and letters: 7c57f9192e81eb6897c24ecd4935cfb5a731a6f9a57abb51f2b35e8b4be7ebc.

<item id="player_head">
  <skull owner="notch"/>
</item>

<item id="player_head">
  <skull id="7c57f9192e81eb6897c24ecd4935cfb5a731a6f9a57abb51f2b35e8b4be7ebc"/>
</item>

And we can specify a lore across multiple lines with the lore element which consists of one or more line elements.

<item id="stone">
  <lore>
    <line>Line 1</line>
    <line>Line 2</line>
  </lore>
</item>

We can also specify enchantments with the enchantments element with multiple enchantment elements inside. These have an id and a level. The id is the name of the enchantment (not case sensitive) and the level is the level of the enchantment.

<item id="stone">
  <enchantments>
    <enchantment id="durability" level="3"/>
    <enchantment id="arrow_damage" level="2"/>
  </enchantments>
</item>

If your item requires a custom model data to be set (e.g., because of a resource pack), you can specify that in the XML as well with the modeldata element.

<item id="stone">
  <modeldata>1</modeldata>
</item>

Now, the last thing is a tad more complicated. Alright, imagine we have a GUI in which we can change our flight speed to five different settings. One for default, one for slightly faster, one for even faster, etc. Now each item points to a different method, but you notice the methods end up looking very similar:

public void flightSpeed1(InventoryClickEvent event) {
  ((Player) event.getWhoClicked()).setFlySpeed(0.1F);
}

public void flightSpeed2(InventoryClickEvent event) {
  ((Player) event.getWhoClicked()).setFlySpeed(0.2F);
}

public void flightSpeed3(InventoryClickEvent event) {
  ((Player) event.getWhoClicked()).setFlySpeed(0.3F);
}

//etc.

Obviously this is quite a waste. That's why there are properties. Properties are additional parameters that will be send when someone clicks and are defined in the GUI. With these properties you can make one universal method for all your flight speeds and define in the XML file what the value should be. So let's take a look at our properties in the XML file.

<item id="stone" onClick="flightSpeed">
  <properties>
    <property type="float">0.1</property>
  </properties>
</item>
<item id="stone" onClick="flightSpeed">
  <properties>
    <property type="float">0.2</property>
  </properties>
</item>

As you can see we can specify a property and give it a value, in our case 0.1 and 0.2. We can specify a type for the property, in this case float. There are nine different types.

  • byte
  • short
  • integer
  • long
  • float
  • double
  • character
  • boolean
  • string

If you don't specify a type, string will be chosen as the default one. We can also have multiple properties for one item. Do note that if you have two items pointing at the same method, that these items have to have the same property types, in the same order.

So our code can now be changed to:

public void flightSpeed(InventoryClickEvent event, float speed) {
  ((Player) event.getWhoClicked()).setFlySpeed(speed);
}

Which can be used for all our fly speeds. The speed parameter will have the value specified in the XML file and will be applied to every player dynamically.

If you want you can register your custom property types as well. Just do:

Gui.registerProperty("myproperty", string -> {});

The string is the text entered and you should make it return the type you want it to be.

Clone this wiki locally