Multiple plugins with same main command

Discussion in 'Plugin Development' started by AndyAndreiH, Feb 14, 2015.

Thread Status:
Not open for further replies.
  1. Hello everyone!

    I am currently working on a plugin suite which consists of a main module and other modules like authentication or land protection. I would like my plugin to have one single main command like /plugin, and then all the other modules to add their own subcommands (ex: /plugin auth, /plugin land, /plugin other). Is this possible and if so, how?

    My current setup where I just have a command executor for each plugin makes it so that if the subcommand is not present in the main module which is loaded first, the command returns false before the module can execute the code for that particular subcommand.

    Thank you for your time!

    Best regards,
    Andrei
     
    Datdenkikniet likes this.
  2. Offline

    Dudemister1999

    You could always have a static function to register sub-commands, and have your main module be in charge of the command*.

    Example:

    Code:
    MainCommand.registerSubCommand("test", new SubCommandExecutor())
    class SubCommandExecutor implements CommandExecutor
    Then, if you keep a HashMap of subcommands and CommandExecutors, you can just check (In the main onCommand()) if it's a registered subcommand, and call* the subcommand's onCommand() method.

    Sorry if it doesn't make much sense, I can create an example if required.

    *(Edited to fix obvious typos)
     
    Last edited: Feb 14, 2015
  3. That actually does makes sense, but it rises another question: How can I get the class of the module? I am currently loading it in the main module, but I can't access the classes of the loaded module, or I don't know how, anyway.

    An idea would be is to have all SubCommandExecutors in the main module and have the different modules add the subcommands to the HashMap?

    I'm not exactly sure on how to do it, so if it's not too much to ask a concrete example would be superb.

    I should mention the fact that the modules are completely separate plugins, having the modules depend on the main one in the plugin.yml file. The main plugin then loads them in the onEnable() function using the getPluginManager() functions.
     
  4. Offline

    Dudemister1999

    @AndyAndreiH What you'd do is call the register function in the onEnable() of the modules.

    Code:
    public static void registerModuleCommand(String cmd, CommandExecutor executor)
    {
      if(moduleCommands.containsKey(cmd))
      {
        //Already registered
      }
      else
      {
        moduleCommands.put(cmd, executor);
      }
    }
    Put that in your main command, and keep a HashMap<String, CommandExecutor>.

    Then, in onCommand():

    Code:
    if(args.length > 0)
    {
      if(args.length > 1)
      {
        if(moduleCommands.containsKey(args[1]))
        { //It's a sub command
          CommandExecutor subCommand = moduleCommands.get(args[1]);
          subCommand.onCommand(all, the, args);
        }
        else
        {
        //It isn't a sub-command, what is wrong?
        }
      }
    }
    Note I wrote this all on the fly, so it won't be perfect.

    I had to do the exact same thing (Main plugin, the loader) then a bunch of separate plugin modules. This is how I accomplished it.
     
    AndyAndreiH likes this.
  5. Thank you very much for the prompt reply. This will definitely work and it will save me a lot of hassle!

    EDIT: One last question (hehe): How do I get the class of the main module? If I call the function by instantiating a new main class, that'll mean I'll have 50 hashmaps with different data.
     
  6. Offline

    Dudemister1999

    @AndyAndreiH Make sure you store the Hashmap and subcommand registering method in the command class, and just add your main module as a dependency

    plugin.yml:
    depend: [MainModule]

    Then in your IDE, just like you added Bukkit as a dependency, add your main plugin jar as one.
     
  7. And when I call it it won't instantiate it, rather call the existing object. That makes sense. Thank you!
     
  8. Offline

    Dudemister1999

Thread Status:
Not open for further replies.

Share This Page