Solved Argument Troubles

Discussion in 'Plugin Development' started by LandonTheGeek, Nov 4, 2013.

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

    LandonTheGeek

    Hello, I recently ran my broadcast command on my server, and when I run it with just the command, it adds in my prefix but nothing else. Can someone help me with placing arguments into my code? Thanks. ~Beast

    Code:
      public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
      {
        int i;
        if (commandLabel.equalsIgnoreCase("ebc")) {
            if ((sender.hasPermission("easybroadcast.broadcast")) || ((sender instanceof ConsoleCommandSender))) {
          StringBuilder sb = new StringBuilder();
          for (i = 0; i < args.length; i++) {
            sb.append(args[i]).append(" ");
          }
          if (sender.hasPermission("easybroadcast.broadcast"))
            Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("ManualPrefix")) + ChatColor.translateAlternateColorCodes('&', getConfig().getString("ManualMessageColor")) + sb.toString());
          else {
            sender.sendMessage(ChatColor.LIGHT_PURPLE + "You don't have permission!");
                  }
            }
        }
     
  2. Offline

    GusGold

    HelpfulBeast
    I can't see a problem in your StringBuilder, but I do see one with your permission checks.
    Formated Code (open)
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    2. int i;
    3. if (commandLabel.equalsIgnoreCase("ebc")) {
    4. if ((sender.hasPermission("easybroadcast.broadcast")) || ((sender instanceof ConsoleCommandSender))) {
    5. StringBuilder sb = new StringBuilder();
    6. for (i = 0; i < args.length; i++) {
    7. sb.append(args).append(" ");
    8. }
    9. if (sender.hasPermission("easybroadcast.broadcast"))
    10. Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("ManualPrefix")) + ChatColor.translateAlternateColorCodes('&', getConfig().getString("ManualMessageColor")) + sb.toString());
    11. else {
    12. sender.sendMessage(ChatColor.LIGHT_PURPLE + "You don't have permission!");
    13. }
    14. }
    15. }
    16. }
    On line 4, you check if they have permission, which is all fine and dandy, but then, within that if, you again check if they have permission, but don't check if they are a ConsoleCommandSender. You should change your method like so:
    New Code (open)
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    2. int i;
    3. if (commandLabel.equalsIgnoreCase("ebc")) {
    4. if ((sender.hasPermission("easybroadcast.broadcast")) || ((sender instanceof ConsoleCommandSender))) {
    5. StringBuilder sb = new StringBuilder();
    6. for (i = 0; i < args.length; i++) {
    7. sb.append(args).append(" ");
    8. }
    9. Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("ManualPrefix")) + ChatColor.translateAlternateColorCodes('&', getConfig().getString("ManualMessageColor")) + sb.toString());
    10. } else {
    11. sender.sendMessage(ChatColor.LIGHT_PURPLE + "You don't have permission!");
    12. }
    13. }
    14. }
    Also, note that you can setup for loops like:
    Code:java
    1. for (int i = 0; i < aNumber; i++){}
     
  3. Offline

    LandonTheGeek

    GusGold
    Thanks. :) For some reason, I am getting a error on the last bracket in the statement. Here is the fixed:

    Code:
      public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            int i;
            if (commandLabel.equalsIgnoreCase("ebc")) {
                if ((sender.hasPermission("easybroadcast.broadcast")) || ((sender instanceof ConsoleCommandSender))) {
                    StringBuilder sb = new StringBuilder();
                    for (i = 0; i < args.length; i++) {
                        sb.append(args).append(" ");
                    }
                    Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("ManualPrefix")) + ChatColor.translateAlternateColorCodes('&', getConfig().getString("ManualMessageColor")) + sb.toString());
                } else {
                    sender.sendMessage(ChatColor.LIGHT_PURPLE + "You don't have permission!");
                }
            }
        } /* This bracket here. The error is "Syntax error on token "}", { expected after this token" */
    
     
  4. Offline

    GusGold

    HelpfulBeast
    You need an extra '}' to close your onCommand method
     
  5. Offline

    LandonTheGeek

    GusGold
    I added an extra one, and the error is as shown above still.
     
  6. Offline

    GusGold

  7. Offline

    LandonTheGeek

  8. Offline

    DailyLove

    HelpfulBeast Don't add an extra one

    return true; or return false at the end of the boolean
     
  9. Offline

    GusGold

    HelpfulBeast
    Ahh, okay, So where it says return false; remove the } preceeding it. When i said to add it earlier, it didn't have the return false after.

    HelpfulBeast
    You will also want to return true; after you do your Bukkit.broadcastMessage(); and after "You don't have permission"

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  10. Offline

    LandonTheGeek

    GusGold DailyLove
    Alright cool. So I run my command, and here is what I get:
    [​IMG]

    It seems like my string is messed up. Here is the new and updated code. :):

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    2. int i;
    3. if (commandLabel.equalsIgnoreCase("ebc")) {
    4. if ((sender.hasPermission("easybroadcast.broadcast")) || ((sender instanceof ConsoleCommandSender))) {
    5. StringBuilder sb = new StringBuilder();
    6. for (i = 0; i < args.length; i++) {
    7. sb.append(args).append(" ");
    8. }
    9. Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("ManualPrefix")) + ChatColor.translateAlternateColorCodes('&', getConfig().getString("ManualMessageColor")) + sb.toString());
    10. return true;
    11. } else {
    12. sender.sendMessage(ChatColor.LIGHT_PURPLE + "You don't have permission!");
    13. return true;
    14. }
    15. }
    16. return false;
    17. }
    18.  
     
  11. Offline

    GusGold

    HelpfulBeast
    Yeah, that's giving you the object details, not the content.
    If you didn't want to use StringBuilder, you could just replace sb.append() with:
    Code:java
    1. broadcastMessage += args[ i];
    but don't forget to initiate it before the for loop with
    Code:java
    1. String broadcastMessage ="";
     
  12. Offline

    LandonTheGeek

    GusGold
    Alright, so the String broadcastMessage =""; is giving me a yellow line under "broadcastMessage", and yes I added the "broadcastMessage += args;" thing. Shall I just add SurpressWarnings?

    Thanks for your continuous help!
     
  13. Offline

    GusGold

    HelpfulBeast
    I assume the warning is that the variable is unused? Make sure you replaced the sb.toString() with broadcastMessage when you broadcast the message to the server
     
  14. Offline

    LandonTheGeek

    GusGold
    I added it, still yellow: String broadcastMessage. Here is the full method. I may need someone else's opinion on this, but it looks like it is fine to me. :p

    Code:Java
    1.  
    Code:Java
    1.  
    Code:Java
    1.  
    2. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    3. int i;
    4. if (commandLabel.equalsIgnoreCase("ebc")) {
    5. if ((sender.hasPermission("easybroadcast.broadcast")) || ((sender instanceof ConsoleCommandSender))) {
    6. StringBuilder sb = new StringBuilder();
    7. String broadcastMessage ="";
    8. for (i = 0; i < args.length; i++) {
    9. broadcastMessage += args[ i];
    10. }
    11. Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("ManualPrefix")) + ChatColor.translateAlternateColorCodes('&', getConfig().getString("ManualMessageColor")) + sb.toString());
    12. return true;
    13. } else {
    14. sender.sendMessage(ChatColor.LIGHT_PURPLE + "You don't have permission!");
    15. return true;
    16. }
    17. }
    18. return false;
    19. }
    20. [COLOR=#ffcc00][COLOR=#000000][/COLOR][/COLOR]
     
  15. Offline

    GusGold

    HelpfulBeast
    Alright,
    Old (open)
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, StringcommandLabel, String[] args){
    2. int i;
    3. if (commandLabel.equalsIgnoreCase("ebc")) {
    4. if ((sender.hasPermission("easybroadcast.broadcast")) || ((sender instanceof ConsoleCommandSender))) {
    5. StringBuilder sb = new StringBuilder();
    6. String broadcastMessage ="";
    7. for (i = 0; i < args.length; i++) {
    8. broadcastMessage += args[i];
    9. }
    10. Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("ManualPrefix")) + ChatColor.translateAlternateColorCodes('&', getConfig().getString("ManualMessageColor")) + sb.toString());
    11. return true;
    12. } else {
    13. sender.sendMessage(ChatColor.LIGHT_PURPLE + "You don't have permission!");
    14. return true;
    15. }
    16. }
    17. return false;
    18. }[/i]
    New (open)
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, StringcommandLabel, String[] args){
    2. int i;
    3. if (commandLabel.equalsIgnoreCase("ebc")) {
    4. if ((sender.hasPermission("easybroadcast.broadcast")) || ((sender instanceof ConsoleCommandSender))) {
    5.  
    6. String broadcastMessage ="";
    7. for (i = 0; i < args.length; i++) {
    8. broadcastMessage += args[i];
    9. }
    10. Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("ManualPrefix")) + ChatColor.translateAlternateColorCodes('&', getConfig().getString("ManualMessageColor")) + broadcastMessage);
    11. return true;
    12. } else {
    13. sender.sendMessage(ChatColor.LIGHT_PURPLE + "You don't have permission!");
    14. return true;
    15. }
    16. }
    17. return false;
    18. }[/i]
    So on line 5, I removed the sb as you don't need that anymore, and at the end of line 10, I replaced 'sb.toString()' with 'broadcastMessage'.
    Have a go at that :)


    HelpfulBeast
    Also, just ignore the [ /i] at the end of the lines, it's because the format parser thinks that our args[ i]; means we want italics... -_-

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  16. Offline

    LandonTheGeek

    GusGold
    Oh my god you are godlike. :) One last thing. Can we set up some arguments to where if the sender enters nothing after the command, it returns a message saying that you need something after the command? (Hope that made sense. :p) Here is the updating file. I did some editing to my likings.

    Code:Java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    2. {
    3. if (commandLabel.equalsIgnoreCase("ebc")) {
    4. if ((sender.hasPermission("easybroadcast.broadcast")) || ((sender instanceof ConsoleCommandSender))) {
    5.  
    6. int i;
    7. String broadcastMessage ="";
    8. for (i = 0; i < args.length; i++) {
    9. broadcastMessage += args[i];
    10.  
    11. }
    12. Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("ManualPrefix")) + ChatColor.translateAlternateColorCodes('&', getConfig().getString("ManualMessageColor")) + broadcastMessage);
    13. return true;
    14. } else
    15. {
    16. sender.sendMessage(ChatColor.LIGHT_PURPLE + "You don't have permission!");
    17. return true;
    18. }
    19. }
    20. return false;
    21. }[/i]


    GusGold

    And no problem. :) I caught that.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  17. Offline

    GusGold

    ;)
    Easy peasy!
    Right after checking for permissions
    Code:java
    1. if ((sender.hasPermission("easybroadcast.broadcast")) || ((sender instanceof ConsoleCommandSender))) {
    Check if the length of args = 0:
    Code:java
    1. if (args.length == 0){
    2. sender.sendMessage("Y U no message!");
    3. return true;
    4. }

    EDIT: Fixed derp
     
  18. Offline

    LandonTheGeek

    GusGold
    Cool! So for some reason, it didn't like the "length()" idea so I changed it to "length". Here is the method:

    Code:Java
    1. if(args.length == 0){
    2. sender.sendMessage(ChatColor.RED + "Command Usage: /ebc YourBroadcastHere");
    3. return true;
    4. }


    Now let me do testing....... Results: Much love sir! :D Works perfectly! I will use everything for future reference!

    Now just.... One more question. xD If for say on a command I wanted to add an args length that runs off of a certain keyword as to: "/test word1" "/test word2". How infact, would this be done? Thanks again! ~Beast
     
  19. Offline

    GusGold

    Woops, yeah, length is a property, not a method :oops:

    Also, I don't quite get what you mean 'args length of a certain keyword'?
     
  20. Offline

    LandonTheGeek

    GusGold

    Like say.... Like... Hm.. Here is an example:

    "/fly on"
    sender.setFly (enabled) for instance.

    "/fly off"
    sender.setFly (disabled)

    Each one does their own thing. Hopefully I am not confusing you too much.
     
  21. Offline

    GusGold

    HelpfulBeast
    Ahh gotcha :)
    For your /fly on|off example, you would have your onCommand() and you would check that your cmd.equalIgnoreCase("fly). This checks that the cmd was indeed '/fly'. Then you check that the sender has supplied at least one argument: if (args.length > 0)
    if it is, then you check if args[0].equalsIgnoreCase("on")
    if so, toggle fly mode to be on for them
    then if args[0].equalsIgnoreCase("off")
    and toggle fly off.

    HelpfulBeast
    What your actually code could look like:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args){
    2. if (cmd.equalsIgnoreCase("fly"){
    3. if(args.length > 0){
    4. if(args[0].equalsIgnoreCase("on"){
    5. if(sender instanceof Player)
    6. sender.setFlying(true);
    7. else
    8. sender.sendMessage("Need to be a player to fly");
    9. } else if(args[0].equalsIgnoreCase("off"){
    10. if(sender instanceof Player)
    11. sender.setFlying(false);
    12. else
    13. sender.sendMessage("Need to be a player to fly");
    14. }
    15. return true;
    16. } else {
    17. sender.sendMessage("You need to supply an argument");
    18. }
    19. }
    20. return false;
    21. }
    22.  
    23. EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
    HelpfulBeast likes this.
  22. Offline

    LandonTheGeek

    GusGold
    Alright cool. I am setting that up in a different class just to test. Here is an error I am getting. I will explain it after the picture.

    [​IMG]
    It tells me to add a ";" instead of the brackets. What seems to be the deal here? That doesn't make sense. Haha. Sorry

    @GusGold
    WHOA! Jesus. I didn't need the fly part of it. xD Was just an example. Much love to you. I will use the args examples as information for further use. :)

    As of right now, SOLVED.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  23. Offline

    GusGold

    Yeah, you need an if surrounding your args[0].equalsIgnoreCase("on")
     
    HelpfulBeast likes this.
  24. Offline

    LandonTheGeek

    GusGold
    Cool, also Gus, I decided to add permissions into that method. Is the else{} part of my permissions right with the statement? Should it work as that?

    Edit: Unproper formatting. Better:

    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
        {
            if(commandLabel.equalsIgnoreCase("fly")){
                if(sender.hasPermission("permission.permission") || sender instanceof ConsoleCommandSender){
                if(args.length > 0){
                    if(args[0].equalsIgnoreCase("on")){
                        // Actions Here
                    } else if(args[0].equalsIgnoreCase("off")){
                        // Actions Here
                    } else{
                        sender.sendMessage("Supply an argument!");
                    }
                } else{
                    sender.sendMessage("You don't have permission.");
                }
            }
        }
            return false;
        }
     
  25. Offline

    GusGold

    :) You're welcome!

    Your else needs to be one '}' level lower. It's currently 'elsing' the if(args.length >0)

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

    LandonTheGeek

    GusGold
    LOL! :p Thanks to you! That is probably all I am asking for now. :)

    If it weren't for you, I would have made almost zero progress today. If I need anything in a while, I will just Tahg. Sorry for being a pain in the butt at times. You have helped greatly.
     
    GusGold likes this.
  27. Offline

    GusGold

    HelpfulBeast likes this.
  28. Offline

    LandonTheGeek

    GusGold
    Okay you know what. Back to bothering.... Sorry :(

    I was doing some testing on the command we were working on /ebc. When I try to enter spaces in between my words... It joins them together... My oh my.

    [​IMG]

    Alright... My code:

    Code:Not Again
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    2. {
    3. if (commandLabel.equalsIgnoreCase("ebc")) {
    4. if ((sender.hasPermission("easybroadcast.broadcast")) || ((sender instanceof ConsoleCommandSender))) {
    5.  
    6. if(args.length == 0){
    7. sender.sendMessage(ChatColor.RED + "Command Usage: /ebc Broadcastmessagehere");
    8. return true;
    9. }
    10.  
    11. int i;
    12. String broadcastMessage ="";
    13. for (i = 0; i < args.length; i++) {
    14. broadcastMessage += args[i];
    15.  
    16. }
    17. Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("ManualPrefix")) + ChatColor.translateAlternateColorCodes('&', getConfig().getString("ManualMessageColor")) + broadcastMessage);
    18. return true;
    19. } else
    20. {
    21. sender.sendMessage(ChatColor.LIGHT_PURPLE + "You don't have permission!");
    22. return true;
    23. }
    24. }
    25. return false;
    26. }[/i]


    GusGold
    If you don't mind taking a look at that issue for me that would be great.

    But I have to go to sleep, school tomorrow. Lol. After school, I will answer your response (if any) and work from there. :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  29. Offline

    JPG2000

    HelpfulBeast may I suggest something?

    I suggest you google, watch or maybe read a little about if operators and booleans. It would really help, and you would be getting this stuff like a breeze!
     
  30. Offline

    GusGold

    HelpfulBeast
    Change
    Code:java
    1. broadcastMessage += args[i];[/i]
    to
    Code:java
    1. broadcastMessage += args[i] + " ";[/i]
    Ignore those damn '[ /i]' again
     
Thread Status:
Not open for further replies.

Share This Page