From 7131021a86b66b6fb25c22e5287896b095d89ea3 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Thu, 3 Nov 2016 16:50:58 +0100 Subject: [PATCH] Fixed Reflections exception and added AddCommand With a beatiful piece of code. --- .../buttondevteam/lib/chat/TBMCChatAPI.java | 44 ++++++++++++++++--- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java b/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java index c1804d9..ae46009 100644 --- a/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java +++ b/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java @@ -1,7 +1,9 @@ package buttondevteam.lib.chat; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Modifier; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.Set; @@ -11,6 +13,7 @@ import org.reflections.scanners.SubTypesScanner; import org.reflections.util.ClasspathHelper; import org.reflections.util.ConfigurationBuilder; +import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCPlayer; public class TBMCChatAPI { @@ -53,10 +56,10 @@ public class TBMCChatAPI { */ public static void AddCommands(JavaPlugin plugin, Class acmdclass) { plugin.getLogger().info("Registering commands for " + plugin.getName()); - Reflections rf = new Reflections( - new ConfigurationBuilder().setUrls(ClasspathHelper.forClassLoader(plugin.getClass().getClassLoader())) - .addClassLoader(plugin.getClass().getClassLoader()).addScanners(new SubTypesScanner()) - .filterInputsBy((String pkg) -> pkg.contains(acmdclass.getPackage().getName()))); + Reflections rf = new Reflections(new ConfigurationBuilder() + .setUrls(ClasspathHelper.forPackage(acmdclass.getPackage().getName(), + plugin.getClass().getClassLoader())) + .addClassLoader(plugin.getClass().getClassLoader()).addScanners(new SubTypesScanner())); Set> cmds = rf.getSubTypesOf(TBMCCommandBase.class); for (Class cmd : cmds) { try { @@ -66,13 +69,42 @@ public class TBMCChatAPI { c.plugin = plugin; commands.put(c.GetCommandPath(), c); } catch (InstantiationException e) { - e.printStackTrace(); + TBMCCoreAPI.SendException("An error occured while registering command " + cmd.getName(), e); } catch (IllegalAccessException e) { - e.printStackTrace(); + TBMCCoreAPI.SendException("An error occured while registering command " + cmd.getName(), e); } } } + /** + *

+ * This method adds a plugin's command to help and sets it's executor. + *

+ *

+ * The command must be registered in the caller plugin's plugin.yml. Otherwise the plugin will output a messsage to console. + *

+ *

+ * Using this method after the server is done loading will have no effect. + *

+ * + * @param plugin + * The caller plugin + * @param thecmdclass + * The command's class to create it (because why let you create the command class) + */ + public static void AddCommand(JavaPlugin plugin, Class thecmdclass, Object... params) { + plugin.getLogger().info("Registering command " + thecmdclass.getName() + " for " + plugin.getName()); + try { + TBMCCommandBase c = thecmdclass + .getConstructor(Arrays.stream(params).map(p -> p.getClass()).toArray(Class[]::new)) + .newInstance(params); + c.plugin = plugin; + commands.put(c.GetCommandPath(), c); + } catch (Exception e) { + TBMCCoreAPI.SendException("An error occured while registering command " + thecmdclass.getName(), e); + } + } + /** *

* Add player information for {@link PlayerInfoCommand}. Only mods can see the given information.