Argument error

Discussion in 'Plugin Development' started by emore25, Mar 22, 2017.

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

    emore25

    Hey i've got a plugin problem that i can't seem to wrap my head around i'm getting this error when typing /ban (command i've coded for the plguin) and i've set it so if there args.length == 0 then return you need more condations, also my other part of the code where it's args.length == 1 or more then it works fine i don't know what the problem is.
    Code:
    package me.emore25.kick;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    
    public class Ban implements CommandExecutor {
      
        Main1 plugin;
       
        public Ban(Main1 passedPlugin) {
       
       
            this.plugin = passedPlugin;
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args ) {
            Player player = (Player) sender;
             Player target = Bukkit.getServer().getPlayer(args[0]);
           
           
    
            if(commandLabel.equalsIgnoreCase("ban") || commandLabel.equalsIgnoreCase("b")) {
                  if(!player.hasPermission("noban.mod")){
                      player.sendMessage(ChatColor.GRAY + "[Kick] " + ChatColor.DARK_RED + "You don't have permission.");
                  } else {
                if (args.length == 0 ) {
                    player.sendMessage(ChatColor.GRAY + "[Kick] " + ChatColor.DARK_RED + "Please specifiy a player.");
                    return true;
                   
                }            
               
                if(!(sender instanceof Player)) {
                      target.kickPlayer(ChatColor.GRAY + "[Kick] " + ChatColor.DARK_GREEN + "You have been banned");
                      target.setBanned(true);
                      return true;
                     
                     
                  } else if(args.length == 1) {
                   
            if (target == null) {
            player.sendMessage(ChatColor.GRAY + "[Kick] " + ChatColor.DARK_RED + "Player is not online");
            return true;
            }
    etc....
    Error produce is:
    Code:
    [22:30:43 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'ban' in plugin Kick v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.11.2.jar:git-Spigot-96235ab-2aa5ac6]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.11.2.jar:git-Spigot-96235ab-2aa5ac6]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(CraftServer.java:650) ~[spigot-1.11.2.jar:git-Spigot-96235ab-2aa5ac6]
            at net.minecraft.server.v1_11_R1.PlayerConnection.handleCommand(PlayerConnection.java:1345) [spigot-1.11.2.jar:git-Spigot-96235ab-2aa5ac6]
            at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1180) [spigot-1.11.2.jar:git-Spigot-96235ab-2aa5ac6]
            at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.11.2.jar:git-Spigot-96235ab-2aa5ac6]
            at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.11.2.jar:git-Spigot-96235ab-2aa5ac6]
            at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.11.2.jar:git-Spigot-96235ab-2aa5ac6]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_121]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_121]
            at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [spigot-1.11.2.jar:git-Spigot-96235ab-2aa5ac6]
            at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:747) [spigot-1.11.2.jar:git-Spigot-96235ab-2aa5ac6]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot-1.11.2.jar:git-Spigot-96235ab-2aa5ac6]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot-1.11.2.jar:git-Spigot-96235ab-2aa5ac6]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot-1.11.2.jar:git-Spigot-96235ab-2aa5ac6]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_121]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
            at me.emore25.kick.Ban.onCommand(Ban.java:23) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.11.2.jar:git-Spigot-96235ab-2aa5ac6]
            ... 15 more
    
     
  2. Your assigning the target as a player before you test if there is enough Args to do so. Simply declare the target AFTER you test the length of the args
     
    emore25 likes this.
  3. Offline

    emore25

    Thank you very much. Such a stupid error to make by me.
     
  4. Offline

    mine-care

    Let me outline a few code-style and practice issues you should consider:
    Main1 is not really a good name for a class. Classes are typically named based on their function(s) Thats why most classes in the Java API have sensible names like "Socket" which resembles a TCP socket, Exception which resembles.. you guessed it, an exception :p I understand that "Main" is a convenient name however try to name it something like PluginNameMain or something. Also sticking numbers at the end of names is also discouraged because it is likely to cause problems. If you have Main and Main1 you are very likely to miss this 1 and spend the rest of the day trying to figure why it all goes wrong.

    Although in perticular cases you dont need to check before casting, in this one you must check if sender is a Player before casting it otherwise errors will pop up if the sender isnt a player. Try it yourself, execute the command from console :p

    Thats where your error lyes as it was mentioned above you get the argument at position 0 (first argument) without actually knowing if it exists in the first place!

    Use Command#getName() instead of the label where you can specify aliases and you dont have to check for both ban and b ;)
     
Thread Status:
Not open for further replies.

Share This Page