Problem/Bug MSG Question

Discussion in 'Plugin Help/Development/Requests' started by bubblefat_, Dec 29, 2014.

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

    bubblefat_

    Why isn't the args.length working? When I go ingame it doesn't work.

    [​IMG]

    Code:
            if(cmd.getName().equalsIgnoreCase("msg")) {
            
                Player target = Bukkit.getPlayer(args[0]);
    
                if(target != null) {
                    String message = "Hi dood! I don't have anything to say so... hi.";
    
           
                    for(int i = 1; i != args.length; i++)
                        message += args[1] + " ";
    
                    target.sendMessage(ChatColor.GOLD + "PM> " + ChatColor.RED + ChatColor.BOLD + sender.getName() + ChatColor.GRAY + ChatColor.BOLD + " >> " + ChatColor.RED + ChatColor.BOLD + target.getName() + "" + ChatColor.GRAY + ChatColor.BOLD + " " +message);
                    sender.sendMessage(ChatColor.GOLD + "PM> " + ChatColor.RED + ChatColor.BOLD + sender.getName() + ChatColor.GRAY + ChatColor.BOLD + " >> " + ChatColor.RED + ChatColor.BOLD + target.getName() + "" + ChatColor.GRAY + ChatColor.BOLD + " " + message);
                } else if(!(target != null)) {
    
                    sender.sendMessage(ChatColor.GOLD + "PM> " + ChatColor.GRAY + "Player not found.");
               
                } else if(args.length == 0) {
    
                    sender.sendMessage(ChatColor.GOLD + "PM> " + ChatColor.GRAY + "Wrong usage.");
                }
                return true;
            }
     
  2. Offline

    pie_flavor

    First of all, this looks ugly. Write ยง where you would normally put & in a standard color code. If you can't stomach that, write "import static org.bukkit.ChatColor.*" and jsut write GOLD or GRAY without ChatColor in front of it.
    Second, problem area.
    Code:java
    1. Player target = Bukkit.getPlayer(args[0]);

    If args[0] is nonexistent, this will throw an exception and break the whole process. It will never get to the elseif.
    Solution: Reorganize your conditionals. Put the last one first, before the variable definition. You should get in the habit of returning false if the user did something wrong, start by ending the if statement with that. This eliminates the need for elses. Then, put the definition, and then put another if statement. The next one can be else if you want.

    Parting note:
    Code:
    } else if (!(target != null)) {
    First of all, an if is not necessary. } else { will suffice, as the if statement holds the only alternative to the previous one. Second, just look at this.
    Code:
    (!(target != null))
    That is a double negative, clear as day and useless as a chocolate doorknob.
     
    17xDillz1997 likes this.
Thread Status:
Not open for further replies.

Share This Page