Error with args

Discussion in 'Plugin Development' started by TheCupOfTea, Mar 25, 2018.

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

    TheCupOfTea

    When I use my command with args it works without errors but when I type the command without argument error pops up
    Code:
    an internal error occurred while attempting to perform this command
    
    Class of command:
    Code:
    package me.arbata;
    
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class GAMEMODE implements CommandExecutor {
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
            if (cmd.getName().equalsIgnoreCase("gamemode") && sender instanceof Player) {
    
                int lenght = args.length;
    
                if (lenght == 1) {
                    if (args[0].equals("0")) {
    
                        Player player = (Player) sender;
    
                        if (player.hasPermission("bess.gamemode")) {
                            player.setGameMode(GameMode.SURVIVAL);
                            player.sendMessage(ChatColor.GOLD + "Žaidimo rėžimas buvo pasirinktas į " + ChatColor.RED
                                    + "išlikimo" + ChatColor.GOLD + "!");
                            TitleAPI.sendTitle(player, 20, 20, 20, ChatColor.RED + "Išlikimo",
                                    ChatColor.GOLD + "Žaidimo rėžimas buvo pasirinktas!");
    
                        } else {
                            player.sendMessage(ChatColor.RED + "Jūs neturite teisės!");
                            TitleAPI.sendTitle(player, 20, 20, 20, "", ChatColor.RED + "Tu neturi teisės!");
    
                        }
    
                        return true;
                    }
                }
            } if (args[0].equals("1")) {
    
                int lenght = args.length;
    
                if (lenght == 1) {
    
                        Player player = (Player) sender;
    
                        if (player.hasPermission("bess.gamemode")) {
                            player.setGameMode(GameMode.CREATIVE);
                            player.sendMessage(ChatColor.GOLD + "Žaidimo rėžimas buvo pasirinktas į " + ChatColor.RED + "kūrybinį" + ChatColor.GOLD + "!");
                            TitleAPI.sendTitle(player, 20, 20, 20, ChatColor.RED + "Kūrybinis",
                                    ChatColor.GOLD + "Žaidimo rėžimas buvo pasirinktas!");
    
                        } else {
                            player.sendMessage(ChatColor.RED + "Jūs neturite teisės!");
                            TitleAPI.sendTitle(player, 20, 20, 20, "", ChatColor.RED + "Tu neturi teisės!");
    
                        }
    
                        return true;
                    }
                } if (args[0].equals("2")) {
    
                    int lenght = args.length;
    
                    if (lenght == 1) {
    
                            Player player = (Player) sender;
    
                            if (player.hasPermission("bess.gamemode")) {
                                player.setGameMode(GameMode.ADVENTURE);
                                player.sendMessage(ChatColor.GOLD + "Žaidimo rėžimas buvo pasirinktas į " + ChatColor.RED + "nuotykių" + ChatColor.GOLD + "!");
                                TitleAPI.sendTitle(player, 20, 20, 20, ChatColor.RED + "Nuotykių",
                                        ChatColor.GOLD + "Žaidimo rėžimas buvo pasirinktas!");
    
                            } else {
                                player.sendMessage(ChatColor.RED + "Jūs neturite teisės!");
                                TitleAPI.sendTitle(player, 20, 20, 20, "", ChatColor.RED + "Tu neturi teisės!");
    
                            }
    
                            return true;
                        }
                    } if (args[0].equals("3")) {
    
                        int lenght = args.length;
    
                        if (lenght == 1) {
    
                                Player player = (Player) sender;
    
                                if (player.hasPermission("bess.gamemode")) {
                                    player.setGameMode(GameMode.SPECTATOR);
                                    player.sendMessage(ChatColor.GOLD + "Žaidimo rėžimas buvo pasirinktas į " + ChatColor.RED + "Stebėtojo" + ChatColor.GOLD + "!");
                                    TitleAPI.sendTitle(player, 20, 20, 20, ChatColor.RED + "Stebėtojo",
                                            ChatColor.GOLD + "Žaidimo rėžimas buvo pasirinktas!");
    
                                } else {
                                    player.sendMessage(ChatColor.RED + "Jūs neturite teisės!");
                                    TitleAPI.sendTitle(player, 20, 20, 20, "", ChatColor.RED + "Tu neturi teisės!");
    
                                }
    
                                return true;
                            }
                        }
           
            return false;
        }
    
    }
    
     
  2. Offline

    timtower Administrator Administrator Moderator

    @TheCupOfTea Because you also have parts where the length check is after the code that is using args[0]
     
  3. Offline

    TheCupOfTea

    So I have to change to args[1], args[2], etc?
     
  4. @TheCupOfTea Your second if statement isn't inside the first one
    So you have:
    Code:
    if(args.length==1) {
        if(args[0].equals("0") {
            // Do something
        }
    }
    if(args[0].equals("1") {
        // Do something
    }
    Instead of:
    Code:
    if(args.length==1) {
        if(args[0].equals("0") {
            // Do something
        }else if(args[0].equals("0") {
            // Do something else
        }
    }
    So when you're not entering any arguments it's not running inside your if statement that checks the length, then it continues to read if(args[0].equals("1")) which doesn't exist because there's no arguments.
     
  5. Offline

    TheCupOfTea

    Code:
    package me.arbata;
    
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class GAMEMODE implements CommandExecutor {
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
            if (cmd.getName().equalsIgnoreCase("gamemode") && sender instanceof Player) {
    
                if (args[0].equals("0")) {
                int lenght = args.length;
                if (lenght == 1) {
                        Player player = (Player) sender;
    
    
    
                        if (player.hasPermission("bess.gamemode")) {
                            player.setGameMode(GameMode.SURVIVAL);
                            player.sendMessage(ChatColor.GOLD + "Žaidimo rėžimas buvo pasirinktas į " + ChatColor.RED
                                    + "išlikimo" + ChatColor.GOLD + "!");
                            TitleAPI.sendTitle(player, 20, 20, 20, ChatColor.RED + "Išlikimo",
                                    ChatColor.GOLD + "Žaidimo rėžimas buvo pasirinktas!");
    
                        } else {
                            player.sendMessage(ChatColor.RED + "Jūs neturite teisės!");
                            TitleAPI.sendTitle(player, 20, 20, 20, "", ChatColor.RED + "Tu neturi teisės!");
    
                        }
    
                        return true;
                    }
                }
            }else if (args[0].equals("1")) {
    
                int lenght = args.length;
    
                if (lenght == 1) {
    
                        Player player = (Player) sender;
    
                        if (player.hasPermission("bess.gamemode")) {
                            player.setGameMode(GameMode.CREATIVE);
                            player.sendMessage(ChatColor.GOLD + "Žaidimo rėžimas buvo pasirinktas į " + ChatColor.RED + "kūrybinį" + ChatColor.GOLD + "!");
                            TitleAPI.sendTitle(player, 20, 20, 20, ChatColor.RED + "Kūrybinis",
                                    ChatColor.GOLD + "Žaidimo rėžimas buvo pasirinktas!");
    
                        } else {
                            player.sendMessage(ChatColor.RED + "Jūs neturite teisės!");
                            TitleAPI.sendTitle(player, 20, 20, 20, "", ChatColor.RED + "Tu neturi teisės!");
    
                        }
    
                        return true;
                    }
                }else if (args[0].equals("2")) {
    
                    int lenght = args.length;
    
                    if (lenght == 1) {
    
                            Player player = (Player) sender;
    
                            if (player.hasPermission("bess.gamemode")) {
                                player.setGameMode(GameMode.ADVENTURE);
                                player.sendMessage(ChatColor.GOLD + "Žaidimo rėžimas buvo pasirinktas į " + ChatColor.RED + "nuotykių" + ChatColor.GOLD + "!");
                                TitleAPI.sendTitle(player, 20, 20, 20, ChatColor.RED + "Nuotykių",
                                        ChatColor.GOLD + "Žaidimo rėžimas buvo pasirinktas!");
    
                            } else {
                                player.sendMessage(ChatColor.RED + "Jūs neturite teisės!");
                                TitleAPI.sendTitle(player, 20, 20, 20, "", ChatColor.RED + "Tu neturi teisės!");
    
                            }
    
                            return true;
                        }
                    }else if (args[0].equals("3")) {
    
                        int lenght = args.length;
    
                        if (lenght == 1) {
    
                                Player player = (Player) sender;
    
                                if (player.hasPermission("bess.gamemode")) {
                                    player.setGameMode(GameMode.SPECTATOR);
                                    player.sendMessage(ChatColor.GOLD + "Žaidimo rėžimas buvo pasirinktas į " + ChatColor.RED + "Stebėtojo" + ChatColor.GOLD + "!");
                                    TitleAPI.sendTitle(player, 20, 20, 20, ChatColor.RED + "Stebėtojo",
                                            ChatColor.GOLD + "Žaidimo rėžimas buvo pasirinktas!");
    
                                } else {
                                    player.sendMessage(ChatColor.RED + "Jūs neturite teisės!");
                                    TitleAPI.sendTitle(player, 20, 20, 20, "", ChatColor.RED + "Tu neturi teisės!");
    
                                }
    
                                return true;
                            }
                        }
           
            return false;
        }
    
    }
    
    Like that?
     
  6. Offline

    timtower Administrator Administrator Moderator

    @TheCupOfTea You still check args[0] before the length.
    Length check should be the first check using the args.
     
  7. Offline

    TheCupOfTea

    So I have to change this
    Code:
    } else if (args[0].equals("1")) {
    
                int lenght = args.length;
    
                if (lenght == 1) {
    Into this
    Code:
            } int lenght = args.length;          
            if (lenght == 1) {
            if (args[0].equals("0")) {
    And delete to other arguments
    Code:
                int lenght = args.length;
    
                if (lenght == 1) {
    [/CODE]
     
  8. Offline

    timtower Administrator Administrator Moderator

    @TheCupOfTea First the length check, then the arguments, if your amount of arguments is variable then use a >= check
     
  9. Offline

    KarimAKL

    Code:
    if (lenght == 1) {
      if (args[0].equals("0")) {
        //Code if arg 1 is 0
      }
    } else if (lenght == 0) {
      //Code if no args was set
    }
    
     
    Last edited by a moderator: Apr 20, 2018
Thread Status:
Not open for further replies.

Share This Page