Pagination - hyndor/SightMenu GitHub Wiki

Paginated template

Paginated template consists of many simple single templates, also for easy template creation sight menu api provides you with paginated template builder. All you need to create a paginated template is just specify some pages and also specify main page which will be opened first. You'll write smth simillar to this.

    private PaginatedMenuTemplate initPaginatedTemplate() {
        List<MenuTemplate> templates = initManySingleTemplates();
        return apiInstance
                .templateBuilder()
                .paginatedTemplate()
                .setMainPage(templates.get(0))
                .setPages(templates)
                .build();
    }

    private List<MenuTemplate> initManySingleTemplates() {
        List<MenuTemplate> templates = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            templates.add(createOneFromPaginatedTemplate());
        }
        return templates;
    }

Ok. We have created seven default single templates and set them as pages, also we have specified the main page.

Paginated session

All we need to create a paginated session is just to make a one method call.

apiInstance.getMenuFactory().createPaginatedSession(event.getPlayer(), paginatedMenuTemplate);

And... that's all. Main page will be opened. But wait, how can we create for example item that will change pages? Menu session can act as a proxy. We can send header(payload) to menu session and also can subsribe for that headers. PaginatedSession subscribes for payloads and can change pages when receives headers.

    private MenuTemplate createOneFromPaginatedTemplate() {
        return apiInstance
                .templateBuilder()
                .singleTemplate()
                .setName("Page " + count++)
                .setOpenProcessor(MenuOpenProcessors.standardOpen())
                .setRows(5)
                .withItem(
                        apiInstance
                                .itemBuilder()
                                .cachedItem()
                                .withClickListener(menuItemClick -> {
                                    menuItemClick.getPlayer().sendMessage("We are going to the next page");
                                    menuItemClick.getSession().sendHeader(MenuHeaders.SWITCH_NEXT_PAGE);
                                })
                                .setMenuIcon(new MenuIcon(new ItemStack(Material.STONE), 5))
                                .build()
                )
                .createMenuTemplateImpl();
    }

MenuHeaders class contains some useful headers. There is also another way to move through pages, directly using InventorySwitcher.

               .withItem(
                        apiInstance
                                .itemBuilder()
                                .cachedItem()
                                .withClickListener(menuItemClick -> {
                                    apiInstance.getSessionResolver().getLastPaginatedSession(menuItemClick.getPlayer()).get().getSwitcher().switchNext();
                                })
                                .setMenuIcon(new MenuIcon(new ItemStack(Material.STONE), 5))
                                .build()
                )

We have used new class session resolver. It manages all active sessions. For example you want to get currently opened player's session, so you can simply call to session resolver.

Optional<MenuSession> sessionOptional = apiInstance.getSessionResolver().getSession(menuItemClick.getPlayer());
sessionOptional.ifPresent(menuSession -> menuItemClick.getPlayer().sendMessage("Ohh. You have opened session"));