args[0] outOfBoundsException

Discussion in 'Spigot Plugin Development' started by DecisionsYT, Jun 10, 2016.

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

    In my report feature, the basic layout is this. /Report (Player) (Reason)

    When I do /Report, hoping for the usage message, I get this error: http://pastebin.com/y8HnEigq

    However, when I do the full (expected) command with the player and reason, it works just fine.

    CODE:

    Code (Text):
    package Reports;

    import java.io.IOException;
    import java.util.List;

    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;

    import me.DecisionsYT.Main;

    public class Reports implements Listener, CommandExecutor {
       
    Main plugin;
       
        public Reports(Main passedPlugin) {
            this.plugin = passedPlugin;
        }

       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String string, String[] args) {

            Player player = (Player) sender;
           
           
            if (args.length < 0) {
                if (player.hasPermission("ChatX.Report")) {
                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Reports-Usage")));
                }
            } else {
                StringBuilder str = new StringBuilder();
                  for (int i = 1; i < args.length; i++) {
                    str.append(args[i] + " ");
                  }
                 
                 
                  sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Reported-Player").replace("{target}", args[0]).replace("{reason}", str)));
                 
                  String report = args[0] + " reported by " + sender.getName() + ". Reason: " + str;
                 
                  List<String> reports = plugin.getReportsConfig().getStringList("Reports");
                  reports.add(report);
                  plugin.getReportsConfig().set("Reports", reports);
                 
                  try {
                    plugin.getReportsConfig().save(plugin.getReportsFile());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                 
           
            }
           
            if (!player.hasPermission("ChatX.Report")) {
                player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("No-Perm-Message")));
            }

            return true;
           
            }
        }
    LINE 41:

    Code (Text):
    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Reported-Player").replace("{target}", args[0]).replace("{reason}", str)));
     
  2. if (args.length < 0) {
    should be
    if (args.length ==0) {
     
  3. Return after args check
     
  4. Thanks worked
     
  5. Fixed anyways, but returning isn't required besides the end of the onCommand method. But returning on each bit is good practice I guess
     
Thread Status:
Not open for further replies.