Internal Error

Discussion in 'Plugin Development' started by Glass_Eater84, Jun 15, 2014.

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

    Glass_Eater84

    Hello,
    I was wondering why I get an internal error whenever someone does /slap (just /slap) it works with a player name but not just /slap.
    here is my code.

    Code:java
    1. public class Main extends JavaPlugin implements Listener {
    2.  
    3. public String name = ChatColor.RED + "[" + ChatColor.GREEN + "FunCommands" + ChatColor.RED + "]" + ChatColor.WHITE;
    4.  
    5. @SuppressWarnings("deprecation")
    6. @Override
    7. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    8. Player player = (Player) sender;
    9. Player p = Bukkit.getServer().getPlayer(args[0]);
    10.  
    11.  
    12.  
    13. if (label.equalsIgnoreCase("slap")) {
    14. if (args.length == 0) {
    15. sender.sendMessage(ChatColor.RED + "Usage: /slap <player>");
    16. return true;
    17. } else if (args.length == 1) {
    18. Player v = Bukkit.getPlayer(args[0]);
    19. if (v != null) {
    20. v.damage(1);
    21. Bukkit.broadcastMessage(ChatColor.GREEN + p.getName() + ChatColor.RED + " slapped " + ChatColor.GREEN + v.getName() + ".");
    22. } else {
    23. sender.sendMessage(ChatColor.RED + "Player not found: " + args[0] + ".");
    24. return true;
    25. }
    26. } else {
    27. sender.sendMessage(ChatColor.RED + "Unknown argument, or maybe allow slapping with extra damage here?");
    28. return true;
    29. }
    30. }
    31. return false;
    32.  
    33. }
    34.  
    35.  
    36.  
    37. public static int clamp(float value, int min, int max) {
    38. return Math.min(Math.max(Math.round(value), min), max);
    39. }
    40. }


    Thank you much in advance,
    Glass
     
  2. Offline

    Onlineids

  3. Offline

    TheEntropy

    THe getPlayer function is returning null most-likely. If the player doesn't exist or none was entered, the .getPlayer() function will return null. Under:
    Code:
    Player p = Bukkit.getServer().getPlayer(args[0]);
    
    You should put something like:

    Code:
    if (p == null) {
        sender.sendMessage("The specified player couldn't be found");
        return true;
    }
    
     
  4. Offline

    Glass_Eater84

    TheEntropy
    Still getting the internal error.
    Here is updated code.
    Code:java
    1. package me.glasseater.funcommands;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class Main extends JavaPlugin implements Listener {
    12.  
    13. public String name = ChatColor.RED + "[" + ChatColor.GREEN + "FunCommands" + ChatColor.RED + "]" + ChatColor.WHITE;
    14.  
    15. @SuppressWarnings("deprecation")
    16. @Override
    17. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    18. Player player = (Player) sender;
    19. Player p = Bukkit.getServer().getPlayer(args[0]);
    20. if (p == null) {
    21. sender.sendMessage("The specified player couldn't be found");
    22. return true;
    23. }
    24.  
    25.  
    26. if (label.equalsIgnoreCase("slap")) {
    27. if (args.length == 0) {
    28. sender.sendMessage(ChatColor.RED + "Usage: /slap <player>");
    29. return true;
    30. } else if (args.length == 1) {
    31. Player v = Bukkit.getPlayer(args[0]);
    32. if (v != null) {
    33. v.damage(1);
    34. Bukkit.broadcastMessage(ChatColor.GREEN + p.getName() + ChatColor.RED + " slapped " + ChatColor.GREEN + v.getName() + ".");
    35. } else {
    36. sender.sendMessage(ChatColor.RED + "Player not found: " + args[0] + ".");
    37. return true;
    38. }
    39. } else {
    40. sender.sendMessage(ChatColor.RED + "Unknown argument, or maybe allow slapping with extra damage here?");
    41. return true;
    42. }
    43. }
    44. return false;
    45.  
    46. }
    47.  
    48.  
    49.  
    50. public static int clamp(float value, int min, int max) {
    51. return Math.min(Math.max(Math.round(value), min), max);
    52. }
    53. }
     
  5. Offline

    AoH_Ruthless

  6. Offline

    TheEntropy

    Glass_Eater84 Quickly scanning through the code, it looks like there is an un-needed "}" on line 38, try removing it. If you could also copy and paste the error that prints out in the console and paste it in code tags, that would help me greatly.
     
  7. Offline

    Glass_Eater84

    TheEntropy AoH_Ruthless Onlineids
    Code:
    21:58:47 INFO]: glasseater84 issued server command: /slap
    [21:58:47 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'slap' in plugin FunCommands v0.1
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:199) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:544) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerConnection.java:932) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:814) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat.java:47) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
        at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
        at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
        at me.glasseater.funcommands.Main.onCommand(Main.java:19) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
    Also, TheEntropy It didn't work if I remove that it just gives me one huge error in eclipse.
     
  8. Offline

    unrealdesign


    You didn't check the size of args[]. It could technically be null, so before any code, you always have to check

    Code:java
    1. if(args.length > 0)
     
  9. Offline

    1Rogue

    You're assuming that arguments are supplied. If none are, the length of the array will be 0 and you will not be able to retrieve any values from it.


    The array wouldn't be null, just empty.
     
  10. Offline

    Onlineids

    Glass_Eater84 Add this if(args.length == 0){
    Not Enough Args
    }
    Add that to top
     
  11. Offline

    unrealdesign

    Explained in 3 different ways. I hope he gets it lol

    edit: I stand corrected - 4 ways now
     
  12. Offline

    TheEntropy

    I apologize, I am very tired at the moment.. It says ArrayIndexOutOfBoundsException, so that means you must check to make sure that there are arguments. Try this:

    Code:
     
    if (args.length > 0) {
     
        // run your code here
     
    } else {
     
        sender.sendMessage("This command requires at least 1 arguments");
     
    }
     
    
     
  13. Offline

    AoH_Ruthless

    Onlineids
    Or he could just remove it entirely because he does the same exact thing again when checking if the args length is 1 or greater.
     
    Onlineids likes this.
  14. Offline

    cs475x

    Remove this:
    Code:java
    1. Player p = Bukkit.getServer().getPlayer(args[0]);
    2. if (p == null) {
    3. sender.sendMessage("The specified player couldn't be found");
    4. return true;
    5. }


    Replace this:
    Code:java
    1. Bukkit.broadcastMessage(ChatColor.GREEN + p.getName() + ChatColor.RED + " slapped " + ChatColor.GREEN + v.getName() + ".");


    ...with this:
    Code:java
    1. Bukkit.broadcastMessage(ChatColor.GREEN + player.getName() + ChatColor.RED + " slapped " + ChatColor.GREEN + v.getName() + ".");


    As a side note, you shouldn't cast the sender object to Player without first checking if it is in fact a player.
     
  15. Offline

    Glass_Eater84

    Code:java
    1. package me.glasseater.funcommands;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class Main extends JavaPlugin implements Listener {
    12.  
    13. public String name = ChatColor.RED + "[" + ChatColor.GREEN + "FunCommands" + ChatColor.RED + "]" + ChatColor.WHITE;
    14.  
    15. @SuppressWarnings("deprecation")
    16. @Override
    17. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    18. Player player = (Player) sender;
    19. Player p = Bukkit.getServer().getPlayer(args[0]);
    20. if (p == null) {
    21. sender.sendMessage("The specified player couldn't be found");
    22. return true;
    23. }
    24.  
    25.  
    26. if (label.equalsIgnoreCase("slap")) {
    27. if (args.length == 0) {
    28. sender.sendMessage(ChatColor.RED + "Usage: /slap <player>");
    29. return true;
    30. } else if (args.length == 1) {
    31. Player v = Bukkit.getPlayer(args[0]);
    32. if (v != null) {
    33. v.damage(1);
    34. Bukkit.broadcastMessage(ChatColor.GREEN + p.getName() + ChatColor.RED + " slapped " + ChatColor.GREEN + v.getName() + ".");
    35. } else {
    36. sender.sendMessage(ChatColor.RED + "Player not found: " + args[0] + ".");
    37. return true;
    38. }
    39. } else {
    40. sender.sendMessage(ChatColor.RED + "Unknown argument, or maybe allow slapping with extra damage here?");
    41. return true;
    42. }
    43. }
    44. return false;
    45.  
    46. }
    47.  
    48.  
    49.  
    50. public static int clamp(float value, int min, int max) {
    51. return Math.min(Math.max(Math.round(value), min), max);
    52. }
    53. }


    So, would I put this:
    Code:java
    1. if (args.length > 0) {
    at the top, or where exactly would I place that?
     
  16. Offline

    TheEntropy

    Glass_Eater84 I would suggest placing something like that at the very top of the onCommand method (above the Player player = (Player) sender; (Not on every onCommand method you program, but it will work in this instance)

    Code:
     
    if (args.length < 1) {
     
        // notify the sender of their mistake
        sender.sendMessage("This command requires at least 1 argument");
     
        // return false to print out command info from plugin.yml to the sender
        // and prevent any furthur code in this method to run
        return false;
    }
     
    
     
Thread Status:
Not open for further replies.

Share This Page