public class Listeners implements org.bukkit.event.Listener { public void register() { try { for (ClassPath.ClassInfo classInfo : ClassPath.from(AlixAPI.getInstance().getClass().getClassLoader()).getTopLevelClassesRecursive(AlixAPI.getInstance().getClass().getPackage().getName())) { try { classInfo.load().getDeclaredMethods(); } catch (NoClassDefFoundError e) { continue; } for (Method method : classInfo.load().getDeclaredMethods()) { if (method.isAnnotationPresent(Listener.class)) { Listener annotation = method.getAnnotation(Listener.class); RegisteredListener rl = new RegisteredListener(this, (listener, event) -> { try { method.invoke(null, event); } catch (IllegalArgumentException ignored) { } catch (Exception e) { e.printStackTrace(); } }, annotation.priority(), AlixAPI.getInstance(), annotation.ignoreCancelled()); try { HandlerList handlers = (HandlerList) method.getParameters()[0].getType().getDeclaredMethod("getHandlerList").invoke(null); handlers.register(rl); } catch (Exception e) { try { HandlerList handlers = (HandlerList) method.getParameters()[0].getType().getSuperclass().getDeclaredMethod("getHandlerList").invoke(null); handlers.register(rl); } catch (Exception e1) { AlixAPI.getInstance().getLogger().warning("The event " + method.getParameters()[0].getType().getSimpleName() + " does not have a getHandlerList() method!"); } } } } } } catch (Exception e) { e.printStackTrace(); } } }