Placeholders

This page describes how to create and register custom placeholders.

A placeholder is used to display a value in a text. The library provides default placeholders but also enables developers to create their own placeholders to display their custom values in texts.

Create a placeholder

A placeholder is defined by creating a class that implements the Placeholder interface. The following code represents a placeholder for the player's ping:

public class PlayerPingPlaceholder implements Placeholder {

    @Override
    public String getName() {
        return "%player_ping%";
    }

    @Override
    public String getValue(Context context) {
        InventoryViewer viewer = context.getData(CommonContextKey.VIEWER.name(), InventoryViewer.class);
        Player player = viewer.getPlayer();
        return Integer.toString(player.getPing());
    }

    @Override
    public boolean accept(Context context) {
        return context.hasData(CommonContextKey.VIEWER.name());
    }
}

When using the Placeholder interface, several methods must be implemented:

Method name
Description

String getName()

Returns the name of the placeholder. It is recommended to use the format %placeholder_name%.

String getValue(Context context)

Returns the value the placeholder for the provided context.

boolean accept(Context context)

Check that the placeholder can be applied using the provided context.

Note: Some of these methods relies on a Context that is explained in the Context page.

Register a placeholder

To be used in an inventory, a placeholder must be registered in its InventoryDescriptor. This can be done by redefining the addPlaceholders(PlaceholderManager manager) method. The following code registers the placeholder created in the previous step of this tutorial:

Placeholders for pagination items

The library enables developers to paginate items in an inventory and it is then a common use case to need to display some values of what is paginated. This can be done using placeholders.

The process is nearly the same as the previous example. However, the only difference is that, as inventories may contain several paginations, you must ensure that the placeholder is only applied on items of the targeted pagination.

This is done by taking the pagination id as parameter in the class constructor and comparing it in the accept(Context context) method. The following code gives an example for a pagination that paginates a list of Integer:

Last updated