Plugin Help How to stop this error?

Discussion in 'Plugin Help/Development/Requests' started by football70500, Apr 17, 2015.

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

    football70500

    So, I want to make it so that instead of it throwing an internal exception, when you type /report, it just shows "/Report Name Message" I thought i had a fix for it but it turns out, I don't.
    Code:
    [20:54:45] [Server thread/INFO]: Cavasi issued server command: /report
    [20:54:45] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'report' in plugin kReport v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot1649.jar:git-Spigot-1649]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:181) ~[spigot1649.jar:git-Spigot-1649]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:767) ~[spigot1649.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerConnection.java:1043) [spigot1649.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:880) [spigot1649.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java:28) [spigot1649.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat.java:65) [spigot1649.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:186) [spigot1649.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:81) [spigot1649.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.Minecr
    Code:java
    1.  
    2. if (cmd.getName().equalsIgnoreCase("report")) {
    3. if (args.length != 0) {
    4. if (args.length > 1) {
    5. if (target != null) {
    6. if (sender.hasPermission("report.report")) {
    7. if (target != sender) {
    8. sender.sendMessage(ChatColor.GREEN
    9. + "Your report has been filed! Thank you.");
    10. for (Player online : Bukkit.getOnlinePlayers()) {
    11. if (online.hasPermission("hr.getmessage")) {
    12. online.sendMessage(ChatColor.BLUE
    13. + "[Report] "
    14. + ChatColor.YELLOW
    15. + sender2.getName()
    16. + ChatColor.RED
    17. + " has reported "
    18. + ChatColor.YELLOW
    19. + target.getName()
    20. + ChatColor.RED + " for "
    21. + ChatColor.GREEN + message);
    22. return true;
    23. }
    24. }
    25. } else {
    26. sender.sendMessage(ChatColor.RED + "You can't report yourself!");
    27. }
    28. } else {
    29. sender.sendMessage(ChatColor.RED
    30. + "You do not have permission to use /report!");
    31. }
    32. } else {
    33. sender.sendMessage(ChatColor.YELLOW
    34. + target.getDisplayName() + " is not online.");
    35.  
    36. return true;
    37. }
    38. } else {
    39. sender.sendMessage(ChatColor.RED + "/Report <Name> Message");
    40. return true;
    41.  
    42. }
    43. } else {
    44. sender.sendMessage(ChatColor.RED + "/Report <Name> <Message>");
    45. }
    46. }
    47.  


    For some reason this post kept getting deleted :((
     
  2. Offline

    ForsakenRealmz

    Correct me if I'm wrong, but shouldn't Line 3 be changed up?

    What you're doing is checking if the arguments are NOT equal to 0. How many arguments does '/report' have?
     
  3. Offline

    football70500

    As many as you want it to, It works as long as you /report <anonlineplayer> i think they might be hacking. "I think they might be hacking" appears.
     
  4. Offline

    ForsakenRealmz

    @football70500 Ok, so /report is not considered an argument. But /report <AnOnlinePlayer> has 1 argument. See what I'm saying?
     
  5. Offline

    football70500

    Yes, if you just do /Report <AnOnlinePlayer>, you get the message "/Report <name> <message>". I want this same message to be returned when you type just /report. How can I do so?
     
  6. Offline

    nverdier

    @football70500
    1) Set the usage in the plugin.yml to be "/Report <name> <message>"
    2) If the args.length is < 2, return false.
     
  7. Offline

    ForsakenRealmz

    Lol I was trying your way of not "spoon" feeding them!

    You could do what he said, or change your line 3 to '==' instead of '!='
     
  8. Offline

    nverdier

    I didn't spoonfeed them. Spoonfeeding is generally directly giving code.
     
    Last edited by a moderator: Apr 17, 2015
    eyamaz likes this.
  9. Offline

    football70500

    Code:java
    1.  
    2. if (cmd.getName().equalsIgnoreCase("report")) {
    3. if (args.length == 0) {
    4. sender.sendMessage(ChatColor.RED + "/Report <Name> <Message>");
    5. } else {
    6. if (args.length > 1) {
    7. if (target != null) {
    8. if (sender.hasPermission("report.report")) {
    9. if (target != sender) {
    10. sender.sendMessage(ChatColor.GREEN
    11. + "Your report has been filed! Thank you.");
    12. for (Player online : Bukkit.getOnlinePlayers()) {
    13. if (online.hasPermission("hr.getmessage")) {
    14. online.sendMessage(ChatColor.BLUE
    15. + "[Report] "
    16. + ChatColor.YELLOW
    17. + sender2.getName()
    18. + ChatColor.RED
    19. + " has reported "
    20. + ChatColor.YELLOW
    21. + target.getName()
    22. + ChatColor.RED + " for "
    23. + ChatColor.GREEN + message);
    24. return true;
    25. }
    26. }
    27. } else {
    28. sender.sendMessage(ChatColor.RED + "You can't report yourself!");
    29. }
    30. } else {
    31. sender.sendMessage(ChatColor.RED
    32. + "You do not have permission to use /report!");
    33. }
    34. } else {
    35. sender.sendMessage(ChatColor.YELLOW
    36. + target.getDisplayName() + " is not online.");
    37.  
    38. return true;
    39. }
    40. } else {
    41. sender.sendMessage(ChatColor.RED + "/Report <Name> Message");
    42. return true;
    43.  
    44. }
    45. }
    46. }
    47.  
    an error gets thrown when i do that

    @nverdier still get the error:

    Code:java
    1.  
    2. if (cmd.getName().equalsIgnoreCase("report")) {
    3. if (args.length < 2) {
    4. return false;
    5. } else {
    6. if (args.length > 1) {
    7. if (target != null) {
    8. if (sender.hasPermission("report.report")) {
    9. if (target != sender) {
    10. sender.sendMessage(ChatColor.GREEN
    11. + "Your report has been filed! Thank you.");
    12. for (Player online : Bukkit.getOnlinePlayers()) {
    13. if (online.hasPermission("hr.getmessage")) {
    14. online.sendMessage(ChatColor.BLUE
    15. + "[Report] "
    16. + ChatColor.YELLOW
    17. + sender2.getName()
    18. + ChatColor.RED
    19. + " has reported "
    20. + ChatColor.YELLOW
    21. + target.getName()
    22. + ChatColor.RED + " for "
    23. + ChatColor.GREEN + message);
    24. return true;
    25. }
    26. }
    27. } else {
    28. sender.sendMessage(ChatColor.RED + "You can't report yourself!");
    29. }
    30. } else {
    31. sender.sendMessage(ChatColor.RED
    32. + "You do not have permission to use /report!");
    33. }
    34. } else {
    35. sender.sendMessage(ChatColor.YELLOW
    36. + target.getDisplayName() + " is not online.");
    37.  
    38. return true;
    39. }
    40. } else {
    41. sender.sendMessage(ChatColor.RED + "/Report <Name> Message");
    42. return true;
    43.  
    44. }
    45. }
    46. }
    47.  


    Code:
    6:54:20 PM [SEVERE] null
    6:54:20 PM org.bukkit.command.CommandException: Unhandled exception executing command 'report' in plugin kReport v1.0
    6:54:20 PM     at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot1649.jar:git-Spigot-1649]
    6:54:20 PM     at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:181) ~[spigot1649.jar:git-Spigot-1649]
    6:54:20 PM     at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:767) ~[spigot1649.jar:git-Spigot-1649]
    6:54:20 PM     at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerConnection.java:1043) [spigot1649.jar:git-Spigot-1649]
    6:54:20 PM     at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:880) [spigot1649.jar:git-Spigot-1649]
    6:54:20 PM     at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java:28) [spigot1649.jar:git-Spigot-1649]
    6:54:20 PM     at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat.java:65) [spigot1649.jar:git-Spigot-1649]
    6:54:20 PM     at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:186) [spigot1649.jar:git-Spigot-1649]
    6:54:20 PM     at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:81) [spigot1649.jar:git-Spigot-1649]
    6:54:20 PM     at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:734) [spigot1649.jar:git-Spigot-1649]
    6:54:20 PM     at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289) [spigot1649.jar:git-Spigot-1649]
    6:54:20 PM     at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) [spigot1649.jar:git-Spigot-1649]
    6:54:20 PM     at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) [spigot1649.jar:git-Spigot-1649]
    6:54:20 PM     at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [spigot1649.jar:git-Spigot-1649]
    6:54:20 PM Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
    6:54:20 PM     at me.cheezburger.hackerreport.HackerReport.onCommand(HackerReport.java:25) ~[?:?]
    6:54:20 PM     at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot1649.jar:git-Spigot-1649]
    6:54:20 PM     ... 13 more
    
    
    Code:
      report:
        description: Freezes a player.
        usage: /Report <Name> <Message>
    
    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Apr 19, 2015
  10. Offline

    ForsakenRealmz

    @football70500
    Here is what I would do. Remove the args.length > 1 if statement. Because with the if statement before that you're already going to send them the exact same message.

    The way args.length works is like this... If args.length == 0 then you could send a message saying "You did not enter a player!" If args.length == 1 then you could send a message saying "You did not send a message!" Args.length == 0 would mean that the player only typed in /report. What could you do to make it better, now that you know what args.length is going to return?
     
    Last edited: Apr 18, 2015
  11. Offline

    football70500

    I just want the internal error to stop being thrown when you just type /report and I tried fixing it but nothing works.
     
  12. Offline

    ForsakenRealmz

    @football70500 Here is what I have done with my plugin, that is much like yours:
    Code:
    if( args.length < 2 )
    //Not enough arguments
    //Send the user a message saying /report [Player] [Message]
    else
    //Plugin magic here
     
    Last edited: Apr 18, 2015
  13. Offline

    football70500

    And this will make it so that the server doesnt freak and throw an error when you type /Report only?
     
  14. Offline

    ForsakenRealmz

    Yes, (args.length < 2) means this...
    Since args is an array as defined in your method header (onCommand) when you use .length it gets how many elements are in the array. If the user just types '/report', and nothing after then there are 0 arguments. If the user types '/report [PlayerName]' then there is 1 argument. If the user types '/report [PlayerName] [Message]' then there will be 2. So, if the args length is less than 2, it will send the user a message with the correct usage (as long as you define it).
    *Edit Make it (args.length <= 2) if you want the plugin to tell the player correct usage if they only enter 1 word for the message. It will stop 1 word reports.
     
    Last edited: Apr 19, 2015
  15. Offline

    Boomer

    target isn't defined prior to this test section
    the first if with target sees if it is not null, and if not null. do all that stuff.
    The ELSE condition (where target is null, which it will be if its not defined beforehand), takes that Null target object, and then tries to use target.getDisplayname() ... but target WILL be null when it tries to execute this.

    Just something to be aware of when the index error gets resolved..
     
  16. Offline

    football70500

    It still throws the error when you solely type /report
     
  17. Offline

    Boomer

    You're going to have to post more of your code, because realistically, the error message generated about index out of bounds is not an error that is generatable from the code you have shown. An index out of bounds errors occurs when dereferencing an array element that doesn't exist. Arrays are 0-index based, you're likely trying to dereference something like target = args[1] because thats your 'first' argument, but argument 1 doesn't exist, because its args[0]
    Because your code does not show any where where you define TARGET yet, you try to use it, so taht likely means you have tried to define it before checking how many arguments exist. So you may have tried to dereference the arg[] array when there are no elements in the array, or are assigning target to args[1] earlier, where none exists, prior to this if code that is not generating your error.
     
  18. Offline

    football70500

    SAVIOR :D THANK YOU SO DAMN MUCH!

    @Boomer now, how can I make it so that if a target is NOT online and you try to report them, an error doesn't get thrown?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  19. Offline

    ForsakenRealmz

    if( target != null )
     
Thread Status:
Not open for further replies.

Share This Page