From b9161cf38f7429fb2653a04ecbc392d1d0e239ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Spie=C3=9F?= Date: Thu, 2 Feb 2023 12:38:25 +0100 Subject: [PATCH] Add Modal.Builder#addComponents (#2388) --- .../jda/api/interactions/modals/Modal.java | 122 +++++++++++++++--- .../interactions/modal/ModalImpl.java | 14 +- 2 files changed, 114 insertions(+), 22 deletions(-) diff --git a/src/main/java/net/dv8tion/jda/api/interactions/modals/Modal.java b/src/main/java/net/dv8tion/jda/api/interactions/modals/Modal.java index a1d1667e56..d616f091c7 100644 --- a/src/main/java/net/dv8tion/jda/api/interactions/modals/Modal.java +++ b/src/main/java/net/dv8tion/jda/api/interactions/modals/Modal.java @@ -16,12 +16,16 @@ package net.dv8tion.jda.api.interactions.modals; +import net.dv8tion.jda.annotations.ForRemoval; +import net.dv8tion.jda.annotations.ReplaceWith; import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent; import net.dv8tion.jda.api.interactions.components.ActionRow; import net.dv8tion.jda.api.interactions.components.ItemComponent; +import net.dv8tion.jda.api.interactions.components.LayoutComponent; import net.dv8tion.jda.api.utils.data.SerializableData; import net.dv8tion.jda.internal.interactions.modal.ModalImpl; import net.dv8tion.jda.internal.utils.Checks; +import net.dv8tion.jda.internal.utils.Helpers; import javax.annotation.CheckReturnValue; import javax.annotation.Nonnull; @@ -106,9 +110,28 @@ public interface Modal extends SerializableData * A List of {@link net.dv8tion.jda.api.interactions.components.ActionRow ActionRows} that this modal contains. * * @return List of ActionRows + * + * @deprecated Use {@link #getComponents()} instead + */ + @Nonnull + @ForRemoval + @Deprecated + @ReplaceWith("getComponents()") + default List getActionRows() + { + return getComponents().stream() + .filter(ActionRow.class::isInstance) + .map(ActionRow.class::cast) + .collect(Helpers.toUnmodifiableList()); + } + + /** + * A List of {@link LayoutComponent LayoutComponents} that this modal contains. + * + * @return List of LayoutComponents */ @Nonnull - List getActionRows(); + List getComponents(); /** * Creates a new preconfigured {@link Modal.Builder} with the same settings used for this modal. @@ -120,7 +143,7 @@ public interface Modal extends SerializableData default Modal.Builder createCopy() { return new Builder(getId(), getTitle()) - .addActionRows(getActionRows()); + .addComponents(getComponents()); } /** @@ -152,7 +175,7 @@ static Modal.Builder create(@Nonnull String customId, @Nonnull String title) */ class Builder { - private final List components = new ArrayList<>(5); + private final List components = new ArrayList<>(MAX_COMPONENTS); private String id; private String title; @@ -205,7 +228,7 @@ public Builder setTitle(@Nonnull String title) /** * Adds ActionRows to this modal * - * @param actionRows + * @param actionRows * ActionRows to add to the modal, up to 5 * * @throws IllegalArgumentException @@ -219,16 +242,18 @@ public Builder setTitle(@Nonnull String title) * @see ActionRow#isModalCompatible() */ @Nonnull + @ForRemoval + @Deprecated + @ReplaceWith("addComponents(actionRows)") public Builder addActionRows(@Nonnull ActionRow... actionRows) { - Checks.noneNull(actionRows, "Action Rows"); - return addActionRows(Arrays.asList(actionRows)); + return addComponents(actionRows); } /** * Adds ActionRows to this modal * - * @param actionRows + * @param actionRows * ActionRows to add to the modal, up to 5 * * @throws IllegalArgumentException @@ -242,15 +267,63 @@ public Builder addActionRows(@Nonnull ActionRow... actionRows) * @see ActionRow#isModalCompatible() */ @Nonnull + @ForRemoval + @Deprecated + @ReplaceWith("addComponents(actionRows)") public Builder addActionRows(@Nonnull Collection actionRows) { - Checks.noneNull(actionRows, "Components"); + return addComponents(actionRows); + } + + /** + * Adds {@link LayoutComponent LayoutComponents} to this modal + * + * @param components + * {@link LayoutComponent LayoutComponents} to add to the modal, up to {@value MAX_COMPONENTS} total + * + * @throws IllegalArgumentException + *
    + *
  • If any of the provided layouts are null
  • + *
  • If any of the provided components are not compatible with Modals
  • + *
+ * + * @return The same builder instance for chaining + * + * @see LayoutComponent#isModalCompatible() + */ + @Nonnull + public Builder addComponents(@Nonnull LayoutComponent... components) + { + Checks.noneNull(components, "Action Rows"); + return addComponents(Arrays.asList(components)); + } + + /** + * Adds {@link LayoutComponent LayoutComponents} to this modal + * + * @param components + * {@link LayoutComponent LayoutComponents} to add to the modal, up to {@value MAX_COMPONENTS} total + * + * @throws IllegalArgumentException + *
    + *
  • If any of the provided layouts are null
  • + *
  • If any of the provided components are not compatible with Modals
  • + *
+ * + * @return The same builder instance for chaining + * + * @see LayoutComponent#isModalCompatible() + */ + @Nonnull + public Builder addComponents(@Nonnull Collection components) + { + Checks.noneNull(components, "Components"); Checks.checkComponents("Some components are incompatible with Modals", - actionRows, - component -> component.getType().isModalCompatible()); + components, + component -> component.getType().isModalCompatible()); - this.components.addAll(actionRows); + this.components.addAll(components); return this; } @@ -273,7 +346,7 @@ public Builder addActionRows(@Nonnull Collection actionRows @Nonnull public Builder addActionRow(@Nonnull Collection components) { - return addActionRows(ActionRow.of(components)); + return addComponents(ActionRow.of(components)); } /** @@ -295,7 +368,24 @@ public Builder addActionRow(@Nonnull Collection compone @Nonnull public Builder addActionRow(@Nonnull ItemComponent... components) { - return addActionRows(ActionRow.of(components)); + return addComponents(ActionRow.of(components)); + } + + /** + * Returns an immutable list of all ActionRow components + * + * @return An immutable list of all ActionRow components + */ + @Nonnull + @ForRemoval + @Deprecated + @ReplaceWith("getComponents()") + public List getActionRows() + { + return components.stream() + .filter(ActionRow.class::isInstance) + .map(ActionRow.class::cast) + .collect(Helpers.toUnmodifiableList()); } /** @@ -304,7 +394,7 @@ public Builder addActionRow(@Nonnull ItemComponent... components) * @return A modifiable list of all components */ @Nonnull - public List getActionRows() + public List getComponents() { return components; } @@ -336,8 +426,8 @@ public String getId() * * @throws IllegalArgumentException *
    - *
  • If the components are empty
  • - *
  • If there are more than 5 components
  • + *
  • If no components are added
  • + *
  • If more than {@value MAX_COMPONENTS} component layouts are added
  • *
* * @return A Modal diff --git a/src/main/java/net/dv8tion/jda/internal/interactions/modal/ModalImpl.java b/src/main/java/net/dv8tion/jda/internal/interactions/modal/ModalImpl.java index 38eb89c7a7..bca8925297 100644 --- a/src/main/java/net/dv8tion/jda/internal/interactions/modal/ModalImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/interactions/modal/ModalImpl.java @@ -17,10 +17,12 @@ package net.dv8tion.jda.internal.interactions.modal; import net.dv8tion.jda.api.interactions.components.ActionRow; +import net.dv8tion.jda.api.interactions.components.LayoutComponent; import net.dv8tion.jda.api.interactions.modals.Modal; import net.dv8tion.jda.api.utils.data.DataArray; import net.dv8tion.jda.api.utils.data.DataObject; import net.dv8tion.jda.internal.utils.EntityString; +import net.dv8tion.jda.internal.utils.Helpers; import javax.annotation.Nonnull; import java.util.Collections; @@ -31,19 +33,19 @@ public class ModalImpl implements Modal { private final String id; private final String title; - private final List components; + private final List components; public ModalImpl(DataObject object) { this.id = object.getString("id"); this.title = object.getString("title"); - this.components = Collections.unmodifiableList(object.optArray("components").orElseGet(DataArray::empty) + this.components = object.optArray("components").orElseGet(DataArray::empty) .stream(DataArray::getObject) .map(ActionRow::fromData) - .collect(Collectors.toList())); + .collect(Helpers.toUnmodifiableList()); } - public ModalImpl(String id, String title, List components) + public ModalImpl(String id, String title, List components) { this.id = id; this.title = title; @@ -66,7 +68,7 @@ public String getTitle() @Nonnull @Override - public List getActionRows() + public List getComponents() { return components; } @@ -80,7 +82,7 @@ public DataObject toData() .put("title", title); object.put("components", DataArray.fromCollection(components.stream() - .map(ActionRow::toData) + .map(LayoutComponent::toData) .collect(Collectors.toList()))); return object; }