Skip to content

Placeholders

Bogdan T edited this page Jan 27, 2024 · 6 revisions

This library has its own placeholders feature, allowing you to conveniently replace text blocks with data.

The pattern it uses is the following: %{placeholderId}_{args_if_supported}%

Structure

The placeholders are divided into 2 types:

  1. Registrable, i.e. global
  2. Injectable, i.e. local

There is the PlaceholderManager, which is used to save the global placeholders and format the text.

Registrable

The Registrable placeholders are the ones which are automatically used in every text which you try to format.

To register it you need to:

PlaceholderManager.registerPlaceholder(RegistrablePlaceholder object)

RegistrablePlaceholder is just an interface, so, you need to create an implementation for it, or just use SimplePlaceholder and DynamicPlaceholder. In most cases you will be fine with SimplePlaceholder, but if you need a placeholder to support arguments, then use DynamicPlaceholder

Example:

    new SimplePlaceholder(GTWModClient.instance,"player_level",
            ()-> String.valueOf(playerData.getLevel())
    ).register();

    new SimplePlaceholder(GTWModClient.instance,"player_rank",
            ()-> playerData.getRank()
    ).register();

    new SimplePlaceholder(GTWModClient.instance,"player_gang",
            ()-> playerData.getGang()
    ).register();

The second parameter is the pattern. I.e. %player_level% %player_rank% %player_gang%

Injectable

It is the placeholder which is applied only if it is in a PlaceholderContext.

The PlaceholderContext contains the InjectablePlaceholderList objects.

InjectablePlaceholderList contains the Injectable placeholders.

It is made this way, to be able to implement the InjectablePlaceholderList to the classes which might be beneficial to support injectable placeholders. The Config class and DisplayRenderer are such classes

PlaceholderContext can also merge different InjectablePlaceholderList objects, it makes the injecting pretty simple.

Where is it used in a lib:

image

image

image

And more

Clone this wiki locally