[Solved]Probleme with multiple args

Discussion in 'Plugin Development' started by Monowii, May 6, 2012.

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

    Monowii

    Hello, i have a probleme with args, I want to the player choose a number of round with /zm choose <NumberOfRounds>
    I want to the player can only choose a number between 1 and 9
    Code:
                else if (args[0].equalsIgnoreCase("choose"))
                {
                    Server serv = getServer();
                   
                    if (args[1].equalsIgnoreCase("1") || args[1].equalsIgnoreCase("2") || args[1].equalsIgnoreCase("3") || args[1].equalsIgnoreCase("4") || args[1].equalsIgnoreCase("5") || args[1].equalsIgnoreCase("6") || args[1].equalsIgnoreCase("7") || args[1].equalsIgnoreCase("8") || args[1].equalsIgnoreCase("9"))
                    {
                        String rounds = args[1];
                        serv.broadcastMessage("A session start with " +rounds+ " rounds");
                       
                        Player[] playeronline = getServer().getOnlinePlayers();
                       
                        Random random = new Random();
                       
                        int playerint = random.nextInt(playeronline.length);
                       
                        Player zombie = playeronline[playerint];
                       
                        serv.broadcastMessage("The zombie is " + ChatColor.DARK_RED + zombie.getName());
                        Location locactivator = new Location(serv.getWorld("world"), -28, 21, -14);
                        zombie.teleport(locactivator);
                        zombie.sendMessage("You are the zombie");
                    }
                    else
                    {
                        sender.sendMessage("It isn't a correct number of rounds");
                        return false;
                    }
                }
    
    Thank you for your time :)
     
  2. Offline

    Terradominik

    try the following

    Code:
    if ((Integer.parseInt(args[1]) >= 1) && (Integer.parseInt(args[1]) <= 9)) { 
     
  3. Offline

    Monowii

    Don't work ...
    This is all the onCommand part :
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args)
        {
         
            double x = -33.5;
            double y = 22.6;
            double z = -1.5;
     
            if (cmd.getName().equalsIgnoreCase("dr"))
            {
                if (args.length == 0)
                  {
                          sender.sendMessage(ChatColor.DARK_GRAY+"-----"+ChatColor.GOLD+"mwDeathrun"+ChatColor.DARK_GRAY+"-"+ChatColor.GOLD+"Help"+ChatColor.DARK_GRAY+"-----");
                          sender.sendMessage(ChatColor.GRAY+"/dr start "+ChatColor.BLACK+"-"+ChatColor.WHITE+" Start the deathrun minigame");
                          sender.sendMessage(ChatColor.GRAY+"/dr choose "+ChatColor.BLACK+"-"+ChatColor.WHITE+" Choose an avtivator");
                  }
             
                else if (args[0].equalsIgnoreCase("start"))
                {
                    Server serv = getServer();
                 
                    for (Player ply : getServer().getOnlinePlayers())
                    {
                        Location loc = new Location(serv.getWorld("world"), x, y, z);
                        ply.teleport(loc);
                    }
                }
             
                else if (args[0].equalsIgnoreCase("choose"))
                {
                    Server serv = getServer();
                 
                    if (args[1].equalsIgnoreCase("1") || args[1].equalsIgnoreCase("2") || args[1].equalsIgnoreCase("3") || args[1].equalsIgnoreCase("4") || args[1].equalsIgnoreCase("5") || args[1].equalsIgnoreCase("6") || args[1].equalsIgnoreCase("7") || args[1].equalsIgnoreCase("8") || args[1].equalsIgnoreCase("9"))
                    {
                        String manches = args[1];
                        serv.broadcastMessage(ChatColor.DARK_PURPLE + "[Deathrun] "+ChatColor.DARK_GRAY+"Une partie de deathrun commence avec " +manches+ " manches");
                     
                        Player[] playeronline = getServer().getOnlinePlayers();
                     
                        Random random = new Random();
                     
                        int playerint = random.nextInt(playeronline.length);
                     
                        Player activator = playeronline[playerint];
                     
                        serv.broadcastMessage(ChatColor.DARK_PURPLE + "[Deathrun] "+ChatColor.DARK_GRAY+"L'activateur est " + ChatColor.DARK_RED + activator.getName());
                        Location locactivator = new Location(serv.getWorld("world"), -28, 21, -14);
                        activator.teleport(locactivator);
                        activator.sendMessage(ChatColor.DARK_PURPLE + "[Deathrun] "+ChatColor.DARK_GRAY+"Vous etes l'activateur !");
                     
                        activateur = activator;
                        activatorkill = true;
                    }
                    else
                    {
                        sender.sendMessage(ChatColor.DARK_PURPLE + "[Deathrun] "+ChatColor.DARK_GRAY+"Ce n'est pas un nombre de manches correct !");
                        return false;
                    }
                }
            }
        return false;
        }
     
  4. Offline

    Terradominik

    which line don't work ? error message ?
     
  5. Offline

    Monowii

    When I type /dr choose (Error message in console)
    Code:
    2012-05-06 17:50:06 [WARNING] Unexpected exception while parsing console command
    org.bukkit.command.CommandException: Unhandled exception executing command 'dr' in plugin mwDeathrun v0.3
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:166)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:473)
        at org.bukkit.craftbukkit.CraftServer.dispatchServerCommand(CraftServer.java:469)
        at net.minecraft.server.MinecraftServer.b(MinecraftServer.java:596)
        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:565)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:449)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
        at me.monowii.Core.onCommand(Core.java:62)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
        ... 7 more
     
  6. Offline

    Terradominik

    check that there is the args array like

    if (args.length == 0) {
     
  7. Offline

    Monowii

    Same error with
    Code:
                    if (args[1].length() == 0)
                    {
                        sender.sendMessage(ChatColor.DARK_PURPLE + "[Deathrun] "+ChatColor.DARK_GRAY+"Ce n'est pas un nombre de manches correct !");
                        return false;
                    }
     
  8. Offline

    Jogy34

    Just do args.length not args[1].length()
     
  9. Offline

    Monowii

    It is a little bit better now :
    /dr choose display the good message and
    /dr choose tralalala display the good message BUT
    /br choose 1 display the error message (Not in console but in the condition) : sender.sendMessage(ChatColor.DARK_PURPLE + "[Deathrun] "+ChatColor.DARK_GRAY+"Vous n'avez pas specifier le nombre de manches !");
    Code:
                else if (args[0].equalsIgnoreCase("choose"))
                {
                    Server serv = getServer();
     
                    if (args.length == 1)
                    {
                        sender.sendMessage(ChatColor.DARK_PURPLE + "[Deathrun] "+ChatColor.DARK_GRAY+"Vous n'avez pas specifier le nombre de manches !");
                        return false;
                    }
                    else if (args.length == 2)
                    {
                        if (args[1] == "1")
                        {
                            String manches = args[1];
                            serv.broadcastMessage(ChatColor.DARK_PURPLE + "[Deathrun] "+ChatColor.DARK_GRAY+"Une partie de deathrun commence avec " +manches+ " manches");
                         
                            Player[] playeronline = getServer().getOnlinePlayers();
                         
                            Random random = new Random();
                         
                            int playerint = random.nextInt(playeronline.length);
                         
                            Player activator = playeronline[playerint];
                         
                            serv.broadcastMessage(ChatColor.DARK_PURPLE + "[Deathrun] "+ChatColor.DARK_GRAY+"L'activateur est " + ChatColor.DARK_RED + activator.getName());
                            Location locactivator = new Location(serv.getWorld("world"), -28, 21, -14);
                            activator.teleport(locactivator);
                            activator.sendMessage(ChatColor.DARK_PURPLE + "[Deathrun] "+ChatColor.DARK_GRAY+"Vous etes l'activateur !");
                         
                            activateur = activator;
                            activatorkill = true;
                        }
                        else
                        {
                            sender.sendMessage(ChatColor.DARK_PURPLE + "[Deathrun] "+ChatColor.DARK_GRAY+"Ce n'est pas un nombre de manches correct !");
                            return false;
                        }
                    }
                }
     
  10. Offline

    Jogy34

    Code:
     args[1] == "1" 
    That will not work. You cannot compare Strings with binary opperators. use args[0].equalsIgnoreCase("1")
    Also you can do 'int num = Integer.parseInt(args[1])' and then check to see if the value is between 1 and 9
     
  11. Offline

    Monowii

    Yes, it work for number but not with characters :
    Code:
                        if ((Integer.parseInt(args[1]) >= 1) && (Integer.parseInt(args[1]) <= 9))
                        {
                            String manches = args[1];
                            serv.broadcastMessage(ChatColor.DARK_PURPLE + "[Deathrun] "+ChatColor.DARK_GRAY+"Une partie de deathrun commence avec " +manches+ " manches");
                         
                            Player[] playeronline = getServer().getOnlinePlayers();
                         
                            Random random = new Random();
                         
                            int playerint = random.nextInt(playeronline.length);
                         
                            Player activator = playeronline[playerint];
                         
                            serv.broadcastMessage(ChatColor.DARK_PURPLE + "[Deathrun] "+ChatColor.DARK_GRAY+"L'activateur est " + ChatColor.DARK_RED + activator.getName());
                            Location locactivator = new Location(serv.getWorld("world"), -28, 21, -14);
                            activator.teleport(locactivator);
                            activator.sendMessage(ChatColor.DARK_PURPLE + "[Deathrun] "+ChatColor.DARK_GRAY+"Vous etes l'activateur !");
                         
                            activateur = activator;
                            activatorkill = true;
                        }
                        else
                        {
                            sender.sendMessage(ChatColor.DARK_PURPLE + "[Deathrun] "+ChatColor.DARK_GRAY+"Ce n'est pas un nombre de manches correct !");
                            return false;
                        }
    Error message :
    Code:
    2012-05-06 18:59:25 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'dr' in plugin mwDeathrun v0.3
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:166)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:473)
        at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:821)
        at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:781)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:764)
        at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:34)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:229)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:113)
        at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:78)
        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:551)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:449)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.NumberFormatException: For input string: "test"
        at java.lang.NumberFormatException.forInputString(Unknown Source)
        at java.lang.Integer.parseInt(Unknown Source)
        at java.lang.Integer.parseInt(Unknown Source)
        at me.monowii.Core.onCommand(Core.java:69)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
        ... 12 more
     
  12. Offline

    Terradominik

    for characters use

    char x = STRING_NAME.charAt(INDEX);
     
  13. Offline

    CorrieKay

    Youre trying to turn "test" into an integer
     
  14. Offline

    Jogy34

    My best guess would be to do this:
    Code:
    boolean isNumber = true;
    try
    {
        Integer.parseInt(args[0]);
    }catch(Exception e)
    {
        isNumber = false;
    }
    if(isNumber && (Integer.parseInt(args[1]) >= 1) && (Integer.parseInt(args[1] <= 9))
    {
    //other Stuff
    }
    else if(!isNumber)
    {
        sender.sendMessage("You must use a number");
    }
    else
    {
    [INDENT=1]sender.sendMessage("The number must be between 1 and 9");[/INDENT]
    }
     
  15. Offline

    Monowii

    Terradominik and CorrieKay like this.
Thread Status:
Not open for further replies.

Share This Page