[LIB] Annotation based command system

Discussion in 'Resources' started by Minnymin3, Nov 16, 2013.

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

    Minnymin3

    I created a very lightweight annotation based command system that works similar to Bukkit's events system. It removes the necessity to add the command to your plugin.yml but will still allow you to set descriptions and usages through code.

    UPDATE:
    • Added inGameOnly option to command
    • Made commands automatically set executor
    • Added length() and getArgs(int index) to CommandArgs
    • Changed package declaration to com.minnymin.command
    Code on Github https://github.com/minnymin3/CommandFramework

    Single class Gist https://gist.github.com/minnymin3/8114618

    Example:
    Code:java
    1.  
    2. public class DemoPlugin extends JavaPlugin {
    3.  
    4. /** * Your plugin's command framework object. */
    5. CommandFramework framework;
    6.  
    7. public void onEnable() {
    8. /**Initializes a new CommandFramework object**/
    9. framework = new CommandFramework(this);
    10.  
    11. /** This will register all commands inside of this class. It works much the same as the registerEvents() method. Note: Commands do not need to be registered in plugin.yml! */
    12. framework.registerCommands(this);
    13. }
    14.  
    15. @Command(name = "test", aliases = { "testing" }, description = "This is a test command", usage = "This is how you use it")
    16. public void test(CommandArgs args) {
    17. args.getSender().sendMessage("This is a test command");
    18. }
    19. }
    20.  

    This example will create a command called test and register it. It will set the description and usage of the command as well. Notice the aliases option. Aliases will be registered the same way as the regular command and are alternate commands that the method will be invoked with. Here is an example of a sub command:
    Code:java
    1.  
    2. @Command(name = "test.sub", aliases = { "test.subcommand"})
    3. public void testSub(CommandArgs args) {
    4. args.getSender().sendMessage("This is a test subcommand");
    5. }
    6.  

    This will create a sub command of test and will be executed when someone sends the command '/test sub' or '/test subcommand'. Descriptions and usages also work with these.

    Commands may also have permissions set. The command handler will check if the player has permission and if they do not then it will send a message that can also be defined with the annotation. The default no permission message is "You do not have permission to perform that action"
    Code:java
    1.  
    2. @Command(name = "test.sub", aliases = { "test.subcommand"}, permission = "testplugin.subcommand", noPerm = "This is a modified no perm message.")
    3. public void testSub(CommandArgs args) {
    4. args.getSender().sendMessage("This is a test subcommand");
    5. }
    6.  

    You can also create tab completers with an annotation! Same naming conventions apply as the command annotation.
    Code:java
    1.  
    2. @Completer(name = "test", aliases = { "testing" })
    3. public List<String> testCompleter(CommandArgs args) {
    4. List<String> list = new ArrayList<String>();
    5. list.add("Hello");
    6. return list;
    7. }
    8.  
     
  2. Offline

    xTrollxDudex

    Minnymin3
    Might use this sometime. The problem is if you happen to handle multiple commands in a single class, then you're stuck with a bunch of commands with the same permission ;)
     
  3. Offline

    Minnymin3

    I don't follow... The permissions are set per annotation so '/test subcommand' could easily have a different permission than '/test' or '/testing'.
     
  4. Offline

    BungeeTheCookie

    Minnymin3
    You are a boss. I will totally use this sometime.
     
    Minnymin3 likes this.
  5. Offline

    Minnymin3

    Added a TabCompleter annotation! TabCompleters are registered the same way as the regular commands. They must return a List<String> and must also have a single CommandArgs argument.

    Also added a registration for help to be executed after all commands are registered. Add framework.registerHelp() to make your commands appear in your plugin's help ('/help (pluginname)') when commands are not registered in plugin.yml

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  6. Offline

    Ad237

    Minnymin3 This is a very good idea! Will be using this in the future. Do I need all the classes for this work or are they optional features?
     
  7. Offline

    2016mfransen

    Wow beautiful I know what I'm going to use! are we allowed to embed it?
     
  8. Offline

    Minnymin3

    Ad237
    You do in-fact need all the classes
    2016mfransen
    Go right ahead :) (though if its open source the info on the source files should be left)
     
  9. Offline

    Ad237

  10. Offline

    SoThatsIt

    how does this handle sub commands? like if i have a command "test" and then the sub "test.sub" if i call the test sub will it also call the test command or will it only call test.sub?
     
  11. Offline

    Minnymin3

    It will call test.sub only if you execute command /test with no arguments or unhandled arguments will it call test
     
  12. Offline

    SoThatsIt

    ok, awesome! definitely going to use this :D

    another question, if there is no command test.sub2 but someone does /test sub2 will it go to the test command or do nothing?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  13. Offline

    Minnymin3

    If there is no test.sub2 then it will go to test but if you do test.sub.sub2 and there is test.sub it will go to test.sub
     
  14. Offline

    bobacadodl

  15. Offline

    Minnymin3

  16. Offline

    xigsag

    bobacadodl How exactly would I go about using that minimalised version of minnymin's Library?

    Preferably a detailed documentation like what minnymin did :L
     
  17. Offline

    Minnymin3

    You throw the class into your project somewhere and then you do the same as how I explained and then press control + shift + o to import the correct imports.
     
  18. Offline

    xigsag

    Minnymin3 Theres CommandFramework in your example. What's that in boba's example?
     
  19. Offline

    Minnymin3

    Oh he called it quickcommand
     
  20. Offline

    xigsag

    Minnymin3 Oh I see, thanks!

    Minnymin3 One more question, how do I instantiate QuickCommand? I don't think I'm supposed to be changing it to a static class.

    #Edit: Actually, I fixed it. Thanks anyways.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  21. Offline

    Minnymin3

    bobacadodl likes this.
  22. Offline

    DevRosemberg

    Wow man, best util ive seen till now on Bukkit forums, edited it a bit to fit my needs in a plugin i hope to be releasing soon. Regards, DevRo!
     
  23. Offline

    xigsag

    I got a NullPointerException when running it though, at line 43 of CommandFramework.
     
  24. Offline

    Minnymin3

    Are you initializing the command framework in your onEnable? If not then that is probably the issue.
     
  25. Offline

    xigsag

    Do I initialize it via CommandFramework framework = null; then framework = new CommandFramework(this)?

    Ack, I knew I should've done that.
     
  26. Offline

    mba2012

    Wow, this is great :D

    One problem though, I've been trying to get commands working in external classes, but it isn't working :/ Is there any chance you could create a quick guide on how to do so?
     
  27. Offline

    xTrollxDudex

  28. Offline

    mba2012

    Problem is, it isn't working. I think the issue isn't with command registering though.

    I'm getting a NPE error when the constructor for the CommandFramework class is called.
     
  29. Offline

    xTrollxDudex

    mba2012
    Are you sure you did not pass a null referant to the CommandFramework?
     
  30. Offline

    mba2012

    Fairly sure.

    I've found exactly where it's giving an NPE.
    Code:java
    1. if (plugin.getServer().getPluginManager() instanceof SimplePluginManager) {

    I've passed in what I believe to be a valid plugin value, because it works for events etc.
     
Thread Status:
Not open for further replies.

Share This Page