Tutorial Arguments! How To Put Spaces When Developing Commands In Eclipse

Discussion in 'Resources' started by HDTVSteve, Jul 2, 2015.

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

    HDTVSteve

    Hey!
    Recently I have been learning how to make Arguments when making commands, so I bring you the thread that helps you learn how to make arguments in commands. Special Thanks To @Ruptur!

    Arguments
    Arguments are important if you are trying to make a command such as '/plugin help' with normal commands, it would register as '/plugin' or not even register. This is very simple to learn and is easy to work with. (For the people just starting bukkit developing)

    This code:
    Code:
    if (args.length == 0) { // if they did'nt have any arguments in the command
                sender.sendMessage(ChatColor.RED + "Invalid Arguments! Unkown Command 'type /plugin help'");
    is an argument, if you have a command '/plugin help' and they run '/plugin' it becomes an Invalid/Unkown Command, like you would do with single commands.

    Now the proper argument command would be like this
    Code:
    if (args.length == 0) { // if they did'nt have any arguments in the command
                sender.sendMessage(ChatColor.RED + "Invalid Arguments! Unkown Command 'type /plugin help'");
                return true;
                }
                //now we know the argument size is not 0 (can't be -1 or lower either)
                String subcommand = args[0];
                    if (subcommand.equalsIgnoreCase("help")) {
                        // They typed '/plugin help'
                        // Notice! we did'nt check if the arguments was exactly help. they could have typed /plugin help now please
    Now the code looks invalid this is because we dont have "plugin" registered!

    Registering Arguments
    Like we did with arguments we need to register '/plugin' in order for the argument to work! In order to do this we need to make a 'CommandExectuor' so we would have to do something a little like this,
    Code:
        @Override
        public void onEnable() {        
        this.getCommand("plugin").setExecutor(executor);
          
    then add the CommandExecutor field, (you also have to import CommandExecutor)
    Code:
        private CommandExecutor executor;
       
        @Override
        public void onEnable() {
        this.getCommand("plugin").setExecutor(executor);
    getting the hang of it? So the command '/plugin' is now registered and transfers to the line;
    Code:
    if (args.length == 0) { // if they did'nt have any arguments in the command
                sender.sendMessage(ChatColor.RED + "Invalid Arguments! Unkown Command 'type /plugin help'");
                return true;
                }
    So when they run '/plugin' it says to the sender "Invalid Arguments! Unkown Command 'type /plugin help'".

    Plugin.yml (***VERY IMPORTANT***)
    The 'plugin.yml' is very, VERY important! to set it up you need to make it exactly like this;

    Code:
    commands:
       plugin:
          description: Displays a message
          usage: /<command>
    You need to have the beginning of what you named the; this.getCommand("plugin").setExecutor(executor); you cannot have like this;

    Code:
    commands:
       plugin help:
          description: Displays a message
          usage: /<command>
    it will give you an error in the console log!

    Another example:

    this.getCommand("test").setExecutor(executor);
    // subcommand is going to be '/test plugin' so plugin

    pluign.yml
    Code:
    commands:
       test:
          description: A Test command
          usage: /<command>
    get it?

    Final Code
    Your final code should look a little something like this;
    Also run it in your server! Usage: /plugin help, or /plugin
    Code:
    package (PACKAGE NAME);
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Arguments extends JavaPlugin implements Listener {
    
        private CommandExecutor executor;
       
        @Override
        public void onEnable() {
            this.getCommand("plugin").setExecutor(executor);
        }
       
        @Override
        public void onDisable() {
        }
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (args.length == 0) { // if they did'nt have any arguments in the command
                sender.sendMessage(ChatColor.RED + "Invalid Arguments! Unkown Command 'type /plugin help'");
                return true;
                }
                //now we know the argument size is not 0 (can't be -1 or lower either)
                String subcommand = args[0];
                    if (subcommand.equalsIgnoreCase("help")) {
                        // They typed '/plugin help'
                        // Notice! we did'nt check if the arguments was exactly help. they could have typed /plugin help now please
                        sender.sendMesaage("It worked! Thanks for looking at the thread, give it a like!"); {
                }
    }
            returnfalse;
    
              }
     }
    
    If this helped give it a like! Have any questions put them down below! Learn Java @http://docs.oracle.com/javase/tutorial/java/index.html
     
    ChristolisTV and Ruptur like this.
  2. Offline

    Ruptur

  3. Offline

    teej107

    What if I'm not using Eclipse?

    Nice tutorial btw.
     
  4. Offline

    Konato_K

    @teej107 You can't because only Eclipse allows arguments :( :( :(
     
  5. Offline

    HDTVSteve

    @Ruptur thanks!

    @teej107 what do you use? & thx
     
    Last edited by a moderator: Jul 3, 2015
  6. Uh...
     
    teej107 likes this.
  7. Offline

    Konato_K

  8. Offline

    teej107

    IntelliJ, but that doesn't matter. Code should be able to run no matter what you use whether its an IDE or not.

    Ok. I was getting scared to the point that I was hoping it was a joke xD
     
    MrBlackIsBack and Konato_K like this.
  9. @HDTVSteve Okay so there are a few problems with this:

    1) As @teej107 pointed out, this tutorial doesn't really have anything to do with Eclipse. Not sure of the point in narrowing the focus on it, not everybody uses Eclipse

    2) You don't need to set the executor when your onCommand() method is in the main class

    3) There's not really a need to override the onDisable() method if you're not going to do anything with it

    4) It's better for overriden methods (such as onCommand) to have the @Override annotation to prevent a mistake such as a typo.

    5) The formatting for the onCommand() in the final code snippet is broken, and it makes it a little hard to read.

    6) When it "works" you don't return anything, so the command usage will display to the sender, when really it shouldn't do under those circumstances

    And most important two points:

    7) Knowing how to use a String array should really be something that is learned in basic Java (heck, it's the same as the main(String[] args) method) so I'm not sure exactly who this is going to help.

    8) Will throw an NPE in the onEnable().
     
  10. Offline

    Funnygatt

    There are a couple of errors.

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (args.length == 0) { // if they did'nt have any arguments in the command
                sender.sendMessage(ChatColor.RED + "Invalid Arguments! Unkown Command 'type /plugin help'");
                return true;
                }
                //now we know the argument size is not 0 (can't be -1 or lower either)
                String subcommand = args[0];
                    if (subcommand.equalsIgnoreCase("help")) {
                        // They typed '/plugin help'
                        // Notice! we did'nt check if the arguments was exactly help. they could have typed /plugin help now please
                        sender.sendMesaage("It worked! Thanks for looking at the thread, give it a like!"); {
                }
    }
            returnfalse; // Should be return false;
              }
    }
    
    All of that is formatted badly (Not sure if it's the forums' fault, but still).

    1) returnfalse;? Should be return false;
    2) Too many closing brackets

    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (args.length == 0) {
                sender.sendMessage(ChatColor.RED + "Invalid Arguments! Unkown Command 'type /plugin help'");
                return true;
            }
            //now we know the argument size is not 0 (can't be -1 or lower either)
            String subcommand = args[0];
            if (subcommand.equalsIgnoreCase("help")) {
                sender.sendMesaage("It worked! Thanks for looking at the thread, give it a like!");
                return true;
            }
            else{
                return false;
            }
         } 
    }
    
     
  11. @Funnygatt I think you miscounted/were confused by the format/forgot the class closing brace - there are 7 opening braces and 7 closing braces in the final code :) Your point about the format is still valid however, and should be fixed by @HDTVSteve as well as the other issues I outlined above :)
     
    Funnygatt and ChipDev like this.
  12. This is more of a how to check how long an array is and what is in it. I would say this is more of a Java tutorial than a Bukkit tutorial. This can all be learnt in the Java tutorials and should be known before doing Bukkit. As for this, I will be locking this thread.
     
    ChipDev likes this.
Thread Status:
Not open for further replies.

Share This Page