MethCmd [Lib] [API 3.0]

There once was the question, how Sponge’s command API should look like, and someone mentioned an annotation based API. A cool idea but it wasn’t used, due to it’s limited possibilities.

But if someone still need’s very simple but still quite powerful commands, i present to you:


MethCmd

(don’t know if final name xD - suggestions?)

I initially made this, while working on my own private plugin. But i thought i could share it here :slightly_smiling:

How simple is it?

If you need a command like /info that does nothing more than showing some text? There you go:

@Command(label="main", permission="a.permission")
public String mainCmd(){
    return "I am useless!";
}

Result:

How do i add arguments?

Just add them: :wink:

@Command(label="main sub", permission="another.permission", description="Does something...")
public Text onTest(String word){
    return Text.of(TextColors.GREEN, "I am " + word + "!");
}

You can always use parameters of common classes like:
String, all primitive types and their wrapper-classes, Player, User
even every Enum-type and Sponges CatalogType instances are working!

If you use CommandSource as a method parameter, the command’s source will be injected there! (It won’t add an argument to the actual command)

So you can simply create commands like:

@Command(label="main sub", permission="another.permission", description="Does something...", allowedSources=Player.class)
public Text onTest(CommandSource src, TextColor color, String word){
    return Text.of(color, src.getName(), " is ", word, "!");
}

Result:

What, if someone uses the command wrong?

Command usages and error messages like Too many Arguments! or ‘TBlueeeeF’ isn’t an online player! are generated automatically!

It looks like this:

API Setup:

If you are using it as a plugin-dependency, this should explain it quickly:

@Listener
public void onInit(GameInitializationEvent evt){
        
    //get the MethodCommandService instance
    Optional<MethodCommandService> optService = Sponge.getServiceManager().provide(MethodCommandService.class);
    if (!optService.isPresent()){
        logger.error("MethodCommandService not available! Is MethCmd installed?");
        return;
    }
        
    //Register extra parsers
    optService.get().addArgumentParser(this, new MySpecialArgument());
        
    //Register all commands within an object
    optService.get().registerCommands(this, someObjectWithCommandsInIt);
        
}


If you however just want to import it as library into your plugins class-path without it being an annoying external plugin (i did not test this):

MethodCommandService service = new CommandHandler(yourPluginInstance);
should do the trick.

More Questions:

Is it possible to create optional arguments?
Yep, you can use @Nullable on a parameter (i’ll be null if not found) or this annotation: @Default("10") and the string-value of that annotation will be optionally used to parse the command!

What about TabSuggestions?
Working fine! :slight_smile:

How are the arguments named in the usage-text?
If the methods parameter has a @Label("foo") annotation, this value is used…
otherwise the paramers name is used (if present, this depends on how you compile your project)
otherwise the name of the parameter-type (this is mostly the parameters class name) is used.

Is using VarArgs possible?
Yes it is! Just try it:
public void tpToSource(CommandSource to, Player... players)

Can i add my own classes to use them as arguments?
Sure! There is an interface called ArgumentParser. And it’s not hard to implement! Most methods are default anyways :smiley:

Those error messages are stupid, can i change/translate them?
English isn’t my first language, so apologize any weird grammar/word usage =)
If there is some bad sentence, just add an issue on GitHub or post it in this thread!
Also: i am planning to add the possibility to change those messages via config or through the API.

Wiki?

Working on it atm…
Here is the Wiki.

Code?

Here is the GitHub Repo.

Download?

The current build is compiled with the SpongeAPI 3.0.1 and tested with SpongeForge 1014
I really don’t know how to use GitHub or Gradle correctly but this should work: Link to some GitHub-Release

Finally…

This is the first time i release some form of plugin.
So please tell me what you think about it, and if there is something you think i could improve!

3 Likes

This would be best suited in the Plugins/Resources category right?

Edit: yep, its moved

2 Likes

I have to admit, this is pretty awesome looking…