Solved ArrayIndexOutOfBounds Error on a line of code

Discussion in 'Plugin Development' started by PimpDuck, Sep 8, 2013.

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

    PimpDuck

    Okay, for some reason I'm getting an ArrayIndexOutOfBounds error on line 70? I don't know why!!!! Help?
    Code:java
    1. package me.PimpDuck.DCNear;
    2.  
    3.  
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Location;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Entity;
    10. import org.bukkit.entity.Player;
    11.  
    12. public class CommandClass implements CommandExecutor {
    13.  
    14. private Main plugin;
    15.  
    16. public CommandClass(Main instance)
    17. {
    18. plugin = instance;
    19. }
    20.  
    21. @Override
    22. public boolean onCommand(CommandSender sender, Command cmd, String command, String[] args) {
    23. if(cmd.getName().equalsIgnoreCase("near")){
    24. Player player = (Player) sender;
    25. String prefix = plugin.getConfig().getString("chat-prefix");
    26. prefix = ChatColor.translateAlternateColorCodes('&', prefix);
    27.  
    28. if ((sender instanceof Player)) {
    29. if (args.length > 0)
    30. {
    31. if (args[0].equalsIgnoreCase("help")) {
    32. player.sendMessage(ChatColor.RED + "=============" + ChatColor.BLUE + "[ " + ChatColor.AQUA + "Diamcraft Near" + ChatColor.BLUE + " ]" + ChatColor.RED + "=============");
    33. player.sendMessage(ChatColor.BLACK + "* " + ChatColor.GOLD + "(/near) shows who's near you.");
    34. player.sendMessage(ChatColor.BLACK + "* " + ChatColor.GOLD + "(/near help) shows you this page of course. :p");
    35. player.sendMessage(ChatColor.RED + "------------------" + ChatColor.GOLD + "Ranks" + ChatColor.RED + "------------------");
    36. player.sendMessage(ChatColor.BLACK + "* " + ChatColor.GOLD + "GOD ranks distance is set to " + plugin.getConfig().getInt("distance.god") + " blocks.");
    37. player.sendMessage(ChatColor.BLACK + "* " + ChatColor.GOLD + "UB3R ranks distance is set to " + plugin.getConfig().getInt("distance.ub3r") + " blocks.");
    38. player.sendMessage(ChatColor.BLACK + "* " + ChatColor.GOLD + "Legend ranks distance is set to " + plugin.getConfig().getInt("distance.legend") + " blocks.");
    39. player.sendMessage(ChatColor.BLACK + "* " + ChatColor.GOLD + "Super ranks distance is set to " + plugin.getConfig().getInt("distance.super") + " blocks.");
    40. player.sendMessage(ChatColor.RED + "=========================================");
    41. return false;
    42. }
    43. if(args[0].equalsIgnoreCase("reload")){
    44. if(player.hasPermission("dcnear.reload") || player.hasPermission("dcnear.*")){
    45. plugin.reloadConfig();
    46. player.sendMessage(prefix + " " + ChatColor.GOLD + "Configuration Reloaded!");
    47. return false;
    48. }else{
    49. player.sendMessage(prefix + " " + ChatColor.RED + "You don't have permission to do this!");
    50. return true;
    51. }
    52. }
    53. }
    54. double range = 0;
    55. if (Main.playerDelayed(player))
    56. {
    57. if(Main.getRemainingTime(player) < 1)
    58. {
    59. Main.removeDelayedPlayer(player);
    60. }else
    61. {
    62. int remaining = Main.getRemainingTime(player);
    63. String minutes = remaining == 1 ? " minute" : " minutes";
    64.  
    65. player.sendMessage(prefix + " " + ChatColor.GOLD + ("You must wait " + remaining + minutes + " before using /near again!"));
    66. return false;
    67. }
    68. }
    69.  
    70. if(args.length > 1 && !args[0].equalsIgnoreCase("help") || !args[0].equalsIgnoreCase("reload") || !player.hasPermission("dcnear.*")){
    71. player.sendMessage(prefix + " " + ChatColor.DARK_RED + "Too many arguments!");
    72. return false;
    73. }
    74.  
    75. if (player.hasPermission("dcnear.god") || player.hasPermission("dcnear.ub3r") || player.hasPermission("dcnear.legend") || player.hasPermission("dcnear.super") || player.hasPermission("dcnear.*")) {
    76. if (args.length == 1 && player.hasPermission("dcnear.*")) {
    77. try {
    78. range = Double.parseDouble(args[0]);
    79. } catch (NumberFormatException nfe) {
    80. player.sendMessage(prefix + " " + ChatColor.RED + "Argument must be a number!");
    81. return false;
    82. }
    83. }
    84. if (args.length == 0 && player.hasPermission("dcnear.god")) {
    85. range = plugin.getConfig().getDouble("distance.god");
    86. Main.addDelayedPlayer(player);
    87. }
    88. if(args.length == 0 && player.hasPermission("dcnear.ub3r")){
    89. range = plugin.getConfig().getDouble("distance.ub3r");
    90. Main.addDelayedPlayer(player);
    91. }
    92. if(args.length == 0 && player.hasPermission("dcnear.legend")){
    93. range = plugin.getConfig().getDouble("distance.legend");
    94. Main.addDelayedPlayer(player);
    95. }
    96. if(args.length == 0 && player.hasPermission("dcnear.super")){
    97. range = plugin.getConfig().getDouble("distance.super");
    98. Main.addDelayedPlayer(player);
    99. }
    100. if(args.length == 0 && player.hasPermission("dcnear.*")){
    101. player.sendMessage(prefix + " " + ChatColor.GOLD + "Try /near [Number]");
    102. Main.removeDelayedPlayer(player);
    103. return true;
    104. }
    105. if (range != 0) {
    106. Location start_loc = player.getLocation();
    107. StringBuilder sb = new StringBuilder();
    108. for (Entity nearbyEntity : player.getNearbyEntities(range, range, range)) {
    109. if (nearbyEntity instanceof Player) {
    110. Location end_loc = nearbyEntity.getLocation();
    111. int distance = (int) start_loc.distance(end_loc);
    112.  
    113. if(distance <= range){
    114. sb.append(((Player) nearbyEntity).getName()).append(" (").append(ChatColor.DARK_RED).append(distance).append("m").append(ChatColor.WHITE).append("), ");
    115. }
    116. }
    117. }
    118. String message;
    119. if (sb.length() > 0) {
    120. message = sb.toString().substring(0, (sb.length() - 2));
    121. } else {
    122. message = "none";
    123. }
    124. player.sendMessage(ChatColor.GOLD + "Players nearby: " + ChatColor.WHITE + message);
    125. }
    126. } else {
    127. player.sendMessage(prefix + " " + ChatColor.RED + "You are not a high enough rank to do this! Do /near help for more information.");
    128. return true;
    129. }
    130. } else {
    131. player.sendMessage(prefix + " " + ChatColor.DARK_RED + "Only in game players can use this command!");
    132. return false;
    133. }
    134. return true;
    135. }
    136. return false;
    137. }
    138. }
     
  2. Offline

    Squid_Boss

    Is your permission set in your plugin.yml ?
     
  3. Offline

    PimpDuck

    I'm pretty sure :) Squid_Boss
    Code:
    name: DCNear
    main: me.PimpDuck.DCNear.Main
    version: 1.0
    commands:
      near:
        description: Gets nearby players
    permissions:
      dcnear.god:
        default: op
        description: Allow players to use the /dcnear command for GOD's distance
      dcnear.ub3r:
        default: op
        description: Allow players to use the /dcnear command for Ub3r's distance
      dcnear.legend:
        default: op
        description: Allow players to use the /dcnear command for Legend's distance
      dcnear.super:
        default: op
        description: Allow players to use the /dcnear command for Super's distance
      dcnear.reload:
        default: op
        description: Allow players to reload the config
      dcnear.*:
        default: op
        description: Allow players to use the /dcnear [distance] command
        children:
          dcnear.god: true
          dcnear.ub3r: true
          dcnear.legend: true
          dcnear.super: true
          dcnear.reload: true
     
  4. Offline

    Squid_Boss

    Oh, the issue:
    Code:java
    1. if(args.length > 1 && !args[0].equalsIgnoreCase("help") || !args[0].equalsIgnoreCase("reload") || !player.hasPermission("dcnear.*"))

    You're checking if the arguments is bigger than 1, but you're checking the for args[0] ....at most, it should be args[1]
     
  5. Offline

    PimpDuck

    Squid_Boss I did this and I'm still getting
    Code:
    19:30:07 [INFO] PimpDuck issued server command: /near
    19:30:07 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'near
    ' in plugin DCNear v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    9)
            at org.bukkit.craftbukkit.v1_6_R2.CraftServer.dispatchCommand(CraftServe
    r.java:523)
            at net.minecraft.server.v1_6_R2.PlayerConnection.handleCommand(PlayerCon
    nection.java:964)
            at net.minecraft.server.v1_6_R2.PlayerConnection.chat(PlayerConnection.j
    ava:882)
            at net.minecraft.server.v1_6_R2.PlayerConnection.a(PlayerConnection.java
    :839)
            at net.minecraft.server.v1_6_R2.Packet3Chat.handle(SourceFile:49)
            at net.minecraft.server.v1_6_R2.NetworkManager.b(NetworkManager.java:296
    )
            at net.minecraft.server.v1_6_R2.PlayerConnection.e(PlayerConnection.java
    :118)
            at net.minecraft.server.v1_6_R2.ServerConnection.b(SourceFile:37)
            at net.minecraft.server.v1_6_R2.DedicatedServerConnection.b(SourceFile:3
    0)
            at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:5
    90)
            at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:2
    26)
            at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:4
    86)
            at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java
    :419)
            at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:5
    82)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
            at me.PimpDuck.DCNear.CommandClass.onCommand(CommandClass.java:70)
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
            ... 15 more
    Anyone?? :/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  6. PimpDuck
    Try making it check if it's greater than Zero for the if statement. Or, change it to greaterthan-or-equal-to:

    Code:java
    1. if( args.length >= 1 && /*other code.*/)
     
    PimpDuck likes this.
  7. Offline

    PimpDuck

    Thanks man, it works great :) Mersenne Twister
     
Thread Status:
Not open for further replies.

Share This Page