CommandExecutor difference

Discussion in 'Plugin Development' started by DoggyCode™, Apr 26, 2016.

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

    DoggyCode™

    What's the difference between

    public class Clazz implements CommandExecutor
    &
    public class Clazz2 extends VanillaCommand

    ?

    Can I use VanillaCommand if I want. What's the disadvantages? Do I register it the same way? (I believe VanillaCommand extends Command, which has some cool methods I've never seen)

    Code:
    HealCommand extends VanillaCommand{
    
      public HealCommand(){
        super("heal");
        this.description = "Heal yourself!";
        this.usageMessage = "/heal";
        this.setPermission("plugin.command.heal");
      }
    
      //add the methods
      @Override
      public boolean execute(CommandSender sender, String currentAlias, String[] args){
        if (!testPermission(sender)) return false;
        if ((args.length < 2)) {
          sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
          return false;
         }
         Player p = (Player)sender;
         p.setHealth(20);
         return true;
       }
    
    //then also make the TabComplete
    }
    EDIT: It is actually possible, VanillaCommand extends Command. We can put the class extending VanillaCommand into a CommandMap. I know that we can get the server's
    CommandMap through reflection:
    Code:
        public void onEnable(){
            try{
                if(Bukkit.getServer() instanceof CraftServer){
                    final Field f = CraftServer.class.getDeclaredField("commandMap");
                    f.setAccessible(true);
                    cmap = (CommandMap)f.get(Bukkit.getServer());
                }
            } catch (Exception e){
                e.printStackTrace();
            }
        }
        public CommandMap getCommandMap(){
            return cmap;
        }
    We should now be able to use

    Code:
    HealCommand heal = new HealCommand();
    heal.register(cmap);
     
    Last edited: Apr 26, 2016
  2. Offline

    mcdorli

    You can't, it's only there for bukkit to use. Btw, Clazz is a commandExecutor, Clazz 2 is a command, they aren't the same.
     
  3. Offline

    DoggyCode™

    So I can't make for example:

    Code:
    HealCommand extends VanillaCommand{
    
      public HealCommand(){
        super("heal");
        this.description = "Heal yourself!";
        this.usageMessage = "/heal";
        this.setPermission("plugin.command.heal");
      }
    
      //add the methods
      @Override
      public boolean execute(CommandSender sender, String currentAlias, String[] args){
        if (!testPermission(sender)) return false;
        if ((args.length < 2)) {
          sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
          return false;
         }
         Player p = (Player)sender;
         p.setHealth(20);
         return true;
       }
    
    //then also make the TabComplete
    }
    Then register this to a CommandMap which will work in game?
     
  4. Offline

    mcdorli

    Why would you do it?
     
  5. Offline

    DoggyCode™

    Only out of curiousity if I could make a working command using VanillaCommand

    EDIT: It is actually possible, VanillaCommand extends Command. We can put the class extending VanillaCommand into a CommandMap. I know that we can get the server's
    CommandMap through reflection:
    Code:
        public void onEnable(){
            try{
                if(Bukkit.getServer() instanceof CraftServer){
                    final Field f = CraftServer.class.getDeclaredField("commandMap");
                    f.setAccessible(true);
                    cmap = (CommandMap)f.get(Bukkit.getServer());
                }
            } catch (Exception e){
                e.printStackTrace();
            }
        }
        public CommandMap getCommandMap(){
            return cmap;
        }
    We should now be able to use

    Code:
    HealCommand heal = new HealCommand();
    heal.register(cmap);
     
    Last edited: Apr 26, 2016
  6. Offline

    Firestar311

    Even if you can, you shouldn't. It is not good practice. It is much easier to use the CommandExecutor class and then register it with Bukkit like
    Code:
    plugin.getCommand("whatever").setExecutor(new YourExecutor());
     
  7. Offline

    DoggyCode™

    I knooow. Jesus xD as I said, it was only out of curiosity. One plus with it is that you don't have to type up the details in the plugin.yml. you could probably make your own class which would make this process simpler, and which would require you to not register the command in the plugin.yml

    Sent from my SM-N9005 using Tapatalk
     
  8. Offline

    Konato_K

    @DoggyCode™ That's not really a plus, to be honest I find annoying using someone else's plugin that has no documentation, then your way to find things usually the plugin yml for the command and permissions, and when this are missing then welp..
     
    DoggyCode™ likes this.
  9. Offline

    DoggyCode™

    That's true. But my point was, you can create a command using Vanilla Command. What people think is negative about it isn't really my concern. Was really just curious.

    Sent from my SM-N9005 using Tapatalk
     
  10. Offline

    Konato_K

    @DoggyCode™ You can also not use returns in a method and throw exceptions instead, then know what the value is by the exception type or passing the exception message :D
     
  11. Offline

    teej107

  12. Offline

    DoggyCode™

    Was thinking of doing it with reflection, but again, I'm not gonna do this as it's not really worth it. I only had a question of if it was possible, and I got a no, then figured it it was possible. So conclusion is: it's possible and my question is answered ;)
     
  13. Offline

    Davesan

    I made it in some of my plugins. Needed that to make my addons (plugin expansions) able to register their own root commands. Aaaand it works pretty much like you described first. Through reflection you can get the command map and then add your command. Good plugins have documention, which is not the plugin.yml, but rather a page/pages on the project site here on bukkit, or on its own webpage.
    (However, I did not use that VanillaCommand class thingy, I simply extended Command or like that, never heard of that class be4.)
     
    DoggyCode™ likes this.
  14. Offline

    Konato_K

    @DoggyCode™ If the question is solved then please mark the thread as such
     
  15. Offline

    DoggyCode™

    Bukkit uses VanillaCommand for their default commands such as Gamemode, tp, etc...

    Sent from my SM-N9005 using Tapatalk
     
Thread Status:
Not open for further replies.

Share This Page