Why am I being shown the "usage:" if the command is successful?

Discussion in 'Plugin Development' started by Appljuze, Nov 6, 2012.

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

    Appljuze

    Here's my code so far:
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("heal") || commandLabel.equalsIgnoreCase("h")){
                if(args.length == 0){
                    // /heal = 0 args  /heal bench3 = 1 args
                    player.setHealth(20);
                    player.sendMessage(ChatColor.BLUE + "You have been healed!");
                }else if(args.length == 1){
                    if  (player.getServer().getPlayer(args[0]) != null){
                    Player targetPlayer = player.getServer().getPlayer(args[0]);
                    targetPlayer.setHealth(20);
                    targetPlayer.sendMessage(ChatColor.RED + "You have been healed by " + player.getDisplayName());
                }else{
                    player.sendMessage(ChatColor.RED + "Player not online!");
                   
                }
                }
            }
            if(commandLabel.equalsIgnoreCase("helloserver")){
                player.sendMessage("Hello" + player.getDisplayName());
        }
            return false;
        }
    }
    
    Whenever I execute the commands, it shows the usage as well as the message for completing the command. I think this has something to do with the return, but isn't return:false supposed to only show the usage if the command wasn't correct?
     
  2. Offline

    gomeow

    Where ever it should have worked, put in return true;
    Otherwise it will reach the end and still return false
     
  3. Offline

    ZeusAllMighty11

    If player is null, I return false btw
     
  4. Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("heal") || commandLabel.equalsIgnoreCase("h")){
                if(args.length == 0){
                    // /heal = 0 args  /heal bench3 = 1 args
                    player.setHealth(20);
                    player.sendMessage(ChatColor.BLUE + "You have been healed!");
    return true;
                }else if(args.length == 1){
                    if  (player.getServer().getPlayer(args[0]) != null){
                    Player targetPlayer = player.getServer().getPlayer(args[0]);
                    targetPlayer.setHealth(20);
                    targetPlayer.sendMessage(ChatColor.RED + "You have been healed by " + player.getDisplayName());
    return true;
                }else{
                    player.sendMessage(ChatColor.RED + "Player not online!");
                   return false;
                }
                }
            }
            if(commandLabel.equalsIgnoreCase("helloserver")){
                player.sendMessage("Hello" + player.getDisplayName());
        }
            return false;
        }
    }
    
    Hope this helps. as said above, Return true where the command should complete so after you have been healed, and target healed, but then return false on faliure, that way it will show the command help.

    Damo.
     
  5. Offline

    Appljuze

    This return: false and return:true is something I cannot get a grasp on! Why would it be return:true for /heal and /heal <player> but not for /helloserver?
     
  6. Offline

    hutattedonmyarm

    You'd also want to return true for /helloserver since there's nothing wrong ;)
    I always return true, only if the user made a mistake I catch that and return false
     
  7. Offline

    Appljuze

    Okay I think here is where I'm getting confused. You said "You'd want to return true for /helloserver since there's nothing wrong"... well why would you compile something that was wrong? Wouldn't you always want to return:true then?
     
  8. Offline

    hutattedonmyarm

    I'm trying to say, that a player can't use this command wrong, and no errors can appear..
     
  9. Offline

    Appljuze

    So if you put return:false, it would leave the possibility open for them to type the command wrong? But if you leave it return:true, it will count the command as right regardless of whether it's "right" or not? Forgive my nubish-ness, just starting out with Java.
     
  10. Offline

    hutattedonmyarm

    Kind of...
    return false means, the user has made an error (forgot an argument, etc) and return true means that his syntax is right.
    So if argument 0 is a material, and the users types DRT instead of DIRT I'd send an error-message (Material not found) and return true, since the syntax was right.
    If the argument 0 is missing I return false.
    And because /helloserver can't be used wrongly (I'd just ignore arguments...) I'd always return true, because there's nothing the user could do wrong, besides misspelling the command (But in this case Bukkit wouldn't even call your plugin)
     
  11. Offline

    Sagacious_Zed Bukkit Docs

    In java methods can have a return type. The return type of the onCommand is a boolean, which the method can return a true or a false.

    Bukkit calls your method in response to the correct command.
    If your method logic returns true, then bukkit will take no further action. If your method logic returns false, bukkit will display the usage to the sender.
     
  12. Offline

    Appljuze

    Okay I think I'm starting to understand it now. Could either of you explain a case in which you would want to use return:false?
     
  13. Offline

    Hoolean

    You would use return false to make the command show the usage. Also be aware that after a return is called, no code after the statement is called, so it can be used for terminating methods prematurely.
     
  14. Offline

    hutattedonmyarm

    If you want to write a message-command like /msg you'd probably use a syntax like /msg <player> <message>
    Now, if args.length < 2 There's (at least) one argument missing, so you'd return false
     
  15. Offline

    zack6849

    For example.
    If the player was offline, or if they didnt specify a player, return false.
    However, if they DID specify a player and the player wasn't null, execute your normal code, then return true.
    Basically, return false any time the user did something wrong.
     
  16. Offline

    Appljuze

    Okay I understand that, but why did my command for /heal <player> show the usage even when I entered it correctly?
     
  17. Offline

    hutattedonmyarm

    Because you don't return true anywhere, so your code gets executed to the very bottom where it says return false.
    It's your job to determine whether to return true or false and do it
     
    zack6849 likes this.
Thread Status:
Not open for further replies.

Share This Page