Skip to content

GUI Contents For Developers

Fulminazzo edited this page Aug 6, 2024 · 10 revisions

This section is dedicated to server admins. If you are looking for the developers' page, click here

Many of the fields referenced on this page might be or require platform-specific values. Check the glossary


In this guide, we will go over the creation and management of GUI contents, the elements that will be displayed upon opening the GUIs. The goal of this page is to teach the developer how they can create, manage interactions and special effects with these special objects.

At the moment, only one implementation is present: ItemGUIContent. Since this class inherits from Item, its creation is very similar to the one of items, thanks to the method chaining system:

ItemGUIContent.newInstance("stone_sword").setDisplayName("Strong sword").setDurability(1337);

This means that all the Item methods are available, and the content can be handled as such. One might even create an Item first, and then convert it to an ItemGUIContent thanks to the copy(Class) method:

Item.newItem().setMaterial("dye").setDurability(4).setDisplayName("&9Custom Lapis Lazuli").copy(ItemGUIContent.class);

However, GUI contents allow further customization and more control over normal items. We will go through each option one by one to analyze them deeply and understand their meaning:

Table of contents
Requirements
Priorities
Sound
Actions
Variables

Requirements

The display of GUI contents can be controlled thanks to requirements. It is possible for certain elements to be hidden or replaced for users who do not meet some conditions. This can be done with the GUIContent#setViewRequirements(RequirementChecker) method in two ways:

  • using PermissionRequirement: a special type of requirement that will check if the viewer has the specified Minecraft permission;
    ItemGUIContent.newInstance().setViewRequirements(new PermissionRequirement("bukkit.command.version"));
    // Or, short version
    ItemGUIContent.newInstance().setViewRequirements("bukkit.command.version");
  • using RequirementChecker, a general functional interface that accepts a lambda function with the viewer as the parameter and requires a boolean to be returned.
    ItemGUIContent.newInstance().setViewRequirements(v -> v.getName().equals("Fulminazzo"));

Priorities

The priority allows defining which content should be displayed first in the case of a conflict. Being of type int, negative values are allowed, but natural numbers are to be preferred. The content with higher priority will be displayed first.

GUIContent content = ItemGUIContent.newInstance(); 
// ...
content.setPriority(10);

Sound

As the names of the methods suggest, a sound for clicking with the content can be specified. The sound has to be of type Sound from the wrappers module.

GUIContent content = ItemGUIContent.newInstance(); 
// ...
content.setClickSound(new Sound("VILLAGER_HAPPY", 10.0F, 1.0F));

Actions

Variables

Similarly to what happens for GUIs, also contents accept variables that can be declared in this section. They will be then replaced upon displaying the full content:

GUIContent content = ItemGUIContent.newInstance(); 
// ...
content.setVariable("name", "Strong sword");