How to add subcommands ?

Discussion in 'Plugin Development' started by Hannobal, Jul 14, 2012.

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

    Hannobal

    Hello everyone,

    I am programming a new plugin and I want to add some commands + subcommands. I already searched very much for this in YouTube and this Forum, but no solution.

    For example: One command should be easily "/pg", this should return me a help message (for example) and now I want to add the subcommand (or argument, I don't know) "list" and it should show me a full list of players (for example). So the full command is "/pg list" or maybe "/pg info" or something like that.

    The question is how can I add one or more subcommands (like "list" or "info") ?

    Can you give me the Java Code please?

    I think the solution is so easy that it is too difficult for me -_-

    Thanks for your help,
    Greetings Hannobal
     
  2. Offline

    ZeusAllMighty11

    I literally just learned how this was the other day. It's still a little confusing to me, so I hope I don't confuse you in the process of me helping you.


    Ok, so to make the command base you do the basic onCommand:
    PHP:
    public boolean onCommand(CommandSender senderCommand cmdString labelString[] args){
    Player s = (Player)sender;
    if(
    cmd.getName().equalsIgnoreCase("yourcommand")){
    Now if you wanted to make a command like: /houses add player:
    PHP:
    public boolean onCommand(CommandSender senderCommand cmdString labelString[] args){
    Player s = (Player)sender// defines player sender as s
    if(cmd.getName().equalsIgnoreCase("houses")){ // command label
    if (instanceof Player){ // instance of player - checks if player
    if (args.length == || args.length 2){ // checks if /houses was typed or /houses add ONLY
                                 
    s.sendMessage("Incorrect usage"); // This means the player didn't have enough args
    if (args[0].equalsIgnoreCase("add") || args[0].equalsIgnoreCase("remove")){ // checks if /houses ADD was typed or /houses REMOVE
    if (args[0].equalsIgnoreCase("add") && args.length == 2){ // if it was indeed add
                                    //code to add player to house whether it be tp or kill etc
                             
    if (args[0].equalsIgnoreCase("remove") && args.length == 2){ // if it wasnt add and it was remove
                                      //code to remove player from houses or whatever
    I'd reccomend learning about args.length in some java tuts

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
    Hannobal likes this.
  3. Offline

    Sagacious_Zed Bukkit Docs

  4. Offline

    EnvisionRed

    Well thanks. I was wondering why you did that
     
  5. Offline

    Hannobal

    Cool thanks Zeus ! :)

    So I unterstand your code in this way:
    The command base "houses" isn't a part from the args, it is simply seperate. The first part of the args is the subcommand "add" or "remove".

    Am I right? :)

    This code helps me very much, Thanks again :D
     
  6. Offline

    lololmaker

    Yes,
    the base command (etc. /compare) is not in args, everything after that (etc. /compare one two) is in args[] --starting with args[0]
     
  7. Offline

    Hannobal

  8. Offline

    Tempelchat

    I use the CommandExecutor because I like the fact that I can organize all command related things in an own class.
     
  9. Offline

    Hannobal

    Yes but like I unterstand this the CommandExecuter is only useful if you have more than one base command like "/houses" or "/compare", right?
     
  10. Offline

    Tempelchat

    If your command handling is very complex, it could be clearer than having all the handling in your plugin-class. Additionally you can do multiple base-commands in separate CommandExecutors or handle all in one executor - doesn't matter. I think it depends on you preference.
     
  11. Offline

    lololmaker

    A tip, if you use only one command, you can skip checking which command was it, save about a line of code :D
    For example, game plugin I'm currently working on is AholaGames (Ahola prefix is for all plugins, from server name). It uses one registered generic command /ag and everything else is in arguments. For example /ag start /ag stop. This is good mainly for the fact that it's almost impossible that it would ever conflict with other plugins.
     
  12. Offline

    Hannobal

    Okay, thanks you all very much again :)

    Greetings Hannobal
     
  13. Offline

    Sagacious_Zed Bukkit Docs

    I just made a new commit for one possible use of giving each subcommand it's own commandExecutor.
     
  14. Offline

    Hannobal

    Okay and where I can follow / see this commit?
     
  15. Offline

    Sagacious_Zed Bukkit Docs

  16. Offline

    Hannobal

    Ahh, sorry my fail^^
    I thought you did it in bukkit repository, not in yours :D
     
Thread Status:
Not open for further replies.

Share This Page