In Need of Help Solving a Bug

Discussion in 'Plugin Development' started by Rocky_990, Apr 17, 2015.

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

    Rocky_990

    Solved

    Hello,
    I've been developing a plugin for my server recently (mainly to test my knowledge, but it will also be of use on my server), and it works well, except from one bug:
    When I run the command, it does what it's supposed to, but THEN tells the user that an internal error occurred.
    It only happens on the command "/help", and there is "/help list" which works just fine without the error.

    Main.java:
    Code:
    package me.jake.CustomHelp;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
        public void onEnable() {
            getLogger().info("Plugin Enabled!");
        }
        public void onDisable() {
            getLogger().info("Plugin Disabled!");
        }
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (sender instanceof Player) {
                Player player = (Player) sender;
                // Default help page
                if(cmd.getName().equalsIgnoreCase("help")) {
                    //player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + "Help" + ChatColor.DARK_GRAY + "]" + ChatColor.GRAY + " This is your help guide. Here you can find help for many aspects of the game which you may be struggling with. There are " + ChatColor.AQUA + "many pages of help " + ChatColor.GRAY + "which you can access with the command: " + ChatColor.AQUA + "/help <page>" + ChatColor.GRAY + ". The pages are listed on /help list. Have fun and good luck! " );
                    if (args.length == 0) { //Sender only typed '/hello' and nothing else
                        player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + "Help" + ChatColor.DARK_GRAY + "]" + ChatColor.GRAY + " This is your help guide. Here you can find help for many aspects of the game which you may be struggling with. There are " + ChatColor.AQUA + "many pages of help " + ChatColor.GRAY + "which you can access with the command: " + ChatColor.AQUA + "/help <page>" + ChatColor.GRAY + ". The pages are listed on /help list. Have fun and good luck! " );
                    }
                    if (args[0].equalsIgnoreCase("list")) {
                        // If use sends /help list, send message below.
                        player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + "Help" + ChatColor.DARK_GRAY + "]" + ChatColor.GRAY + " Help - List\n" + ChatColor.GRAY + " /help - Default help page\n/help list - Lists pages of help");
                    }
                    return true;
                }
              
                // UNWORKING
                //if(cmd.getName().equalsIgnoreCase("help list")) {
                //    player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + "Help" + ChatColor.DARK_GRAY + "]" + ChatColor.GRAY + " /help - Default help page\n/help list - Lists pages of help");
                //}
              
            }
          
            return false;
        }
    
    
    }
    
    plugin.yml:
    Code:
    name: CustomHelp
    author: Rocky_990
    version: 1.1.2
    description: Displays custom help.
    main: me.jake.CustomHelp.Main
    commands:
      help:
        description: Displays custom help main page.
      help list:
        description: Lists custom help pages.
    
    Console when command is run:
    Code:
    [00:20:39] [Server thread/INFO]: Rocky_990 issued server command: /help
    [00:20:39] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'help' in plugin CustomHelp v1.1.2
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[minecraft_server.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) ~[minecraft_server.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServer.java:701) ~[minecraft_server.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerConnection.java:956) [minecraft_server.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:817) [minecraft_server.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java:28) [minecraft_server.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat.java:47) [minecraft_server.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157) [minecraft_server.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [minecraft_server.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667) [minecraft_server.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [minecraft_server.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [minecraft_server.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [minecraft_server.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [minecraft_server.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
    at me.jake.CustomHelp.Main.onCommand(Main.java:25) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[minecraft_server.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    
    Thanks a lot to anyone who tries to help remove the internal error; very much appreciated.

    ~Rocky_990 (Jake)
     
    Last edited: Apr 17, 2015
  2. Offline

    BrickBoy55

    @Rocky_990

    It's on line 25 of Main.java. Which in your original post is a }, so you must of copied it wrong.
     
  3. Offline

    Rocky_990

    @BrickBoy55 , thank you for your help although I'm still unsure on what's wrong with that? That is there due to the if statement...
     
  4. Offline

    BrickBoy55

    That's the line your error is at.

    Show line 25 in your Main class.
     
  5. Offline

    Rocky_990

    @BrickBoy55
    Code:
    if (args[0].equalsIgnoreCase("list")) {
     
  6. Code:java
    1. if (args.length == 0) { //Sender only typed '/hello' and nothing else
    2. player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + "Help" + ChatColor.DARK_GRAY + "]" + ChatColor.GRAY + " This is your help guide. Here you can find help for many aspects of the game which you may be struggling with. There are " + ChatColor.AQUA + "many pages of help " + ChatColor.GRAY + "which you can access with the command: " + ChatColor.AQUA + "/help <page>" + ChatColor.GRAY + ". The pages are listed on /help list. Have fun and good luck! " );
    3.  
    4. //Code continues because you didnt return true.
    5. }
    6. if (args[0] <-- ERROR because the args.length may be 0
     
    Rocky_990 likes this.
  7. Offline

    dsouzamatt

    @Rocky_990 This:
    Code:
    if (args[0].equalsIgnoreCase("list")) {
    Should be an else if statement; only if the lines after the first if statement are not run do you want this bit to run.
     
    Rocky_990 likes this.
  8. Offline

    Rocky_990

    dsouzamatt likes this.
Thread Status:
Not open for further replies.

Share This Page