[Library] CommandMethod, a smarter way to write commands

Discussion in 'Resources' started by Ranzdo, Oct 7, 2012.

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

    Ranzdo

    During the time I have developed plugins for Bukkit the most boring thing was to program all the verifiers, transformers etc for commands. Often you would end up with 80% of the method in user verification code. As a perfectionist in code structure, I cried everytime I copied and pasted those methods into every single if clause/onCommand method.

    After a while I got the idea to use annonations. This idea has already been implmented though, WorldGuard has their commands annonated what usage they have etc but they still got that String[] with all the arguments that needed to be verified for each command.

    However, I got also the idea to annonate the parameters to set rules what needed to be verified. This is what I came up with.

    https://github.com/Ranzdo/MethodCommand

    You can download the complete jar at: http://ci.ranzdo.se/job/MethodCommand/lastSuccessfulBuild/

    This library focus on these ideas:
    • Reduces the amount of argument validation code you have to write, so you only need to write the code that the command actually does.
    • Implements an easy to use flag system.
    • Makes it easier to code sub commands (/root sub)
    • Makes it easier to code help messages.
    A simple example:
    Code:
    @Command(identifier="send", description="Sends a message to a player")
    public void sendMessageToPlayerCommand(
    Player sender,
    [USER=58724]Arg[/USER](name="send_to") Player player,
    [USER=15093]WildCard[/USER] [USER=58724]Arg[/USER](name="message") String message
    ){
    player.sendMessage(message);
    }
    
    //Can somone tell me how to not tag people in the code example...

    So, how do you use it? Since it is a library you need to include it in your plugin yourself the same way you include bukkit, mysql-drivers etc. I have started to write the documentation on https://github.com/Ranzdo/MethodCommand/wikibut it is far from done.

    The library is still in "beta" phase since I have not tested it so much yet, but in those project that I have used it in it has atleast been useful and worked for me.

    Since I don't got alot of time to spend on this project anymore because of work/studies anyone who feels like they want to expand this library more contact me on skype (skypename:ghostpost) or make a pull request on github.
     
    Redpath, mkremins, Minecrell and 2 others like this.
  2. O my god, i just love this.
    Gonna use this in my plugin as soon as i have vacation, or maybe even before that:p
     
  3. Offline

    coaster3000

    Best library I have seen.

    You need to teach me reflection / Annotations some time xD

    I must learn it. It would help my plugins so much.
     
  4. Offline

    EDawg878

    Wow, amazing job. I love this library!

    Question: How do I register a command from a different class other than the main?

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

    Ranzdo

    CommandHandler.registerCommand() takes an object as an parameter. That parameter can be whatever you want and it will only register the methods that is annotated with Command. So for example:

    JavaPlugin class
    Code:java
    1.  
    2. private CommandHandler commandHandler;
    3. public void onEnable() {
    4. commandHandler = new CommandHandler();
    5. A a = new A();
    6. commandHandler.registerCommands(a);
    7. }
    8.  


    A class
    Code:java
    1.  
    2. @Command(identifier="command")
    3. public void command1(
    4. Player sender,
    5. [USER=58724]Arg[/USER](name="string") String arg1
    6. ){
    7. //Do a command here.
    8. }
    9.  
     
  6. Offline

    Ranzdo

  7. Offline

    coaster3000


    What is
    Code:java
    1.  
    2. [USER=58724]Arg[/USER](name="string") String arg1


    It makes no sense. I know annotations but that does not look like an annotation.. It confused me at first when i saw thread without the wiki... Just prob might be a good question for people not looking at wiki but looking here... I know how to use the library now But that syntax is little confusing to me?
     
  8. Offline

    Ranzdo

    There is a bukkit user named Arg and when I try to write Arg I tag him... the BBCODE automatically generates and I don't know how to stop it.
     
  9. Offline

    Hydrosis

    On the documentation, where it says " /test [one_string]", does that mean the player has to use the brackets to use the command?
     
  10. Offline

    EDawg878

    No, that is simply the name of the argument.
     
    Hydrosis likes this.
  11. Offline

    EDawg878

    Could you create a plugin version of this library so all my plugins that depend on it can just hook into it instead of including it in each plugin?
     
  12. Offline

    IDragonfire

    Agree, also it gives you new possibilities, like better help pages ...
     
  13. Offline

    smith_61

    I added the ability for it to be loaded as a plugin. And it will register commands if they don't exist in the plugin.yml. I am waiting for the pull request to be accepted, but if you want to try it out you can get it here.
    https://github.com/smith61/MethodCommand
     
  14. Offline

    Minecrell

    I really like this, thank you :p
     
  15. Offline

    AmoebaMan

    I've seen a few of these around, and I've got my own that fits snugly in a single class, but your is by far the most comprehensive I've seen, particularly with respect to arguments.

    The only thing you're really missing is support for subcommands, and the only problems I have with are that...
    1. It's an entire plugin that I'd have to depend on and force my users to download.
    2. It's a bit more complex than it could be.
    Besides that, brilliant work!
     
  16. Offline

    Ranzdo

    Thank you for your feedback!

    The library does support subcommand acually, just add it in the identifier seperated by space. For example: identifier="mycommand mysubcommand" .

    This is not a plugin, it is a library and you can include the library in your project by adding the jar (downloadable at http://ci.ranzdo.se/job/MethodCommand/22/) to your class-path (like the bukkit.jar) and then check it at "Order & Export" if you have eclipse. It will then be included in your plugin jar when you export your plugin in eclipse.
     
  17. Ranzdo

    Hey, I found this and think it would be very useful, that is, if I could get it working. I have added the Jar as a library in eclipse and configured everything correctly but when I use the @Command annotation I get an error.

    Show Spoiler

    My code is:
    Show Spoiler

    @Command (
    identifier = "root sub1 sub2",
    description = "test",
    permission = { "test.perm1.perm2" },
    onlyPlayers = true
    )
    public void Root_sub1_sub2(
    Player sender
    ){

    }

    the error is:
    Type mismatch: cannot convert from Command to Annotation

    Any help would be great!
     
  18. Offline

    Melair

    Ranzdo this looks to be an awesome library which I'd like to use, but it appears your Jenkins and Maven repo are currently down. I can fudge something on my end, but it'd be handy to get them back!
     
Thread Status:
Not open for further replies.

Share This Page