Command Args for a Reason to be sent

Discussion in 'Plugin Development' started by JarFile, Jan 3, 2015.

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

    JarFile

    I'm trying to make is where if someone types /warn [playername] [times warned] [reason]. They get the reason why they were warned with how many times they have been warned. x/3. I have the times warned but, I need help with the reason. How would I do that?
    Code:
    Code:
            if(commandLabel.equalsIgnoreCase("warn"))
            {
                Player p = (Player) s;
                if(p.hasPermission("lightningedit.warn"))
                {
                    if(args.length == 1)
                    {
                        for(Player warn : Bukkit.getServer().getOnlinePlayers())
                        {
                            if(warn.getName().equalsIgnoreCase(args[0]))
                            {
                                if(args.length == 2)
                                {
                                    if(args[1].equalsIgnoreCase("1"))
                                    {
                                       
                                    }
                                    else if(args[1].equalsIgnoreCase("2"))
                                    {
                                       
                                    }
                                    else if(args[1].equalsIgnoreCase("3"))
                                    {
                                       
                                    }
                                    else
                                    {
                                        p.playSound(p.getLocation(), Sound.CAT_HISS, 1, 2);
                                        p.sendMessage(ChatColor.DARK_RED + "Invalid Syntax </warn [player] [times warned] [reason]");
                                    }
                                }
                                else
                                {
                                    p.playSound(p.getLocation(), Sound.CAT_HISS, 1, 2);
                                    p.sendMessage(ChatColor.DARK_RED + "Invalid Syntax </warn [player] [times warned] [reason]");
                                }
                            }
                            else
                            {
                                p.playSound(p.getLocation(), Sound.CAT_HISS, 1, 2);
                                p.sendMessage(ChatColor.DARK_RED + "Invalid Syntax </warn [player] [times warned] [reason]");
                            }
                        }
                    }
                    else
                    {
                    p.playSound(p.getLocation(), Sound.CAT_HISS, 1, 2);
                    p.sendMessage(ChatColor.DARK_RED + "Invalid Syntax </warn [player] [times warned] [reason]");
                    }
                }
                else
                {
                    p.playSound(p.getLocation(), Sound.CAT_HISS, 1, 2);
                    p.sendMessage(ChatColor.DARK_RED + "Denied Access.");
                }
            }
    
     
  2. @JarFile
    Code:
    public static String[] getMsgFromArgs(final String[] args, final int indexBegin, final int indexEnd) {
        return Arrays.copyOfRange(args, indexBegin, indexEnd);
    }
    From this, you can input your args, set the indexBegin as 3 and set the indexEnd as the length of the args. All you have to do is convert your new String[] into a String :).
     
  3. Offline

    HeadGam3z

    Just to let you know... This code:
    Code:
     for(Player warn : Bukkit.getServer().getOnlinePlayers())
                        {
                            if(warn.getName().equalsIgnoreCase(args[0]))
                            {
                                if(args.length == 2)
                                {
                                    if(args[1].equalsIgnoreCase("1"))
                                    {
                                     
                                    }
                                    else if(args[1].equalsIgnoreCase("2"))
                                    {
                                     
                                    }
                                    else if(args[1].equalsIgnoreCase("3"))
                                    {
                                     
                                    }
                                    else
                                    {
                                        p.playSound(p.getLocation(), Sound.CAT_HISS, 1, 2);
                                        p.sendMessage(ChatColor.DARK_RED + "Invalid Syntax </warn [player] [times warned] [reason]");
                                    }
                                }
                                else
                                {
                                    p.playSound(p.getLocation(), Sound.CAT_HISS, 1, 2);
                                    p.sendMessage(ChatColor.DARK_RED + "Invalid Syntax </warn [player] [times warned] [reason]");
                                }
                            }
                            else
                            {
                                p.playSound(p.getLocation(), Sound.CAT_HISS, 1, 2);
                                p.sendMessage(ChatColor.DARK_RED + "Invalid Syntax </warn [player] [times warned] [reason]");
                            }
                        }
    Will never run if the arguments are not equal to one. Your nested if (args.length == x)s are screwing you over.

    Edit: Actually, it will never run.
     
  4. Offline

    Skionz

    @JarFile Use the >= operator instead of the == operator.
     
  5. Offline

    JarFile

    @DJSkepter Gives me lots of errors when I put that into the code...
     
  6. Offline

    gal0511Dev

    I use this in my banning plugin:
    Code:
    StringBuilder str = new StringBuilder();
                      for (int i = 3; i < args.length; i++) {
                        str.append(args[i] + " ");
                      }
                      String reason= str.toString();
     
  7. Offline

    1Rogue

    You kind of defeated the entire purpose of using a stringbuilder if you're still concatenating the strings in the loop:

    Code:java
    1. //Assuming bounds checks have already been done
    2. StringBuilder sb = new StringBuilder(args[2]); //including the first word
    3. for (int i = 3; i < args.length; i++) {
    4. sb.append(' ').append(args[i]); //Append a space, then the word
    5. }
    6. String reason = sb.toString();[/i]
     
Thread Status:
Not open for further replies.

Share This Page