From 7a703d216508f419b3c3c67e27d697c77725ad83 Mon Sep 17 00:00:00 2001 From: allenlol <1124796703@qq.com> Date: Tue, 2 Oct 2018 16:20:39 -0700 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BC=98=E5=8C=96=20PluginUtils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 使用 JavaPlugin 自带的 getFile 方法来获取插件文件 优化 --- .../me/skymc/taboolib/plugin/PluginUtils.java | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/main/java/me/skymc/taboolib/plugin/PluginUtils.java b/src/main/java/me/skymc/taboolib/plugin/PluginUtils.java index b1fb0a3..832b597 100644 --- a/src/main/java/me/skymc/taboolib/plugin/PluginUtils.java +++ b/src/main/java/me/skymc/taboolib/plugin/PluginUtils.java @@ -29,19 +29,14 @@ public class PluginUtils { PluginUtils() { } - public static File getPluginFile(Plugin plugin) { - for (File pluginFile : new File("plugins").listFiles()) { - if (pluginFile.getName().endsWith(".jar")) { - try { - PluginDescriptionFile desc = Main.getInst().getPluginLoader().getPluginDescription(pluginFile); - if (desc.getName().equalsIgnoreCase(plugin.getName())) { - return pluginFile; - } - } catch (InvalidDescriptionException ignored) { - } - } + private File getPluginFile(JavaPlugin plugin) { + try { + Method method = JavaPlugin.class.getDeclaredMethod("getFile"); + method.setAccessible(true); + return (File) method.invoke(plugin); + } catch (ReflectiveOperationException e) { + throw new RuntimeException("Could not get plugin file", e); } - return null; } public static boolean isPluginExists(String name) { From ccc1d6970739a563390200c2d71ebe6da02a21bc Mon Sep 17 00:00:00 2001 From: allenlol <1124796703@qq.com> Date: Tue, 2 Oct 2018 17:42:50 -0700 Subject: [PATCH 2/4] Update PluginUtils.java --- src/main/java/me/skymc/taboolib/plugin/PluginUtils.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/me/skymc/taboolib/plugin/PluginUtils.java b/src/main/java/me/skymc/taboolib/plugin/PluginUtils.java index 832b597..b28b211 100644 --- a/src/main/java/me/skymc/taboolib/plugin/PluginUtils.java +++ b/src/main/java/me/skymc/taboolib/plugin/PluginUtils.java @@ -9,6 +9,8 @@ import org.bukkit.command.PluginCommand; import org.bukkit.command.SimpleCommandMap; import org.bukkit.event.Event; import org.bukkit.plugin.*; +import org.bukkit.plugin.java.JavaPlugin; +import java.lang.reflect.Method; import java.io.File; import java.io.IOException; From eaf9bac6ec0a5f04db2b04d19a6e73823337d5e5 Mon Sep 17 00:00:00 2001 From: allenlol <1124796703@qq.com> Date: Tue, 2 Oct 2018 17:48:58 -0700 Subject: [PATCH 3/4] Update PluginUtils.java --- src/main/java/me/skymc/taboolib/plugin/PluginUtils.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/me/skymc/taboolib/plugin/PluginUtils.java b/src/main/java/me/skymc/taboolib/plugin/PluginUtils.java index b28b211..bba458d 100644 --- a/src/main/java/me/skymc/taboolib/plugin/PluginUtils.java +++ b/src/main/java/me/skymc/taboolib/plugin/PluginUtils.java @@ -31,9 +31,9 @@ public class PluginUtils { PluginUtils() { } - private File getPluginFile(JavaPlugin plugin) { + private File getPluginFile(Plugin plugin) { try { - Method method = JavaPlugin.class.getDeclaredMethod("getFile"); + Method method = plugin.getClass().getDeclaredMethod("getFile"); method.setAccessible(true); return (File) method.invoke(plugin); } catch (ReflectiveOperationException e) { From b69db05b618de562900c1dbe85f134741e552920 Mon Sep 17 00:00:00 2001 From: allenlol <1124796703@qq.com> Date: Tue, 2 Oct 2018 17:49:48 -0700 Subject: [PATCH 4/4] Update PluginUtils.java --- src/main/java/me/skymc/taboolib/plugin/PluginUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/me/skymc/taboolib/plugin/PluginUtils.java b/src/main/java/me/skymc/taboolib/plugin/PluginUtils.java index bba458d..1c26336 100644 --- a/src/main/java/me/skymc/taboolib/plugin/PluginUtils.java +++ b/src/main/java/me/skymc/taboolib/plugin/PluginUtils.java @@ -31,7 +31,7 @@ public class PluginUtils { PluginUtils() { } - private File getPluginFile(Plugin plugin) { + public static File getPluginFile(Plugin plugin) { try { Method method = plugin.getClass().getDeclaredMethod("getFile"); method.setAccessible(true);