diff --git a/pom.xml b/pom.xml index 7ef1bda5..b38564ff 100644 --- a/pom.xml +++ b/pom.xml @@ -19,7 +19,7 @@ org.apache.maven.plugins maven-compiler-plugin - 3.11.0 + 3.12.1 ${java.version} ${java.version} @@ -28,7 +28,7 @@ org.apache.maven.plugins maven-shade-plugin - 3.5.1 + 3.5.2 package @@ -82,7 +82,7 @@ commons-codec commons-codec - 1.16.0 + 1.16.1 @@ -94,7 +94,7 @@ org.json json - 20231013 + 20240205 com.google.code.gson @@ -105,13 +105,13 @@ org.springframework.boot spring-boot-starter-websocket - 3.1.5 + 3.2.3 org.jetbrains annotations - 24.0.1 + 24.1.0 compile @@ -129,7 +129,7 @@ org.jsoup jsoup - 1.16.2 + 1.17.2 com.squareup.okhttp3 @@ -139,7 +139,7 @@ joda-time joda-time - 2.12.5 + 2.12.7 com.github.codahale diff --git a/src/main/java/com/seailz/discordjar/events/EventDispatcher.java b/src/main/java/com/seailz/discordjar/events/EventDispatcher.java index 2cc51adf..7045b9ae 100644 --- a/src/main/java/com/seailz/discordjar/events/EventDispatcher.java +++ b/src/main/java/com/seailz/discordjar/events/EventDispatcher.java @@ -30,10 +30,12 @@ public class EventDispatcher { private static class ListenerMethodPair { final DiscordListener listener; final Method method; + final String customId; - ListenerMethodPair(DiscordListener listener, Method method) { + ListenerMethodPair(DiscordListener listener, Method method, String customId) { this.listener = listener; this.method = method; + this.customId = customId; } } @@ -53,7 +55,12 @@ public void addListener(DiscordListener... listeners) { for (Method method : listener.getClass().getMethods()) { if (method.isAnnotationPresent(EventMethod.class)) { Class eventType = (Class) method.getParameterTypes()[0]; - listenersByEventType.computeIfAbsent(eventType, k -> new ArrayList<>()).add(new ListenerMethodPair(listener, method)); + EventMethod eventMethod = method.getAnnotation(EventMethod.class); + + String customId = null; + if (eventMethod.requireCustomId() != null && !eventMethod.requireCustomId().equals("")) customId = eventMethod.requireCustomId(); + + listenersByEventType.computeIfAbsent(eventType, k -> new ArrayList<>()).add(new ListenerMethodPair(listener, method, customId)); } } } @@ -90,6 +97,18 @@ public void dispatchEvent(Event event, Class type, DiscordJar d } } + if (listenerMethodPair.customId != null) { + if (event instanceof CustomIdable) { + if (((CustomIdable) event).getCustomId() == null) { + continue; + } + + if (!((CustomIdable) event).getCustomId().matches(listenerMethodPair.customId)) { + continue; + } + } + } + method.setAccessible(true); DiscordJarThreadAllocator.requestThread(() -> { try { diff --git a/src/main/java/com/seailz/discordjar/events/annotation/EventMethod.java b/src/main/java/com/seailz/discordjar/events/annotation/EventMethod.java index dc991aec..c4d89fe1 100644 --- a/src/main/java/com/seailz/discordjar/events/annotation/EventMethod.java +++ b/src/main/java/com/seailz/discordjar/events/annotation/EventMethod.java @@ -6,10 +6,8 @@ import java.lang.annotation.Target; /** - * This annotation is used to mark methods that should be called when an event is fired. - * If a listener method isn't marked with this annotation, it will not be called. - * - * THIS ANNOTATION IS NO LONGER REQUIRED. It exists purely for backwards compatibility. + * This annotation is used to mark methods that should be called when an event is fired. + *
If a listener method isn't marked with this annotation, it will not be called. * * @author Seailz * @see com.seailz.discordjar.events.DiscordListener @@ -19,4 +17,26 @@ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface EventMethod { + + + /** + * If this field is set, then the event will only run if the customid of the event == the value of this field. + *
Example: + *
+ * + *
@EventMethod(requireCustomId="my_custom_id) + *
public void onModalInteractionEvent(@NotNull ModalInteractionEvent event) { + *
// This will only run if the custom id of the event is "my_custom_id" + *
event.reply("The custom ID is my_custom_id!").run(); + *
} + *
+ *

+ * It's also a regex, so you can do things like this: @RequireCustomId("my_custom_id|my_custom_id2") which will match both "my_custom_id" and "my_custom_id2" , + * or @RequireCustomId("my_custom_id-.*") which will match any custom id that starts with "my_custom_id-". + *

+ * @author Seailz + * @since 1.0 + */ + String requireCustomId() default ""; + } diff --git a/src/main/java/com/seailz/discordjar/gateway/heartbeat/HeartLogic.java b/src/main/java/com/seailz/discordjar/gateway/heartbeat/HeartLogic.java index 9760dc22..5ccaa60d 100644 --- a/src/main/java/com/seailz/discordjar/gateway/heartbeat/HeartLogic.java +++ b/src/main/java/com/seailz/discordjar/gateway/heartbeat/HeartLogic.java @@ -68,7 +68,6 @@ public void start() { Thread thread = new Thread(() -> { while (true) { if (!running) { - System.out.println("Heartbeat thread stopped."); try { Thread.sleep(1000); } catch (InterruptedException e) { diff --git a/src/main/java/com/seailz/discordjar/utils/annotation/RequireCustomId.java b/src/main/java/com/seailz/discordjar/utils/annotation/RequireCustomId.java index fefdbefd..41c55072 100644 --- a/src/main/java/com/seailz/discordjar/utils/annotation/RequireCustomId.java +++ b/src/main/java/com/seailz/discordjar/utils/annotation/RequireCustomId.java @@ -1,5 +1,7 @@ package com.seailz.discordjar.utils.annotation; +import com.seailz.discordjar.events.annotation.EventMethod; + import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @@ -23,9 +25,11 @@ *

* Using this annotation, however, will cause some performance loss, so use it sparingly or if time is not a concern. * @author Seailz + * @deprecated Use the {@link EventMethod#requireCustomId()} field instead. * @since 1.0 */ @Retention(RetentionPolicy.RUNTIME) +@Deprecated(since = "1.0") @Target({java.lang.annotation.ElementType.METHOD}) public @interface RequireCustomId { String value(); diff --git a/src/main/java/com/seailz/discordjar/utils/rest/DiscordRequest.java b/src/main/java/com/seailz/discordjar/utils/rest/DiscordRequest.java index 1d0b672e..c7aadb17 100644 --- a/src/main/java/com/seailz/discordjar/utils/rest/DiscordRequest.java +++ b/src/main/java/com/seailz/discordjar/utils/rest/DiscordRequest.java @@ -352,8 +352,8 @@ private DiscordResponse invoke(String contentType, boolean auth) throws Unhandle } } catch (JSONException ignored) { } - Logger.getLogger("DiscordJar") - .warning("[REST] 404: " + message); +// Logger.getLogger("DiscordJar") +// .warning("[REST] 404: " + message); return new DiscordResponse(404, null, null, null); }