Dynamic Title Update - devnatan/inventory-framework GitHub Wiki

The Dynamic Title Update feature allows the developer to change the title of a view that's open, being displayed to the player, to be changed without requiring closing it, changing the title and opening it again.

Dynamic Title Update is a Protocol-Level Integration so its behavior is based on the current version and software of the server it is running on and may be inconsistent. See the Versions Compatibility table to know if it will work on your server.

Basic Usage

In the example below, we will create a counter with initial value of zero and schedules the view to update every 0,5 seconds and on each update the counter will be incremented by one.

private final MutableIntState counterState = mutableState(0);

@Override
public void onInit(ViewConfigBuilder config) {
    config.title("Initial title")
        .scheduleUpdate(10L /* intervalInTicks */);
}

@Override
public void onUpdate(Context update) {
    int count = counterState.increment(update);
    String newTitle = String.format("Updated title: %d", count);

    update.updateTitleForEveryone(newTitle);
}

Using resetTitle* after updateTitle* resets the title to "Initial title".

This example uses State Management and Scheduled Updates features.

Using Text Components (v3.7.0+)

Starting from v3.7.0, you can use Adventure Text Components as inventory titles.

[!NOTE] To use Adventure components as titles, make sure the inventory-framework-platform-paper module is on your classpath. See the Installation guide for details.

private final MutableIntState countState = mutableState(0);

@Override
public void onInit(ViewConfigBuilder config) {
    config.title("&4Auto update")
        .scheduleUpdate(10L /* intervalInTicks */);
}

@Override
public void onUpdate(Context update) {
    int count = countState.increment(update);
    Random random = ThreadLocalRandom.current();
    TextColor textColor = TextColor.color(
            random.nextInt(0, 255),
            random.nextInt(0, 255),
            random.nextInt(0, 255)
    );
    Component newTitle = Component.text(
        String.format("Auto update (%d)", count),
        textColor
    );

    update.updateTitleForPlayer(newTitle);
}

API Methods Signature

// Updates the title for all players in the context
public void updateTitleForEveryone(String title);

// Updates the title for the subject player of the context
public void updateTitleForPlayer(String title);

// Updates the title for a specific player in the context
public void updateTitleForPlayer(String title, T player);

// Resets the title to the initial title of the context for all players in the context
public void resetTitleForEveryone();

// Resets the title to the initial title for the subject player of the context
public void resetTitleForPlayer();

// Resets the title to the initial title of the context for a specific player
public void resetTitleForPlayer(T player);

updateTitleForPlayer and resetTitleForPlayer the "player" is the subject of the context, if it's used in a click context the title will be updated only to the player that clicked on something.

Versions Compatibility

Server version Minimum IF version Status
1.8 v1.0 ✅ Supported
1.9–1.16 v2.4.0 ✅ Supported
1.17–1.18 v3.0.0 ✅ Supported
1.19 v3.0.0-rc.2 ✅ Supported
1.20 v3.0.2 ✅ Supported
1.20.4 v3.1.0 ✅ Supported
1.21.1–1.21.4 v3.3.0 ✅ Supported
1.21.5–1.21.10 v3.5.5 ✅ Supported