Abbreviated Commands Tutorial (Organization).

Discussion in 'Resources' started by TACTICALboom, Feb 25, 2012.

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

    TACTICALboom

    Hello Bukkit.org learners! As you may know, having abbreviations for the commands in your plugins is a good thing to do when working with plugins that have similar if not the same command prefix, or it could be used as a way to of giving your plugin identity, either way really.

    Requirements for this tutorial:
    • A basic knowledge of Java(you may be able to complete this without, but you wont understand)
    • A basic knowledge of the Bukkit API
    • 2 Large Popsicle sticks.
    Normally, as you should know if you've read any other tutorial or worked with Bukkit at all, the standard method of command register in the main class would be (after you have made 'exename' an object instance of your executor class.)
    Code:Java
    1. getCommand("acommandname").setExecutor(exename);

    Well the way the getCommand method works, basically, it checks input against the supplied input, if it matches it will send that strings arguments to the the class object you created. You can then respond based on arguments.

    Lets get right into the important stuff:
    NOTE: Remember, this is the way I do it, some of this is personal preference.

    So first off, in your main class, you only have to register 1, yes 1, command.
    Everything else will become an argument. That's the idea.
    This tutorial is more about good organization, rather then new concepts.

    1) Make a main class.
    2) Make a sub-package of that class with '.command' on the end of it.
    3) In that new package, create a 'CommandCore' class.
    4) Also in that new package create a 'basic' class.

    In your main class, where you define your variables:
    Code:Java
    1. private CommandCore cmdc;


    In your onEnable():
    Code:Java
    1. getCommand("basic").setExecutor(cmdc);


    Now, head over to your CommandCore class.
    Create your constructor
    Code:Java
    1. public CommandCore(Main main) {
    2. // TODO Auto-generated constructor stub
    3. }


    Of course make your onCommand as usual:
    Code:Java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {


    Wait, wait, wait! slow down, go back to the top of the class, right after the class declaration, and define the the other class in your command package.
    Code:Java
    1. private Basic bsc = new Basic();


    Now head over to your Basic class.
    Only make boolean methods in your 'action' classes.
    NOTE: CommandCore will point to actions based on args[].
    In Basic.java
    Code:Java
    1. public boolean welcome(CommandSender sender, Command cmd, String label, String[] args)
    2. {
    3. sender.sendMessage(ChatColor.DARK_AQUA + "Welcome");
    4. return true;
    5. }


    Okay, so hold on here a moment. (Ignore the main class for the moment, as there is nothing left to be written in it) we have a CommandCore class with a basic onCommand, so whenever the text "basic" is entered as a command, it will use the CommandCore executor. We don't need to check what the player entered because if it was sent to this executor, the command had to have been "basic" right? So the only thing we have to look for is arguments! yay! And because we have an object instance of the "basic" class, which holds actual command functions, we can access its methods.
    So, if we want "Welcome" to display if the player dosen't enter any arguments we could( in CommandCore)
    Code:Java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    2. {
    3. if(args.length == 0)
    4. {
    5. return bsc.welcome(sender , cmd , label, args);
    6. }
    7. }

    That will execute the code within the 'welcome' method in the 'basic' class AND return its boolean value. If you wanted to go more in in depth you could make another class, in the command package, called 'greet'.
    within hello you could say
    Code:Java
    1. public boolean greet(CommandSender sender, Command cmd, String label, String[] args)
    2. {
    3. if(args.length == 1)
    4. {
    5. sender.sendMessage("Please specify kind of greeting");
    6. return true;
    7. }
    8. else if(args[1].equalsIgnoreCase("hi"))
    9. {
    10. sender.sendMessage("Hi, yourself good sir.");
    11. return true;
    12. }
    13. else if(args[1].equalsIgnoreCase("sup"))
    14. {
    15. sender.sendMessage("Sup Brah! Ya feel me?");
    16. return true;
    17. }
    18. else
    19. {
    20. sender.sendMessage("I don't Recognize that one...");
    21. return true;
    22. }


    So now you will be able to (in CommandCore)
    at the top
    Code:Java
    1. private greet gt = new greet();

    and in onCommand
    Code:Java
    1.  
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    3. {
    4. if(args.length == 0)
    5. {
    6. return bsc.welcome(sender , cmd , label , args);
    7. }
    8. else if(args[0].equalsIgnoreCase("hello"))
    9. {
    10. return gt.greet(sender , cmd , label , args);
    11. } //All the other arguments are sent to these classes, so they too, must use the same
    12. //params. You can think of it as extending the onCommand method.
    13. }

    Another thing that could be useful for users:
    in the plugin.yml
    Code:
    name: meh
    main: me.package.main.Main
    version: 1.0
    commands:
       Basic:
          aliases: [ b, bsc ]
          description: Shows welcome and instruction
          usage: /b <command>
    
    The player will then only have to use '/b' as their prefix, because we have set 'basic's alias to 'b' or
    'bsc'.

    Here is an example of this method being used in a more fleshed out manner (Sorry for not giving any context, but this plugin is still in developement):
    Code:java
    1. package me.sajo.militaris.command;
    2.  
    3. import me.sajo.militaris.Militaris;
    4. import me.sajo.militaris.command.data.MilitarisChat;
    5. import me.sajo.militaris.command.data.MilitarisIO;
    6. import me.sajo.militaris.command.data.MilitarisIndex;
    7. import me.sajo.militaris.command.data.MilitarisMil;
    8. import me.sajo.militaris.command.data.MilitarisPayRank;
    9. import me.sajo.militaris.command.data.MilitarisRand;
    10. import me.sajo.militaris.command.data.MilitarisRank;
    11. import me.sajo.militaris.command.data.MilitarisRankE;
    12. import org.bukkit.command.Command;
    13. import org.bukkit.command.CommandExecutor;
    14. import org.bukkit.command.CommandSender;
    15. import org.bukkit.entity.Player;
    16. @SuppressWarnings("unused")
    17. public class CommandCore implements CommandExecutor
    18. {
    19. //private MilitarisPayCommands mpc = new MilitarisPayCommands();
    20. private MilitarisIndex index = new MilitarisIndex();
    21. private MilitarisChat chat = new MilitarisChat();
    22. private MilitarisPayRank payrank = new MilitarisPayRank();
    23. private MilitarisRank rank = new MilitarisRank();
    24. private MilitarisRankE rankE = new MilitarisRankE();
    25. private MilitarisRand rand = new MilitarisRand();
    26. private MilitarisMil Mili = new MilitarisMil();
    27. private MilitarisIO milio = new MilitarisIO();
    28.  
    29. public CommandCore(Militaris militaris) {
    30. // TODO Auto-generated constructor stub
    31. }
    32.  
    33. @Override
    34. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    35. {
    36. if(sender instanceof Player == false)
    37. {
    38. sender.sendMessage("You must be a player!");
    39. return true;
    40. }
    41. else
    42. {
    43.  
    44. if((args.length < 1))
    45. {
    46. //if here there was no args.
    47. return Mili.Mil(sender, cmd, label, args);
    48. }
    49.  
    50. if(args[0].equalsIgnoreCase("chat"))
    51. {
    52. return chat.milchat(sender, cmd, label, args);
    53. }
    54. if(args[0].equalsIgnoreCase("reload"))
    55. {
    56. return milio.milreload(sender, cmd, label, args);
    57. }
    58. if(args[0].equalsIgnoreCase("save"))
    59. {
    60. return milio.milsave(sender, cmd, label, args);
    61. }
    62. if(args[0].equalsIgnoreCase("index"))
    63. {
    64. return index.index(sender, cmd, label, args);
    65. }
    66. if(args[0].equalsIgnoreCase("ranks"))
    67. {
    68. return rank.milranks(sender, cmd, label, args);
    69. }
    70. if(args[0].equalsIgnoreCase("setrank"))
    71. {
    72. return rank.setrank(sender, cmd, label, args);
    73. }
    74. if(args[0].equalsIgnoreCase("promote"))
    75. {
    76. return rankE.promo(sender, cmd, label, args);
    77. }
    78. if(args[0].equalsIgnoreCase("demote"))
    79. {
    80. return rankE.dmote(sender, cmd, label, args);
    81. }
    82.  
    83. }
    84.  
    85. return false;
    86. }
    87.  
    88. }
    89.  


    So, maybe that tutorial meant something to you, maybe it didn't. Feel free to express your own ideas about how you would have done it, I enjoy hearing them.
     
    Last edited: Jan 19, 2015
    acuddlyheadcrab and fromgate like this.
  2. Offline

    nikpmup

    great tutorial! I cant believe no one commented on this!
     
Thread Status:
Not open for further replies.

Share This Page