From 7ae50ae63336a7bd95db9c1a0e25773395d01583 Mon Sep 17 00:00:00 2001 From: Sky Date: Mon, 21 Oct 2024 11:40:03 +0200 Subject: [PATCH] :bug: Fixed double event registering --- .../disky/api/events/DiSkyEvent.java | 2 +- .../disky/api/events/EventListener.java | 19 ++++++++++++++----- .../itsthesky/disky/managers/BotManager.java | 10 ---------- 3 files changed, 15 insertions(+), 16 deletions(-) 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 9b2db03..0f14f39 100644 --- a/src/main/java/info/itsthesky/disky/api/events/DiSkyEvent.java +++ b/src/main/java/info/itsthesky/disky/api/events/DiSkyEvent.java @@ -156,7 +156,7 @@ public void register(@NotNull Trigger t) { }); } - }, checker(), logChecker(), getLogType()); + }, checker(), logChecker(), getLogType(), bot); EventListener.addListener(listener); } 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 ecdd153..1ea0498 100644 --- a/src/main/java/info/itsthesky/disky/api/events/EventListener.java +++ b/src/main/java/info/itsthesky/disky/api/events/EventListener.java @@ -26,6 +26,7 @@ public class EventListener extends ListenerAdapter { private final BiConsumer consumer; private final Predicate checker; + private final @Nullable String specificBotName; private final boolean isWaitingLogEvent; private final @Nullable ActionType logType; private final Predicate logChecker; @@ -35,11 +36,12 @@ public class EventListener extends ListenerAdapter { public EventListener(Class paramClass, BiConsumer consumer, Predicate checker, Predicate logChecker, - @Nullable ActionType actionType) { + @Nullable ActionType actionType, @Nullable String specificBotName) { this.clazz = paramClass; this.consumer = consumer; this.checker = checker; this.logChecker = logChecker; + this.specificBotName = specificBotName; this.isWaitingLogEvent = actionType != null; this.logType = actionType; @@ -48,7 +50,6 @@ public EventListener(Class paramClass, public static void addListener(EventListener listener) { removeListener(listener); listeners.add(listener); - DiSky.getManager().registerGlobalListener(listener); } public static void removeListener(EventListener listener) { @@ -57,8 +58,13 @@ public static void removeListener(EventListener listener) { } public static void registerAll(Bot bot) { - listeners.forEach(listener -> bot.getInstance().removeEventListener(listener)); - listeners.forEach(listener -> bot.getInstance().addEventListener(listener)); + 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 @@ -94,8 +100,11 @@ public void onGenericEvent(@NotNull GenericEvent event) { } } - private String hash() { + public String hash() { return " [class hash: " + this.hashCode() + "]"; } + public Class getClazz() { + return clazz; + } } \ No newline at end of file diff --git a/src/main/java/info/itsthesky/disky/managers/BotManager.java b/src/main/java/info/itsthesky/disky/managers/BotManager.java index e499466..8aadb3f 100644 --- a/src/main/java/info/itsthesky/disky/managers/BotManager.java +++ b/src/main/java/info/itsthesky/disky/managers/BotManager.java @@ -1,7 +1,6 @@ package info.itsthesky.disky.managers; import com.google.common.collect.Sets; -import info.itsthesky.disky.DiSky; import info.itsthesky.disky.api.events.EventListener; import info.itsthesky.disky.api.skript.ErrorHandler; import info.itsthesky.disky.core.Bot; @@ -27,7 +26,6 @@ */ public class BotManager { - private final LinkedList> queuedListeners = new LinkedList<>(); private boolean anyBotEnabled = false; private final JavaPlugin plugin; @@ -53,7 +51,6 @@ private void configureBot(Bot bot) { bot.getInstance().addEventListener(new ReactionListener()); bot.getInstance().addEventListener(new MessageManager(bot)); bot.getInstance().addEventListener(new MemberRemoveEventListener()); - bot.getInstance().addEventListener(queuedListeners.toArray()); } public void shutdown() { @@ -142,13 +139,6 @@ public void removeBot(Bot bot) { bots.addAll(set); } - public void registerGlobalListener(EventListener listener) { - if (anyBotEnabled) - execute(bot -> bot.getInstance().addEventListener(listener)); - else - queuedListeners.add(listener); - } - public Bot getBotByName(String name) { return bots.stream().filter(bot -> bot.getName().equalsIgnoreCase(name)).findAny().orElse(null); }