Registering commands at runtime?

Discussion in 'Plugin Development' started by Njol, Feb 17, 2012.

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

    Njol

    Hello devs,

    Is it possible to register commands at runtime? I found CommandMap.register(...), but it's not possible to get the server's command map via Bukkit. I think I know how to get the one from CraftBukkit, but I'd like not to build my plugin against CraftBukkit.
    So my question is if it's already possible to register commands at runtime, and if not, whether it will be possible soon.

    Also is it possible that PlayerCommandPreprocessEvent and ServerCommandEvent don't fire for unknown commands? Because currently I use these instead but they don't work.

    *bump*

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  2. mayby whit JavaPlugin.getPluginCommand("CommandName")
     
  3. Offline

    Njol

    It will return null for unknown commands:
     
  4. Offline

    Don Redhorse

    if you want to register commands at runtime you need use a player chat listener... you CAN'T register normal commands at runtime
     
  5. Offline

    Njol

    Does only PlayerChatEvent work? That will prevent any of my commands from being run from the console.
     
  6. Offline

    Don Redhorse

    well I think you can also use the command handler from WorldEdit or Citizen... that looks quite powerfull
     
  7. Offline

    Njol

    I decided to directly access CraftBukkit.
    This is the code I use to get the command map if anyone is interested:
    Code:java
    1. static CommandMap commandMap = null;
    2. static {
    3. try {
    4.  
    5. if (Bukkit.getPluginManager() instanceof SimplePluginManager) {
    6. final Field f = SimplePluginManager.class.getDeclaredField("commandMap");
    7. f.setAccessible(true);
    8.  
    9. commandMap = (CommandMap) f.get(Bukkit.getPluginManager());
    10. }
    11.  
    12. } catch (final NoSuchFieldException e) {
    13. outdatedError(e);
    14. } catch (final SecurityException e) {
    15. outdatedError(e);
    16. } catch (final IllegalArgumentException e) {
    17. outdatedError(e);
    18. } catch (final IllegalAccessException e) {
    19. outdatedError(e);
    20. }
    21. }

    Before adding commands I check whether commandMap is null and log an error message if it is.
     
Thread Status:
Not open for further replies.

Share This Page