From 4751083fa10573e6890dfef115d3a674582e990c Mon Sep 17 00:00:00 2001 From: Giuliopime <60524738+Giuliopime@users.noreply.github.com> Date: Tue, 14 Nov 2023 12:47:44 +0100 Subject: [PATCH] chore: javax annotations, streams instead of for loops, entity builder for entitlement, primitive types, deferrable interface impl for premium required reply callback --- .../dv8tion/jda/api/entities/Entitlement.java | 73 +++++++++------ .../entitlement/EntitlementCreateEvent.java | 5 +- .../entitlement/EntitlementDeleteEvent.java | 5 +- .../entitlement/EntitlementUpdateEvent.java | 5 +- .../GenericInteractionCreateEvent.java | 3 +- .../GenericCommandInteractionEvent.java | 3 +- ...enericComponentInteractionCreateEvent.java | 3 +- .../IPremiumRequiredReplyCallback.java | 3 +- .../internal/entities/EntitlementImpl.java | 88 ++++++++++++------- .../jda/internal/entities/EntityBuilder.java | 15 ++++ .../handle/EntitlementCreateHandler.java | 3 +- .../handle/EntitlementDeleteHandler.java | 3 +- .../handle/EntitlementUpdateHandler.java | 3 +- .../interactions/InteractionImpl.java | 20 ++--- .../command/CommandInteractionImpl.java | 3 +- 15 files changed, 142 insertions(+), 93 deletions(-) diff --git a/src/main/java/net/dv8tion/jda/api/entities/Entitlement.java b/src/main/java/net/dv8tion/jda/api/entities/Entitlement.java index 3e713149d4..28557e9ec0 100644 --- a/src/main/java/net/dv8tion/jda/api/entities/Entitlement.java +++ b/src/main/java/net/dv8tion/jda/api/entities/Entitlement.java @@ -16,6 +16,7 @@ package net.dv8tion.jda.api.entities; +import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.time.OffsetDateTime; @@ -23,27 +24,57 @@ * Represents a Discord Entitlement for premium App subscriptions. *
This should contain all information provided from Discord about an {@link Entitlement Entitlement}. */ -public interface Entitlement +public interface Entitlement extends ISnowflake { /** + * The id of the entitlement * * @return The id of the {@link Entitlement Entitlement} */ - String getId(); + + @Override + long getIdLong(); /** + * The id of the sku related to this {@link Entitlement Entitlement} * * @return The id of the SKU related to this {@link Entitlement Entitlement} */ + long getSkuIdLong(); + + /** + * The id of the sku related to this {@link Entitlement Entitlement} + * + * @return The id of the SKU related to this {@link Entitlement Entitlement} + */ + @Nonnull String getSkuId(); /** + * The id of the parent application of this {@link Entitlement Entitlement} * * @return The id of the parent application of this {@link Entitlement Entitlement} */ + long getApplicationIdLong(); + + /** + * The id of the parent application of this {@link Entitlement Entitlement} + * + * @return The id of the parent application of this {@link Entitlement Entitlement} + */ + @Nonnull String getApplicationId(); /** + * The id of the user that purchased the {@link Entitlement Entitlement} + * + * @return The id of the user that purchased the {@link Entitlement Entitlement} + */ + @Nullable + Long getUserIdLong(); + + /** + * The id of the user that purchased the {@link Entitlement Entitlement} * * @return The id of the user that purchased the {@link Entitlement Entitlement} */ @@ -51,18 +82,22 @@ public interface Entitlement String getUserId(); /** + * The guild id that is granted access to the {@link Entitlement Entitlement}'s sku * * @return The id of the guild that is granted access to the {@link Entitlement Entitlement}'s sku, * or Null if this entitlement is related to a subscription of type "User Subscription" */ @Nullable - String getGuildId(); + Long getGuildIdLong(); - /* - Mentioned by Discord in the example but not included in the api specification - @Nullable - String getPromotionId(); + /** + * The guild id that is granted access to the {@link Entitlement Entitlement}'s sku + * + * @return The id of the guild that is granted access to the {@link Entitlement Entitlement}'s sku, + * or Null if this entitlement is related to a subscription of type "User Subscription" */ + @Nullable + String getGuildId(); /** * The type of the Entitlement @@ -71,26 +106,19 @@ public interface Entitlement * * @return the {@link Entitlement Entitlement} type, 8 is the only possible value */ - Integer getType(); + int getType(); /** + * Whether the {@link Entitlement Entitlement} has been deleted or not. + * * @return True if the {@link Entitlement Entitlement} was deleted, False otherwise * * @see net.dv8tion.jda.api.events.entitlement.EntitlementDeleteEvent */ - Boolean getDeleted(); - - /* - Mentioned by Discord in the example but not included in the api specification - Long getGiftCodeFlags(); - */ - - /* - Mentioned by Discord in the example but not included in the api specification - Boolean getConsumed(); - */ + boolean getDeleted(); /** + * The start date at which the {@link Entitlement Entitlement} is valid. * * @return Start date at which the {@link Entitlement Entitlement} is valid. Not present when using test entitlements. */ @@ -98,15 +126,10 @@ public interface Entitlement OffsetDateTime getStartsAt(); /** + * Date at which the {@link Entitlement Entitlement} is no longer valid. * * @return Date at which the {@link Entitlement Entitlement} is no longer valid. Not present when using test entitlements. */ @Nullable OffsetDateTime getEndsAt(); - - /* - Mentioned by Discord in the example but not included in the api specification - @Nullable - String getSubscriptionId(); - */ } diff --git a/src/main/java/net/dv8tion/jda/api/events/entitlement/EntitlementCreateEvent.java b/src/main/java/net/dv8tion/jda/api/events/entitlement/EntitlementCreateEvent.java index a5633ea0d6..b52fe994df 100644 --- a/src/main/java/net/dv8tion/jda/api/events/entitlement/EntitlementCreateEvent.java +++ b/src/main/java/net/dv8tion/jda/api/events/entitlement/EntitlementCreateEvent.java @@ -18,7 +18,8 @@ import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.Entitlement; -import org.jetbrains.annotations.NotNull; + +import javax.annotation.Nonnull; /** * Indicates that a user subscribed to a SKU. @@ -27,7 +28,7 @@ */ public class EntitlementCreateEvent extends GenericEntitlementEvent { - public EntitlementCreateEvent(@NotNull JDA api, long responseNumber, @NotNull Entitlement entitlement) + public EntitlementCreateEvent(@Nonnull JDA api, long responseNumber, @Nonnull Entitlement entitlement) { super(api, responseNumber, entitlement); } diff --git a/src/main/java/net/dv8tion/jda/api/events/entitlement/EntitlementDeleteEvent.java b/src/main/java/net/dv8tion/jda/api/events/entitlement/EntitlementDeleteEvent.java index 46107c4a3b..90cf35491b 100644 --- a/src/main/java/net/dv8tion/jda/api/events/entitlement/EntitlementDeleteEvent.java +++ b/src/main/java/net/dv8tion/jda/api/events/entitlement/EntitlementDeleteEvent.java @@ -18,7 +18,8 @@ import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.Entitlement; -import org.jetbrains.annotations.NotNull; + +import javax.annotation.Nonnull; /** * Indicates a user's entitlement is deleted. Entitlement deletions are infrequent, and occur when: @@ -37,7 +38,7 @@ */ public class EntitlementDeleteEvent extends GenericEntitlementEvent { - public EntitlementDeleteEvent(@NotNull JDA api, long responseNumber, @NotNull Entitlement entitlement) + public EntitlementDeleteEvent(@Nonnull JDA api, long responseNumber, @Nonnull Entitlement entitlement) { super(api, responseNumber, entitlement); } diff --git a/src/main/java/net/dv8tion/jda/api/events/entitlement/EntitlementUpdateEvent.java b/src/main/java/net/dv8tion/jda/api/events/entitlement/EntitlementUpdateEvent.java index 18173f6234..e3c2c98cb2 100644 --- a/src/main/java/net/dv8tion/jda/api/events/entitlement/EntitlementUpdateEvent.java +++ b/src/main/java/net/dv8tion/jda/api/events/entitlement/EntitlementUpdateEvent.java @@ -18,7 +18,8 @@ import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.Entitlement; -import org.jetbrains.annotations.NotNull; + +import javax.annotation.Nonnull; /** * Indicates a user's subscription has renewed for the next billing period. @@ -32,7 +33,7 @@ */ public class EntitlementUpdateEvent extends GenericEntitlementEvent { - public EntitlementUpdateEvent(@NotNull JDA api, long responseNumber, @NotNull Entitlement entitlement) + public EntitlementUpdateEvent(@Nonnull JDA api, long responseNumber, @Nonnull Entitlement entitlement) { super(api, responseNumber, entitlement); } diff --git a/src/main/java/net/dv8tion/jda/api/events/interaction/GenericInteractionCreateEvent.java b/src/main/java/net/dv8tion/jda/api/events/interaction/GenericInteractionCreateEvent.java index 676d147f5f..24d40d8f20 100644 --- a/src/main/java/net/dv8tion/jda/api/events/interaction/GenericInteractionCreateEvent.java +++ b/src/main/java/net/dv8tion/jda/api/events/interaction/GenericInteractionCreateEvent.java @@ -25,7 +25,6 @@ import net.dv8tion.jda.api.events.Event; import net.dv8tion.jda.api.interactions.DiscordLocale; import net.dv8tion.jda.api.interactions.Interaction; -import org.jetbrains.annotations.NotNull; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -124,7 +123,7 @@ public User getUser() return interaction.getUser(); } - @NotNull + @Nonnull @Override public List getEntitlements() { diff --git a/src/main/java/net/dv8tion/jda/api/events/interaction/command/GenericCommandInteractionEvent.java b/src/main/java/net/dv8tion/jda/api/events/interaction/command/GenericCommandInteractionEvent.java index cee4c41a7c..b81d5c5fb2 100644 --- a/src/main/java/net/dv8tion/jda/api/events/interaction/command/GenericCommandInteractionEvent.java +++ b/src/main/java/net/dv8tion/jda/api/events/interaction/command/GenericCommandInteractionEvent.java @@ -26,7 +26,6 @@ import net.dv8tion.jda.api.requests.restaction.interactions.ModalCallbackAction; import net.dv8tion.jda.api.requests.restaction.interactions.PremiumRequiredCallbackAction; import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction; -import org.jetbrains.annotations.NotNull; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -121,7 +120,7 @@ public ModalCallbackAction replyModal(@Nonnull Modal modal) return getInteraction().replyModal(modal); } - @NotNull + @Nonnull @Override public PremiumRequiredCallbackAction replyWithPremiumRequired() { diff --git a/src/main/java/net/dv8tion/jda/api/events/interaction/component/GenericComponentInteractionCreateEvent.java b/src/main/java/net/dv8tion/jda/api/events/interaction/component/GenericComponentInteractionCreateEvent.java index f300d55cb9..2a04315b09 100644 --- a/src/main/java/net/dv8tion/jda/api/events/interaction/component/GenericComponentInteractionCreateEvent.java +++ b/src/main/java/net/dv8tion/jda/api/events/interaction/component/GenericComponentInteractionCreateEvent.java @@ -29,7 +29,6 @@ import net.dv8tion.jda.api.requests.restaction.interactions.ModalCallbackAction; import net.dv8tion.jda.api.requests.restaction.interactions.PremiumRequiredCallbackAction; import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction; -import org.jetbrains.annotations.NotNull; import javax.annotation.Nonnull; @@ -127,7 +126,7 @@ public ModalCallbackAction replyModal(@Nonnull Modal modal) return interaction.replyModal(modal); } - @NotNull + @Nonnull @Override public PremiumRequiredCallbackAction replyWithPremiumRequired() { diff --git a/src/main/java/net/dv8tion/jda/api/interactions/callbacks/IPremiumRequiredReplyCallback.java b/src/main/java/net/dv8tion/jda/api/interactions/callbacks/IPremiumRequiredReplyCallback.java index 6e65245289..7893fc0262 100644 --- a/src/main/java/net/dv8tion/jda/api/interactions/callbacks/IPremiumRequiredReplyCallback.java +++ b/src/main/java/net/dv8tion/jda/api/interactions/callbacks/IPremiumRequiredReplyCallback.java @@ -16,7 +16,6 @@ package net.dv8tion.jda.api.interactions.callbacks; -import net.dv8tion.jda.api.interactions.Interaction; import net.dv8tion.jda.api.requests.restaction.interactions.PremiumRequiredCallbackAction; import javax.annotation.CheckReturnValue; @@ -27,7 +26,7 @@ * *

Replying with {@link #replyWithPremiumRequired()} will automatically acknowledge this interaction. */ -public interface IPremiumRequiredReplyCallback extends Interaction +public interface IPremiumRequiredReplyCallback extends IDeferrableCallback { @Nonnull @CheckReturnValue diff --git a/src/main/java/net/dv8tion/jda/internal/entities/EntitlementImpl.java b/src/main/java/net/dv8tion/jda/internal/entities/EntitlementImpl.java index f2aa417218..f4834f4d2c 100644 --- a/src/main/java/net/dv8tion/jda/internal/entities/EntitlementImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/entities/EntitlementImpl.java @@ -17,28 +17,24 @@ package net.dv8tion.jda.internal.entities; import net.dv8tion.jda.api.entities.Entitlement; -import net.dv8tion.jda.api.utils.data.DataObject; +import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.time.OffsetDateTime; public class EntitlementImpl implements Entitlement { - private String id; - private String skuId; - private String applicationId; - @Nullable - private String userId; - @Nullable - private String guildId; - private Integer type; - private Boolean deleted; - @Nullable + private long id; + private long skuId; + private long applicationId; + private Long userId; + private Long guildId; + private int type; + private boolean deleted; private OffsetDateTime startsAt; - @Nullable private OffsetDateTime endsAt; - public EntitlementImpl(String id, String skuId, String applicationId, @Nullable String userId, @Nullable String guildId, Integer type, Boolean deleted, @Nullable OffsetDateTime startsAt, @Nullable OffsetDateTime endsAt) + public EntitlementImpl(long id, long skuId, long applicationId, Long userId, Long guildId, int type, boolean deleted, @Nullable OffsetDateTime startsAt, @Nullable OffsetDateTime endsAt) { this.id = id; this.skuId = skuId; @@ -51,71 +47,97 @@ public EntitlementImpl(String id, String skuId, String applicationId, @Nullable this.endsAt = endsAt; } - public EntitlementImpl(DataObject data) { - this.id = data.getString("id"); - this.skuId = data.getString("sku_id"); - this.applicationId = data.getString("application_id"); - this.userId = data.getString("user_id", null); - this.guildId = data.getString("guild_id", null); - this.type = data.getInt("type"); - this.deleted = data.getBoolean("deleted"); - this.startsAt = data.getOffsetDateTime("starts_at", null); - this.endsAt = data.getOffsetDateTime("ends_at", null); + @Override + public long getIdLong() + { + return id; } @Override - public String getId() + public long getSkuIdLong() { - return id; + return skuId; } + @Nonnull @Override public String getSkuId() { - return skuId; + return Long.toUnsignedString(skuId); } @Override - public String getApplicationId() + public long getApplicationIdLong() { return applicationId; } + @Nonnull @Override + public String getApplicationId() + { + return Long.toUnsignedString(applicationId); + } + @Nullable - public String getUserId() + @Override + public Long getUserIdLong() { return userId; } + @org.jetbrains.annotations.Nullable @Override + public String getUserId() + { + if (userId == null) + { + return null; + } + + return Long.toUnsignedString(userId); + } + @Nullable - public String getGuildId() + @Override + public Long getGuildIdLong() { return guildId; } + @Nullable @Override - public Integer getType() + public String getGuildId() + { + if (guildId == null) + { + return null; + } + + return Long.toUnsignedString(guildId); + } + + @Override + public int getType() { return type; } @Override - public Boolean getDeleted() + public boolean getDeleted() { return deleted; } - @Override @Nullable + @Override public OffsetDateTime getStartsAt() { return startsAt; } - @Override @Nullable + @Override public OffsetDateTime getEndsAt() { return endsAt; diff --git a/src/main/java/net/dv8tion/jda/internal/entities/EntityBuilder.java b/src/main/java/net/dv8tion/jda/internal/entities/EntityBuilder.java index 09b06f2ad8..742cc44d45 100644 --- a/src/main/java/net/dv8tion/jda/internal/entities/EntityBuilder.java +++ b/src/main/java/net/dv8tion/jda/internal/entities/EntityBuilder.java @@ -2558,6 +2558,21 @@ public AuditLogChange createAuditLogChange(DataObject change) return new AuditLogChange(oldValue, newValue, key); } + public Entitlement createEntitlement(DataObject object) + { + return new EntitlementImpl( + object.getLong("id"), + object.getLong("sku_id"), + object.getLong("application_id"), + object.isNull("user_id") ? null : object.getLong("user_id"), + object.isNull("guild_id") ? null : object.getLong("guild_id"), + object.getInt("type"), + object.getBoolean("deleted"), + object.getOffsetDateTime("starts_at", null), + object.getOffsetDateTime("ends_at", null) + ); + } + private Map changeToMap(Set changesList) { return changesList.stream().collect(Collectors.toMap(AuditLogChange::getKey, UnaryOperator.identity())); diff --git a/src/main/java/net/dv8tion/jda/internal/handle/EntitlementCreateHandler.java b/src/main/java/net/dv8tion/jda/internal/handle/EntitlementCreateHandler.java index 60be97ac96..32367ecff4 100644 --- a/src/main/java/net/dv8tion/jda/internal/handle/EntitlementCreateHandler.java +++ b/src/main/java/net/dv8tion/jda/internal/handle/EntitlementCreateHandler.java @@ -19,7 +19,6 @@ import net.dv8tion.jda.api.events.entitlement.EntitlementCreateEvent; import net.dv8tion.jda.api.utils.data.DataObject; import net.dv8tion.jda.internal.JDAImpl; -import net.dv8tion.jda.internal.entities.EntitlementImpl; public class EntitlementCreateHandler extends SocketHandler { @@ -31,7 +30,7 @@ public EntitlementCreateHandler(JDAImpl api) @Override protected Long handleInternally(DataObject content) { - getJDA().handleEvent(new EntitlementCreateEvent(getJDA(), responseNumber, new EntitlementImpl(content))); + getJDA().handleEvent(new EntitlementCreateEvent(getJDA(), responseNumber, getJDA().getEntityBuilder().createEntitlement(content))); return null; } } diff --git a/src/main/java/net/dv8tion/jda/internal/handle/EntitlementDeleteHandler.java b/src/main/java/net/dv8tion/jda/internal/handle/EntitlementDeleteHandler.java index a6ea0f1fee..22a54e3bbd 100644 --- a/src/main/java/net/dv8tion/jda/internal/handle/EntitlementDeleteHandler.java +++ b/src/main/java/net/dv8tion/jda/internal/handle/EntitlementDeleteHandler.java @@ -19,7 +19,6 @@ import net.dv8tion.jda.api.events.entitlement.EntitlementDeleteEvent; import net.dv8tion.jda.api.utils.data.DataObject; import net.dv8tion.jda.internal.JDAImpl; -import net.dv8tion.jda.internal.entities.EntitlementImpl; public class EntitlementDeleteHandler extends SocketHandler { @@ -31,7 +30,7 @@ public EntitlementDeleteHandler(JDAImpl api) @Override protected Long handleInternally(DataObject content) { - getJDA().handleEvent(new EntitlementDeleteEvent(getJDA(), responseNumber, new EntitlementImpl(content))); + getJDA().handleEvent(new EntitlementDeleteEvent(getJDA(), responseNumber, getJDA().getEntityBuilder().createEntitlement(content))); return null; } } diff --git a/src/main/java/net/dv8tion/jda/internal/handle/EntitlementUpdateHandler.java b/src/main/java/net/dv8tion/jda/internal/handle/EntitlementUpdateHandler.java index a622ff84b5..11d0cd3515 100644 --- a/src/main/java/net/dv8tion/jda/internal/handle/EntitlementUpdateHandler.java +++ b/src/main/java/net/dv8tion/jda/internal/handle/EntitlementUpdateHandler.java @@ -20,7 +20,6 @@ import net.dv8tion.jda.api.events.entitlement.EntitlementUpdateEvent; import net.dv8tion.jda.api.utils.data.DataObject; import net.dv8tion.jda.internal.JDAImpl; -import net.dv8tion.jda.internal.entities.EntitlementImpl; public class EntitlementUpdateHandler extends SocketHandler { @@ -32,7 +31,7 @@ public EntitlementUpdateHandler(JDAImpl api) @Override protected Long handleInternally(DataObject content) { - getJDA().handleEvent(new EntitlementUpdateEvent(getJDA(), responseNumber, new EntitlementImpl(content))); + getJDA().handleEvent(new EntitlementUpdateEvent(getJDA(), responseNumber, getJDA().getEntityBuilder().createEntitlement(content))); return null; } } diff --git a/src/main/java/net/dv8tion/jda/internal/interactions/InteractionImpl.java b/src/main/java/net/dv8tion/jda/internal/interactions/InteractionImpl.java index d579a4832f..c21b742451 100644 --- a/src/main/java/net/dv8tion/jda/internal/interactions/InteractionImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/interactions/InteractionImpl.java @@ -30,17 +30,14 @@ import net.dv8tion.jda.api.utils.data.DataArray; import net.dv8tion.jda.api.utils.data.DataObject; import net.dv8tion.jda.internal.JDAImpl; -import net.dv8tion.jda.internal.entities.EntitlementImpl; -import net.dv8tion.jda.internal.entities.GuildImpl; -import net.dv8tion.jda.internal.entities.MemberImpl; -import net.dv8tion.jda.internal.entities.UserImpl; +import net.dv8tion.jda.internal.entities.*; import net.dv8tion.jda.internal.entities.channel.concrete.PrivateChannelImpl; -import org.jetbrains.annotations.NotNull; import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; public class InteractionImpl implements Interaction { @@ -112,13 +109,10 @@ public InteractionImpl(JDAImpl jda, DataObject data) this.user = user; } - DataArray entitlementsArray = data.getArray("entitlements"); - entitlements = new ArrayList<>(); - - for (int i=0; i getEntitlements() { diff --git a/src/main/java/net/dv8tion/jda/internal/interactions/command/CommandInteractionImpl.java b/src/main/java/net/dv8tion/jda/internal/interactions/command/CommandInteractionImpl.java index ceec669453..03ddaca2ed 100644 --- a/src/main/java/net/dv8tion/jda/internal/interactions/command/CommandInteractionImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/interactions/command/CommandInteractionImpl.java @@ -29,7 +29,6 @@ import net.dv8tion.jda.internal.requests.restaction.interactions.PremiumRequiredCallbackActionImpl; import net.dv8tion.jda.internal.requests.restaction.interactions.ReplyCallbackActionImpl; import net.dv8tion.jda.internal.utils.Checks; -import org.jetbrains.annotations.NotNull; import javax.annotation.Nonnull; @@ -65,7 +64,7 @@ public ModalCallbackAction replyModal(@Nonnull Modal modal) } - @NotNull + @Nonnull @Override public PremiumRequiredCallbackAction replyWithPremiumRequired() {