Recipe arguments

The RecipeArgument class lets you retrieve Bukkit's ComplexRecipe object.

Example - Giving a player the result of a recipe

Say we want to give yourself the result of a specific recipe. Since Bukkit's Recipe class contains the getResult() method, we will use that in our example. We want to create the following command:

/giverecipe <recipe>

As such, we easily implement it by specifying the RecipeArgument, casting it and adding it to the player's inventory:

new CommandAPICommand("giverecipe")
    .withArguments(new RecipeArgument("recipe"))
    .executesPlayer((player, args) -> {
        ComplexRecipe recipe = (ComplexRecipe) args[0];
        player.getInventory().addItem(recipe.getResult());
    })
    .register();

Example - Unlocking a recipe for a player

In this example, we'll use the ComplexRecipe's getKey() method to write an example to to unlock a recipe for a player. For this command, we'll use the following syntax:

/unlockrecipe <player> <recipe>

This is then implemented trivially as follows:

new CommandAPICommand("unlockrecipe")
    .withArguments(new PlayerArgument("player"))
    .withArguments(new RecipeArgument("recipe"))
    .executes((sender, args) -> {
        Player target = (Player) args[0];
        ComplexRecipe recipe = (ComplexRecipe) args[1];

        target.discoverRecipe(recipe.getKey());
    })
    .register();