Skip to content

Code Samples

Natan Vieira edited this page Jul 16, 2023 · 16 revisions

Counter

Preview
private final MutableIntState counterState = mutableState(0);

@Override
public void onInit(ViewConfigBuilder config) {
    config.title("Counter")
        .cancelOnClick()
        .layout("  - # +  ");
}

@Override
public void onFirstRender(RenderContext render) {
    // Current count item
    render.layoutSlot('#')
        // Note the `watch` here, this will update the item when `counterState` gets updated
        .watch(counterState)
        .renderWith(() -> new ItemStack(Material.GOLD_INGOT, counterState.get(render)));

    // Decrement button
    render.layoutSlot('-', new ItemStack(Material.ARROW))
        .onClick(counterState::decrement);

    // Increment button
    render.layoutSlot('+', new ItemStack(Material.ARROW))
        .onClick(counterState::increment);
}
Clone this wiki locally