Code help

Discussion in 'Spigot Plugin Help' started by Euromancy, Jun 16, 2015.

  1. JamesJ

    Supporter

    Code (Text):


             if (args.length == 0) {
                return false;
             }
             if(args.length == 1) {
                 sender.sendMessage(ChatColor.GREEN + "Use /report <player> <reason>");
             }
             if(args.length >= 2) {
                 String targetName = args[0];
                 Player player = (Player) sender;
                 Player target = Bukkit.getPlayer(targetName);
                 if (target == null) {
                     player.sendMessage(targetName + " is offline!");
                 }
                 StringBuilder sb = new StringBuilder();
                 for (int i = 1; i < args.length; i++) {
                     sb.append(args[i]);
                     if (i < args.length) {
                         sb.append(" ");
                     }
                 }
                 player.sendMessage("Player " + targetName + ChatColor.RED + " reported for " + sb.toString());
             }
     
    Can easily be fixed.

    You're checking if the argument length is 0, that's fine. If it is, you return, so all code below the 'return false' wont be called, and you've also instructed the player how to use the command (from the plugin.yml)

    You then check if the argument length is 1, which is useless. If you use
    Code (Text):

    if(args.length >= 2){
        //DO REPORT STUFF HERE
    } else {
        //Tell the player how to use the correct syntax
    }
     
    You're killing 2 birds with one stone (you're removing 2 checks and adding them to an existing check).
     
    #2 JamesJ, Jun 16, 2015
    Last edited: Jun 16, 2015
  2. JamesJ

    Supporter

    You haven't done what I said to do -.-
     
  3. wat to do then XD
    I'm a noob, and thank you for helping <3
     
  4. Kk so I added a returnment statement below args == o
    return false;
    and I also added a returnment statement below the other code, the one under it.
     
  5. Alright I don't know if I will be much of help but I still try
    Code (Text):
      if (cmd.getName().equalsIgnoreCase("report")) {
             if (!sender.hasPermission("test.test")) {
                 sender.sendMessage(ChatColor.RED + "No permissions.");
             }
    Alright that looks good
    Hmm. I think your error is in here (I am not a Java master either so don't expect me to be 100% correct)
    Code (Text):
     if (args.length == 0) {
                     return false;
             }
             if(args.length == 2) {
                     return false;
             }
             else
             {
                 sender.sendMessage(ChatColor.GREEN + "Use /report <player> <reason>");
         
     
    If you are going to be doing
    Code (Text):
      if (args.length == 0) {
                     return false;
             }
             if(args.length == 2) {
                     return false;
             }
     
    You might as well be doing
    Code (Text):

    if (args.length < 2) {
                    sender.sendMessage(ChatColor.GREEN+ "Use /report<player> <reason>");
                    return true;
                }
     
    Also you never closed your else statement ( The "sender.sendMessage(ChatColor.GREEN + "Use /report <player> <reason>");")