Skip to content

Commit

Permalink
Add Modal.Builder#addComponents (#2388)
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed Feb 2, 2023
1 parent a7f2518 commit b9161cf
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 22 deletions.
122 changes: 106 additions & 16 deletions src/main/java/net/dv8tion/jda/api/interactions/modals/Modal.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ActionRow> 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<ActionRow> getActionRows();
List<LayoutComponent> getComponents();

/**
* Creates a new preconfigured {@link Modal.Builder} with the same settings used for this modal.
Expand All @@ -120,7 +143,7 @@ public interface Modal extends SerializableData
default Modal.Builder createCopy()
{
return new Builder(getId(), getTitle())
.addActionRows(getActionRows());
.addComponents(getComponents());
}

/**
Expand Down Expand Up @@ -152,7 +175,7 @@ static Modal.Builder create(@Nonnull String customId, @Nonnull String title)
*/
class Builder
{
private final List<ActionRow> components = new ArrayList<>(5);
private final List<LayoutComponent> components = new ArrayList<>(MAX_COMPONENTS);
private String id;
private String title;

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -242,15 +267,63 @@ public Builder addActionRows(@Nonnull ActionRow... actionRows)
* @see ActionRow#isModalCompatible()
*/
@Nonnull
@ForRemoval
@Deprecated
@ReplaceWith("addComponents(actionRows)")
public Builder addActionRows(@Nonnull Collection<? extends ActionRow> 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
* <ul>
* <li>If any of the provided layouts are null</li>
* <li>If any of the provided components are not compatible with Modals</li>
* </ul>
*
* @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
* <ul>
* <li>If any of the provided layouts are null</li>
* <li>If any of the provided components are not compatible with Modals</li>
* </ul>
*
* @return The same builder instance for chaining
*
* @see LayoutComponent#isModalCompatible()
*/
@Nonnull
public Builder addComponents(@Nonnull Collection<? extends LayoutComponent> 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;
}

Expand All @@ -273,7 +346,7 @@ public Builder addActionRows(@Nonnull Collection<? extends ActionRow> actionRows
@Nonnull
public Builder addActionRow(@Nonnull Collection<? extends ItemComponent> components)
{
return addActionRows(ActionRow.of(components));
return addComponents(ActionRow.of(components));
}

/**
Expand All @@ -295,7 +368,24 @@ public Builder addActionRow(@Nonnull Collection<? extends ItemComponent> 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<ActionRow> getActionRows()
{
return components.stream()
.filter(ActionRow.class::isInstance)
.map(ActionRow.class::cast)
.collect(Helpers.toUnmodifiableList());
}

/**
Expand All @@ -304,7 +394,7 @@ public Builder addActionRow(@Nonnull ItemComponent... components)
* @return A modifiable list of all components
*/
@Nonnull
public List<ActionRow> getActionRows()
public List<LayoutComponent> getComponents()
{
return components;
}
Expand Down Expand Up @@ -336,8 +426,8 @@ public String getId()
*
* @throws IllegalArgumentException
* <ul>
* <li>If the components are empty</li>
* <li>If there are more than 5 components</li>
* <li>If no components are added</li>
* <li>If more than {@value MAX_COMPONENTS} component layouts are added</li>
* </ul>
*
* @return A Modal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,19 +33,19 @@ public class ModalImpl implements Modal
{
private final String id;
private final String title;
private final List<ActionRow> components;
private final List<LayoutComponent> 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<ActionRow> components)
public ModalImpl(String id, String title, List<LayoutComponent> components)
{
this.id = id;
this.title = title;
Expand All @@ -66,7 +68,7 @@ public String getTitle()

@Nonnull
@Override
public List<ActionRow> getActionRows()
public List<LayoutComponent> getComponents()
{
return components;
}
Expand All @@ -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;
}
Expand Down

0 comments on commit b9161cf

Please sign in to comment.