Creating sessions and templates - hyndor/SightMenu GitHub Wiki

Creating simple template with builder

As I have already mentioned template represents an actual view of future menu session. Let's try to create one.

private void initSingleTemplate() {
        MenuTemplate template = apiInstance
                .templateBuilder()
                .singleTemplate()
                .setName("Cool menu name")
                .setOpenProcessor(MenuOpenProcessors.standardOpen())
                .setRows(5)
                .createMenuTemplateImpl();
    }

What have we done? From our apiInstance we got templateBuilder, asked for single template (later we'll discuss paginated template), set name (Menu title), set open processor(Class that do open our menu as bukkit inventory), set rows in our menu and finally we have created menu template.

Items

For now our template doesn't have any items. Template consists of many MenuItem objects. SightMenu provides you with two default implementations of MenuItem, cached and per player. What is the difference? If the icon is static and is the same for all players you should select cached, if it is dynamic for example you want to show player's name in lore, then you select per player MenuItem. Let's add some items to our template.

private void initSingleTemplateWithItems() {
       apiInstance
                .templateBuilder()
                .singleTemplate()
                .setName("Cool menu name")
                .setOpenProcessor(MenuOpenProcessors.standardOpen())
                .setRows(5)
                .withItem(
                        apiInstance.itemBuilder()
                                .perPlayerItem()
                                .withClickListener(menuItemClick -> menuItemClick.getPlayer().sendMessage("You have clicked! Congratulations"))
                                .withClickListener(menuItemClick -> menuItemClick.getPlayer().sendMessage("Yeah. You can specify as many click listeners as you want"))
                                .setIconRequestConsumer(iconRequest -> new MenuIcon(ItemStackBuilder.create()
                                        .setMaterial(Material.STONE)
                                        .addBlankLore()
                                        .addLore("Your name: " + iconRequest.getPlayer().getName())
                                        .build(), 5)
                                )
                                .build()
                )
                .withItem(
                        apiInstance.itemBuilder()
                                .cachedItem()
                                .setMenuIcon(new MenuIcon(ItemStackBuilder.create()
                                        .setMaterial(Material.STONE)
                                        .build(), 6)
                                )
                                .build()
                )
                .createMenuTemplateImpl();
}

MenuItem constructor consists of two parameters: MenuIcon and index. Index system starts with 0.

Creating session

Ok. When we have our template for our session we want to show that template for players, we have to create menu session. You should get MenuFactory object and invoke createSingleSession with parameters: template and player. Let's see an example.

apiInstance.getMenuFactory().createSingleSession(player, template);

And that's all you need to create session. When you invoke create session it is automatically shows menu to the player.

I hope that now you have a basic understanding of how SightMenu works and how to create single menu session