Skip to content

Commit

Permalink
[ci skip] Add more info to Listener exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberedCake committed Jun 26, 2023
1 parent 9285ace commit 92c58b2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,17 @@ public void init(String path) {
try {
listener = (Listener) clazz.getDeclaredConstructors()[0].newInstance();
} catch (InvocationTargetException invocationTargetException) {
if(CyberAPI.getInstance().getDescription().getMain().startsWith(clazz.getPackageName())) {
if(CyberAPI.getInstance().getDescription().getMain().startsWith(clazz.getPackageName()))
listener = (Listener) CyberAPI.getInstance();
}else{
throw invocationTargetException;
}
else throw new IllegalStateException("Listener failed to load: " + clazz.getCanonicalName() + " - " + clazz.getDeclaredConstructors()[0].getName(), invocationTargetException);
}
try {
if(CyberAPI.getInstance().getSettings().getDisabledAutoRegisteredClasses() != null && Arrays.asList(CyberAPI.getInstance().getSettings().getDisabledAutoRegisteredClasses()).contains(clazz)) continue;
autoRegisteredListeners++;
CyberAPI.getInstance().registerListener(listener);
CyberAPI.getInstance().getAPILogger().verbose("Registered listener automatically: " + clazz.getCanonicalName());
} catch (Exception exception) {
CyberAPI.getInstance().getAPILogger().error("An error occurred whilst registering listener at " + clazz.getCanonicalName() + " - " + clazz.getConstructors()[0].getName() + ": " + ChatColor.DARK_GRAY + exception);
CyberAPI.getInstance().getAPILogger().verboseException(exception);
throw new IllegalStateException("Listener failed to register automatically: " + clazz.getCanonicalName() + " - " + clazz.getDeclaredConstructors()[0].getName(), exception);
}
}
if(path == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.cybercake.cyberapi.common.builders.settings.Settings;
import net.cybercake.cyberapi.spigot.CyberAPI;
import net.cybercake.cyberapi.spigot.Validators;
import net.cybercake.cyberapi.spigot.chat.Log;
import org.bukkit.ChatColor;
import org.bukkit.event.Listener;

Expand All @@ -27,26 +28,28 @@ public static ListenerManager listenerManager() {
public void init(String path) {
try {
long mss = System.currentTimeMillis();
Log.warn("start");
for(Class<?> clazz : CyberAPI.getInstance().getPluginClasses()) {
if(!(Validators.isSubtype(clazz, SpigotListener.class))) continue;
Log.warn("valid->" + clazz.getCanonicalName());
Listener listener;
try {
listener = (Listener) clazz.getDeclaredConstructors()[0].newInstance();
} catch (InvocationTargetException invocationTargetException) {
Log.warn("try->alt");
if(CyberAPI.getInstance().getDescription().getMain().startsWith(clazz.getPackageName())) {
Log.warn("try->cast,main");
listener = (Listener) CyberAPI.getInstance();
}else{
throw invocationTargetException;
}
else throw new IllegalStateException("Listener failed to load: " + clazz.getCanonicalName() + " - " + clazz.getDeclaredConstructors()[0].getName(), invocationTargetException);
}
try {
if(CyberAPI.getInstance().getSettings().getDisabledAutoRegisteredClasses() != null && Arrays.asList(CyberAPI.getInstance().getSettings().getDisabledAutoRegisteredClasses()).contains(clazz)) continue;
autoRegisteredListeners++;
CyberAPI.getInstance().registerListener(listener);
CyberAPI.getInstance().getAPILogger().verbose("Registered listener automatically: " + clazz.getCanonicalName());
} catch (Exception exception) {
CyberAPI.getInstance().getAPILogger().error("An error occurred whilst registering listener at " + clazz.getCanonicalName() + " - " + clazz.getConstructors()[0].getName() + ": " + ChatColor.DARK_GRAY + exception);
CyberAPI.getInstance().getAPILogger().verboseException(exception);
throw new IllegalStateException("Listener failed to register automatically: " + clazz.getCanonicalName() + " - " + clazz.getDeclaredConstructors()[0].getName(), exception);
}
}
if(path == null) {
Expand All @@ -58,4 +61,4 @@ public void init(String path) {
}
}

}
}

0 comments on commit 92c58b2

Please sign in to comment.