All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.freya02.botcommands.api.pagination.menu.BasicMenu Maven / Gradle / Ivy

package com.freya02.botcommands.api.pagination.menu;

import com.freya02.botcommands.api.components.Components;
import com.freya02.botcommands.api.components.data.InteractionConstraints;
import com.freya02.botcommands.api.pagination.PaginatorSupplier;
import com.freya02.botcommands.api.pagination.TimeoutInfo;
import com.freya02.botcommands.api.pagination.paginator.BasicPaginator;
import com.freya02.botcommands.api.pagination.transformer.EntryTransformer;
import com.freya02.botcommands.api.utils.ButtonContent;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.internal.utils.Checks;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @param  Type of the menu elements
 * @param  Type of the implementor
 */
public abstract class BasicMenu> extends BasicPaginator {
	protected final Map> pages;

	protected BasicMenu(@NotNull Components componentsService,
						InteractionConstraints constraints,
						TimeoutInfo timeout,
						boolean hasDeleteButton,
						ButtonContent firstContent,
						ButtonContent previousContent,
						ButtonContent nextContent,
						ButtonContent lastContent,
						ButtonContent deleteContent,
						@NotNull Map> pages,
						@Nullable PaginatorSupplier supplier) {
		super(componentsService, constraints, timeout, pages.size(), supplier, hasDeleteButton, firstContent, previousContent, nextContent, lastContent, deleteContent);

		this.pages = pages;
	}

	@SuppressWarnings("unchecked")
	@Override
	@NotNull
	protected MessageEmbed getEmbed() {
		final EmbedBuilder builder;

		if (supplier != null) {
			builder = new EmbedBuilder(supplier.get((T) this, messageBuilder, components, page));
		} else {
			builder = new EmbedBuilder();
		}

		final MenuPage menuPage = pages.get(page);

		builder.appendDescription(menuPage.content());

		return builder.build();
	}

	@NotNull
	protected static  Map> makePages(@NotNull List entries,
	                                                         @NotNull EntryTransformer transformer,
	                                                         @NotNull RowPrefixSupplier rowPrefixSupplier,
	                                                         int maxEntriesPerPage) {
		final Map> pages = new HashMap<>();

		int page = 0;
		int oldEntry = 0;
		StringBuilder builder = new StringBuilder();

		for (int i = 0, entriesSize = entries.size(); i < entriesSize; i++) {
			E entry = entries.get(i);

			final String s = transformer.toString(entry);
			Checks.notLonger(s, MessageEmbed.TEXT_MAX_LENGTH - 8, "Entry #" + i + " string");

			if (i - oldEntry >= maxEntriesPerPage || builder.length() + s.length() > MessageEmbed.TEXT_MAX_LENGTH - 8) {
				pages.put(page, new MenuPage<>(builder.toString(), entries.subList(oldEntry, i)));

				page++;
				oldEntry = i;

				builder.setLength(0);
			}

			builder.append(rowPrefixSupplier.apply(i - oldEntry + 1, maxEntriesPerPage)).append(s).append('\n');
		}

		pages.put(page, new MenuPage<>(builder.toString(), entries.subList(oldEntry, entries.size())));

		return pages;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy