Solved Im getting an internal error when I dont put any args beacuse of the add section how do I fix it?

Discussion in 'Spigot Plugin Development' started by ImJustHere, Feb 13, 2020.

  1. Ik it's stupid....
    Code (Text):
       if(cmd.getName().equalsIgnoreCase("Exmple"))
                {
                    if(args.length == 0)
                    {
                        //command
                    }          
                    if(player.hasPermission("Exmple.*"))
                    {
                        if(args[0].equalsIgnoreCase("add"))
                         {
                                     //command
                          }  
                    }
               }
               
     
  2. Code (Text):
    if(cmd.getName().equalsIgnoreCase("Exmple"))
                {
                    if(args.length == 0) return true;
                    if(!player.hasPermission("Exmple.*")) return true;
                 
                    if(args[0].equalsIgnoreCase("add"))
                    {
                        //command
                    }
               }
    What isn't working exactly? Your code should be working fine even though it's not pretty convenient.
    Also don't look for '*' permissions, put them into your plugin.yml and use children instead.
     
  3. If I don't type an args I'm getting an error
     
  4. You don't need args though... What's your full method code? What's the full error stacktrace?
     
  5. Code (Text):
    [22:53:45] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'staff' in plugin SolomonStaff v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.craftbukkit.v1_14_R1.CraftServer.dispatchCommand(CraftServer.java:710) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PlayerConnection.handleCommand(PlayerConnection.java:1641) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PlayerConnection.a(PlayerConnection.java:1481) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PacketPlayInChat.a(PacketPlayInChat.java:47) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.TickTask.run(SourceFile:18) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeTask(SourceFile:144) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeNext(SourceFile:118) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.aX(MinecraftServer.java:910) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.executeNext(MinecraftServer.java:903) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.awaitTasks(SourceFile:127) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.sleepForTick(MinecraftServer.java:887) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:820) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
            at me.RoboBoy99.Staff.Command.MainCommand.onCommand(MainCommand.java:28) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            ... 17 more

    full mothod

    Code (Text):
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
            if (sender instanceof Player)
            {
                Player player = (Player) sender;
                if(cmd.getName().equalsIgnoreCase("Staff"))
                {        
                    Data.StaffContains(player);
                   
                    if(player.hasPermission("Staff.*"))
                    {
                        if(args[0].equalsIgnoreCase("add"))
                        {
                         
                            if(Bukkit.getPlayer(args[1]) != null)
                            {  
                                Player target = Bukkit.getPlayerExact(args[1]);
                                Data.AddPlayer(target);
                            }
                        }
                    }
                }
            }
            return false;
        }
     
  6. What's the line 28?
    Btw, could you please follow naming conventions on your package name? This triggers me so much
     
  7. @Maxx_Qc
    Line 28:
    Code (Text):
    if(args[0].equalsIgnoreCase("add"))
     
  8. The reason is because the condition
    Code (Java):
    if(args[0].equalsIgnoreCase("add"))
    is unable to check args[0] since you have 0 arguments. It is checking for args[0] (which is the first argument), but that argument doesn't exist.

    The solution is to wrap it in an else statement. This ensures that the bottom section of code will only execute if there is at least one argument:
    Code (Java):
    if (args.length == 0)
    {
        //command
    }    
    else
    {
        if (player.hasPermission("Exmple.*"))
        {
            if(args[0].equalsIgnoreCase("add"))
            {
                //command
            }
        }
    }
     
    #8 j3kennard, Feb 13, 2020
    Last edited: Feb 13, 2020
    • Like Like x 1
  9. thx its working now
     
  10. Great. Please leave a positive rating if you don't mind.

    Oh, and mark the thread as solved so people don't keep replying.

    Have a good day.