Plugin Returning an Internal Error when it's Command is Executed

Discussion in 'Plugin Development' started by DibDibs, Apr 23, 2015.

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

    DibDibs

    Hiya, I've recently made a plugin for my server, it's quite a simple one but I'm stuck with it. It's basically supposed to add names to a YML file when a command is executed in-game, but it doesn't want to work.

    Here's the errors and my Java code:-

    Basic Error (open)

    [23:06:56 INFO]: SpinballWhizzer issued server command: /addnew false
    [23:06:56 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'addnew' in plugin SafeChest v1.0


    Full Error (open)

    [23:06:47] [Server thread/INFO]: SpinballWhizzer issued server command: /addnew SpinballWhizzer
    [23:06:47] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'addnew' in plugin SafeChest v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-3fc97ff]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-3fc97ff]
    at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:625) ~[craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-3fc97ff]
    at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1058) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-3fc97ff]
    at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:919) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-3fc97ff]
    at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(SourceFile:37) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-3fc97ff]
    at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(SourceFile:9) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-3fc97ff]
    at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-3fc97ff]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.7.0_75]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0_75]
    at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:656) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-3fc97ff]
    at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:284) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-3fc97ff]
    at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:609) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-3fc97ff]
    at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:517) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-3fc97ff]
    at java.lang.Thread.run(Unknown Source) [?:1.7.0_75]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
    at com.SpinballWhizzer.SafeChest.SafeChest.onCommand(SafeChest.java:51) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-3fc97ff]
    ... 14 more


    Plugin Java Code (open)

    Code:
    package com.SpinballWhizzer.SafeChest;
    
    import java.util.ArrayList;
    import java.util.logging.Logger;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import java.awt.List;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class SafeChest extends JavaPlugin implements CommandExecutor{
      
        public final Logger logger = Logger.getLogger("Minecraft");
        public static SafeChest plugin;
      
        @Override
        public void onDisable(){
          
            PluginDescriptionFile pdfFile = this .getDescription();
            this.logger.info(pdfFile.getName() + (" Disabled"));
          
        }
      
        @Override
        public void onEnable(){
          
            PluginDescriptionFile pdfFile = this .getDescription();
            this.logger.info(pdfFile.getName() + " Version: " + pdfFile.getVersion() + (" Disabled"));
      
        }
          
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
          
            if (cmd.getName().equalsIgnoreCase("addnew")) {
    
                if (args.length > 0) {
    
                    File f = new File("SafeChest/players.yml");
                    YamlConfiguration yaml = YamlConfiguration.loadConfiguration(f);
                  
                    yaml.createSection("players." + args[1]);
                  
                    try {
                          yaml.save(f);
                        } catch(IOException e) {
                          // Handle any IO exception here
                          e.printStackTrace();
                        }
                  
    
                } else {
    
                    sender.sendMessage("Not enough arguments!");
    
                }
              
    
              
            }
            return false;
          
        }
    
    }


    plugin.yml (open)

    name: SafeChest
    main: com.SpinballWhizzer.SafeChest.SafeChest
    version: '1.0'
    author: SpinballWhizzer
    load: startup
    description: >
    SafeChest Intergration Plugin
    commands:
    addnew:
    description: Adds Players
    usage: /addnew <player>

    The 'plugin.yml' is spaced correctly and has passed all syntax tests.

    Screenshots (open)
    [​IMG]
    Server Returning Internal Error

    [​IMG]
    Layout of the files and directories in the server's plugin folder.
    (There is currently nothing in 'players.yml' because the plugin should create the text inside, but hasn't because of the internal error.)



    If you need any extra info, reply to this, PM me or e-mail me at dibdib2day@gmail.com
     
  2. Offline

    Koobaczech

    @DibDibs
    Try to change args[1] to args[0].
    When you run /addnew false, addnew is the command, false is argument 1
    Keep in mind, the first non command argument, or argument 1, is array index [0].
    To debug your code, look at the console, its telling you that
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1 //1 is OUT of scope
    at com.SpinballWhizzer.SafeChest.SafeChest.onCommand(SafeChest.java:51) ~[?:?] //51 is the line where the error is
    Complete Code (open)

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if (cmd.getName().equalsIgnoreCase("addnew")) {
                if (args.length == 1)//If you do >0 it could lead to problems. Better to be specific
                 {
                    File f = new File("SafeChest/players.yml");
                    YamlConfiguration yaml = YamlConfiguration.loadConfiguration(f);         
                    yaml.createSection("players." + args[0]);   //Here is what we changed       
                    try {
                          yaml.save(f);
                        } catch(IOException e) {
                          // Handle any IO exception here
                          e.printStackTrace();
                        }       
                } else {
                    sender.sendMessage("Not enough arguments!");
                }             
            }
            return false; 
        }
    }
     
    Last edited: Apr 23, 2015
  3. Offline

    sgavster

    @Koobaczech Don't spoon feed
    @DibDibs Your JavaPlugin should not also extend CommandExecutor.
     
    RawCode likes this.
  4. Offline

    Konato_K

    @sgavster JavaPlugin doesn't need to implement CommandExecutor, but implementing it won't hurt anything.
     
    Msrules123 likes this.
  5. Offline

    RawCode

    implementation *tag* used only by code verifier, no matter how many interfaces implemented by class, this wont have any effect on codeflow or performance.
    as stated above, spoonfeeding is no way.

    add debug output for every field each forth line in order to discover faulty state of method.
     
  6. Offline

    Rocoty

    @RawCode @sgavster Oh come on, be at least a bit reasonable. Dude clearly explained why changing the index would help. I don't see what's so bad about this. Not spoon feeding in my eyes.
     
    Koobaczech and 1Rogue like this.
  7. Offline

    Koobaczech

    @sgavster youropinion=null; System.gc();
     
  8. @RawCode @sgavster @Rocoty i would say it is kind off spoon feeding, because he just gave working fixed code to him. That is spoonfeeding, no way it isn't. BUT it is totally fine. At this point i agree with Rocoty. He showed him the error, he told him how to read the exception and find out what caused the error, he expalined him why the error is happening and he told him how to fix it. There is no way he didn't get it. And the error is changing one or two values so dont blame him for doing that. Thats perfectly fine. I see it more like explaining the problem on an example which also is the solution. In a case like that no reason for complaining about that

    @Koobaczech clever joke there ;) bet he wont get it xD
     
    Rocoty likes this.
  9. Offline

    Rocoty

    @Shmobi I suppose that's kind of what I was getting at, yes.
     
  10. Offline

    Koobaczech

    Thanks guys! I just want to help others as best as i can and see Bukkit thrive. I know how frustrating and difficult it can be to learn a language from scratch, and still know nothing of it. Delegating a users question to a "Learn Java" or "This link will teach you more than me fixing your issue right now" is not helpful. If a user even has to click that link, chances are they have not learned java, and learning a language means taking a lot of college courses immersing you in the world of correct coding conventions, correct algorithms, ethics, basic coding, etc @Shmobi @Rocoty
     
Thread Status:
Not open for further replies.

Share This Page