How to get CommandMap with default commands?

Discussion in 'Plugin Development' started by avatarDr, Jan 13, 2013.

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

    avatarDr

    Subj. I get the SimpleCommandMap using method from here, but it only contains plugin commands.
    Code:
                SimplePluginManager spm=(SimplePluginManager)plugin.getServer().getPluginManager();
                Field f=SimplePluginManager.class.getDeclaredField("commandMap");
                f.setAccessible(true);
                SimpleCommandMap scm=(SimpleCommandMap)f.get(spm);
                f.setAccessible(false);
     
  2. Offline

    camyono

    I dont see a direct way like yours.

    If you check here:
    https://github.com/Bukkit/Bukkit/blob/master/src/main/java/org/bukkit/command/SimpleCommandMap.java

    you will see that the "bukkit" label of the default commands will be overwritten by aliases or fallback prefixes:

    So you need to find the association between a plugin and it's commands. Then you can remove the plugin specific commands from the list of all commands to get the default commands.

    Otherwise explain what you are trying to do - maybe there is another way to work with it.

    SimpleCommandMap.java:135
    Code:java
    1.  
    2. // If the command exists but is an alias we overwrite it, otherwise we rename it based on the fallbackPrefix
    3. while (knownCommands.containsKey(lowerLabel) && !aliases.contains(lowerLabel)) {
    4. lowerLabel = lowerPrefix + ":" + lowerLabel;
    5. registerdPassedLabel = false;
    6. }
    7.  
     
  3. Offline

    avatarDr

    I want to remove some standard commands which I never use.
    (And I don't have any plugin command, use my own system to handle them.)
     
  4. Offline

    camyono

    Something like this?:

    Code:java
    1.  
    2.  
    3. SimplePluginManager spm = (SimplePluginManager) this.getServer()
    4. .getPluginManager();
    5.  
    6. Field f = null;
    7. f = SimplePluginManager.class.getDeclaredField("commandMap");
    8. f.setAccessible(true);
    9.  
    10. SimpleCommandMap scm = (SimpleCommandMap) f.get(spm);
    11.  
    12. Collection<Command> coll = scm.getCommands();
    13.  
    14. for (Command c : coll) {
    15. if (c.getName().contains("idontneedthiscommand")) { // Check whether
    16. // you need or
    17. // not
    18. c.unregister(scm); // unregister
    19. }
    20. }
    21.  
    22. f.setAccessible(false);
    23.  
     
  5. Offline

    avatarDr

    Yes... Did you read my first post?
     
  6. Offline

    camyono

    As i mentioned in my post - check the code:

    https://github.com/Bukkit/Bukkit/blob/master/src/main/java/org/bukkit/command/SimpleCommandMap.java

    The command map includes the default command as in:

    Code:java
    1.  
    2. private void setDefaultCommands(final Server server) {
    3. register("bukkit", new SaveCommand());
    4. register("bukkit", new SaveOnCommand());
    5. register("bukkit", new SaveOffCommand());
    6. register("bukkit", new StopCommand());
    7. register("bukkit", new VersionCommand("version"));
    8. register("bukkit", new ReloadCommand("reload"));
    9. register("bukkit", new PluginsCommand("plugins"));
    10. register("bukkit", new TimingsCommand("timings"));
    11. }
    12.  


    For the Vanilla ones you need to get this field:

    Code:java
    1.  
    2. protected static final Set<VanillaCommand> fallbackCommands = new HashSet<VanillaCommand>();
    3. static {
    4. fallbackCommands.add(new ListCommand());
    5. fallbackCommands.add(new OpCommand());
    6. fallbackCommands.add(new DeopCommand());
    7. fallbackCommands.add(new BanIpCommand());
    8. fallbackCommands.add(new PardonIpCommand());
    9. fallbackCommands.add(new BanCommand());
    10. fallbackCommands.add(new PardonCommand());
    11. fallbackCommands.add(new KickCommand());
    12. fallbackCommands.add(new TeleportCommand());
    13. fallbackCommands.add(new GiveCommand());
    14. fallbackCommands.add(new TimeCommand());
    15. fallbackCommands.add(new SayCommand());
    16. fallbackCommands.add(new WhitelistCommand());
    17. fallbackCommands.add(new TellCommand());
    18. fallbackCommands.add(new MeCommand());
    19. fallbackCommands.add(new KillCommand());
    20. fallbackCommands.add(new GameModeCommand());
    21. fallbackCommands.add(new HelpCommand());
    22. fallbackCommands.add(new ExpCommand());
    23. fallbackCommands.add(new ToggleDownfallCommand());
    24. fallbackCommands.add(new BanListCommand());
    25. fallbackCommands.add(new DefaultGameModeCommand());
    26. fallbackCommands.add(new SeedCommand());
    27. fallbackCommands.add(new DifficultyCommand());
    28. fallbackCommands.add(new WeatherCommand());
    29. fallbackCommands.add(new SpawnpointCommand());
    30. fallbackCommands.add(new ClearCommand());
    31. fallbackCommands.add(new GameRuleCommand());
    32. fallbackCommands.add(new EnchantCommand());
    33. }
    34.  


    Just read the code its all in there. If this is not what you meant you may explain it in other words.
     
  7. Offline

    avatarDr

  8. Offline

    camyono

    No everything is ok =)

    There is a Map with the known commands ( Your screenshot ) and there is a Set with the default MC Server commands.
    Thats why i told you to check the source code.

    Try this you will see:

    Code:java
    1.  
    2. public void showSimpleCommands() throws Exception{
    3.  
    4. SimplePluginManager simplePluginManager = (SimplePluginManager) this.getServer()
    5. .getPluginManager();
    6.  
    7. Field commandMapField = SimplePluginManager.class.getDeclaredField("commandMap");
    8. commandMapField.setAccessible(true);
    9.  
    10. SimpleCommandMap simpleCommandMap = (SimpleCommandMap) commandMapField.get(simplePluginManager);
    11.  
    12. System.out.println("------------------");
    13. System.out.println("Default Commands:");
    14.  
    15. for ( Command c : simpleCommandMap.getCommands() ) { // Only the few commands you see
    16.  
    17. System.out.println(c.getName());
    18.  
    19. }
    20.  
    21. System.out.println("------------------");
    22. System.out.println("Vanilla Commands:");
    23.  
    24. for ( Command c : simpleCommandMap.getFallbackCommands() ) { // Extended list with vanilla default commands
    25.  
    26. System.out.println(c);
    27.  
    28. }
    29.  
    30. System.out.println("------------------");
    31.  
    32. commandMapField.setAccessible(false);
    33.  
    34. }
    35.  
     
  9. Offline

    avatarDr

    Oh, dam. It's a shame I didn't see them. Thanks!
     
Thread Status:
Not open for further replies.

Share This Page