Enchantment argument

An enchantment argument suggesting a list of Minecraft enchantments

The EnchantmentArgument class lets users input a specific enchantment. As you would expect, the cast type is Bukkit's Enchantment class.

Example - Giving a player an enchantment on their current item

Say we want to give a player an enchantment on the item that the player is currently holding. We will use the following command syntax:

/enchantitem <enchantment> <level>

Since most enchantment levels range between 1 and 5, we will also make use of the IntegerArgument to restrict the level of the enchantment by usng its range constructor.

new CommandAPICommand("enchantitem")
    .withArguments(new EnchantmentArgument("enchantment"))
    .withArguments(new IntegerArgument("level", 1, 5))
    .executesPlayer((player, args) -> {
        Enchantment enchantment = (Enchantment) args.get("enchantment");
        int level = (int) args.get("level");
        
        // Add the enchantment
        player.getInventory().getItemInMainHand().addEnchantment(enchantment, level);
    })
    .register();
CommandAPICommand("enchantitem")
    .withArguments(EnchantmentArgument("enchantment"))
    .withArguments(IntegerArgument("level", 1, 5))
    .executesPlayer(PlayerCommandExecutor { player, args ->
        val enchantment = args["enchantment"] as Enchantment
        val level = args["level"] as Int

        // Add the enchantment
        player.inventory.itemInMainHand.addEnchantment(enchantment, level)
    })
    .register()
commandAPICommand("enchantitem") {
    enchantmentArgument("enchantment")
    integerArgument("level", 1, 5)
    playerExecutor { player, args ->
        val enchantment = args["enchantment"] as Enchantment
        val level = args["level"] as Int

        // Add the enchantment
        player.inventory.itemInMainHand.addEnchantment(enchantment, level)
    }
}