From 3f70d7ff097cb5c7525d2aaef4ffaddd13f53af8 Mon Sep 17 00:00:00 2001 From: Sky Date: Fri, 25 Oct 2024 15:39:14 +0200 Subject: [PATCH] :rocket: Reworked the event listener system --- build.gradle | 2 +- .../disky/api/events/DiSkyEvent.java | 7 ++-- .../disky/api/events/EventListener.java | 27 +------------- .../java/info/itsthesky/disky/core/Bot.java | 19 ++-------- .../itsthesky/disky/managers/BotManager.java | 1 + .../disky/managers/CoreEventListener.java | 37 +++++++++++++++++++ 6 files changed, 48 insertions(+), 45 deletions(-) create mode 100644 src/main/java/info/itsthesky/disky/managers/CoreEventListener.java diff --git a/build.gradle b/build.gradle index 4caf4dc..b9dfc54 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ group = 'info.itsthesky' // Semantic Versioning def major = '4' def minor = '20' -def patch = '1' +def patch = '2' def channel = '' def channelVersion = '' diff --git a/src/main/java/info/itsthesky/disky/api/events/DiSkyEvent.java b/src/main/java/info/itsthesky/disky/api/events/DiSkyEvent.java index 0f14f39..9727f7b 100644 --- a/src/main/java/info/itsthesky/disky/api/events/DiSkyEvent.java +++ b/src/main/java/info/itsthesky/disky/api/events/DiSkyEvent.java @@ -7,9 +7,8 @@ import ch.njol.skript.log.SkriptLogger; import info.itsthesky.disky.DiSky; import info.itsthesky.disky.core.SkriptUtils; +import info.itsthesky.disky.managers.CoreEventListener; import net.dv8tion.jda.api.audit.ActionType; -import net.dv8tion.jda.api.audit.AuditLogEntry; -import net.dv8tion.jda.api.events.GenericEvent; import net.dv8tion.jda.api.events.guild.GuildAuditLogEntryCreateEvent; import org.bukkit.event.Event; import org.jetbrains.annotations.NotNull; @@ -157,14 +156,14 @@ public void register(@NotNull Trigger t) { } }, checker(), logChecker(), getLogType(), bot); - EventListener.addListener(listener); + CoreEventListener.addListener(listener); } @Override public void unregister(final @NotNull Trigger t) { if (listener != null) { listener.enabled = false; - EventListener.removeListener(listener); + CoreEventListener.removeListener(listener); } listener = null; diff --git a/src/main/java/info/itsthesky/disky/api/events/EventListener.java b/src/main/java/info/itsthesky/disky/api/events/EventListener.java index 1ea0498..8833bbb 100644 --- a/src/main/java/info/itsthesky/disky/api/events/EventListener.java +++ b/src/main/java/info/itsthesky/disky/api/events/EventListener.java @@ -16,11 +16,10 @@ import java.util.function.Predicate; /** - * Made by Blitz, minor edit by Sky for DiSky + * Made by Blitz, edited by Sky for DiSky */ -public class EventListener extends ListenerAdapter { +public class EventListener { - public final static ArrayList> listeners = new ArrayList<>(); public boolean enabled = true; private final Class clazz; private final BiConsumer consumer; @@ -47,27 +46,6 @@ public EventListener(Class paramClass, this.logType = actionType; } - public static void addListener(EventListener listener) { - removeListener(listener); - listeners.add(listener); - } - - public static void removeListener(EventListener listener) { - listeners.remove(listener); - DiSky.getManager().execute(bot -> bot.getInstance().removeEventListener(listener)); - } - - public static void registerAll(Bot bot) { - listeners.forEach(listener -> { - if (listener.specificBotName != null && !listener.specificBotName.equalsIgnoreCase(bot.getName())) - return; - - DiSky.debug("Registering event listener " + listener.clazz.getSimpleName() + " for bot " + bot.getName() + listener.hash()); - bot.getInstance().addEventListener(listener); - }); - } - - @Override public void onGuildAuditLogEntryCreate(GuildAuditLogEntryCreateEvent event) { DiSky.debug("received log event " + event.getEntry().getType() + " by DiSky."); if (isWaitingLogEvent && event.getEntry().getType() == logType) { @@ -85,7 +63,6 @@ public void onGuildAuditLogEntryCreate(GuildAuditLogEntryCreateEvent event) { } @SuppressWarnings("unchecked") - @Override public void onGenericEvent(@NotNull GenericEvent event) { if (enabled && clazz.isInstance(event)) { DiSky.debug("Event " + event.getClass().getSimpleName() + " received by DiSky. Is it valid? " + checker.test((T) event) + "." + hash()); diff --git a/src/main/java/info/itsthesky/disky/core/Bot.java b/src/main/java/info/itsthesky/disky/core/Bot.java index 9d85770..7298c3b 100644 --- a/src/main/java/info/itsthesky/disky/core/Bot.java +++ b/src/main/java/info/itsthesky/disky/core/Bot.java @@ -4,28 +4,18 @@ import ch.njol.skript.util.Timespan; import info.itsthesky.disky.BotApplication; import info.itsthesky.disky.DiSky; -import info.itsthesky.disky.api.events.EventListener; import info.itsthesky.disky.api.events.SimpleDiSkyEvent; import info.itsthesky.disky.elements.structures.slash.BotReadyWaiter; import info.itsthesky.disky.elements.structures.slash.SlashManager; import net.dv8tion.jda.api.JDA; -import net.dv8tion.jda.api.JDABuilder; -import net.dv8tion.jda.api.entities.*; -import net.dv8tion.jda.api.entities.channel.*; -import net.dv8tion.jda.api.entities.channel.attribute.*; -import net.dv8tion.jda.api.entities.channel.middleman.*; -import net.dv8tion.jda.api.entities.channel.concrete.*; -import net.dv8tion.jda.api.entities.channel.concrete.NewsChannel; -import net.dv8tion.jda.api.entities.channel.concrete.PrivateChannel; -import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; -import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; -import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.Role; +import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.entities.channel.Channel; import org.bukkit.event.Event; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.lang.reflect.Method; - /** * Class that will handle every information about a bot. * @author ItsTheSky @@ -51,7 +41,6 @@ public Bot(String name, JDA instance, BotOptions options, @Nullable BotApplicati this.slashManager = SlashManager.getManager(this); BotReadyWaiter.onBotLoaded(this); - EventListener.registerAll(this); } public String getName() { diff --git a/src/main/java/info/itsthesky/disky/managers/BotManager.java b/src/main/java/info/itsthesky/disky/managers/BotManager.java index 8aadb3f..5eedc02 100644 --- a/src/main/java/info/itsthesky/disky/managers/BotManager.java +++ b/src/main/java/info/itsthesky/disky/managers/BotManager.java @@ -51,6 +51,7 @@ private void configureBot(Bot bot) { bot.getInstance().addEventListener(new ReactionListener()); bot.getInstance().addEventListener(new MessageManager(bot)); bot.getInstance().addEventListener(new MemberRemoveEventListener()); + bot.getInstance().addEventListener(new CoreEventListener(bot)); } public void shutdown() { diff --git a/src/main/java/info/itsthesky/disky/managers/CoreEventListener.java b/src/main/java/info/itsthesky/disky/managers/CoreEventListener.java new file mode 100644 index 0000000..264394c --- /dev/null +++ b/src/main/java/info/itsthesky/disky/managers/CoreEventListener.java @@ -0,0 +1,37 @@ +package info.itsthesky.disky.managers; + +import info.itsthesky.disky.core.Bot; +import net.dv8tion.jda.api.events.Event; +import net.dv8tion.jda.api.events.GenericEvent; +import net.dv8tion.jda.api.events.guild.GuildAuditLogEntryCreateEvent; +import net.dv8tion.jda.api.hooks.EventListener; + +import java.util.ArrayList; + +public class CoreEventListener implements EventListener { + + public final static ArrayList> AllRegisteredListeners = new ArrayList<>(); + + private final Bot bot; + public CoreEventListener(Bot bot) { + this.bot = bot; + } + + @Override + public void onEvent(GenericEvent event) { + if (event instanceof final GuildAuditLogEntryCreateEvent e) + AllRegisteredListeners.forEach(listener -> listener.onGuildAuditLogEntryCreate(e)); + + for (final info.itsthesky.disky.api.events.EventListener listener : AllRegisteredListeners) + listener.onGenericEvent(event); + } + + public static void addListener(info.itsthesky.disky.api.events.EventListener listener) { + AllRegisteredListeners.add(listener); + } + + public static void removeListener(info.itsthesky.disky.api.events.EventListener listener) { + AllRegisteredListeners.remove(listener); + } + +}