Multiple Commands

Discussion in 'Plugin Development' started by JjPwN1, Aug 3, 2012.

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

    JjPwN1

    I am a new nub to the coding world of bukkit. I am having trouble adding multiple commands to my plugin. Here is my code:
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            Player player = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("specs"));
            player.sendMessage(ChatColor.GOLD + getConfig().getString("Server"));
            player.sendMessage(ChatColor.DARK_RED + getConfig().getString("IP"));
            player.sendMessage(ChatColor.DARK_RED + getConfig().getString("RAM"));
            player.sendMessage(ChatColor.DARK_RED + getConfig().getString("Processor"));
            player.sendMessage(ChatColor.DARK_RED + getConfig().getString("CPU"));
            return false;
        }
    }
    I'm not sure how I play out multiple commands, and when I try }else if(cmd.getName(). etc.. it returns errors on the "else" saying "syntax error on the token "else", { expected" but when I put a { at the end of the line it gives the same error.
     
  2. Offline

    TheSmallBones

    Make a whole new public Boolean onCommand and change onCommand to something different such as onCommand2.
     
  3. Offline

    travja

    You need to start the if block and also end it. So at the end of the if line put "{" and at the end of all the player messages put "}" That should fix it, if not, come back for help
     
  4. Offline

    JjPwN1

    travja
    Code:
        }else if(cmd.getName().equalsIgnoreCase("bbyspecs reload")){
        this.reloadConfig();}
            return false;
        }
    }
    I am making a reload command, is this how I'd do it?
     
  5. Offline

    travja

    Take the "}" off the end of your this.reloadConfig(); Then you should be good.
     
  6. Offline

    JjPwN1

    [​IMG]
    I'm getting these errors. The "else" says there's a { expected, the return false says Void methods cannot return a value, and the last bracket says a } is expected after token
     
  7. Offline

    travja

    Give me your whole onCommand.... Something is off....
     
  8. Offline

    sayaad

    Code:java
    1. }else{
    2. if(cmd.getName().equalsIgnoreCase("bbyspecs") && args.length > 0 && args[0].equalsIgnoreCase("reload")){
    3.  
    4. this.reloadConfig();
    5. return true;
    6. }
    7. }
     
    TheSmallBones likes this.
  9. Offline

    Firefly

    You can't do that unless you explicitly return onCommand2(blabla) in onCommand.
     
  10. Offline

    TheSmallBones

    I didn't understand what he actually wanted until he posted it awhile ago. I thought he was talking about something else :p
     
  11. Offline

    travja

    Is that EVERYTHING inside of your onCommand?
     
  12. Offline

    Sagacious_Zed Bukkit Docs

  13. Offline

    travja

  14. Offline

    Sagacious_Zed Bukkit Docs

    If you even have more than one command, it is much cleaner and easier to understand. No one wants to sort through a mess of if else statements.

    In the OP there is a mismatch pair of braces.
     
  15. Offline

    travja

    I understand. And I think he has a few too many braces yes, I just need to see his class to see where those are....
     
  16. Offline

    JjPwN1

    ALL of my code:
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            Player player = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("specs"));
            player.sendMessage(ChatColor.GOLD + getConfig().getString("Server"));
            player.sendMessage(ChatColor.DARK_RED + getConfig().getString("IP"));
            player.sendMessage(ChatColor.DARK_RED + getConfig().getString("RAM"));
            player.sendMessage(ChatColor.DARK_RED + getConfig().getString("Processor"));
            player.sendMessage(ChatColor.DARK_RED + getConfig().getString("CPU"));
        }else if(cmd.getName().equalsIgnoreCase("bbyspecs reload"))
            this.reloadConfig();
            return false;
        }
    }
     
  17. Offline

    travja

    Ok, you are not starting the first if block... so after your first if statement with the command "specs" instead of ";" use "{" and you should be fine! :D
     
  18. Offline

    joshua katz

  19. Offline

    azyhd

    Well ive seen you dont have a good answer yet, so i tought ill show you.
    Its better to make it like this:

    if(commandLabel.equalsIgnoreCase("specs"){
    player.sendMessage(ChatColor.GOLD + "Server: " + getConfig().getString("Server"));
    player.sendMessage(ChatColor.DARK_RED + "Ip: " + getConfig().getString("IP"));
    player.sendMessage(ChatColor.DARK_RED + "Ram: " + getConfig().getString("RAM"));
    player.sendMessage(ChatColor.DARK_RED + "Processor: " + getConfig().getString("Processor"));
    player.sendMessage(ChatColor.DARK_RED + "Cpu: " + getConfig().getString("CPU"));
    } else
    if(args[0].equalsIgnoreCase("reload"){
    this.reloadConfig();

    I hope this helps you pm me if you got more problems.:p

    Code:
        public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
            Player player = (Player) sender;
            Player tplayer = getServer().getPlayer(args[0]); // /specs [playername]
            if(commandLabel.equalsIgnoreCase("specs")){
                if(args.length== 0){
                    player.sendMessage(ChatColor.GOLD + "Server: " + getConfig().getString("Server"));
                    player.sendMessage(ChatColor.DARK_RED + "Ip: " + getConfig().getString("IP"));
                    player.sendMessage(ChatColor.DARK_RED + "Ram: " + getConfig().getString("RAM"));
                    player.sendMessage(ChatColor.DARK_RED + "Processor: " + getConfig().getString("Processor"));
                    player.sendMessage(ChatColor.DARK_RED + "Cpu: " + getConfig().getString("CPU"));
                   
                } else if(args.length== 1){
                    tplayer.sendMessage(ChatColor.GOLD + "Server: " + getConfig().getString("Server"));
                    tplayer.sendMessage(ChatColor.DARK_RED + "Ip: " + getConfig().getString("IP"));
                    tplayer.sendMessage(ChatColor.DARK_RED + "Ram: " + getConfig().getString("RAM"));
                    tplayer.sendMessage(ChatColor.DARK_RED + "Processor: " + getConfig().getString("Processor"));
                    tplayer.sendMessage(ChatColor.DARK_RED + "Cpu: " + getConfig().getString("CPU"));
                   
                } else    if(args[0].equalsIgnoreCase("reload")){
                    this.reloadConfig();
                   
                } else if(args[0].equalsIgnoreCase("something")){
                    // something
                   
                }
            if(commandLabel.equalsIgnoreCase("NewCommand")){
                // something
                }
            }
            return false;
        }
    }
     
    JjPwN1 likes this.
  20. Offline

    JjPwN1

    azyhd
    I was confused on how to put stuff like "RAM: " and etc before the string in config without the user of the plugin putting a "RAM- " them self. Thanks, this helped.
     
  21. Offline

    azyhd

    No problem and the "Ip:" will just show that it is the ip.
    The player gets the message:

    Ip: 111.11.11.111:25565
     
  22. Offline

    Firefly

    Multiple commands, my way!

    Code:
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
      //args length check
      String subCommand = args[0];
     
     
      if (subCommand.equalsIgnoreCase("derp") {
            return onDerpDerp(sender, command, label, args);
      } else if (subCommand.equalsIgnoreCase("herp) {
            return onDerpHerp(sender, command, label, args);
      }
    }
     
    public boolean onDerpDerp(CommandSender sender, Command command, String label, String[] args) {
        //do Stuff
    }
     
    public boolean onDerpHerp(CommandSender sender, Command command, String label, String[] args) {
        //do Stuff
    }
     
  23. Offline

    azyhd

    Youre using way too much lines for commands, while its possible to make them very small.
     
  24. Offline

    Firefly

    Actually, my way splits my commands up into multiple methods, making it much easier to maintain. I'd rather not stick my 50 line commands into one onCommand()
     
  25. Offline

    azyhd

    Multiple onCommands will make the plugin go less faster then just 1 onCommand
     
  26. Offline

    Firefly

    How so? Splitting one method into multiple ones is proper OOP practice.
     
  27. Offline

    ARNGCollins

    Firefly sorry to message you using a reply. But, I am working on a Multiple Commands for my plugin and ran into some problems. I am having trouble getting all 3 commands to work. Even though they are simple commands like:
    Check Version
    Heal
    Teleport

    If you have time to help. It would mean a lot. As I've been working on this for a while now and really have no idea what i did wrong. Thank you.
     
  28. Offline

    azyhd

    I'm just saying, what was told me.
     
  29. Offline

    Firefly

    May I take a look at the code?
     
Thread Status:
Not open for further replies.

Share This Page