multliple commands?

Discussion in 'Plugin Development' started by quack123456, Feb 16, 2015.

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

    quack123456

    Im really new to developing. So i made a plugin where you do /rules and list the rules. But i cant figure out how to make it so they can do /rules page 1 and /rules page 2 Here is le code
    Code:
    import java.util.logging.Logger;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Ducks extends JavaPlugin {
           
    public static final Logger Log = Logger.getLogger("Minecraft");
           
            public void onEnable(){
                Log.info(ChatColor.GREEN + "Our plugin has Loaded");
               
            }   
            public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
                if(cmd.getName().equalsIgnoreCase("Rules")){
                    sender.sendMessage(ChatColor.RED + " " + ChatColor.BOLD + " " + "Page 1 out of 4");
                    sender.sendMessage(ChatColor.DARK_RED +" " + ChatColor.BOLD + "Rules Please Follow them. If you don't it may result in a ban or mute." );
                    sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "1." + ChatColor.GREEN + " " + ChatColor.BOLD + "No Hacking" );
                    sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "2." + ChatColor.GREEN + " " + ChatColor.BOLD + "No Griefing the Server" );
                    sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "3." + ChatColor.GREEN + " " + ChatColor.BOLD + "No Spamming" );
                    sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "4." + ChatColor.GREEN + " " + ChatColor.BOLD + "No Vulguar Swearing" );
                    sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "5." + ChatColor.GREEN + " " + ChatColor.BOLD + "No Racism " );
                if (cmd.getName().equalsIgnoreCase("Rules Page 2"));   
                    sender.sendMessage(ChatColor.RED + " " + ChatColor.BOLD + " " + "Page 2 out of 4");
                    sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "6." + ChatColor.GREEN + " " + ChatColor.BOLD + "No IRL Scam. If you do you are banned from everything" );
                    sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "7." + ChatColor.GREEN + " " + ChatColor.BOLD + "No Charge Backing on purchases. If you do you are banned from everything" );
                    sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "8." + ChatColor.GREEN + " " + ChatColor.BOLD + "No advertising" );
                    sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "9." + ChatColor.GREEN + " " + ChatColor.BOLD + "No Staff disrespect" );
                    sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "10." + ChatColor.GREEN + " " + ChatColor.BOLD + "Be Nice to everyone" );
                if (cmd.getName().equalsIgnoreCase("Rules Page 3"));   
                    sender.sendMessage(ChatColor.RED + " " + ChatColor.BOLD + " " + "Page 3 out of 4");
                    sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "11." + ChatColor.GREEN + " " + ChatColor.BOLD + "No posting links" );
                    sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "12." + ChatColor.GREEN + " " + ChatColor.BOLD + "No Hacking" );
                    sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "13." + ChatColor.GREEN + " " + ChatColor.BOLD + "No DDODS threats" );
                    sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "14." + ChatColor.GREEN + " " + ChatColor.BOLD + "No advertising services" );
                    sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "15." + ChatColor.GREEN + " " + ChatColor.BOLD + "No Caps" );
                if (cmd.getName().equalsIgnoreCase("Rules Page 4"));
                    sender.sendMessage(ChatColor.RED + " " + ChatColor.BOLD + " " + "Page 4 out of 4");
                    sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "16." + ChatColor.GREEN + " " + ChatColor.BOLD + "No using Modded Clients" );
                    sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "17." + ChatColor.GREEN + " " + ChatColor.BOLD + "No Bypassing mutes" );
                    sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "18." + ChatColor.GREEN + " " + ChatColor.BOLD + "No Bypassing Bans" );
                    sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "19." + ChatColor.GREEN + " " + ChatColor.BOLD + "Don't say sexual stuff" );
           
           
            return false;
    
    
        }
    }
    
     
  2. Offline

    Skionz

    @quack123456 Use the array of arguments (args) passed to your method. This array represents the command's arguments. Here is an example

    "/stuff otherstuff cool"
    stuff is the command.
    otherstuff is the first argument.
    cool is the second argument
     
  3. Offline

    quack123456

    @Skionz im really new to developing, So what do i need to change
     
  4. Offline

    Skionz

  5. Offline

    Koobaczech

    if (cmd.getName().equalsIgnoreCase("Rules"))
    if (args.length > 1)
    {
    if (args[0].equalsIgnoreCase("Page"))
    {
    if (args[1].charAt(0)=='1')
    {
     
  6. Offline

    fandemangas42

    Code:
    cmd.getName()
    This returns the NAME of the command (So in this case : /rules)

    To get the arguments AFTER the command you can do :
    Code:
    args[index]
    Example, if you want to check if the first argument is "page", you can do :
    Code:
    if(args[0].equalsIgnoreCase("page")) {
        //Do some stuff...
    }
    Don't forget to check if the length of args is higher than 0 or you will get an error.

    This is the entire code you should use :

    Code:
    if(cmd.getName().equalsIgnoreCase("rules")) {
        if(args.length == 2) {
            if(args[0].equalsIgnoreCase("page")) {
                try {
                    int page = Integer.parseInt(args[1]);
                
                    //Do some stuff...
    
                    return true;
                }
                catch(NumberFormatException e) {
                    return false;
                }
            }
        }
    }
    The try catch verify if the second argument is a number, else it ends the method.
     
  7. Offline

    Vamure

  8. Offline

    quack123456

    @fandemangas42 @Skionz Like this?

    Code:
    package me.Kyle.Quack;
    
    import java.util.logging.Logger;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Ducks extends JavaPlugin {
       
    public static final Logger Log = Logger.getLogger("Minecraft");
       
        public void onEnable(){
            Log.info(ChatColor.GREEN + "Our plugin has Loaded");
           
        }   
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(cmd.getName().equalsIgnoreCase("rules")) {
                if(args.length == 2) {
                    if(args[0].equalsIgnoreCase("page")) {
                        try {
                            int page = Integer.parseInt(args[1]);
                      
                            sender.sendMessage(ChatColor.RED + " " + ChatColor.BOLD + " " + "Page 1 out of 4");
                            sender.sendMessage(ChatColor.DARK_RED +" " + ChatColor.BOLD + "Rules Please Follow them. If you don't it may  result in a ban or mute." );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "1." + ChatColor.GREEN + " " + ChatColor.BOLD + "No Hacking" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "2." + ChatColor.GREEN + " " + ChatColor.BOLD + "No Griefing the Server" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "3." + ChatColor.GREEN + " " +  ChatColor.BOLD + "No Spamming" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "4." + ChatColor.GREEN + " " + ChatColor.BOLD + "No Vulguar Swearing" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "5." + ChatColor.GREEN + " " + ChatColor.BOLD + "No Racism " );
                           
                            int page2 = Integer.parseInt(args[2]);
                            sender.sendMessage(ChatColor.RED + " " + ChatColor.BOLD + " " + "Page 2 out of 4");
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "6." + ChatColor.GREEN + " " + ChatColor.BOLD   + "No IRL Scam. If you do you are banned from everything" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "7." + ChatColor.GREEN + " " + ChatColor.BOLD + "No Charge Backing on purchases. If you do you are banned from everything" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "8." + ChatColor.GREEN + " " + ChatColor.BOLD + "No advertising" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "9." + ChatColor.GREEN + " " + ChatColor.BOLD + "No Staff disrespect" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "10." + ChatColor.GREEN + " " + ChatColor.BOLD + "Be Nice to everyone" );
                           
                            int page3 = Integer.parseInt(args[3]);
                            sender.sendMessage(ChatColor.RED + " " + ChatColor.BOLD + " " + "Page 3 out of 4");
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "11." + ChatColor.GREEN + " " +  ChatColor.BOLD + "No posting links" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "12." + ChatColor.GREEN + " " +   ChatColor.BOLD + "No Hacking" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "13." + ChatColor.GREEN + " " +  ChatColor.BOLD + "No DDODS threats" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "14." + ChatColor.GREEN + " " +  ChatColor.BOLD + "No advertising services" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "15." + ChatColor.GREEN + " " +  ChatColor.BOLD + "No Caps" );
                           
                            int page4 = Integer.parseInt(args[4]);
                            sender.sendMessage(ChatColor.RED + " " + ChatColor.BOLD + " " + "Page 4 out of 4");
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "16." + ChatColor.GREEN + " " +  ChatColor.BOLD + "No using Modded Clients" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "17." + ChatColor.GREEN + " " + ChatColor.BOLD + "No Bypassing mutes" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "18." + ChatColor.GREEN + " " + ChatColor.BOLD + "No Bypassing Bans" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "19." + ChatColor.GREEN + " " + ChatColor.BOLD + "Don't say sexual stuff" );   
                           
                            return true;
                        }
                        catch(NumberFormatException e) {
                            return false;
                        }
                    }
                }
            }
           
            return false;
    
    
        }
    }
    
     
  9. Offline

    Skionz

    @quack123456 If the length of the array is two then there will not be a third value and an ArrayIndexOutOfBoundsException will be thrown.
     
  10. Offline

    quack123456

    @Skionz so like this?

    Code:
    package me.Kyle.Quack;
    
    import java.util.logging.Logger;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Ducks extends JavaPlugin {
       
    public static final Logger Log = Logger.getLogger("Minecraft");
       
        public void onEnable(){
            Log.info(ChatColor.GREEN + "Our plugin has Loaded");
           
        }   
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(cmd.getName().equalsIgnoreCase("rules")) {
                if(args.length == 1) {
                    if(args[0].equalsIgnoreCase("page")) {
                        try {
                            int page = Integer.parseInt(args[1]);
                      
                            sender.sendMessage(ChatColor.RED + " " + ChatColor.BOLD + " " + "Page 1 out of 4");
                            sender.sendMessage(ChatColor.DARK_RED +" " + ChatColor.BOLD + "Rules Please Follow them. If you don't it may  result in a ban or mute." );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "1." + ChatColor.GREEN + " " + ChatColor.BOLD + "No Hacking" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "2." + ChatColor.GREEN + " " + ChatColor.BOLD + "No Griefing the Server" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "3." + ChatColor.GREEN + " " +  ChatColor.BOLD + "No Spamming" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "4." + ChatColor.GREEN + " " + ChatColor.BOLD + "No Vulguar Swearing" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "5." + ChatColor.GREEN + " " + ChatColor.BOLD + "No Racism " );
                           
                            int page2 = Integer.parseInt(args[2]);
                            sender.sendMessage(ChatColor.RED + " " + ChatColor.BOLD + " " + "Page 2 out of 4");
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "6." + ChatColor.GREEN + " " + ChatColor.BOLD   + "No IRL Scam. If you do you are banned from everything" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "7." + ChatColor.GREEN + " " + ChatColor.BOLD + "No Charge Backing on purchases. If you do you are banned from everything" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "8." + ChatColor.GREEN + " " + ChatColor.BOLD + "No advertising" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "9." + ChatColor.GREEN + " " + ChatColor.BOLD + "No Staff disrespect" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "10." + ChatColor.GREEN + " " + ChatColor.BOLD + "Be Nice to everyone" );
                           
                            int page3 = Integer.parseInt(args[3]);
                            sender.sendMessage(ChatColor.RED + " " + ChatColor.BOLD + " " + "Page 3 out of 4");
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "11." + ChatColor.GREEN + " " +  ChatColor.BOLD + "No posting links" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "12." + ChatColor.GREEN + " " +   ChatColor.BOLD + "No Hacking" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "13." + ChatColor.GREEN + " " +  ChatColor.BOLD + "No DDODS threats" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "14." + ChatColor.GREEN + " " +  ChatColor.BOLD + "No advertising services" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "15." + ChatColor.GREEN + " " +  ChatColor.BOLD + "No Caps" );
                           
                            int page4 = Integer.parseInt(args[4]);
                            sender.sendMessage(ChatColor.RED + " " + ChatColor.BOLD + " " + "Page 4 out of 4");
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "16." + ChatColor.GREEN + " " +  ChatColor.BOLD + "No using Modded Clients" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "17." + ChatColor.GREEN + " " + ChatColor.BOLD + "No Bypassing mutes" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "18." + ChatColor.GREEN + " " + ChatColor.BOLD + "No Bypassing Bans" );
                            sender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.BOLD + "19." + ChatColor.GREEN + " " + ChatColor.BOLD + "Don't say sexual stuff" );   
                           
                            return true;
                        }
                        catch(NumberFormatException e) {
                            return false;
                        }
                    }
                }
            }
           
            return false;
    
    
        }
    }
    
     
  11. Offline

    Skionz

    @quack123456 No. If there is only one value in the array then how would you get a second?
     
  12. Offline

    quack123456

  13. Invisible

    nverdier

    @quack123456 3 of what? You have to check that there are enough values in the array to get the value at index i, so you have to check that the length is i + 1.
     
  14. @quack123456 Your checking is args is one then you get 2, 3 and 4
     
  15. Offline

    SuperOriginal

  16. Offline

    quack123456

    @SuperOriginal i honestly dont care about a logger ill take it out if i want to. My problem is different. @bwfcwalshy @nverdier then what do i switch im honestly bad at this.
     
  17. Invisible

    nverdier

  18. Offline

    SuperOriginal

    If you don't know what to do then read the links I posted. Stop trying to ride a bike before you can walk to it.
     
Thread Status:
Not open for further replies.

Share This Page