Skip to content

Velen Pagination Helper

Mihou edited this page Jun 30, 2021 · 5 revisions

📃 Velen Pagination

Velen also offers a helper class that helps you paginate items easily with the Paginate<T> class, an example usage can be seen below:

VelenCommand.of("paginate", "Tests pagination.", velen, (event, message, user, args) -> {

  List<String> testList = Arrays.asList("Test 0", "Test 1", "Test 2", "Test 3", "Test 4");
  
  // Create a Pagination object (you can also save the pagination object to reuse later).
  new Paginate<>(testList).paginate(event, new PaginateEvent<String>() {

    private EmbedBuilder embed(String currentItem, int arrow, int maximum) {
    
      // Remember to always add +1 to arrow for these types of stuff since
      // arrow returns the raw position which means it starts at 0 instead of 1.
      return new EmbedBuilder().setTitle("Item [" + (arrow + 1) + "/" + maximum + "]")
        .setDescription(currentItem).setColor(Color.BLUE);
        
    }

    private EmbedBuilder embed(String currentItem) {
      return new EmbedBuilder().setTitle("Item")
        .setDescription(currentItem).setColor(Color.BLUE);
    }

    @Override
    public MessageBuilder onInit(MessageCreateEvent event, String currentItem,
      int arrow, Paginator<String> paginator) {
      
      // This is the initial message that will be sent on start of pagination.
      return new MessageBuilder().setEmbed(embed(currentItem, arrow, paginator.size()));
      
    }

    @Override
    public void onPaginate(MessageCreateEvent event, Message paginateMessage, String currentItem,
      int arrow, Paginator<String> paginator) {
      
      // This is what will be executed when you paginate next or backwards.
      paginateMessage.edit(embed(currentItem, arrow, paginator.size()));
      
    }

    @Override
    public MessageBuilder onEmptyPaginator(MessageCreateEvent event) {
    
      // This is sent when the paginator has no items.
      return new MessageBuilder().setContent("There are currently no items!");
      
    }

    @Override
    public void onSelect(MessageCreateEvent event, Message paginateMessage, String itemSelected,
      int arrow, Paginator<String> paginator) {
      
      // Similar to onPaginate except this is sent whenever the user
      // has selected a page that they want.
      paginateMessage.edit(embed(itemSelected));
      
    }
    
  }, Duration.ofMinutes(5));
}).attach();

Similar to VelenEvent, you can also place the handler onto its own class. An example of such can be seen below:

ExamplePaginateEvent.class

This is the handler class for pagination.

class ExamplePaginateEvent implements PaginateEvent<String> {

    private EmbedBuilder embed(String currentItem, int arrow, int maximum) {
      return new EmbedBuilder().setTitle("Item [" + (arrow + 1) + "/" + maximum + "]")
        .setDescription(currentItem).setColor(Color.BLUE);
    }

    private EmbedBuilder embed(String currentItem) {
      return new EmbedBuilder().setTitle("Item")
        .setDescription(currentItem).setColor(Color.BLUE);
    }

    @Override
    public MessageBuilder onInit(MessageCreateEvent event, String currentItem,
      int arrow, Paginator<String> paginator) {
      return new MessageBuilder().setEmbed(embed(currentItem, arrow, paginator.size()));
    }

    @Override
    public void onPaginate(MessageCreateEvent event, Message paginateMessage, String currentItem,
      int arrow, Paginator<String> paginator) {
      paginateMessage.edit(embed(currentItem, arrow, paginator.size()));
    }

    @Override
    public MessageBuilder onEmptyPaginator(MessageCreateEvent event) {
      return new MessageBuilder().setContent("There are currently no items!");
    }

    @Override
    public void onSelect(MessageCreateEvent event, Message paginateMessage, String itemSelected,
      int arrow, Paginator<String> paginator) {
      paginateMessage.edit(embed(itemSelected));
    }
    
}

Main.class

The main class where you register all your Velen commands.

VelenCommand.of("paginate", "Tests pagination.", velen, (event, message, user, args) -> {
  List<String> testList = Arrays.asList("Test 0", "Test 1", "Test 2", "Test 3", "Test 4");
  new Paginate<>(testList).paginate(event, new ExamplePaginateEvent(), Duration.ofMinutes(5));
}).attach();

Optionally, you can customize the emojis (unicode or Javacord Emoji objects) that will be used, for example (this example will use Unicode):

new Paginate<>(items, "➡", "⬅", "👍", "👎").paginate(...);