Skip to content

Annotations

AlphaHelix edited this page Aug 25, 2017 · 2 revisions

How to create a command using annotations?

First thing you have to do is either

  • Make your class extend SimplePack to gain access to annotated commands, annotated items and listeners
  • Make your class extend SimpleAnnotatedCommand to gain access to annotated commands and listeners
  • Have Annotations.COMMAND.registerCommands(this); inside your constructor

After you done this, you can simply create a new method and annotate it with the @Command annotation. All the parameters you add to your method will be used as arguments inside the command which the player has to enter.

Aside of the @Command annotation, there is also a @Permission and a @Complete annotation to either give the command a permission or let the command have auto completion methods.

public class AnnotatedCommandTest extends SimpleAnnotatedCommand {

    /**
     * A basic command: /test
     * - no arguments
     * - only players can execute
     */
    @Command
    public void test(Player p) {
        p.sendMessage("Hello!");
    }

    /**
     * /argumentTest <message>
     *     - all arguments will be joined together to one String and passed as message
     *     - requires permission "testplugin.test.execute"
     */
    @Command(usage = "<message>", min = 1)
    @Permission("testplugin.test.execute")
    public void argumentTest(CommandSender sender, @Joined String message) {
        sender.sendMessage(message);
    }

    /**
     * Completion for /argumentTest
     */
    @Complete
    public void argumentCommand(List<String> completions, CommandSender sender, String message) {
        if (message == null) {
            completions.add("Test Message");
        }
    }

    /**
     * Command with 1 required and 2 optional number arguments
     * - The Integer defaults to 12 if it is not provided
     * - The Double defaults to null if it is not provided
     */
    @Command(usage = "<required> [Int] [Double]",
            min = 1)
    public void optionalCommand(CommandSender sender, String required, @Optional(define = "12") Integer optionalInt, @Optional Double optionalDouble) {
        sender.sendMessage("Required: " + required);
        sender.sendMessage("Int: " + optionalInt);
        sender.sendMessage("Double: " + optionalDouble);
    }
}

How to create an "ItemStack" using annotations?

First thing you have to do is either

  • Make your class extend SimplePack to gain access to annotated commands, annotated items and listeners
  • Make your class extend SimpleAnnotatedItemListener to gain access to annotated items and listeners
  • Have Annotations.ITEMS.registerItems(this); inside your constructor

After you done this, you can simply create a new field and annotate it with the @Item annotation. You don't need to initalize the variable. AlphaLibary will do this for you.

 @Item(material = Material.STONE,
        amount = 5,
        damage = 0,
        itemflags = {ItemFlag.HIDE_PLACED_ON},
        enchantments = {"DAMAGE_ALL:1"},
        lore = {" ", "This is a lore"},
        name = "§1The magic stone",
        unbreakable = true)
    private ItemStack enchantedStone;

All arguments inside the @Item annotation are optional! -> You don't have to provide them.

Clone this wiki locally