Skip to content

Commit

Permalink
🐛 Fixed double event registering
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsTheSky committed Oct 21, 2024
1 parent 23e4228 commit 7ae50ae
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public void register(@NotNull Trigger t) {
});

}
}, checker(), logChecker(), getLogType());
}, checker(), logChecker(), getLogType(), bot);
EventListener.addListener(listener);
}

Expand Down
19 changes: 14 additions & 5 deletions src/main/java/info/itsthesky/disky/api/events/EventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class EventListener<T> extends ListenerAdapter {
private final BiConsumer<T, GuildAuditLogEntryCreateEvent> consumer;
private final Predicate<T> checker;

private final @Nullable String specificBotName;
private final boolean isWaitingLogEvent;
private final @Nullable ActionType logType;
private final Predicate<GuildAuditLogEntryCreateEvent> logChecker;
Expand All @@ -35,11 +36,12 @@ public class EventListener<T> extends ListenerAdapter {
public EventListener(Class<T> paramClass,
BiConsumer<T, GuildAuditLogEntryCreateEvent> consumer,
Predicate<T> checker, Predicate<GuildAuditLogEntryCreateEvent> 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;
Expand All @@ -48,7 +50,6 @@ public EventListener(Class<T> paramClass,
public static void addListener(EventListener<?> listener) {
removeListener(listener);
listeners.add(listener);
DiSky.getManager().registerGlobalListener(listener);
}

public static void removeListener(EventListener<?> listener) {
Expand All @@ -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
Expand Down Expand Up @@ -94,8 +100,11 @@ public void onGenericEvent(@NotNull GenericEvent event) {
}
}

private String hash() {
public String hash() {
return " [class hash: " + this.hashCode() + "]";
}

public Class<T> getClazz() {
return clazz;
}
}
10 changes: 0 additions & 10 deletions src/main/java/info/itsthesky/disky/managers/BotManager.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -27,7 +26,6 @@
*/
public class BotManager {

private final LinkedList<EventListener<?>> queuedListeners = new LinkedList<>();
private boolean anyBotEnabled = false;
private final JavaPlugin plugin;

Expand All @@ -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() {
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 7ae50ae

Please sign in to comment.