Commands

Commands can be declared using the @Command annotation on a class:

@Command("foo")
public class Foo {}

Subcommands

You can add a subcommand to a command by annotating a public method with @Command. The annotated method will be invoked whenever the specified subcommand is executed.

@Command("foo")
public class Foo {
    @Command("bar")
    public void bar() {
        System.out.println("bar");
    }
}

This Foo.bar() is invoked when the /foo bar command is executed.

Default Commands

Default commands are commands that are run without the need for a subcommand. They are declared by annotating a public method with @Command.Default:

@Command("foo")
public class Foo {
    // Normal import
    // e.g. import net.jailgens.enchanted.annotations.Command;
    @Command.Default
    public void foo() {
        System.out.println("foo");
    }
    
    // Import inner class
    // e.g. import net.jailgens.enchanted.annotations.Command.Default;
    @Default
    public void foo() {
        System.out.println("foo");
    }
}

Both methods are invoked upon the execution of /foo.

You can combine default commands and subcommands:

@Command("foo")
public class Foo {
    // /foo
    @Command.Default
    public void foo() {
        System.out.println("foo");
    }
    
    // /foo bar
    @Command("bar")
    public void bar() {
        System.out.println("bar");
    }
    
    // /foo baz
    @Command("baz")
    public void baz() {
        System.out.println("baz");
    }
}

Command Executors

Not to be confused with org.bukkit.command.CommandExecutor.

Command executors are simply something that can execute a command. You can obtain it by adding a CommandExecutor parameter to any subcommand or default command. Some platforms such as Paper provide their own additional command executor's like org.bukkit.command.CommandSender.

@Command("foo")
public class Foo {
    // (Paper) anyone can execute /foo bar
    @Command("bar")
    public void bar(CommandSender sender) {}
    
    // (Paper) only players can execute /foo baz
    @Command("baz")
    public void baz(Player sender) {}
    
    // Anyone can execute this /foo qux, reguardless of implementation or platform
    @Command("qux")
    public void qux(CommandExecutor sender) {}
}

As shown in previous sections, if no command executor is specified, the subcommand or default command can be executed by any CommandExecutor.

Arguments

Command arguments are really easy to define. Simply append a parameter to the end of your command method:

@Command("echo")
public class Echo {
    @Command.Default
    public void echo(CommandExecutor sender, String string) {
        sender.sendMessage(string);
    }
}

You can add additional behaviour to several different aspects of how an argument works such as:

  • How it is parsed

  • How it gets converted into an object

  • If it is required

See Arguments for more details.

Command Groups

Command groups are simply nested subcommands. Their behaviour and definition is just what you'd expect:

@Command("foo")
public class Foo {
    @Command("bar")
    public class Bar {
        // foo bar baz
        @Command("baz")
        public void baz() {}
        
        // foo bar qux
        @Command("qux")
        public void qux() {}
    }
}

Command Metadata

The Enchanted API defines three metadata annotations:

  • @Description - adds a description to a command or subcommand.

  • @Param - sets the name of a parameter.

  • @Usage - sets the usage of a command, subcommand or default command.

Command Aliases

Aliases are a way to add secondary names to a command:

@Command("gamemode")
@Aliases({"gm"})
public class GameMode { ... }

Last updated