📚Full Examples

Here you can find examples of how pagination can be used

Example 1

In Example 1, we show a simpler pagination setup. The code includes the following:

  1. Define elements per page

  2. Define SlotIterator

  3. Create navigation item

  4. Add elements to the pagination

Description

  • We say that only 7 elements are allowed per page.

  • We add the SlotIterator so it knows where to start and how.

  • We set an item at slot 0. This shows by the display name whether we are on the first page or what the previous page is. If we click, we get to the previous page.

  • We add 13 items to the pagination. (Daras result 2 pages. On page 1 there are 7 items and on the 2 page 6 items).

  • We put an item at the end of the inventory. This shows by the display name if we are on the last page or what the next page is. When we click, we go to the next page.

Code

RyseInventory.builder()
        .title("Example 1")
        .rows(1)
        .provider(new InventoryProvider() {
            @Override
            public void init(Player player, InventoryContents contents) {
                Pagination pagination = contents.pagination();
                pagination.setItemsPerPage(7);
                
                pagination.iterator(SlotIterator.builder()
                        .startPosition(1)
                        .type(SlotIterator.SlotIteratorType.HORIZONTAL)
                        .build());
                        
                contents.set(0, 0, IntelligentItem.of(new ItemBuilder(Material.ARROW).
                        amount(pagination.isFirst()
                                ? 1
                                : pagination.page() - 1)
                        .displayName(pagination.isFirst()
                                ? "§c§oThis is the first page"
                                : "§ePage §8⇒ §9" + pagination.newInstance(pagination).previous().page()).build(), event -> {
                    if (pagination.isFirst()) {
                        player.sendMessage("§c§oYou are already on the first page.");
                        return;
                    }
                    RyseInventory currentInventory = pagination.inventory();
                    currentInventory.open(player, pagination.previous().page());
                }));
                for (int i = 0; i < 13; i++) {
                    pagination.addItem(IntelligentItem.empty(new ItemStack(Material.MAGMA_CREAM)));
                }
                int page = pagination.newInstance(pagination).next().page();
                contents.set(0, 8, IntelligentItem.of(new ItemBuilder(Material.ARROW)
                        .amount((pagination.isLast() ? 1 : page))
                        .displayName(!pagination.isLast()
                                ? "§ePage §8⇒ §9" + page :
                                "§c§oThis is the last page").build(), event -> {
                    if (pagination.isLast()) {
                        player.sendMessage("§c§oYou are already on the last page.");
                        return;
                    }
                    RyseInventory currentInventory = pagination.inventory();
                    currentInventory.open(player, pagination.next().page());
                }));
            }
        })
        .build(this)
        .openAll();

Preview

Example 2

Example 2 shows how to use the blacklist in SlotIterator.

Description

  • We say that 16 elements per page are allowed.

  • We define our slot iterator. We start at slot 0. The slot is ignored because of the blacklist.

  • We add 13 elements to the pagination. (As a result, we have only 1 page. We allow 16 elements but add only 13. So not even the 1 page is filled).

Code

RyseInventory.builder()
        .title("Example 2")
        .rows(2)
        .provider(new InventoryProvider() {
            @Override
            public void init(Player player, InventoryContents contents) {
                Pagination pagination = contents.pagination();
                pagination.setItemsPerPage(16);
                
                pagination.iterator(SlotIterator.builder()
                        .startPosition(0)
                        .blackList(2)
                        .type(SlotIterator.SlotIteratorType.HORIZONTAL)
                        .build());
                        
                for (int i = 0; i < 13; i++) {
                    pagination.addItem(IntelligentItem.empty(new ItemStack(Material.MAGMA_CREAM)));
                }
            }
        })
        .build(this)
        .openAll();

Preview

Last updated