Creating a very simple command with one argument. Getting stack trace.

Discussion in 'Plugin Development' started by simondoestuff, Aug 2, 2018.

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

    simondoestuff

    After the plugin didn't work. I rewrote a small part of it to isolate the problem. The problem persists in the smaller snapshot and I don't even know what it is. I have a basic commnd /opc. If given an argument, it should say "we got an argument" if one doesn't specify an argument, then it simple says "no arguments". However it obviously doesn't work this way. Giving an argument, works correctly, lacking an argument, generates a load of stack trace that I can resolve.

    Code:
    package mc.simondoestuff.opcodes;
    
    import net.minecraft.server.v1_12_R1.CommandExecute;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.event.Listener;
    
    public class Commands extends CommandExecute implements Listener, CommandExecutor {
    
        public String cmd1 = "opc";
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase(cmd1)) {
                if (args[0].length() != 0) {
                    sender.sendMessage("no arguments");
                    return true;
                } else {
                    sender.sendMessage("we got an argument");
                    return true;
                }
            }
            return false;
        }
    } // class
    and the stack trace errors in console:

    Code:
    [14:02:35 WARN]: Unexpected exception while parsing console command "opc"
    org.bukkit.command.CommandException: Unhandled exception executing command 'opc' in plugin opcodes v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:648) ~[spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchServerCommand(CraftServer.java:634) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
            at net.minecraft.server.v1_12_R1.DedicatedServer.aP(DedicatedServer.java:444) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:407) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
            at java.lang.Thread.run(Unknown Source) [?:?]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
            at mc.simondoestuff.opcodes.Commands.onCommand(Commands.java:16) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
            ... 8 more
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    simondoestuff

    Thanks for the help @timtower , but it still put out stack trace. I ended up getting help from a professional software developer and this was his fix. He changed that line to:
    Code:
                if ((args != null) && (args.length != 0)) {
    which seemed to be the only working option.
     
  4. Offline

    timtower Administrator Administrator Moderator

    @simondoestuff Then what did you try yourself? args is never null.
     
  5. Online

    KarimAKL

    @simondoestuff
    From what i understand you should change this:
    Code:Java
    1. if (args[0].length() != 0) {

    to:
    Code:Java
    1. if (args.length != 0) {

    Btw "!=" means "is not" meaning that inside this is when you have arguments because it says "!= 0", just saying because you have "!= 0" and then a message saying "no arguments" and else you have a message saying "we got an argument".
     
  6. Offline

    LaPiMoNsTeR

    You have to check if the number of arguments is lower than what you need
    Code:
    if(args.length < 1) {
        // no arguments
    } else {
        // 1 or more than 1 arguments
    }
    And your code is not clean and has some problem
    In your code you compare command name but command name is the name that you used for register the command. To get what the player wrote, you have to compare String label.
     
  7. Offline

    ThePandaPlayer

    @simondoestuff

    Check this line of the error message:
    Code:
     at mc.simondoestuff.opcodes.Commands.onCommand(Commands.java:16) ~[?:?] 
    You should check line 16 of your class to figure out what is causing the issue. Unfortunately, I cannot see line numbers, so you have to check for yourself.
     
Thread Status:
Not open for further replies.

Share This Page