Reflections API for listeners

This commit is contained in:
Telesphoreo 2022-03-19 14:27:55 -05:00
parent 159ed046cb
commit 1b7511e817
5 changed files with 39 additions and 29 deletions

View File

@ -2,7 +2,6 @@ package dev.plex.command.annotation;
public @interface System public @interface System
{ {
String value() default ""; String value() default "";
boolean debug() default false; boolean debug() default false;

View File

@ -42,7 +42,7 @@ public class CommandHandler extends PlexBase
} }
catch (InvocationTargetException | InstantiationException | IllegalAccessException | NoSuchMethodException ex) catch (InvocationTargetException | InstantiationException | IllegalAccessException | NoSuchMethodException ex)
{ {
PlexLog.log("Failed to register " + clazz.getSimpleName() + " as a command!"); PlexLog.error("Failed to register " + clazz.getSimpleName() + " as a command!");
} }
}); });

View File

@ -1,40 +1,44 @@
package dev.plex.handlers; package dev.plex.handlers;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import dev.plex.Plex; import dev.plex.PlexBase;
import dev.plex.listener.PlexListener; import dev.plex.listener.PlexListener;
import dev.plex.listener.impl.AdminListener; import dev.plex.listener.annotation.Toggled;
import dev.plex.listener.impl.BanListener;
import dev.plex.listener.impl.ChatListener;
import dev.plex.listener.impl.CommandListener;
import dev.plex.listener.impl.FreezeListener;
import dev.plex.listener.impl.GameModeListener;
import dev.plex.listener.impl.PlayerListener;
import dev.plex.listener.impl.ServerListener;
import dev.plex.listener.impl.TabListener;
import dev.plex.listener.impl.WorldListener;
import dev.plex.util.PlexLog; import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils;
import java.lang.reflect.InvocationTargetException;
import java.util.List; import java.util.List;
import java.util.Set;
//TODO: Switch to Reflections API public class ListenerHandler extends PlexBase
public class ListenerHandler
{ {
public ListenerHandler() public ListenerHandler()
{ {
Set<Class<? extends PlexListener>> listenerSet = PlexUtils.getClassesBySubType("dev.plex.listener.impl", PlexListener.class);
List<PlexListener> listeners = Lists.newArrayList(); List<PlexListener> listeners = Lists.newArrayList();
listeners.add(new AdminListener());
listeners.add(new BanListener()); listenerSet.forEach(clazz ->
if (Plex.get().config.getBoolean("chat.enabled"))
{ {
listeners.add(new ChatListener()); try
{
Toggled annotation = clazz.getDeclaredAnnotation(Toggled.class);
if (annotation != null)
{
if (plugin.config.getBoolean("chat.enabled") && annotation.enabled())
{
listeners.add(clazz.getConstructor().newInstance());
} }
listeners.add(new CommandListener()); }
listeners.add(new FreezeListener()); else
listeners.add(new GameModeListener()); {
listeners.add(new PlayerListener()); listeners.add(clazz.getConstructor().newInstance());
listeners.add(new ServerListener()); }
listeners.add(new TabListener()); }
listeners.add(new WorldListener()); catch (InvocationTargetException | InstantiationException | IllegalAccessException | NoSuchMethodException ex)
PlexLog.log(String.format("Registered %s listeners!", listeners.size())); {
PlexLog.error("Failed to register " + clazz.getSimpleName() + " as a listener!");
}
});
PlexLog.log(String.format("Registered %s listeners from %s classes!", listeners.size(), listenerSet.size()));
} }
} }

View File

@ -0,0 +1,6 @@
package dev.plex.listener.annotation;
public @interface Toggled
{
boolean enabled() default false;
}

View File

@ -2,8 +2,8 @@ package dev.plex.listener.impl;
import dev.plex.cache.PlayerCache; import dev.plex.cache.PlayerCache;
import dev.plex.listener.PlexListener; import dev.plex.listener.PlexListener;
import dev.plex.listener.annotation.Toggled;
import dev.plex.player.PlexPlayer; import dev.plex.player.PlexPlayer;
import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils; import dev.plex.util.PlexUtils;
import io.papermc.paper.chat.ChatRenderer; import io.papermc.paper.chat.ChatRenderer;
import io.papermc.paper.event.player.AsyncChatEvent; import io.papermc.paper.event.player.AsyncChatEvent;
@ -15,6 +15,7 @@ import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@Toggled(enabled = true)
public class ChatListener extends PlexListener public class ChatListener extends PlexListener
{ {
private final PlexChatRenderer renderer = new PlexChatRenderer(); private final PlexChatRenderer renderer = new PlexChatRenderer();