Solved What's the purpose of "return true/false"?

Discussion in 'Plugin Development' started by GlacialCreeper, May 5, 2014.

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

    GlacialCreeper

    Hello, sorry if I'm in the wrong forum section, but I just started learning Bukkit coding, and on two tutorials I'm learning with, at some point in the boolean onCommand(), one of them uses return true, and the other return false. They both seem to not have any effect on the plugin. So my questions are:
    1. What would be the advantage of using this (having it return true or false)?
    2. Other than if you'd use it for something, does it really matter whether its true or false?
    PS
    I do know that when you have a method-like boolean like this (below), it has to return true or false.
    Code:
    public boolean a(){
    ...
    }
     
  2. Offline

    Drkmaster83

    As far as I know, Bukkit implemented the 'boolean' return into the onCommand method as a way for plugin developer to let Bukkit know if the command was used properly and did not have syntax errors.

    However, I think that it's useless, as many people return either one and it was never fully implemented. I guess that when it was first implemented, people were using it incorrectly or were too confused, but I bet it's still there because Bukkit doesn't want to break so many plugins.

    Returning true and returning false in the onCommand() honestly should not alter the effects at all.
     
  3. Offline

    AoH_Ruthless

    Drkmaster83
    I actually like being able to check if the command is executed. Just because you think it's useless doesn't mean it is; to accommodate for what any person might want, no matter how rare, it was a helpful decision to have a boolean return value.

    With that said, I completely agree with you in saying that it isn't required to have a great plugin and most developers never actually utilize it.
     
  4. Offline

    Necrodoom

  5. Offline

    Rocoty

  6. Offline

    Necrodoom

    Its their choice to reinvent what's already there.
     
  7. Offline

    Bobit

    Seeing as if you register a commands usage, it will automatically tell the user the usage if the onCommand returns false, if you register a command's required permission, will it automatically cancel if the player doesn't have the permission?
    If that's the case, is this compatible with vault permissions?
     
  8. Offline

    1Rogue

    Why would you use vault for checking if a user has a permission? Just use Player#hasPermission
     
  9. Offline

    Bobit

    Yeah, I guess I knew the answer to that question.
    The first one I'm still curious about, but it doesn't particularly matter.
     
  10. Bobit I would assume so, since permission-message is "displayed if the sender does not have permission". If it didn't check, it wouldn't know to send.
     
    Bobit likes this.
  11. Offline

    Garris0n

    It decides whether to send a usage message (from the plugin.yml). It's rather useless if you want any sort of explanation as to why execution failed ("that player doesn't exist", etc).
     
  12. Offline

    Bobit

    Garris0n
    Well, you could do both.
    "Not enough parameters.
    usage: /killp <player> <reason>"
     
  13. Offline

    Garris0n

    Yeah but in most cases it's redundant.
     
  14. Bobit Garris0n GlacialCreeper Generally I use the usage as I think it's helpful if they can see how they're supposed to use it. My simple method for deciding whether to return true follows this simple process:

    - If the command was successful, return true.
    - If the command failed because of non-syntax related issues (e.g. they don't have permission), display an error message ("You don't have permission!") and return true. Obviously they know how to use the command, they just aren't allowed to, so the usage won't help them.
    - If the command failed because of syntax-related issues (e.g. they didn't specify enough arguments), display and error message ("Insufficient arguments!") and return false. They didn't specify enough arguments, so maybe they don't know how to use the command. The usage could provide a helpful reminder on how to do that.

    In my opinion, the boolean has a clear purpose. As Necrodoom suggested, it's up to the developer whether or not they choose to use it.
     
    AoH_Ruthless and desht like this.
  15. Offline

    Bobit

    Wait, how does it know which usage to return if it returns false?
    Because I'm pretty sure that the current way I'm using onCommand isn't normal...
     
  16. Bobit It returns the usage for whatever command they were using, what else would it?
     
    Bobit likes this.
  17. Offline

    GlacialCreeper


    This makes a lot of sense at first, but uh, it still wouldnt do anything other than return a boolean :/ And, I mean no disrespect, I still dont see how you would use it to your advantage.
     
  18. Offline

    Bobit

    GlacialCreeper
    If it returns false and you have the command's usage in the plugin.yml, it automatically tells the player:

    usage: /[command they typed] [variables of command]
     
  19. Offline

    SnipsRevival

    I always return true and print out specialized messages for each possible misuse of the command (non-existent player, too many/few arguments, command sender is not a player, etc). I just like the amount of flexibility you have by not returning false. Plus you are able to customize the colour of your usage message when you don't return false.
     
  20. GlacialCreeper Think of the boolean like asking the question "should I block the sending of the usage message?" Generally, if they make a mistake with how to use it, it would be a good idea to send them the usage message. If they don't make a mistake (e.g. if the command succeeds or they don't have permission) then the usage message won't be helpful, so it's a good idea to block it. Of course some prefer to just return true and then send their own messages, which is fine too.

    SnipsRevival By the way, did you know you actually can colour the usage messages, but from inside the plugin? :p

    PHP:
    getCommand("whatever").setUsage(ChatColor.GREEN "Hello!");
     
    Bobit likes this.
  21. Offline

    GlacialCreeper

    Ah, ok guys I finally get it now, lol. Thanks!
    And, uh, last question:
    Wouldn't this override the usage message in the plugin.yml or cause a conflict?

    Or would the usages have to be in the plugin or the plugin.yml for it to work (if it has to be just one of them)?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
    AdamQpzm likes this.
  22. Offline

    AoH_Ruthless

    AdamQpzm likes this.
  23. Offline

    GlacialCreeper

    Ok, thanks!
     
Thread Status:
Not open for further replies.

Share This Page