Skip to content

Scheduled Updates

Natan Vieira edited this page Jul 28, 2023 · 9 revisions

Scheduled Updates is a Inventory Framework built-in feature that allows the developer to schedule automatic updates for any view, basically it's a "call update every X period".

Table of Contents

Configuration

Scheduled update is part of the view configuration, so use onInit to set it.
The configuration is called scheduleUpdate and has as a parameter the time interval of each update in ticks.

@Override
public void onInit(ViewConfigBuilder config) {
    config.scheduleUpdate(intervalInTicks);
}

Code Examples

Automatic update every 1 second (20 ticks)

@Override
public void onInit(ViewConfigBuilder config) {
    config.scheduleUpdate(20L);
}

Since auto-update just updates the view, dynamically-rendered items will react to this update.
The example below uses Dynamic Item Rendering and Advanced State Management.

private final MutableIntState counterState = mutableState(0);

@Override
public void onInit(ViewConfigBuilder config) {
    config.scheduleUpdate(20L);
}

@Override
public void onFirstRender(RenderContext render) {
    render.firstSlot().renderWith(() -> new ItemStack(
            /* type = */   Material.DIAMOND, 
            /* amount = */ counterState.increment(render)
    ));
}

How It Works Internally

There is a global scheduler for all scheduled updates for all contexts in a view.

When a new player opens a view, this view is added to a collection of lists of this scheduler responsible for the automatic update that every certain period of time executes the update() function of all the contexts currently present in the view.

  • The trigger for the internal scheduler to run the automatic update is the beginning of a new player's view onOpen.
  • When a player closes a view, if its context is considered invalid (there is no other viewer in the same context), also applying to all other contexts present in that view, the scheduler is interrupted.
Clone this wiki locally