Skip to content

Commit b416e5f

Browse files
authored
Merge branch 'discord-jda:master' into master
2 parents 011d7b0 + d547708 commit b416e5f

99 files changed

Lines changed: 1604 additions & 375 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/validate.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on:
44
push:
55
branches: [ master ]
66
pull_request:
7+
merge_group:
78

89
jobs:
910
build:

src/main/java/net/dv8tion/jda/api/components/Components.java

Lines changed: 26 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,28 @@
1616

1717
package net.dv8tion.jda.api.components;
1818

19+
import net.dv8tion.jda.api.components.thumbnail.Thumbnail;
1920
import net.dv8tion.jda.api.components.tree.ComponentTree;
20-
import net.dv8tion.jda.api.components.tree.MessageComponentTree;
21-
import net.dv8tion.jda.api.components.tree.ModalComponentTree;
21+
import net.dv8tion.jda.api.components.utils.ComponentDeserializer;
22+
import net.dv8tion.jda.api.components.utils.ComponentSerializer;
23+
import net.dv8tion.jda.api.utils.FileUpload;
2224
import net.dv8tion.jda.api.utils.data.DataArray;
2325
import net.dv8tion.jda.api.utils.data.DataObject;
24-
import net.dv8tion.jda.internal.components.UnknownComponentImpl;
25-
import net.dv8tion.jda.internal.components.actionrow.ActionRowImpl;
26-
import net.dv8tion.jda.internal.components.buttons.ButtonImpl;
27-
import net.dv8tion.jda.internal.components.container.ContainerImpl;
28-
import net.dv8tion.jda.internal.components.filedisplay.FileDisplayImpl;
29-
import net.dv8tion.jda.internal.components.label.LabelImpl;
30-
import net.dv8tion.jda.internal.components.mediagallery.MediaGalleryImpl;
31-
import net.dv8tion.jda.internal.components.section.SectionImpl;
32-
import net.dv8tion.jda.internal.components.selections.EntitySelectMenuImpl;
33-
import net.dv8tion.jda.internal.components.selections.StringSelectMenuImpl;
34-
import net.dv8tion.jda.internal.components.separator.SeparatorImpl;
35-
import net.dv8tion.jda.internal.components.textdisplay.TextDisplayImpl;
36-
import net.dv8tion.jda.internal.components.textinput.TextInputImpl;
37-
import net.dv8tion.jda.internal.components.thumbnail.ThumbnailImpl;
38-
import net.dv8tion.jda.internal.components.utils.ComponentsUtil;
39-
import net.dv8tion.jda.internal.utils.Checks;
4026

4127
import javax.annotation.Nonnull;
4228
import java.util.List;
43-
import java.util.function.Function;
4429
import java.util.stream.Collectors;
4530

31+
import static net.dv8tion.jda.internal.entities.EntityBuilder.DEFAULT_COMPONENT_DESERIALIZER;
32+
4633
/**
4734
* Utility class for components.
35+
*
36+
* <p>This utility builds on {@link ComponentDeserializer} to deserialize components.
37+
* The implementation provided by {@link ComponentDeserializer} allows to deserialize components with implicit {@link FileUpload FileUploads} like {@link Thumbnail}.
38+
*
39+
* @see ComponentDeserializer
40+
* @see ComponentSerializer
4841
*/
4942
public class Components
5043
{
@@ -55,58 +48,17 @@ public class Components
5548
* @param data
5649
* The {@link DataArray} to create the component tree from
5750
*
58-
* @return A {@link ComponentTree} representing the provided data
59-
*
6051
* @throws IllegalArgumentException
6152
* If the provided data is {@code null}, or the component is not of type {@link T}
53+
*
54+
* @return A {@link ComponentTree} representing the provided data
55+
*
56+
* @see ComponentDeserializer#deserializeAs(Class, DataObject)
6257
*/
6358
@Nonnull
6459
public static <T extends Component> T parseComponent(@Nonnull Class<T> componentType, @Nonnull DataObject data)
6560
{
66-
Checks.notNull(componentType, "Component type");
67-
Checks.notNull(data, "Data");
68-
69-
final IComponentUnion component = parseComponent(data);
70-
return ComponentsUtil.safeUnionCastWithUnknownType("component", component, componentType);
71-
}
72-
73-
@Nonnull
74-
private static IComponentUnion parseComponent(@Nonnull DataObject data)
75-
{
76-
switch (Component.Type.fromKey(data.getInt("type")))
77-
{
78-
case ACTION_ROW:
79-
return new ActionRowImpl(data);
80-
case BUTTON:
81-
return new ButtonImpl(data);
82-
case STRING_SELECT:
83-
return new StringSelectMenuImpl(data);
84-
case TEXT_INPUT:
85-
return new TextInputImpl(data);
86-
case USER_SELECT:
87-
case ROLE_SELECT:
88-
case MENTIONABLE_SELECT:
89-
case CHANNEL_SELECT:
90-
return new EntitySelectMenuImpl(data);
91-
case SECTION:
92-
return new SectionImpl(data);
93-
case TEXT_DISPLAY:
94-
return new TextDisplayImpl(data);
95-
case THUMBNAIL:
96-
return new ThumbnailImpl(data);
97-
case MEDIA_GALLERY:
98-
return new MediaGalleryImpl(data);
99-
case FILE_DISPLAY:
100-
return new FileDisplayImpl(data);
101-
case SEPARATOR:
102-
return new SeparatorImpl(data);
103-
case CONTAINER:
104-
return new ContainerImpl(data);
105-
case LABEL:
106-
return new LabelImpl(data);
107-
default:
108-
return new UnknownComponentImpl(data);
109-
}
61+
return DEFAULT_COMPONENT_DESERIALIZER.deserializeAs(componentType, data);
11062
}
11163

11264
/**
@@ -116,18 +68,17 @@ private static IComponentUnion parseComponent(@Nonnull DataObject data)
11668
* @param data
11769
* The {@link DataArray} to create the components from
11870
*
119-
* @return A {@link List} of {@link T} representing the provided data
120-
*
12171
* @throws IllegalArgumentException
12272
* If the provided data is {@code null}, or one of the components is not of type {@link T}
73+
*
74+
* @return A {@link List} of {@link T} representing the provided data
75+
*
76+
* @see ComponentDeserializer#deserializeAs(Class, DataArray)
12377
*/
12478
@Nonnull
12579
public static <T extends Component> List<T> parseComponents(@Nonnull Class<T> componentType, @Nonnull DataArray data)
12680
{
127-
Checks.notNull(componentType, "Component type");
128-
Checks.notNull(data, "Data");
129-
130-
return parseTo(componentType, data, Function.identity());
81+
return DEFAULT_COMPONENT_DESERIALIZER.deserializeAs(componentType, data).collect(Collectors.toList());
13182
}
13283

13384
/**
@@ -137,41 +88,18 @@ public static <T extends Component> List<T> parseComponents(@Nonnull Class<T> co
13788
* @param data
13889
* The {@link DataArray} to create the component tree from
13990
*
140-
* @return A {@link ComponentTree} representing the provided data
141-
*
14291
* @throws IllegalArgumentException
14392
* If the provided data is {@code null}, or one of the components is not compatible.
14493
* @throws UnsupportedOperationException
14594
* If the provided tree type is not supported
14695
*
96+
* @return A {@link ComponentTree} representing the provided data
97+
*
98+
* @see ComponentDeserializer#deserializeAsTree(Class, DataArray)
14799
*/
148100
@Nonnull
149-
@SuppressWarnings("unchecked")
150101
public static <T extends ComponentTree<?>> T parseTree(@Nonnull Class<T> treeType, @Nonnull DataArray data)
151102
{
152-
Checks.notNull(treeType, "Tree type");
153-
Checks.notNull(data, "Data");
154-
155-
if (MessageComponentTree.class.isAssignableFrom(treeType))
156-
return (T) parseTo(MessageTopLevelComponentUnion.class, data, MessageComponentTree::of);
157-
else if (ModalComponentTree.class.isAssignableFrom(treeType))
158-
return (T) parseTo(ModalTopLevelComponentUnion.class, data, ModalComponentTree::of);
159-
else if (ComponentTree.class.isAssignableFrom(treeType))
160-
return (T) parseTo(IComponentUnion.class, data, ComponentTree::of);
161-
else
162-
throw new UnsupportedOperationException("Cannot deserialize to tree of type " + treeType.getName());
163-
}
164-
165-
@Nonnull
166-
private static <R, U extends Component> R parseTo(
167-
@Nonnull Class<U> topLevelComponentType,
168-
@Nonnull DataArray data,
169-
@Nonnull Function<List<U>, R> treeFactory
170-
)
171-
{
172-
final List<U> components = data.stream(DataArray::getObject)
173-
.map(obj -> parseComponent(topLevelComponentType, obj))
174-
.collect(Collectors.toList());
175-
return treeFactory.apply(components);
103+
return DEFAULT_COMPONENT_DESERIALIZER.deserializeAsTree(treeType, data);
176104
}
177105
}

src/main/java/net/dv8tion/jda/api/components/actionrow/ActionRow.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public interface ActionRow extends MessageTopLevelComponent, ContainerChildCompo
5656
@Nonnull
5757
static ActionRow of(@Nonnull Collection<? extends ActionRowChildComponent> components)
5858
{
59-
return ActionRowImpl.of(components);
59+
return ActionRowImpl.validated(components);
6060
}
6161

6262
/**

src/main/java/net/dv8tion/jda/api/components/container/Container.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public interface Container extends MessageTopLevelComponent, IReplaceable, IDisa
6464
@Nonnull
6565
static Container of(@Nonnull Collection<? extends ContainerChildComponent> components)
6666
{
67-
return ContainerImpl.of(components);
67+
return ContainerImpl.validated(components);
6868
}
6969

7070
/**

src/main/java/net/dv8tion/jda/api/components/label/Label.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public interface Label extends ModalTopLevelComponent
6666
@Nonnull
6767
static Label of(@Nonnull String label, @Nullable String description, @Nonnull LabelChildComponent child)
6868
{
69-
return LabelImpl.of(label, description, child);
69+
return LabelImpl.validated(label, description, child);
7070
}
7171

7272
/**

src/main/java/net/dv8tion/jda/api/components/mediagallery/MediaGallery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public interface MediaGallery extends Component, MessageTopLevelComponent, Conta
6161
@Nonnull
6262
static MediaGallery of(@Nonnull Collection<? extends MediaGalleryItem> items)
6363
{
64-
return MediaGalleryImpl.of(items);
64+
return MediaGalleryImpl.validated(items);
6565
}
6666

6767
/**

src/main/java/net/dv8tion/jda/api/components/section/Section.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public interface Section extends MessageTopLevelComponent, ContainerChildCompone
7474
@Nonnull
7575
static Section of(@Nonnull SectionAccessoryComponent accessory, @Nonnull Collection<? extends SectionContentComponent> components)
7676
{
77-
return SectionImpl.of(accessory, components);
77+
return SectionImpl.validated(accessory, components);
7878
}
7979

8080
/**

src/main/java/net/dv8tion/jda/api/components/selections/SelectOption.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import javax.annotation.CheckReturnValue;
2828
import javax.annotation.Nonnull;
2929
import javax.annotation.Nullable;
30+
import java.util.Objects;
3031

3132
/**
3233
* One of the possible options provided in a {@link SelectMenu}.
@@ -309,4 +310,23 @@ public static SelectOption fromData(@Nonnull DataObject data)
309310
data.optObject("emoji").map(EntityBuilder::createEmoji).orElse(null)
310311
);
311312
}
313+
314+
@Override
315+
public boolean equals(Object o)
316+
{
317+
if (o == this) return true;
318+
if (!(o instanceof SelectOption)) return false;
319+
SelectOption that = (SelectOption) o;
320+
return isDefault == that.isDefault
321+
&& Objects.equals(label, that.label)
322+
&& Objects.equals(value, that.value)
323+
&& Objects.equals(description, that.description)
324+
&& Objects.equals(emoji, that.emoji);
325+
}
326+
327+
@Override
328+
public int hashCode()
329+
{
330+
return Objects.hash(label, value, description, isDefault, emoji);
331+
}
312332
}

src/main/java/net/dv8tion/jda/api/components/selections/StringSelectMenu.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,17 @@ public List<SelectOption> getOptions()
306306
return options;
307307
}
308308

309+
/**
310+
* Whether the user must populate this select menu in Modals, or {@code null} if not set.
311+
*
312+
* @return Whether this menu must be populated, or null
313+
*/
314+
@Nullable
315+
public Boolean isRequired()
316+
{
317+
return required;
318+
}
319+
309320
/**
310321
* Configures which of the currently applied {@link #getOptions() options} should be selected by default.
311322
*

0 commit comments

Comments
 (0)