diff --git a/menu/src/main/java/com/jagrosh/jdautilities/menu/EmbedPaginator.java b/menu/src/main/java/com/jagrosh/jdautilities/menu/EmbedPaginator.java new file mode 100644 index 00000000..f5d7e3ad --- /dev/null +++ b/menu/src/main/java/com/jagrosh/jdautilities/menu/EmbedPaginator.java @@ -0,0 +1,625 @@ +/* + * Copyright 2016-2018 John Grosh (jagrosh) & Kaidan Gustave (TheMonitorLizard) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.jagrosh.jdautilities.menu; + +import com.jagrosh.jdautilities.commons.waiter.EventWaiter; +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.MessageBuilder; +import net.dv8tion.jda.api.entities.*; +import net.dv8tion.jda.api.events.message.GenericMessageEvent; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent; +import net.dv8tion.jda.api.exceptions.PermissionException; +import net.dv8tion.jda.api.requests.RestAction; +import net.dv8tion.jda.internal.utils.Checks; + +import java.util.*; +import java.util.concurrent.TimeUnit; +import java.util.function.BiFunction; +import java.util.function.Consumer; + +/** + * A {@link com.jagrosh.jdautilities.menu.Menu Menu} implementation, nearly identical to + * {@link com.jagrosh.jdautilities.menu.Paginator Paginator}, that displays an individual + * {@link net.dv8tion.jda.api.entities.MessageEmbed MessageEmbed} on each page instead of a list of text items. + * + *
Like Paginator, reaction functions allow the user to traverse to the last page using the left arrow, the next
+ * page using the right arrow, and to stop the EmbedPaginator prematurely using the stop reaction.
+ *
+ * @author Andre_601
+ */
+public class EmbedPaginator extends Menu{
+
+ private final BiFunction Note that setting this doesn't mean that left and right text inputs provided via
+ * {@link EmbedPaginator.Builder#setLeftRightText(String, String)} will be invalidated if they were set
+ * previously! To invalidate those, provide {@code null} for one or both of the parameters of that method.
+ *
+ * @param allowTextInput
+ * {@code true} if the EmbedPaginator will allow page-number text input.
+ *
+ * @return This builder
+ */
+ public Builder allowTextInput(boolean allowTextInput)
+ {
+ this.allowTextInput = allowTextInput;
+ return this;
+ }
+
+ /**
+ * Sets the {@link com.jagrosh.jdautilities.menu.EmbedPaginator EmbedPaginator} to traverse left or right
+ * when a provided text input is sent in the form of a Message to the
+ * {@link net.dv8tion.jda.api.entities.GuildChannel GuildChannel} the menu is displayed in.
+ *
+ * If one or both these parameters are provided {@code null} this resets both of them and they will no
+ * longer be available when the Paginator is built.
+ *
+ * @param left
+ * The left text input, causes the EmbedPaginator to traverse one page left.
+ * @param right
+ * The right text input, causes the EmbedPaginator to traverse one page right.
+ *
+ * @return This builder
+ */
+ public Builder setLeftRightText(String left, String right)
+ {
+ if(left == null || right == null)
+ {
+ leftText = null;
+ rightText = null;
+ }
+ else
+ {
+ leftText = left;
+ rightText = right;
+ }
+ return this;
+ }
+ }
+}
+ *
+ */
+ @Override
+ public EmbedPaginator build()
+ {
+ Checks.check(waiter != null, "Must set an EventWaiter");
+ Checks.check(!embeds.isEmpty(), "Must include at least one item to paginate");
+
+ return new EmbedPaginator(
+ waiter, users, roles, timeout, unit, text, finalAction, waitOnSinglePage, embeds, bulkSkipNumber,
+ wrapPageEnds, leftText, rightText, allowTextInput
+ );
+ }
+
+ /**
+ * Sets the text of the {@link net.dv8tion.jda.api.entities.Message Message} to be displayed when the
+ * {@link com.jagrosh.jdautilities.menu.EmbedPaginator EmbedPaginator} is built.
+ *
+ * @param text
+ * The Message content to be displayed above the embed when the EmbedPaginator is built.
+ *
+ * @return This builder
+ */
+ public Builder setText(String text)
+ {
+ this.text = (i0, i1) -> text;
+ return this;
+ }
+
+ /**
+ * Sets the text of the {@link net.dv8tion.jda.api.entities.Message Message} to be displayed relative to the
+ * total page number and the current page as determined by the provided
+ * {@link java.util.function.BiFunction BiFunction}.
+ *
As the page changes, the BiFunction will re-process the current page number and the total page number,
+ * allowing for the displayed text of the Message to change depending on the page number.
+ *
+ * @param textBiFunction
+ * The BiFunction that uses both current and total page numbers, to get text for the Message
+ *
+ * @return This builder
+ */
+ public Builder setText(BiFunction
This method creates a new, basic MessageEmbed containing only the provided String as description.
+ *
Use the {@link com.jagrosh.jdautilities.menu.Paginator Paginator} for more Embed customization,
+ * without providing your own MessageEmbed instances.
+ *
+ * @param items
+ * The String list of items to add as MessageEmbeds
+ *
+ * @throws java.lang.IllegalArgumentException
+ * When one of the provided Strings is longer than 2048 characters.
+ *
+ * @return This builder
+ */
+ public Builder addItems(String... items)
+ {
+ for(String item : items)
+ {
+ Checks.check(item.length() <= MessageEmbed.TEXT_MAX_LENGTH, "Text may not be longer than 2048 characters.");
+ this.embeds.add(new EmbedBuilder().setDescription(item).build());
+ }
+ return this;
+ }
+
+ /**
+ * Sets the {@link net.dv8tion.jda.api.entities.MessageEmbed MessageEmbeds} to paginate.
+ *
This method clears all previously set items before adding the provided MessageEmbeds.
+ *
+ * @param embeds
+ * The MessageEmbed list of items to add
+ *
+ * @return This builder
+ */
+ public Builder setItems(MessageEmbed... embeds)
+ {
+ this.embeds.clear();
+ addItems(Arrays.asList(embeds));
+ return this;
+ }
+
+ /**
+ * Sets the {@link net.dv8tion.jda.api.entities.MessageEmbed MessageEmbeds} to paginate.
+ *
This method clears all previously set items before adding the provided collection of MessageEmbeds.
+ *
+ * @param embeds
+ * The collection of MessageEmbeds to set.
+ *
+ * @return This builder
+ */
+ public Builder setItems(Collection
This method clears all previously set items before setting each String as a new MessageEmbed.
+ *
Use the {@link com.jagrosh.jdautilities.menu.Paginator Paginator} for more Embed customization,
+ * without providing your own MessageEmbed instances.
+ *
+ * @param items
+ * The String list of items to add
+ *
+ * @throws java.lang.IllegalArgumentException
+ * When one of the provided Strings is longer than 2048 characters.
+ *
+ * @return This builder
+ */
+ public Builder setItems(String... items)
+ {
+ this.embeds.clear();
+ addItems(items);
+ return this;
+ }
+
+ /**
+ * Sets the {@link com.jagrosh.jdautilities.menu.EmbedPaginator EmbedPaginator}'s bulk-skip function to
+ * skip multiple pages using alternate forward and backwards reactions.
+ *
+ * @param bulkSkipNumber
+ * The number of pages to skip when the bulk-skip reactions are used.
+ *
+ * @return This builder
+ */
+ public Builder setBulkSkipNumber(int bulkSkipNumber)
+ {
+ this.bulkSkipNumber = Math.max(bulkSkipNumber, 1);
+ return this;
+ }
+
+ /**
+ * Sets the {@link com.jagrosh.jdautilities.menu.EmbedPaginator EmbedPaginator} to wrap from the last page
+ * to the first when traversing right and vice versa from the left.
+ *
+ * @param wrapPageEnds
+ * {@code true} to enable wrapping.
+ *
+ * @return This builder
+ */
+ public Builder wrapPageEnds(boolean wrapPageEnds)
+ {
+ this.wrapPageEnds = wrapPageEnds;
+ return this;
+ }
+
+ /**
+ * Sets the {@link com.jagrosh.jdautilities.menu.EmbedPaginator EmbedPaginator} to allow a page number to
+ * be specified by a user via text.
+ *
+ *