Pagination

This page describes how to register paginations in an inventory provider.

Pagination is the fact of splitting a list of values into pages. This is very useful when you want to display a large list of values in a container with a restricted size.

In CraftVentory, you can paginate a list of Java objects and convert each item to an InventoryItem to be displayed in an inventory.

Pagination configuration

To learn how to configure a pagination in an inventory configuration file, please refer to the Paginations configuration section.

Create a pagination provider

Like inventories, paginations are configurable. As a pagination relies on a list of Java object, there must be a link between its configuration and this list.

Let's say we want to paginate the following list of integers:

List<Integer> values = new ArrayList<>();

for(int i = 0; i < 50; i++) {
    values.add(i);
}

To paginate this list, you must create a pagination provider to let the library understand what data is paginated. This can be done by redefining the addProviders(ProviderManager manager) method from the InventoryDescriptor interface:

public class CustomInventoryDescriptor implements InventoryDescriptor {

    ...

    @Override
    public void addProviders(ProviderManager manager) {

        List<Integer> values = new ArrayList<>();

        for(int i = 0; i < 50; i++) {
            values.add(i);
        }
        
        manager.addProvider(new PaginationProvider<>("pagination-1", Integer.class, inventory -> values));
    }
    
    ...
} 

The PaginationProvider class takes the following paramaters in its constructors:

  • paginationId: The id of the pagination. It must be the same as the one defined in the configuration file of the inventory.

  • dataType: The data type that will be paginated. In the example, it is an Integer but it must be any Java class type.

  • supplier: A function that retrieves and returns the list of values to paginate. Here, we simply use the variable values defined above but we can imagine that the list comes from another class.

An inventory can contain several paginations. Each pagination must have its own pagination provider.

Example

Configuration :

id: "custom-inventory"

type: "CHEST_9x6"

title: "&6Custom title"

pattern:
  - "&&&&&&&&&"
  - "&1111111&"
  - "&1111111&"
  - "&1111111&"
  - "&P&&&&&N&"
  - "&&&&&&&&C"

content:
  close:
    item:
      type: BARRIER
    symbol: "C"
    actions:
      close:
        type: "CLOSE"

paginations:
  pagination-1:
    id: "pagination-1"
    symbol: "1"
    pagination-item:
      item:
        type: DIAMOND
        name: "Item n°%pagination_item_number%"
    previous-page-item:
      item:
        type: ARROW
        name: "Page %previous_page%"
      symbol: "P"
    next-page-item:
      item:
        type: ARROW
        name: "Page %next_page%"
      symbol: "N"

Last updated