Skip to content

Commit

Permalink
🚀 Reworked the event listener system
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsTheSky committed Oct 25, 2024
1 parent 7ae50ae commit 3f70d7f
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 45 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/info/itsthesky/disky/api/events/DiSkyEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
27 changes: 2 additions & 25 deletions src/main/java/info/itsthesky/disky/api/events/EventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> extends ListenerAdapter {
public class EventListener<T> {

public final static ArrayList<EventListener<?>> listeners = new ArrayList<>();
public boolean enabled = true;
private final Class<T> clazz;
private final BiConsumer<T, GuildAuditLogEntryCreateEvent> consumer;
Expand All @@ -47,27 +46,6 @@ public EventListener(Class<T> 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) {
Expand All @@ -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());
Expand Down
19 changes: 4 additions & 15 deletions src/main/java/info/itsthesky/disky/core/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/info/itsthesky/disky/managers/CoreEventListener.java
Original file line number Diff line number Diff line change
@@ -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<info.itsthesky.disky.api.events.EventListener<?>> 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 <D extends Event> void addListener(info.itsthesky.disky.api.events.EventListener<D> listener) {
AllRegisteredListeners.add(listener);
}

public static <D extends Event> void removeListener(info.itsthesky.disky.api.events.EventListener<D> listener) {
AllRegisteredListeners.remove(listener);
}

}

0 comments on commit 3f70d7f

Please sign in to comment.