When I create a target, whenever I run a command that doesn't even involve a target, i get an error.

Discussion in 'Plugin Development' started by football70500, Nov 2, 2014.

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

    football70500

    [​IMG]
    It was fine until I created commands that involve a target. target = Bukkit.getServer().getPlayer(args[1]);, getPlayer is crossed out and I dont know what to use instead of it.
    Code:java
    1. public class ModList extends JavaPlugin implements Listener{
    2.  
    3. public ArrayList<String> admins = new ArrayList<String>();
    4. public ArrayList<String> mods = new ArrayList<String>();
    5. public ArrayList<String> adminsperm = new ArrayList<String>();
    6. public ArrayList<String> modsperm = new ArrayList<String>();
    7. @Override
    8. public void onEnable(){
    9. getLogger().info("ModList has been enabled!");
    10. getServer().getPluginManager().registerEvents(this, this);
    11.  
    12. }
    13.  
    14. @Override
    15. public void onDisable(){
    16. getLogger().info("ModList has been disabled!");
    17. }
    18.  
    19. @EventHandler
    20. public void onJoin(PlayerJoinEvent e){
    21. if(e.getPlayer().hasPermission("admins.add")){
    22. admins.add(e.getPlayer().getName());
    23. Bukkit.broadcastMessage(ChatColor.GREEN + "A Staff member has joined the game!");
    24. }
    25. if(e.getPlayer().hasPermission("mods.add")){
    26. mods.add(e.getPlayer().getName());
    27. Bukkit.broadcastMessage(ChatColor.GREEN + "A Staff member has joined the game!");
    28. }
    29. /*if(e.getPlayer().hasPermission("permlist.admins.add")){
    30.   adminsperm.add(e.getPlayer().getName());
    31.   }
    32.   if(e.getPlayer().hasPermission("permlist.mods.add")){
    33.   modsperm.add(e.getPlayer().getName());
    34.   }
    35.   */
    36. }
    37.  
    38. @EventHandler
    39. public void onLeave(PlayerQuitEvent e){
    40. if(admins.contains(e.getPlayer().getName())){
    41. admins.remove(e.getPlayer().getName());
    42. Bukkit.broadcastMessage(ChatColor.RED + "A Staff member has left the game!");
    43. }
    44. if(mods.contains(e.getPlayer().getName())){
    45. mods.remove(e.getPlayer().getName());
    46. Bukkit.broadcastMessage(ChatColor.RED + "A Staff member has left the game!");
    47. }
    48. }
    49.  
    50. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    51. Player target = null;
    52. if (cmd.getName().equalsIgnoreCase("staff")) {
    53. target = Bukkit.getServer().getPlayer(args[1]);
    54. //List<String> admins = new ArrayList<>();
    55. // List<String> mods = new ArrayList<>();
    56. /*for (Player player : getServer().getOnlinePlayers()) {
    57.   String name = player.getName();
    58.   if (admins.contains(name)) {
    59.   admins.add(name);
    60.   }else if(mods.contains(name)){
    61.   mods.add(name);
    62.   }
    63.   }
    64.   */
    65. if (admins.size() < 0 && mods.size() < 0) {
    66. sender.sendMessage(ChatColor.YELLOW + "No staff is currently online!");
    67. } else {
    68. sender.sendMessage(ChatColor.AQUA + "Admins Online:");
    69. for (String adminsName : admins) {
    70. if(admins.size() < 0){
    71. sender.sendMessage(ChatColor.YELLOW + "No admins are currently online.");
    72. }else if (admins.size() > 0){
    73. sender.sendMessage(ChatColor.AQUA + "• " + ChatColor.YELLOW + adminsName );
    74. }
    75. }
    76. sender.sendMessage(ChatColor.AQUA + "Mods Online:");
    77. for(String modsName : mods){
    78. if(mods.size() < 0){
    79. sender.sendMessage(ChatColor.YELLOW + "No mods are currently online.");
    80. }else if (mods.size() > 0){
    81. sender.sendMessage(ChatColor.AQUA + "• " + ChatColor.YELLOW + modsName );
    82. }
    83. }
    84. }
    85.  
    86. }
    87. if(cmd.getName().equalsIgnoreCase("listadmin")){
    88. if(sender.hasPermission("set.admin")){
    89. target = Bukkit.getServer().getPlayer(args[1]);
    90. if(args.length == 1){
    91. if(sender instanceof Player){
    92. if(target != null){
    93. if(!target.isOnline()){
    94. sender.sendMessage(ChatColor.RED + "Player must be online to add them to the staff list!");
    95. }else if(target.isOnline()){
    96. adminsperm.add(target.getName());
    97. sender.sendMessage(ChatColor.GREEN + target.getName() + " has been added to the Admin list!");
    98. target.sendMessage(ChatColor.YELLOW + "You have been added to the Admin list!");
    99. }
    100. }
    101. }
    102. }
    103. }
    104. if(cmd.getName().equalsIgnoreCase("listmod")){
    105. if(sender.hasPermission("set.mod")){
    106. target = Bukkit.getServer().getPlayer(args[1]);
    107. if(args.length == 1){
    108. if(sender instanceof Player){
    109. if(target != null){
    110. if(!target.isOnline()){
    111. sender.sendMessage(ChatColor.RED + "Player must be online to add them to the staff list!");
    112. }else if(target.isOnline()){
    113. modsperm.add(target.getName());
    114. sender.sendMessage(ChatColor.GREEN + target.getName() + " has been added to the Mod list!");
    115. target.sendMessage(ChatColor.YELLOW + "You have been added to the Mod list!");
    116. }
    117. }
    118. }
    119. }
    120. }
    121. if(cmd.getName().equalsIgnoreCase("stafflist")){
    122. if (adminsperm.size() < 0 && modsperm.size() < 0) {
    123. sender.sendMessage(ChatColor.YELLOW + "There is no staff for this server!");
    124. } else {
    125. sender.sendMessage(ChatColor.AQUA + "Admins:");
    126. for (String adminsName : adminsperm) {
    127. if(adminsperm.size() < 0){
    128. sender.sendMessage(ChatColor.YELLOW + "There are no admins for this server!");
    129. }else if (adminsperm.size() > 0){
    130. sender.sendMessage(ChatColor.AQUA + "• " + ChatColor.YELLOW + adminsName );
    131. }
    132. }
    133. sender.sendMessage(ChatColor.AQUA + "Mods:");
    134. for(String modsName : modsperm){
    135. if(modsperm.size() < 0){
    136. sender.sendMessage(ChatColor.YELLOW + "There are no mods for this server!");
    137. }else if (modsperm.size() > 0){
    138. sender.sendMessage(ChatColor.AQUA + "• " + ChatColor.YELLOW + modsName );
    139. }
    140. }
    141. }
    142. }
    143. }
    144. }
    145. return false;
    146. }
    147. }
     
  2. Crossed out means deprecated. Bukkit.getPlayer(String name) is just deprecated to raise awareness about 1.8 and the UUID updates, and it wants you to use Bukkit.getPlayer(UUID uuid). You can still use Bukkit.getPlayer(String name) fine though.
     
  3. WeeziMonkey
    That has nothing to do with this.

    football70500
    You are getting the error because you are trying to access an element in the array that doesn't exist. You need to first check if the args.length is equal to something. If args.length is 1. then only 1 argument was used in the command. This should give you the basic idea.

    Edit: Your error is pointing to this line
    Code:java
    1. for (String adminsName : admins) {

    Are you sure you gave us the correct code?
     
  4. Offline

    davidp027

    Code:
    try{
         target = Bukkit.getServer().getPlayer(args[1]);
    }catch(Exception ex){
         sender.sendMessage("Invalid target");
    }
     
  5. Offline

    Watto

    You should check the admins.size before iterating through the array
     
  6. Offline

    fireblast709

    Watto Iterations do not require you to check the size
    davidp027 What... no. Don't catch Exception, like, ever. (ok there are plugins that like to throw Exception for some reason, then it's required for your code to even compile, but they shouldn't be throwing Exceptions in the first place)
     
    Hawktasard likes this.
  7. Offline

    Watto

    fireblast709

    I know but there is literally no point of checking the size inside the iterator, its best to check outside before even running the iterator.
     
  8. Offline

    rbrick

    Check the arguments length before getting the player.....
     
  9. Offline

    NonameSL

    No. Just no. All of the comments above me are WRONG.
    Your exception is an ArrayIndexOutOfBoundsException caused by the index 1.
    What does that mean? It means that there is no such index as index 1 in args - meaning there are no 2 arguments, there are one ([0] = first, [1] = second).

    If you're trying to capture the first index, you are'nt doing it correctly - what you want is [0] and not [1]. If you're trying to capture the second one - then you wrote the command with no second argument.

    Just check the size of the arguments before you use it, and check if the target is null (if the target doesn't exist.)
     
    Skionz likes this.
  10. Offline

    fireblast709

  11. fireblast709 Of course he has! Who would make a broad (and obviously incorrect) statement such as 'all the answers are above me are incorrect' if they hadn't read the posts! How else would they know? Also, not like NonameSL sounds arrogant or anything there...
     
Thread Status:
Not open for further replies.

Share This Page