Get all the available commands

Discussion in 'Plugin Development' started by anerach, Feb 25, 2012.

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

    anerach

    Hello,
    I'm trying to make a command logger, I'm using the "PlayerCommandPreprocessEvent" Event.
    But the problem is that this event gets triggered everytime a player says a command, even for commands that don't exists.
    Is it possible to get a list of all the available commands, including the commands from plugins so I can verify wich one actually exist?
     
  2. Offline

    Royal_Soda

    I'm sure this is possible since Essentials does it for their /help command. Although I'm not too sure how they do it.. :oops:
     
  3. Offline

    Bertware

    I guess you could scrape all plugin.yml files, but that would be inefficient I guess. I am sure there is a method to do this, as I saw some other plugins that retrieved all commands.
     
  4. Offline

    anerach

    Yea, I know.
    Thats why I asked how to do it since I knew it should possible :p.
     
  5. when you know that essentials does it, you can take a look at the source of it and see how they've done it ;)
     
  6. Offline

    anerach

    I did, but I don't understand it.
    I just started to learn how to make plugins and my java knowledge isn't that good either.

    Help please.

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

    Njol

    You can use Bukkit.getPluginCommand(commandLabel) != null to test whether commandLabel is a valid command, but this only works for commands added by plugins.
    In regards of the default commands i could only find Bukkit.getCommandAliases(), but the documentation is unclear about what it actually contains.
     
  8. Offline

    anerach

    Thanks, this works for plugin commands however it doesn't work with every bukkit command but its a start.
     
  9. Offline

    Technius

    There is a better way of doing it, but this is what I have:
    Code:java
    1. PluginManager manager = getServer().getPluginManager();
    2. SimplePluginManager spm = (SimplePluginManager) manager;
    3. List<Plugin> plugins = null;
    4. Map<String, Plugin> lookupNames = null;
    5. SimpleCommandMap commandMap = null;
    6. Map<String, Command> knownCommands = null;
    7. if (spm != null)
    8. {
    9. Field pluginsField = spm.getClass().getDeclaredField("plugins");
    10. Field lookupNamesField = spm.getClass().getDeclaredField("lookupNames");
    11. Field commandMapField = spm.getClass().getDeclaredField("commandMap");
    12. pluginsField.setAccessible(true);
    13. lookupNamesField.setAccessible(true);
    14. commandMapField.setAccessible(true);
    15. plugins = (List<Plugin>) pluginsField.get(spm);
    16. lookupNames = (Map<String, Plugin>) lookupNamesField.get(spm);
    17. commandMap = (SimpleCommandMap) commandMapField.get(spm);
    18. Field knownCommandsField = commandMap.getClass().getDeclaredField("knownCommands");
    19. knownCommandsField.setAccessible(true);
    20. knownCommands = (Map<String, Command>) knownCommandsField.get(commandMap);
    21. }
    22.  
    23. if (commandMap != null)
    24. {
    25. for (Iterator<Map.Entry<String, Command>> it = knownCommands.entrySet().iterator(); it.hasNext())
    26. {
    27. Map.Entry<String, Command> entry = it.next();
    28. if (entry.getValue() instanceof PluginCommand)
    29. {
    30. PluginCommand c = (PluginCommand) entry.getValue();
    31. //"c" is the command
    32. }
    33. }
    34. }

    Edit: My formatting got screwed up >.<
     
  10. Offline

    michaelkrauty

    I figured I'd revive this thread for those who need to know how to do this (it took me quite a while to figure this out)

    Code:
    for (HelpTopic cmdLabel : getServer().getHelpMap().getHelpTopics()) {
        getServer().broadcastMessage(cmdLabel.getName());
    }

    Have a nice day :)
     
Thread Status:
Not open for further replies.

Share This Page