From 7f3aa75c44be0350ff732e4bc3b4be5e1c936d0f Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Sun, 23 Feb 2020 10:54:09 +1300 Subject: [PATCH] Don't fatal error if someone's using a dumb jar system --- .../libraryaddict/disguise/LibsDisguises.java | 6 ++-- .../utilities/reflection/ClassGetter.java | 3 +- .../reflection/asm/WatcherSanitizer.java | 36 +++++++++++++------ .../translations/TranslateFiller.java | 17 ++++++--- 4 files changed, 43 insertions(+), 19 deletions(-) diff --git a/src/main/java/me/libraryaddict/disguise/LibsDisguises.java b/src/main/java/me/libraryaddict/disguise/LibsDisguises.java index 9e8f24b9..8a930550 100644 --- a/src/main/java/me/libraryaddict/disguise/LibsDisguises.java +++ b/src/main/java/me/libraryaddict/disguise/LibsDisguises.java @@ -34,13 +34,13 @@ public class LibsDisguises extends JavaPlugin { if (!Bukkit.getServer().getWorlds().isEmpty()) { reloaded = true; + getLogger() + .severe("Lib's Disguises was reloaded! Please do not report any bugs! This plugin can't handle " + + "reloads gracefully!"); return; } WatcherSanitizer.init(); - - getLogger().severe("Lib's Disguises was reloaded! Please do not report any bugs! This plugin can't handle " + - "reloads gracefully!"); } @Override diff --git a/src/main/java/me/libraryaddict/disguise/utilities/reflection/ClassGetter.java b/src/main/java/me/libraryaddict/disguise/utilities/reflection/ClassGetter.java index 1f23f419..4a3ee13b 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/reflection/ClassGetter.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/reflection/ClassGetter.java @@ -31,9 +31,10 @@ public class ClassGetter { if (src != null) { URL resource = src.getLocation(); - if (resource.getPath().endsWith(".jar")) { + if (resource.getPath().toLowerCase().endsWith(".jar")) { processJarfile(resource, pkgname, classes); } else { + System.out.println("Not sure how you got here: " + resource.getPath()); for (File f : new File(resource.getPath() + "/" + pkgname.replace(".", "/")).listFiles()) { if (!f.getName().endsWith(".class")) { continue; diff --git a/src/main/java/me/libraryaddict/disguise/utilities/reflection/asm/WatcherSanitizer.java b/src/main/java/me/libraryaddict/disguise/utilities/reflection/asm/WatcherSanitizer.java index ad0db887..b754b2c5 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/reflection/asm/WatcherSanitizer.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/reflection/asm/WatcherSanitizer.java @@ -24,9 +24,6 @@ public class WatcherSanitizer { public static void checkPreLoaded() throws NoSuchFieldException, IllegalAccessException { JavaPluginLoader javaLoader = (JavaPluginLoader) LibsDisguises.getInstance().getPluginLoader(); - Field cM = JavaPluginLoader.class.getDeclaredField("classes"); - cM.setAccessible(true); - Map> classes = (Map>) cM.get(javaLoader); Field lM = JavaPluginLoader.class.getDeclaredField("loaders"); lM.setAccessible(true); List loaders = (List) lM.get(javaLoader); @@ -38,15 +35,9 @@ public class WatcherSanitizer { for (Object loader : loaders) { Map> lClasses = (Map>) lF.get(loader); + PluginDescriptionFile desc = (PluginDescriptionFile) dF.get(loader); - for (Class c : lClasses.values()) { - if (!c.getName().startsWith("me.libraryaddict.disguise.disguisetypes.watchers.") && - !c.getName().equals("me.libraryaddict.disguise.disguisetypes.FlagWatcher")) { - continue; - } - - PluginDescriptionFile desc = (PluginDescriptionFile) dF.get(loader); - + if (hasWatcher(lClasses)) { LibsDisguises.getInstance().getLogger().severe(desc.getFullName() + " has been a naughty plugin, they're declaring access to the disguise watchers before Lib's " + "Disguises can properly load them! They should add 'LibsDisguises' to the 'depend' section of" + @@ -54,6 +45,29 @@ public class WatcherSanitizer { break; } } + + Field cM = JavaPluginLoader.class.getDeclaredField("classes"); + cM.setAccessible(true); + Map> classes = (Map>) cM.get(javaLoader); + + if (hasWatcher(classes)) { + LibsDisguises.getInstance().getLogger() + .severe("Somehow the main server has a Watcher instance! Hopefully there was a plugin mentioned " + + "above! This is a bug!"); + } + } + + private static boolean hasWatcher(Map> classes) { + for (Class c : classes.values()) { + if (!c.getName().startsWith("me.libraryaddict.disguise.disguisetypes.watchers.") && + !c.getName().equals("me.libraryaddict.disguise.disguisetypes.FlagWatcher")) { + continue; + } + + return true; + } + + return false; } public static void init() { diff --git a/src/main/java/me/libraryaddict/disguise/utilities/translations/TranslateFiller.java b/src/main/java/me/libraryaddict/disguise/utilities/translations/TranslateFiller.java index 081836bd..ede54d8c 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/translations/TranslateFiller.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/translations/TranslateFiller.java @@ -1,6 +1,7 @@ package me.libraryaddict.disguise.utilities.translations; import me.libraryaddict.disguise.disguisetypes.DisguiseType; +import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.params.ParamInfo; import me.libraryaddict.disguise.utilities.params.ParamInfoManager; import me.libraryaddict.disguise.utilities.reflection.ClassGetter; @@ -80,12 +81,20 @@ public class TranslateFiller { TranslateType.DISGUISE_OPTIONS.save("baby", "Used as a shortcut for setBaby when disguising an entity"); TranslateType.DISGUISE_OPTIONS.save("adult", "Used as a shortcut for setBaby(false) when disguising an entity"); - for (Class c : ClassGetter.getClassesForPackage("org.bukkit.entity")) { - if (c != Entity.class && Entity.class.isAssignableFrom(c) && c.getAnnotation(Deprecated.class) == null) { - TranslateType.DISGUISES.save(c.getSimpleName(), - "Name for the " + c.getSimpleName() + " EntityType, " + "this is used in radius commands"); + try { + for (Class c : ClassGetter.getClassesForPackage("org.bukkit.entity")) { + if (c != Entity.class && Entity.class.isAssignableFrom(c) && + c.getAnnotation(Deprecated.class) == null) { + TranslateType.DISGUISES.save(c.getSimpleName(), + "Name for the " + c.getSimpleName() + " EntityType, " + "this is used in radius commands"); + } } } + catch (Exception ex) { + DisguiseUtilities.getLogger() + .severe("Error while trying to read entity types, assuming you're using a weird jar loader and " + + "not making this fatal.."); + } TranslateType.DISGUISES.save("EntityType", "Used for the disgiuse radius command to list all entitytypes"); TranslateType.DISGUISES