- * This method adds a plugin's commands to help and sets their executor. - *
- *- *
- * The command classes have to have a constructor each with no parameters - *- * 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 acmdclass A command's class to get the package name for commands. The provided class's package and subpackages are scanned for commands. - */ - public static synchronized void AddCommands(JavaPlugin plugin, Class extends TBMCCommandBase> acmdclass) { - plugin.getLogger().info("Registering commands from " + acmdclass.getPackage().getName()); - Reflections rf = new Reflections(new ConfigurationBuilder() - .setUrls(ClasspathHelper.forPackage(acmdclass.getPackage().getName(), - plugin.getClass().getClassLoader())) - .addUrls( - ClasspathHelper.forClass(OptionallyPlayerCommandBase.class, - OptionallyPlayerCommandBase.class.getClassLoader()), - ClasspathHelper.forClass(PlayerCommandBase.class, PlayerCommandBase.class.getClassLoader())) // http://stackoverflow.com/questions/12917417/using-reflections-for-finding-the-transitive-subtypes-of-a-class-when-not-all - .addClassLoader(plugin.getClass().getClassLoader()).addScanners(new SubTypesScanner())); - Set- * This method adds a plugin's command to help and sets it's executor. They will be automatically unregistered on plugin disable. - *
- *- * 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 extends TBMCCommandBase> thecmdclass, Object... params) { - // plugin.getLogger().info("Registering command " + thecmdclass.getSimpleName() + " for " + plugin.getName()); - try { - TBMCCommandBase c; - if (params.length > 0) - c = thecmdclass.getConstructor(Arrays.stream(params).map(Object::getClass).toArray(Class[]::new)) - .newInstance(params); - else - c = thecmdclass.newInstance(); - c.plugin = plugin; - CommandCaller.RegisterCommand(c); //Will check for nulls - commands.put(c.GetCommandPath(), c); - } catch (Exception e) { - TBMCCoreAPI.SendException("An error occured while registering command " + thecmdclass.getSimpleName(), e); - } - } - - /** - *- * This method adds a plugin's command to help and sets its executor. They will be automatically unregistered on plugin disable. - *
- *- * The command must be registered in the caller plugin's plugin.yml. Otherwise the plugin will output a message to console. - *
- *- * Using this method after the server is done loading will have no effect. - *
- * - * @param plugin The caller plugin - * @param cmd The command to add - */ - public static void AddCommand(JavaPlugin plugin, TBMCCommandBase cmd) { - try { - if (plugin == null) throw new IllegalArgumentException("The plugin is null!"); - if (cmd == null) throw new IllegalArgumentException("The command is null!"); - cmd.plugin = plugin; - CommandCaller.RegisterCommand(cmd); //Checks for other nulls - commands.put(cmd.GetCommandPath(), cmd); - } catch (Exception e) { - TBMCCoreAPI.SendException("An error occured while registering command " + (cmd == null ? "n u l l" : cmd.GetCommandPath()), e); - } - } - - /** - *- * This method adds a plugin's command to help and sets its executor. They will be automatically unregistered on component disable. - *
- *- * The command must be registered in the caller plugin's plugin.yml. Otherwise the plugin will output a message to console. - *
- *- * Using this method after the server is done loading will have no effect. - *
- * - * @param component The caller component - * @param cmd The command to add - */ - public static void AddCommand(Component component, TBMCCommandBase cmd) { - try { - if (component == null) throw new IllegalArgumentException("The component is null!"); - if (cmd == null) throw new IllegalArgumentException("The command is null!"); - cmd.plugin = component.getPlugin(); - cmd.component = component; - CommandCaller.RegisterCommand(cmd); //Checks for other nulls - commands.put(cmd.GetCommandPath(), cmd); - } catch (Exception e) { - TBMCCoreAPI.SendException("An error occured while registering command " + (cmd == null ? "n u l l" : cmd.GetCommandPath()), e); - } - } - - /** - * Removes all commands from the plugin - * - * @param plugin The plugin to remove commands from - */ - public static void RemoveCommands(JavaPlugin plugin) { - commands.values().removeIf(c -> { - try { - if (c.plugin == plugin) { - CommandCaller.UnregisterCommand(c); - return true; //Remove - } - return false; - } catch (Exception e) { - TBMCCoreAPI.SendException("An error occured while unregistering commands for " + plugin.getName(), e); - return true; //Remove if it couldn't get the plugin command - } - }); - } - - /** - * Removes all commands from the component - * - * @param component The component to remove commands from - */ - public static void RemoveCommands(Component component) { - commands.values().removeIf(c -> { - try { - if (c.component == component) { - CommandCaller.UnregisterCommand(c); - return true; //Remove - } - return false; - } catch (Exception e) { - TBMCCoreAPI.SendException("An error occured while unregistering commands for " + component.getClass().getSimpleName(), e); - return true; //Remove if it couldn't get the plugin command - } - }); - } - /** * Sends a chat message to Minecraft. Make sure that the channel is registered with {@link #RegisterChatChannel(Channel)}.