Dynamic registration of commands

Discussion in 'Plugin Development' started by Nitnelave, Mar 17, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Nitnelave

    How can you register a command dynamically, in the onEnable() of a plugin? Because I want to be able to set an alias to a command in the config, and when I load the plugin and read from the config, I set the command's aliases to those defined in it.
     
  2. Offline

    Don Redhorse

    well that is something I also would like to know.

    atm I do this with a player chat listener but I would like to use the original method setAliases() instead.

    To me it looks like it doesn't work if you do it after the fact, or that it requires the aliases already defined in the plugin.yml

    will need to take a look into the code
     
  3. Offline

    nisovin

    Just tell server admins to set custom aliases in the bukkit.yml file. If everyone learned about this feature, you wouldn't need to worry about this at all.
     
  4. Offline

    Nitnelave

    Just for info, to share with everyone, my code looks like that :
    Code:java
    1.  
    2. commandExecutor = new CreeperCommandManager(this); //my command manager
    3. CommandMap commandMap = null;
    4. try{
    5. Field field = SimplePluginManager.class.getDeclaredField("commandMap");
    6. field.setAccessible(true);
    7. commandMap = (CommandMap)(field.get(getServer().getPluginManager()));
    8. e.printStackTrace();
    9. }
    10. e.printStackTrace();
    11. }
    12.  
    13. String[] aliases = {"CreeperHeal",config.alias};
    14. CreeperCommand com = new CreeperCommand(aliases, "", "", commandExecutor);
    15.  
    16. commandMap.register("_", com);
     
  5. Offline

    Don Redhorse

    reflection to the rescue... does this work?
     
  6. Offline

    Nitnelave

    Yes, it does, why? You can't make it work? or does something look weird? It's true that we are accessing a protected field from outside the package, but... it works!
     
  7. Offline

    Don Redhorse

    just wanted to know before I try to integrate that into my project... one less missing piece of my HelperClasses.

    I hope I can than finish the stuff this evening and start developing a new plugin finally.

    hmm.. i have issues with line 16 and 18..

    I use a class which implements CommandExecutor (so it is the command really).

    Line 18 registers the command _ and points to your CreeperCommand.. so in my case _ would be the command I want to use and com would in theory be this... but how can I add my aliases?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  8. Offline

    Darkman2412

    I think CreeperCommand extends Command (or maybe PluginCommand).
     
  9. Offline

    Don Redhorse

  10. Offline

    Nitnelave

    CreeperCommand extends Command, and CreeperCommandManager extends commandExecutor.

    The "_" is the prefix if the command is already registered, and CreeperCommand is the command, including the aliases I defined in the CreeperCommand constructor. But your command musn't be registered in the plugin.yml! Otherwise It just takes in account the aliases defined there. You can register a command only once, and it will keep the aliases it had at that time (you can't change them).
     
  11. Offline

    Don Redhorse

    may I ask how do you know all this... it is not in the javadocs afaik... but why do I really ask...

    Ok, thanks a lot for the explanation, I will go for a player chat listener than.
     
Thread Status:
Not open for further replies.

Share This Page