Commands

Find out how to create a Command using OneConfig

Getting Started

To create a new Command, you need to use the @Command(value = "commandName", decription = "description") annotation for a class, like this:

@Command(value = ExampleMod.MODID, description = "Just a simple command")
public class ExampleCommand {

Simple! Now, you will have a command like /examplemod, but the command doesn't do anything yet, lets change that. To create a function for a command, we use the @Main annotation, like this:

    @Main
    // ps. this can't have any arguments, sorry :(
    private void main() {
      System.out.println("Hello, World!");
    }

Awesome! now /examplemod will print in the console "Hello, World!".

But first, we need to register our command, so OneConfig knows it exists. Just add this to your main class's init method:

    // snip!
    CommandManager.register(new ExampleCommand());
    // some more code...
}

Sub Commands

But what about sub commands? They are easy too! We just have to make a new method using the @SubCommand(description = "description") annotation, and then do the same thing as above, like this:

Sub Command Groups

Imagine a place where you could have sub commands in groups, like /hello ping pong and /hello ping pingpong. Well, you can! Just create a new nested or inner class, like this:

Using Arguments

Using arguments is simple too! You just add them as normal arguments to a method, like normal Java. Here are some examples demonstrating features of the arguments:

So, this quick demo hopefully has enlightened you on all the OneConfig command system has to offer. It has a much nicer syntax than brigadier or legacy command handling, and is (almost) just as feature rich! Here is a quick recap:

  • Any method will be added as its name if annotated with @SubCommand

  • Classes or methods can be of any visibility, and static or non-static

  • Must be registered using CommandManager.register(new MyCommand());

  • Descriptions and aliases are displayed in help messages, and it is recommended you describe what each command does, and its arguments, even if it is just naming them.

Last updated

Was this helpful?